Commit 8d452189 authored by sumpfralle's avatar sumpfralle

show a little bit of activity when loading big model files


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@857 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 643306c3
...@@ -215,7 +215,7 @@ def load_fonts(callback=None): ...@@ -215,7 +215,7 @@ def load_fonts(callback=None):
progress_counter = None progress_counter = None
for font_file in all_font_files: for font_file in all_font_files:
charset = pycam.Importers.CXFImporter.import_font(font_file, charset = pycam.Importers.CXFImporter.import_font(font_file,
callback=progress_counter) callback=progress_counter.update)
if (not progress_counter is None) and progress_counter.increment(): if (not progress_counter is None) and progress_counter.increment():
break break
if not charset is None: if not charset is None:
...@@ -1892,6 +1892,9 @@ class ProjectGui: ...@@ -1892,6 +1892,9 @@ class ProjectGui:
if self.view3d and not self.view3d.enabled: if self.view3d and not self.view3d.enabled:
# initialization failed - don't do anything # initialization failed - don't do anything
return return
if not self.model:
# no model loaded - don't enable the window
return
current_state = not ((self.view3d is None) or (not self.view3d.is_visible)) current_state = not ((self.view3d is None) or (not self.view3d.is_visible))
if value is None: if value is None:
new_state = not current_state new_state = not current_state
...@@ -2503,6 +2506,7 @@ class ProjectGui: ...@@ -2503,6 +2506,7 @@ class ProjectGui:
window.destroy() window.destroy()
@gui_activity_guard @gui_activity_guard
@progress_activity_guard
def load_model_file(self, widget=None, filename=None): def load_model_file(self, widget=None, filename=None):
if callable(filename): if callable(filename):
filename = filename() filename = filename()
...@@ -2520,9 +2524,13 @@ class ProjectGui: ...@@ -2520,9 +2524,13 @@ class ProjectGui:
program_locations[key[len(prefix):]] = self.settings.get(key) program_locations[key[len(prefix):]] = self.settings.get(key)
file_type, importer = pycam.Importers.detect_file_type(filename) file_type, importer = pycam.Importers.detect_file_type(filename)
if file_type and callable(importer): if file_type and callable(importer):
self.update_progress_bar(text="Loading model file ...")
# "cancel" is not allowed
self.disable_progress_cancel_button()
self.load_model(importer(filename, self.load_model(importer(filename,
program_locations=program_locations, program_locations=program_locations,
unit=self.settings.get("unit"))) unit=self.settings.get("unit"),
callback=self.update_progress_bar))
self.set_model_filename(filename) self.set_model_filename(filename)
else: else:
log.error("Failed to detect filetype!") log.error("Failed to detect filetype!")
...@@ -2978,6 +2986,8 @@ class ProjectGui: ...@@ -2978,6 +2986,8 @@ class ProjectGui:
self._progress_start_time = time.time() self._progress_start_time = time.time()
self.update_progress_bar("", 0) self.update_progress_bar("", 0)
self.progress_cancel_button.set_sensitive(True) self.progress_cancel_button.set_sensitive(True)
# enable "pulse" mode for a start (in case of unknown ETA)
self.progress_bar.pulse()
self.progress_widget.show() self.progress_widget.show()
else: else:
self.progress_widget.hide() self.progress_widget.hide()
...@@ -2994,6 +3004,9 @@ class ProjectGui: ...@@ -2994,6 +3004,9 @@ class ProjectGui:
if not percent is None: if not percent is None:
percent = min(max(percent, 0.0), 100.0) percent = min(max(percent, 0.0), 100.0)
self.progress_bar.set_fraction(percent/100.0) self.progress_bar.set_fraction(percent/100.0)
if (percent is None) and (self.progress_bar.get_fraction() == 0):
# use "pulse" mode until we reach 1% of the work to be done
self.progress_bar.pulse()
# "estimated time of arrival" text # "estimated time of arrival" text
time_estimation_suffix = " remaining ..." time_estimation_suffix = " remaining ..."
if self.progress_bar.get_fraction() > 0: if self.progress_bar.get_fraction() > 0:
......
...@@ -105,7 +105,7 @@ class CXFParser(object): ...@@ -105,7 +105,7 @@ class CXFParser(object):
# Update the GUI from time to time. # Update the GUI from time to time.
# This is useful for the big unicode font. # This is useful for the big unicode font.
if self.callback and (len(self.letters) % 100 == 0): if self.callback and (len(self.letters) % 100 == 0):
self.callback.update() self.callback()
if (len(line) >= 3) and (line[2] == "]"): if (len(line) >= 3) and (line[2] == "]"):
# single character # single character
character = line[1] character = line[1]
......
...@@ -44,11 +44,12 @@ class DXFParser: ...@@ -44,11 +44,12 @@ class DXFParser:
"COLOR": 62, "COLOR": 62,
} }
def __init__(self, inputstream): def __init__(self, inputstream, callback=None):
self.inputstream = inputstream self.inputstream = inputstream
self.line_number = 0 self.line_number = 0
self.lines = [] self.lines = []
self._input_stack = [] self._input_stack = []
self.callback = callback
self.parse_content() self.parse_content()
self.optimize_line_order() self.optimize_line_order()
...@@ -61,6 +62,8 @@ class DXFParser: ...@@ -61,6 +62,8 @@ class DXFParser:
groups.append(current_group) groups.append(current_group)
remaining_lines = self.lines[:] remaining_lines = self.lines[:]
while remaining_lines: while remaining_lines:
if self.callback and self.callback():
return
if not current_group: if not current_group:
current_group.append(remaining_lines.pop(0)) current_group.append(remaining_lines.pop(0))
else: else:
...@@ -150,6 +153,8 @@ class DXFParser: ...@@ -150,6 +153,8 @@ class DXFParser:
key, value = self._read_key_value() key, value = self._read_key_value()
while (not key is None) \ while (not key is None) \
and not ((key == self.KEYS["MARKER"]) and (value == "EOF")): and not ((key == self.KEYS["MARKER"]) and (value == "EOF")):
if self.callback and self.callback():
return
if key == self.KEYS["MARKER"]: if key == self.KEYS["MARKER"]:
if value in ("SECTION", "TABLE", "LAYER", "ENDTAB", "ENDSEC"): if value in ("SECTION", "TABLE", "LAYER", "ENDTAB", "ENDSEC"):
# we don't handle these meta-information # we don't handle these meta-information
...@@ -209,7 +214,7 @@ class DXFParser: ...@@ -209,7 +214,7 @@ class DXFParser:
return None return None
def import_model(filename, program_locations=None, unit=None): def import_model(filename, program_locations=None, unit=None, callback=None):
try: try:
infile = open(filename,"rb") infile = open(filename,"rb")
except IOError, err_msg: except IOError, err_msg:
...@@ -217,10 +222,14 @@ def import_model(filename, program_locations=None, unit=None): ...@@ -217,10 +222,14 @@ def import_model(filename, program_locations=None, unit=None):
% (filename, err_msg)) % (filename, err_msg))
return None return None
result = DXFParser(infile) result = DXFParser(infile, callback=callback)
lines = result.get_model()["lines"] lines = result.get_model()["lines"]
if callback and callback():
log.warn("DXFImporter: load model operation was cancelled")
return None
if lines: if lines:
model = pycam.Geometry.Model.ContourModel() model = pycam.Geometry.Model.ContourModel()
for l in lines: for l in lines:
......
...@@ -29,7 +29,7 @@ import os ...@@ -29,7 +29,7 @@ import os
log = pycam.Utils.log.get_logger() log = pycam.Utils.log.get_logger()
def import_model(filename, program_locations=None, unit=None): def import_model(filename, program_locations=None, unit=None, callback=None):
if not os.path.isfile(filename): if not os.path.isfile(filename):
log.error("PSImporter: file (%s) does not exist" % filename) log.error("PSImporter: file (%s) does not exist" % filename)
return None return None
...@@ -53,9 +53,13 @@ def import_model(filename, program_locations=None, unit=None): ...@@ -53,9 +53,13 @@ def import_model(filename, program_locations=None, unit=None):
success = convert_eps2dxf(filename, dxf_file_name, location=pstoedit_path) success = convert_eps2dxf(filename, dxf_file_name, location=pstoedit_path)
if not success: if not success:
result = None result = None
elif callback and callback():
log.warn("PSImporter: load model operation canceled")
result = None
else: else:
log.info("Successfully converted PS file to DXF file") log.info("Successfully converted PS file to DXF file")
result = pycam.Importers.DXFImporter.import_model(dxf_file_name, unit=unit) result = pycam.Importers.DXFImporter.import_model(dxf_file_name,
unit=unit, callback=callback)
# always remove the dxf file # always remove the dxf file
remove_temp_file(dxf_file_name) remove_temp_file(dxf_file_name)
return result return result
......
...@@ -53,7 +53,8 @@ def UniqueVertex(x, y, z): ...@@ -53,7 +53,8 @@ def UniqueVertex(x, y, z):
vertices += 1 vertices += 1
return Point(x, y, z) return Point(x, y, z)
def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None,
callback=None):
global vertices, edges, kdtree global vertices, edges, kdtree
vertices = 0 vertices = 0
edges = 0 edges = 0
...@@ -106,6 +107,9 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): ...@@ -106,6 +107,9 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None):
if binary: if binary:
for i in range(1, numfacets + 1): for i in range(1, numfacets + 1):
if callback and callback():
log.warn("STLImporter: load model operation cancelled")
return None
a1 = unpack("<f", f.read(4))[0] a1 = unpack("<f", f.read(4))[0]
a2 = unpack("<f", f.read(4))[0] a2 = unpack("<f", f.read(4))[0]
a3 = unpack("<f", f.read(4))[0] a3 = unpack("<f", f.read(4))[0]
...@@ -180,6 +184,9 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): ...@@ -180,6 +184,9 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None):
current_line = 0 current_line = 0
for line in f: for line in f:
if callback and callback():
log.warn("STLImporter: load model operation cancelled")
return None
current_line += 1 current_line += 1
m = solid.match(line) m = solid.match(line)
if m: if m:
......
...@@ -76,7 +76,7 @@ def convert_eps2dxf(eps_filename, dxf_filename, location=None): ...@@ -76,7 +76,7 @@ def convert_eps2dxf(eps_filename, dxf_filename, location=None):
process.stderr.read())) process.stderr.read()))
return False return False
def import_model(filename, program_locations=None, unit=None): def import_model(filename, program_locations=None, unit=None, callback=None):
if not os.path.isfile(filename): if not os.path.isfile(filename):
log.error("SVGImporter: file (%s) does not exist" % filename) log.error("SVGImporter: file (%s) does not exist" % filename)
return None return None
...@@ -110,6 +110,10 @@ def import_model(filename, program_locations=None, unit=None): ...@@ -110,6 +110,10 @@ def import_model(filename, program_locations=None, unit=None):
if not success: if not success:
remove_temp_file(eps_file_name) remove_temp_file(eps_file_name)
return None return None
if callback and callback():
remove_temp_file(eps_file_name)
log.warn("SVGImporter: load model operation was cancelled")
return None
log.info("Successfully converted SVG file to EPS file") log.info("Successfully converted SVG file to EPS file")
# convert eps to dxf via pstoedit # convert eps to dxf via pstoedit
...@@ -120,9 +124,13 @@ def import_model(filename, program_locations=None, unit=None): ...@@ -120,9 +124,13 @@ def import_model(filename, program_locations=None, unit=None):
remove_temp_file(eps_file_name) remove_temp_file(eps_file_name)
if not success: if not success:
result = None result = None
elif callback and callback():
log.warn("SVGImporter: load model operation was cancelled")
result = None
else: else:
log.info("Successfully converted EPS file to DXF file") log.info("Successfully converted EPS file to DXF file")
result = pycam.Importers.DXFImporter.import_model(dxf_file_name, unit=unit) result = pycam.Importers.DXFImporter.import_model(dxf_file_name,
unit=unit, callback=callback)
# always remove the dxf file # always remove the dxf file
remove_temp_file(dxf_file_name) remove_temp_file(dxf_file_name)
return result return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment