def libraryLoad(self, path): self.toolTableView.setUpdatesEnabled(False) self.model.clear() self.model.setHorizontalHeaderLabels(self.columnNames()) if path: with open(path) as fp: PathPreferences.setLastPathToolTable(os.path.basename(path)) library = json.load(fp) for toolBit in library['tools']: nr = toolBit['nr'] bit = PathToolBit.findBit(toolBit['path']) if bit: PathLog.track(bit) tool = PathToolBit.Declaration(bit) self._toolAdd(nr, tool, bit) else: PathLog.error("Could not find tool #{}: {}".format( nr, library['tools'][nr])) self.toolTableView.resizeColumnsToContents() self.toolTableView.setUpdatesEnabled(True) self.form.setWindowTitle("{} - {}".format( self.title, os.path.basename(path) if path else '')) self.path = path self.updateToolbar()
def test10(self): '''find the relative path of a tool bit''' shape = 'endmill.fcstd' path = PathToolBit.findShape(shape) self.assertIsNot(path, None) self.assertGreater(len(path), len(shape)) rel = PathToolBit.findRelativePathShape(path) self.assertEqual(rel, shape)
def toolBitNew(self): PathLog.track() # select the shape file shapefile = PathToolBitGui.GetToolShapeFile() if shapefile is None: # user canceled return # select the bit file location and filename filename = PathToolBitGui.GetNewToolFile() if filename is None: return # Parse out the name of the file and write the structure loc, fil = os.path.split(filename) fname = os.path.splitext(fil)[0] fullpath = "{}{}{}.fctb".format(loc, os.path.sep, fname) PathLog.debug("fullpath: {}".format(fullpath)) self.temptool = PathToolBit.ToolBitFactory().Create(name=fname) self.temptool.BitShape = shapefile self.temptool.Proxy.unloadBitBody(self.temptool) self.temptool.Label = fname self.temptool.Proxy.saveToFile(self.temptool, fullpath) self.temptool.Document.removeObject(self.temptool.Name) self.temptool = None # add it to the model self.factory.newTool(self.toolModel, fullpath)
def toolEdit(self, selected): PathLog.track() item = self.toolModel.item(selected.row(), 0) if self.temptool is not None: self.temptool.Document.removeObject(self.temptool.Name) if selected.column() == 0: # editing Nr pass else: tbpath = item.data(_PathRole) self.temptool = PathToolBit.ToolBitFactory().CreateFrom( tbpath, "temptool") self.editor = PathToolBitEdit.ToolBitEditor( self.temptool, self.form.toolTableGroup, loadBitBody=False) QBtn = (PySide.QtGui.QDialogButtonBox.Ok | PySide.QtGui.QDialogButtonBox.Cancel) buttonBox = PySide.QtGui.QDialogButtonBox(QBtn) buttonBox.accepted.connect(self.accept) buttonBox.rejected.connect(self.reject) layout = self.editor.form.layout() layout.addWidget(buttonBox) self.lockon() self.editor.setupUI()
def test01(self): '''Find a tool shapee from an invalid absolute path.''' path = PathToolBit.findShape( '/this/is/unlikely/a/valid/path/v-bit.fcstd') self.assertIsNot(path, None) self.assertNotEqual(path, '/this/is/unlikely/a/valid/path/v-bit.fcstd')
def __libraryLoad(self, path, datamodel): PathLog.track(path) PathPreferences.setLastFileToolLibrary(path) # self.currenLib = path with open(path) as fp: library = json.load(fp) for toolBit in library['tools']: try: nr = toolBit['nr'] bit = PathToolBit.findToolBit(toolBit['path'], path) if bit: PathLog.track(bit) tool = PathToolBit.Declaration(bit) datamodel.appendRow(self._toolAdd(nr, tool, bit)) else: PathLog.error("Could not find tool #{}: {}".format(nr, toolBit['path'])) except Exception as e: msg = "Error loading tool: {} : {}".format(toolBit['path'], e) FreeCAD.Console.PrintError(msg)
def open(self, path=None, dialog=False): '''open(path=None, dialog=False) ... load library stored in path and bring up ui. Returns 1 if user pressed OK, 0 otherwise.''' if path: fullPath = PathToolBit.findLibrary(path) if fullPath: self.libraryLoad(fullPath) else: self.libraryOpen() elif dialog: self.libraryOpen() return self.form.exec_()
def librarySave(self): library = {} tools = [] library['version'] = 1 library['tools'] = tools for row in range(self.model.rowCount()): toolNr = self.model.data(self.model.index(row, 0), PySide.QtCore.Qt.EditRole) toolPath = self.model.data(self.model.index(row, 0), _PathRole) if PathPreferences.toolsStoreAbsolutePaths(): tools.append({'nr': toolNr, 'path': toolPath}) else: tools.append({'nr': toolNr, 'path': PathToolBit.findRelativePathTool(toolPath)}) with open(self.path, 'w') as fp: json.dump(library, fp, sort_keys=True, indent=2)
def toolAdd(self): PathLog.track() # pylint: disable=broad-except try: nr = 0 for row in range(self.model.rowCount()): itemNr = int(self.model.item(row, 0).data(PySide.QtCore.Qt.EditRole)) nr = max(nr, itemNr) nr += 1 for i, foo in enumerate(PathToolBitGui.GetToolFiles(self.form)): tool = PathToolBit.Declaration(foo) self._toolAdd(nr + i, tool, foo) self.toolTableView.resizeColumnsToContents() except Exception: PathLog.error('something happened') PathLog.error(traceback.print_exc())
def newTool(self, datamodel, path): ''' Adds a toolbit item to a model ''' PathLog.track() try: nr = 0 for row in range(datamodel.rowCount()): itemNr = int(datamodel.item(row, 0).data(PySide.QtCore.Qt.EditRole)) nr = max(nr, itemNr) nr += 1 tool = PathToolBit.Declaration(path) except Exception as e: PathLog.error(e) datamodel.appendRow(self._toolAdd(nr, tool, path))
def setupAttributes(self, tool): self.proto = PathToolBit.AttributePrototype() self.props = sorted(self.proto.properties) self.delegate = PathSetupSheetGui.Delegate(self.form) self.model = QtGui.QStandardItemModel(len(self.props), 3, self.form) self.model.setHorizontalHeaderLabels(['Set', 'Property', 'Value']) for i, name in enumerate(self.props): prop = self.proto.getProperty(name) isset = hasattr(tool, name) if isset: prop.setValue(getattr(tool, name)) self.model.setData(self.model.index(i, 0), isset, QtCore.Qt.EditRole) self.model.setData(self.model.index(i, 1), name, QtCore.Qt.EditRole) self.model.setData(self.model.index(i, 2), prop, PathSetupSheetGui.Delegate.PropertyRole) self.model.setData(self.model.index(i, 2), prop.displayString(), QtCore.Qt.DisplayRole) self.model.item(i, 0).setCheckable(True) self.model.item(i, 0).setText('') self.model.item(i, 1).setEditable(False) self.model.item(i, 1).setToolTip(prop.info) self.model.item(i, 2).setToolTip(prop.info) if isset: self.model.item(i, 0).setCheckState(QtCore.Qt.Checked) else: self.model.item(i, 0).setCheckState(QtCore.Qt.Unchecked) self.model.item(i, 1).setEnabled(False) self.model.item(i, 2).setEnabled(False) self.form.attrTable.setModel(self.model) self.form.attrTable.setItemDelegateForColumn(2, self.delegate) self.form.attrTable.resizeColumnsToContents() self.form.attrTable.verticalHeader().hide() self.model.dataChanged.connect(self.updateData)
def test20(self): """Find a tool library from file name""" path = PathToolBit.findToolLibrary("Default.fctl") self.assertIsNot(path, None) self.assertNotEqual(path, "Default.fctl")
def test14(self): """Find a tool bit from a valid absolute path.""" path = PathToolBit.findToolBit(testToolBit()) self.assertIsNot(path, None) self.assertEqual(path, testToolBit())
def test03(self): '''Not find a tool shape from an invalid absolute path.''' path = PathToolBit.findToolShape(testToolShape(TestInvalidDir)) self.assertIsNone(path)
def test04(self): '''Find a tool shape from a valid absolute path.''' path = PathToolBit.findToolShape(testToolShape()) self.assertIsNot(path, None) self.assertEqual(path, testToolShape())
def test01(self): '''Not find a relative path shape if not stored in default location''' path = PathToolBit.findToolShape(TestToolShapeName) self.assertIsNone(path)
def test02(self): '''Find a relative path shape if it's local to a bit path''' path = PathToolBit.findToolShape(TestToolShapeName, testToolBit()) self.assertIsNot(path, None) self.assertEqual(path, testToolShape())
def test23(self): '''Not find a tool library from an invalid absolute path.''' path = PathToolBit.findToolLibrary(testToolLibrary(TestInvalidDir)) self.assertIsNone(path)
def test10(self): '''Find a tool bit from file name''' path = PathToolBit.findToolBit('5mm_Endmill.fctb') self.assertIsNot(path, None) self.assertNotEqual(path, '5mm_Endmill.fctb')
def test20(self): '''Find a tool library from file name''' path = PathToolBit.findToolLibrary('Default.fctl') self.assertIsNot(path, None) self.assertNotEqual(path, 'Default.fctl')
def test21(self): '''Not find a relative path library if not stored in default location''' path = PathToolBit.findToolLibrary(TestToolLibraryName) self.assertIsNone(path)
def test10(self): """Find a tool bit from file name""" path = PathToolBit.findToolBit("5mm_Endmill.fctb") self.assertIsNot(path, None) self.assertNotEqual(path, "5mm_Endmill.fctb")
def test00(self): """Find a tool shape from file name""" path = PathToolBit.findToolShape("endmill.fcstd") self.assertIsNot(path, None) self.assertNotEqual(path, "endmill.fcstd")
def test11(self): '''Not find a relative path bit if not stored in default location''' path = PathToolBit.findToolBit(TestToolBitName) self.assertIsNone(path)
def test00(self): '''Find a tool shapee from file name''' path = PathToolBit.findShape('endmill.fcstd') self.assertIsNot(path, None) self.assertNotEqual(path, 'endmill.fcstd')
def test24(self): '''Find a tool library from a valid absolute path.''' path = PathToolBit.findToolBit(testToolBit()) self.assertIsNot(path, None) self.assertEqual(path, testToolBit())
def test12(self): '''Find a relative path bit if it's local to a library path''' path = PathToolBit.findToolBit(TestToolBitName, testToolLibrary()) self.assertIsNot(path, None) self.assertEqual(path, testToolBit())
def setupAttributes(self, tool): PathLog.track() self.proto = PathToolBit.AttributePrototype() self.props = sorted(self.proto.properties) self.delegate = PathSetupSheetGui.Delegate(self.form) self.model = QtGui.QStandardItemModel( len(self.props) - 1, 3, self.form) self.model.setHorizontalHeaderLabels(['Set', 'Property', 'Value']) for i, name in enumerate(self.props): PathLog.debug("propname: %s " % name) prop = self.proto.getProperty(name) isset = hasattr(tool, name) if isset: prop.setValue(getattr(tool, name)) if name == "UserAttributes": continue else: self.model.setData(self.model.index(i, 0), isset, QtCore.Qt.EditRole) self.model.setData(self.model.index(i, 1), name, QtCore.Qt.EditRole) self.model.setData(self.model.index(i, 2), prop, PathSetupSheetGui.Delegate.PropertyRole) self.model.setData(self.model.index(i, 2), prop.displayString(), QtCore.Qt.DisplayRole) self.model.item(i, 0).setCheckable(True) self.model.item(i, 0).setText('') self.model.item(i, 1).setEditable(False) self.model.item(i, 1).setToolTip(prop.info) self.model.item(i, 2).setToolTip(prop.info) if isset: self.model.item(i, 0).setCheckState(QtCore.Qt.Checked) else: self.model.item(i, 0).setCheckState(QtCore.Qt.Unchecked) self.model.item(i, 1).setEnabled(False) self.model.item(i, 2).setEnabled(False) if hasattr(tool, "UserAttributes"): for key, value in tool.UserAttributes.items(): PathLog.debug(key, value) c1 = QtGui.QStandardItem() c1.setCheckable(False) c1.setEditable(False) c1.setCheckState(QtCore.Qt.CheckState.Checked) c1.setText('') c2 = QtGui.QStandardItem(key) c2.setEditable(False) c3 = QtGui.QStandardItem(value) c3.setEditable(False) self.model.appendRow([c1, c2, c3]) self.form.attrTable.setModel(self.model) self.form.attrTable.setItemDelegateForColumn(2, self.delegate) self.form.attrTable.resizeColumnsToContents() self.form.attrTable.verticalHeader().hide() self.model.dataChanged.connect(self.updateData)
def test11(self): '''store full path if relative path isn't found''' path = '/this/is/unlikely/a/valid/path/v-bit.fcstd' rel = PathToolBit.findRelativePathShape(path) self.assertEqual(rel, path)
def test13(self): '''Not find a tool bit from an invalid absolute path.''' path = PathToolBit.findToolBit(testToolBit(TestInvalidDir)) self.assertIsNone(path)