def onSave(self, evt=None): '''Save callback from main menu.''' # If we don't have a filename, perform Save As instead if self.filename == '': self.onSaveAs() else: # Write out the file path = os.path.join(self.directory, self.filename) if '.cad' in path: if self.frame.canvas.shape_set.reconstructor() != []: dialogs.warning( '''Must save as .ko file, as .cad files do not include interactive geometry features. Please pick a new filename.''') self.onSaveAs() return with open(path, 'w') as f: f.write(self.frame.editor.text) else: with open(path, 'w') as f: text = self.frame.editor.text shapes = self.frame.canvas.shape_set.reconstructor() pickle.dump({'text': text, 'shapes': shapes}, f) # Tell the canvas and editor that we've saved # (this invokes the callback to change title text) self.frame.canvas.unsaved = False self.frame.editor.SetSavePoint() # Update the status box. self.frame.status = 'Saved file %s' % self.filename
def export_from_cad(self, filetype): cad = koko.TASKS.cached_cad if 'failed' in koko.FRAME.status: dialogs.warning('Design has errors! Export failed.') return elif cad is None: dialogs.warning( 'Design needs to be rendered before exporting! Export failed') return elif koko.TASKS.export_task: dialogs.warning('Export already in progress.') return elif filetype in ['.png', '.svg'] and any( getattr(cad, b) is None for b in ['xmin', 'xmax', 'ymin', 'ymax']): dialogs.warning('Design needs to be bounded along X and Y axes ' + 'to export %s' % filetype) return elif filetype in ['.stl', '.asdf'] and any( getattr(cad, b) is None for b in ['xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax']): dialogs.warning('Design needs to be bounded on all axes ' + 'to export %s' % filetype) return elif filetype == '.svg' and cad.zmin is not None: dialogs.warning('Design must be flat (without z bounds)' + ' to export .svg') return # Open up a dialog box to get the export resolution or settings if filetype == '.asdf': dlg = dialogs.ResolutionDialog(10, '.asdf export', cad) key = None elif filetype == '.stl': dlg = dialogs.ResolutionDialog(10, '.stl export', cad, 'Watertight') key = 'use_cms' elif filetype == '.png': dlg = dialogs.ResolutionDialog(10, '.stl export', cad, 'Heightmap') key = 'make_heightmap' elif filetype in '.svg': dlg = dialogs.ResolutionDialog(10, '.svg export', cad) key = None elif filetype == '.dot': dlg = dialogs.CheckDialog('.dot export', 'Packed arrays') key = 'dot_arrays' else: dlg = None if dlg: if dlg.ShowModal() == wx.ID_OK: kwargs = {} if isinstance(dlg, dialogs.ResolutionDialog): kwargs.update({'resolution': float(dlg.result)}) if key: kwargs.update({key: dlg.checked}) else: kwargs = None dlg.Destroy() else: kwargs = {} # If we didn't get a valid resolution, then abort if kwargs is None: return # Open up a save as dialog to get the export target df = dialogs.save_as(self.directory, extension=filetype) if df[1] == '': return path = os.path.join(*df) koko.TASKS.export(cad, path, **kwargs)
def load(self): """ @brief Loads the current design file @details The file is defined by self.directory and self.filename """ self.clear() path = os.path.join(self.directory, self.filename) if path[-3:] == '.ko' or path[-4:] == '.cad': with open(path, 'r') as f: text = f.read() if text.split('\n')[0] == '## Geometry header ##': koko.PRIMS.reconstruct(eval(text.split('\n')[1])) koko.PRIMS.undo_stack = [koko.PRIMS.reconstructor()] text = '\n'.join(text.split('\n')[3:]) koko.EDITOR.text = text koko.FRAME.status = 'Loaded design file' if path[-4:] == '.cad': dialogs.warning(""" This file has a '.cad' extension, which was superceded by '.ko'. It may use deprecated features or syntax. If it is an example file, the 'kokopelli/examples' folder may include an updated version with a '.ko' extension""" ) self.mode = 'cad' self.first_render = True self.savepoint(True) elif path[-4:] == '.png': self.mode = 'png' img = Image.load(path) koko.CANVAS.load_image(img) koko.GLCANVAS.load_image(img) koko.FAB.set_input(img) wx.CallAfter(self.snap_bounds) elif path[-4:] == '.stl': self.mode = 'stl' mesh = Mesh.load(path) koko.FRAME.get_menu('View', '3D').Check(True) self.render_mode('3D') koko.GLCANVAS.load_mesh(mesh) wx.CallAfter(self.snap_bounds) elif path[-5:] == '.asdf': self.mode = 'asdf' koko.FRAME.status = 'Loading ASDF' wx.Yield() asdf = ASDF.load(path) koko.FRAME.status = 'Triangulating' wx.Yield() mesh = asdf.triangulate() mesh.source = Struct(type=ASDF, file=path, depth=0) koko.FRAME.status = '' koko.FRAME.get_menu('View', '3D').Check(True) self.render_mode('3D') koko.GLCANVAS.load_mesh(mesh) koko.FAB.set_input(asdf) elif path[-4:] == '.vol': self.mode = 'vol' koko.IMPORT.set_target(self.directory, self.filename)
def export_from_cad(self, filetype): cad = koko.TASKS.cached_cad if 'failed' in koko.FRAME.status: dialogs.warning('Design has errors! Export failed.') return elif cad is None: dialogs.warning('Design needs to be rendered before exporting! Export failed') return elif koko.TASKS.export_task: dialogs.warning('Export already in progress.') return elif filetype in ['.png','.svg'] and any( getattr(cad,b) is None for b in ['xmin','xmax','ymin','ymax']): dialogs.warning('Design needs to be bounded along X and Y axes ' + 'to export %s' % filetype) return elif filetype in ['.stl','.asdf'] and any( getattr(cad,b) is None for b in ['xmin','xmax','ymin','ymax','zmin','zmax']): dialogs.warning('Design needs to be bounded on all axes '+ 'to export %s' % filetype) return elif filetype == '.svg' and cad.zmin is not None: dialogs.warning('Design must be flat (without z bounds)'+ ' to export .svg') return # Open up a dialog box to get the export resolution or settings if filetype == '.asdf': dlg = dialogs.ResolutionDialog(10, '.asdf export', cad) key = None elif filetype == '.stl': dlg = dialogs.ResolutionDialog(10, '.stl export', cad, 'Watertight') key = 'use_cms' elif filetype == '.png': dlg = dialogs.ResolutionDialog( 10, '.stl export', cad, 'Heightmap' ) key = 'make_heightmap' elif filetype in '.svg': dlg = dialogs.ResolutionDialog( 10, '.svg export', cad ) key = None elif filetype == '.dot': dlg = dialogs.CheckDialog('.dot export', 'Packed arrays') key = 'dot_arrays' else: dlg = None if dlg: if dlg.ShowModal() == wx.ID_OK: kwargs = {} if isinstance(dlg, dialogs.ResolutionDialog): kwargs.update({'resolution': float(dlg.result)}) if key: kwargs.update({key: dlg.checked}) else: kwargs = None dlg.Destroy() else: kwargs = {} # If we didn't get a valid resolution, then abort if kwargs is None: return # Open up a save as dialog to get the export target df = dialogs.save_as(self.directory, extension=filetype) if df[1] == '': return path = os.path.join(*df) koko.TASKS.export(cad, path, **kwargs)
def export(self, event): ''' General-purpose export callback. Decides which export command to call based on the menu item text.''' item = koko.FRAME.GetMenuBar().FindItemById(event.GetId()) filetype = item.GetLabel() cad = koko.TASKS.cached_cad if 'failed' in koko.FRAME.status: dialogs.warning('Design has errors! Export failed.') return elif cad is None: dialogs.warning('Design needs to be rendered before exporting! Export failed') return elif koko.TASKS.export_task: dialogs.warning('Export already in progress.') return elif filetype in ['.png','.svg'] and any( getattr(cad,b) is None for b in ['xmin','xmax','ymin','ymax']): dialogs.warning('Design needs to be bounded along X and Y axes ' + 'to export %s' % filetype) return elif filetype in ['.stl','.asdf'] and any( getattr(cad,b) is None for b in ['xmin','xmax','ymin','ymax','zmin','zmax']): dialogs.warning('Design needs to be bounded on all axes '+ 'to export %s' % filetype) return elif filetype == '.svg' and cad.zmin is not None: dialogs.warning('Design must be flat (without z bounds)'+ ' to export .svg') return # Open up a dialog box to get the export resolution or settings if filetype == '.asdf': dlg = dialogs.ResolutionDialog(10, '.asdf export', cad, 'Merge leaf cells') elif filetype == '.stl': dlg = dialogs.ResolutionDialog(10, '.stl export', cad, 'Watertight') elif filetype == '.png': dlg = dialogs.ResolutionDialog(10, '.stl export', cad, 'Heightmap') elif filetype in '.svg': dlg = dialogs.ResolutionDialog(10, '%s export' % filetype, cad) elif filetype == '.dot': dlg = dialogs.CheckDialog('.dot export', 'Packed arrays') else: dlg = None if dlg and dlg.ShowModal() == wx.ID_OK: resolution = dlg.result checked = dlg.checked success = True elif dlg: success = False else: resolution = None checked = None success = True if dlg: dlg.Destroy() # If we didn't get a valid resolution, then abort if not success: return # Open up a save as dialog to get the export target df = dialogs.save_as(self.directory, extension=filetype) if df[1] == '': return path = os.path.join(*df) koko.TASKS.export(path, resolution, checked)
def load(self): """ @brief Loads the current design file @details The file is defined by self.directory and self.filename """ self.clear() path = os.path.join(self.directory, self.filename) if path[-3:] == '.ko' or path[-4:] == '.cad': with open(path, 'r') as f: text = f.read() if text.split('\n')[0] == '## Geometry header ##': koko.PRIMS.reconstruct(eval(text.split('\n')[1])) koko.PRIMS.undo_stack = [koko.PRIMS.reconstructor()] text = '\n'.join(text.split('\n')[3:]) koko.EDITOR.text = text koko.FRAME.status = 'Loaded design file' if path[-4:] == '.cad': dialogs.warning(""" This file has a '.cad' extension, which was superceded by '.ko'. It may use deprecated features or syntax. If it is an example file, the 'kokopelli/examples' folder may include an updated version with a '.ko' extension""") self.mode = 'cad' self.first_render = True self.savepoint(True) elif path[-4:] == '.png': self.mode = 'png' img = Image.load(path) koko.CANVAS.load_image(img) koko.GLCANVAS.load_image(img) koko.FAB.set_input(img) wx.CallAfter(self.snap_bounds) elif path[-4:] == '.stl': self.mode = 'stl' mesh = Mesh.load(path) koko.FRAME.get_menu('View', '3D').Check(True) self.render_mode('3D') koko.GLCANVAS.load_mesh(mesh) wx.CallAfter(self.snap_bounds) elif path[-5:] == '.asdf': self.mode = 'asdf' koko.FRAME.status = 'Loading ASDF' wx.Yield() asdf = ASDF.load(path) koko.FRAME.status = 'Triangulating' wx.Yield() mesh = asdf.triangulate() mesh.source = Struct(type=ASDF, file=path, depth=0) koko.FRAME.status = '' koko.FRAME.get_menu('View', '3D').Check(True) self.render_mode('3D') koko.GLCANVAS.load_mesh(mesh) koko.FAB.set_input(asdf) elif path[-4:] == '.vol': self.mode = 'vol' koko.IMPORT.set_target(self.directory, self.filename)