def read(self, filename, listname): "imports a tooltable from a file" try: fileExtension = os.path.splitext(filename[0])[1].lower() xmlHandler = None if fileExtension == '.tooltable': xmlHandler = HeeksTooltableHandler() if fileExtension == '.xml': xmlHandler = FreeCADTooltableHandler() if xmlHandler: parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) parser.setContentHandler(xmlHandler) parser.parse(PathUtil.toUnicode(filename[0])) if not xmlHandler.tooltable: return None ht = xmlHandler.tooltable else: with open(PathUtil.toUnicode(filename[0]), "rb") as fp: ht = self.tooltableFromAttrs(json.load(fp)) tt = self._findList(listname) for t in ht.Tools: newt = ht.getTool(t).copy() tt.addTools(newt) if listname == "<Main>": self.saveMainLibrary(tt) return True except Exception as e: print("could not parse file", e)
def read(self, filename, listname): "imports a tooltable from a file" importedTables = [] try: fileExtension = os.path.splitext(filename[0])[1].lower() xmlHandler = None if fileExtension == ".tooltable": xmlHandler = HeeksTooltableHandler() if fileExtension == ".xml": xmlHandler = FreeCADTooltableHandler() if xmlHandler: parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) parser.setContentHandler(xmlHandler) parser.parse(PathUtil.toUnicode(filename[0])) if not xmlHandler.tooltable: return None ht = xmlHandler.tooltable else: with open(PathUtil.toUnicode(filename[0]), "rb") as fp: tableData = json.load(fp) if isinstance(tableData, dict): ht = self.tooltableFromAttrs(tableData) if ht: importedTables.append(ht) if isinstance(tableData, list): for table in tableData: ht = self.tooltableFromAttrs(table) if ht: importedTables.append(ht) if importedTables: for tt in importedTables: self.toolTables.append(tt) self.saveMainLibrary() return True else: return False except Exception as e: print("could not parse file", e)
def setFromTemplateFile(self, obj, template): """setFromTemplateFile(obj, template) ... extract the properties from the given template file and assign to receiver. This will also create any TCs stored in the template.""" tcs = [] if template: with open(PathUtil.toUnicode(template), "rb") as fp: attrs = json.load(fp) if attrs.get(JobTemplate.Version) and 1 == int(attrs[JobTemplate.Version]): attrs = self.setupSheet.decodeTemplateAttributes(attrs) if attrs.get(JobTemplate.SetupSheet): self.setupSheet.setFromTemplate(attrs[JobTemplate.SetupSheet]) if attrs.get(JobTemplate.GeometryTolerance): obj.GeometryTolerance = float( attrs.get(JobTemplate.GeometryTolerance) ) if attrs.get(JobTemplate.PostProcessor): obj.PostProcessor = attrs.get(JobTemplate.PostProcessor) if attrs.get(JobTemplate.PostProcessorArgs): obj.PostProcessorArgs = attrs.get(JobTemplate.PostProcessorArgs) else: obj.PostProcessorArgs = "" if attrs.get(JobTemplate.PostProcessorOutputFile): obj.PostProcessorOutputFile = attrs.get( JobTemplate.PostProcessorOutputFile ) if attrs.get(JobTemplate.Description): obj.Description = attrs.get(JobTemplate.Description) if attrs.get(JobTemplate.ToolController): for tc in attrs.get(JobTemplate.ToolController): tcs.append(PathToolController.FromTemplate(tc)) if attrs.get(JobTemplate.Stock): obj.Stock = PathStock.CreateFromTemplate( obj, attrs.get(JobTemplate.Stock) ) if attrs.get(JobTemplate.Fixtures): obj.Fixtures = [ x for y in attrs.get(JobTemplate.Fixtures) for x in y ] if attrs.get(JobTemplate.OrderOutputBy): obj.OrderOutputBy = attrs.get(JobTemplate.OrderOutputBy) if attrs.get(JobTemplate.SplitOutput): obj.SplitOutput = attrs.get(JobTemplate.SplitOutput) PathLog.debug("setting tool controllers (%d)" % len(tcs)) obj.Tools.Group = tcs else: PathLog.error( "Unsupported PathJob template version {}".format( attrs.get(JobTemplate.Version) ) ) if not tcs: self.addToolController(PathToolController.Create())
def write(self, filename, listname): "exports the tooltable to a file" tt = self.getTableFromName(listname) if tt: try: def openFileWithExtension(name, ext): fext = os.path.splitext(name)[1].lower() if fext != ext: name = "{}{}".format(name, ext) return (open(PathUtil.toUnicode(name), 'w'), name) if filename[1] == self.TooltableTypeXML: fp,fname = openFileWithExtension(filename[0], '.xml') fp.write('<?xml version="1.0" encoding="UTF-8"?>\n') fp.write(tt.Content) elif filename[1] == self.TooltableTypeLinuxCNC: fp,fname = openFileWithExtension(filename[0], '.tbl') for key in tt.Tools: t = tt.Tools[key] fp.write("T{0} P{0} Y{1} Z{2} A{3} B{4} C{5} U{6} V{7} W{8} D{9} I{10} J{11} Q{12} ;{13}\n".format(key,0,t.LengthOffset,0,0,0,0,0,0,t.Diameter,0,0,0,t.Name)) else: fp,fname = openFileWithExtension(filename[0], '.json') json.dump(self.templateAttrs(), fp, sort_keys=True, indent=2) fp.close() print("Written ", PathUtil.toUnicode(fname)) except Exception as e: # pylint: disable=broad-except print("Could not write file:", e)
def write(self, filename, listname): "exports the tooltable to a file" tt = self._findList(listname) if tt: try: def openFileWithExtension(name, ext): fext = os.path.splitext(name)[1].lower() if fext != ext: name = "{}{}".format(name, ext) return (open(PathUtil.toUnicode(name), 'wb'), name) if filename[1] == self.TooltableTypeXML: fp,fname = openFileWithExtension(filename[0], '.xml') fp.write('<?xml version="1.0" encoding="UTF-8"?>\n') fp.write(tt.Content) elif filename[1] == self.TooltableTypeLinuxCNC: fp,fname = openFileWithExtension(filename[0], '.tbl') for key in tt.Tools: t = tt.Tools[key] fp.write("T{} P{} Y{} Z{} A{} B{} C{} U{} V{} W{} D{} I{} J{} Q{} ;{}\n".format(key,key,0,t.LengthOffset,0,0,0,0,0,0,t.Diameter,0,0,0,t.Name)) else: fp,fname = openFileWithExtension(filename[0], '.json') json.dump(self.templateAttrs(tt), fp, sort_keys=True, indent=2) fp.close() print("Written ", PathUtil.toUnicode(fname)) except Exception as e: print("Could not write file:", e)
def Execute(cls, job, path, dialog=None): attrs = job.Proxy.templateAttrs(job) # post processor settings if dialog and not dialog.includePostProcessing(): attrs.pop(PathJob.JobTemplate.PostProcessor, None) attrs.pop(PathJob.JobTemplate.PostProcessorArgs, None) attrs.pop(PathJob.JobTemplate.PostProcessorOutputFile, None) # tool controller settings toolControllers = dialog.includeToolControllers( ) if dialog else job.Tools.Group if toolControllers: tcAttrs = [tc.Proxy.templateAttrs(tc) for tc in toolControllers] attrs[PathJob.JobTemplate.ToolController] = tcAttrs # stock settings stockAttrs = None if dialog: if dialog.includeStock(): stockAttrs = PathStock.TemplateAttributes( job.Stock, dialog.includeStockExtent(), dialog.includeStockPlacement(), ) else: stockAttrs = PathStock.TemplateAttributes(job.Stock) if stockAttrs: attrs[PathJob.JobTemplate.Stock] = stockAttrs # setup sheet setupSheetAttrs = None if dialog: setupSheetAttrs = job.Proxy.setupSheet.templateAttributes( dialog.includeSettingToolRapid(), dialog.includeSettingCoolant(), dialog.includeSettingOperationHeights(), dialog.includeSettingOperationDepths(), dialog.includeSettingOpsSettings(), ) else: setupSheetAttrs = job.Proxy.setupSheet.templateAttributes( True, True, True) if setupSheetAttrs: attrs[PathJob.JobTemplate.SetupSheet] = setupSheetAttrs encoded = job.Proxy.setupSheet.encodeTemplateAttributes(attrs) # write template with open(PathUtil.toUnicode(path), "w") as fp: json.dump(encoded, fp, sort_keys=True, indent=2)
def setFromTemplateFile(self, obj, template): '''setFromTemplateFile(obj, template) ... extract the properties from the given template file and assign to receiver. This will also create any TCs stored in the template.''' tcs = [] if template: with open(PathUtil.toUnicode(template), 'rb') as fp: attrs = json.load(fp) if attrs.get(JobTemplate.Version) and 1 == int( attrs[JobTemplate.Version]): attrs = self.setupSheet.decodeTemplateAttributes(attrs) if attrs.get(JobTemplate.SetupSheet): self.setupSheet.setFromTemplate( attrs[JobTemplate.SetupSheet]) if attrs.get(JobTemplate.GeometryTolerance): obj.GeometryTolerance = float( attrs.get(JobTemplate.GeometryTolerance)) if attrs.get(JobTemplate.PostProcessor): obj.PostProcessor = attrs.get(JobTemplate.PostProcessor) if attrs.get(JobTemplate.PostProcessorArgs): obj.PostProcessorArgs = attrs.get( JobTemplate.PostProcessorArgs) else: obj.PostProcessorArgs = '' if attrs.get(JobTemplate.PostProcessorOutputFile): obj.PostProcessorOutputFile = attrs.get( JobTemplate.PostProcessorOutputFile) if attrs.get(JobTemplate.Description): obj.Description = attrs.get(JobTemplate.Description) if attrs.get(JobTemplate.ToolController): for tc in attrs.get(JobTemplate.ToolController): tcs.append(PathToolController.FromTemplate(tc)) if attrs.get(JobTemplate.Stock): obj.Stock = PathStock.CreateFromTemplate( obj, attrs.get(JobTemplate.Stock)) PathLog.debug("setting tool controllers (%d)" % len(tcs)) obj.ToolController = tcs else: PathLog.error( translate('PathJob', "Unsupported PathJob template version %s") % attrs.get(JobTemplate.Version)) if not tcs: self.addToolController(PathToolController.Create())
def setFromTemplateFile(self, obj, template): '''setFromTemplateFile(obj, template) ... extract the properties from the given template file and assign to receiver. This will also create any TCs stored in the template.''' tcs = [] if template: with open(PathUtil.toUnicode(template), 'rb') as fp: attrs = json.load(fp) if attrs.get(JobTemplate.Version) and 1 == int(attrs[JobTemplate.Version]): attrs = self.setupSheet.decodeTemplateAttributes(attrs) if attrs.get(JobTemplate.SetupSheet): self.setupSheet.setFromTemplate(attrs[JobTemplate.SetupSheet]) if attrs.get(JobTemplate.GeometryTolerance): obj.GeometryTolerance = float(attrs.get(JobTemplate.GeometryTolerance)) if attrs.get(JobTemplate.PostProcessor): obj.PostProcessor = attrs.get(JobTemplate.PostProcessor) if attrs.get(JobTemplate.PostProcessorArgs): obj.PostProcessorArgs = attrs.get(JobTemplate.PostProcessorArgs) else: obj.PostProcessorArgs = '' if attrs.get(JobTemplate.PostProcessorOutputFile): obj.PostProcessorOutputFile = attrs.get(JobTemplate.PostProcessorOutputFile) if attrs.get(JobTemplate.Description): obj.Description = attrs.get(JobTemplate.Description) if attrs.get(JobTemplate.ToolController): for tc in attrs.get(JobTemplate.ToolController): tcs.append(PathToolController.FromTemplate(tc)) if attrs.get(JobTemplate.Stock): obj.Stock = PathStock.CreateFromTemplate(obj, attrs.get(JobTemplate.Stock)) PathLog.debug("setting tool controllers (%d)" % len(tcs)) obj.ToolController = tcs else: PathLog.error(translate('PathJob', "Unsupported PathJob template version %s") % attrs.get(JobTemplate.Version)) if not tcs: self.addToolController(PathToolController.Create())
def Execute(cls, job, path, dialog=None): attrs = job.Proxy.templateAttrs(job) # post processor settings if dialog and not dialog.includePostProcessing(): attrs.pop(PathJob.JobTemplate.PostProcessor, None) attrs.pop(PathJob.JobTemplate.PostProcessorArgs, None) attrs.pop(PathJob.JobTemplate.PostProcessorOutputFile, None) # tool controller settings toolControllers = dialog.includeToolControllers() if dialog else job.ToolController if toolControllers: tcAttrs = [tc.Proxy.templateAttrs(tc) for tc in toolControllers] attrs[PathJob.JobTemplate.ToolController] = tcAttrs # stock settings stockAttrs = None if dialog: if dialog.includeStock(): stockAttrs = PathStock.TemplateAttributes(job.Stock, dialog.includeStockExtent(), dialog.includeStockPlacement()) else: stockAttrs = PathStock.TemplateAttributes(job.Stock) if stockAttrs: attrs[PathJob.JobTemplate.Stock] = stockAttrs # setup sheet setupSheetAttrs = None if dialog: setupSheetAttrs = job.Proxy.setupSheet.templateAttributes(dialog.includeSettingToolRapid(), dialog.includeSettingOperationHeights(), dialog.includeSettingOperationDepths(), dialog.includeSettingOpsSettings()) else: setupSheetAttrs = job.Proxy.setupSheet.templateAttributes(True, True, True) if setupSheetAttrs: attrs[PathJob.JobTemplate.SetupSheet] = setupSheetAttrs encoded = job.Proxy.setupSheet.encodeTemplateAttributes(attrs) # write template with open(PathUtil.toUnicode(path), 'wb') as fp: json.dump(encoded, fp, sort_keys=True, indent=2)
def write(self, filename, listname): "exports the tooltable to a file" tt = self._findList(listname) if tt: try: def openFileWithExtension(name, ext): fext = os.path.splitext(name)[1].lower() if fext != ext: name = "{}{}".format(name, ext) return (open(PathUtil.toUnicode(name), 'wb'), name) if filename[1] == self.TooltableTypeXML: fp, fname = openFileWithExtension(filename[0], '.xml') fp.write('<?xml version="1.0" encoding="UTF-8"?>\n') fp.write(tt.Content) elif filename[1] == self.TooltableTypeLinuxCNC: fp, fname = openFileWithExtension(filename[0], '.tbl') for key in tt.Tools: t = tt.Tools[key] fp.write( "T{} P{} Y{} Z{} A{} B{} C{} U{} V{} W{} D{} I{} J{} Q{} ;{}\n" .format(key, key, 0, t.LengthOffset, 0, 0, 0, 0, 0, 0, t.Diameter, 0, 0, 0, t.Name)) else: fp, fname = openFileWithExtension(filename[0], '.json') json.dump(self.templateAttrs(tt), fp, sort_keys=True, indent=2) fp.close() print("Written ", PathUtil.toUnicode(fname)) except Exception as e: print("Could not write file:", e)
def openFileWithExtension(name, ext): fext = os.path.splitext(name)[1].lower() if fext != ext: name = "{}{}".format(name, ext) return (open(PathUtil.toUnicode(name), 'wb'), name)
def decodeAttributeString(self, attr): """decodeAttributeString(attr) ... return the decoded string of a template attribute.""" return PathUtil.toUnicode( attr.replace(self.TemplateReference, self.expressionReference()))
def encodeAttributeString(self, attr): '''encodeAttributeString(attr) ... return the encoded string of a template attribute.''' return PathUtil.toUnicode(attr.replace(self.expressionReference(), self.TemplateReference))
def decodeAttributeString(self, attr): '''decodeAttributeString(attr) ... return the decoded string of a template attribute.''' return PathUtil.toUnicode(attr.replace(self.TemplateReference, self.expressionReference()))