From 792cb05473d459bfefac666be9e84351c59f48b7 Mon Sep 17 00:00:00 2001 From: sliptonic Date: Mon, 11 Jul 2022 14:27:42 -0500 Subject: [PATCH 1/2] export a camotics tooltable --- src/Mod/Path/PathScripts/PathCamoticsGui.py | 2 - src/Mod/Path/PathScripts/PathToolBit.py | 6 ++ .../Path/PathScripts/PathToolBitLibraryGui.py | 96 +++++++++++++++++-- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathCamoticsGui.py b/src/Mod/Path/PathScripts/PathCamoticsGui.py index c75f6e6a0d..f68948a9d7 100644 --- a/src/Mod/Path/PathScripts/PathCamoticsGui.py +++ b/src/Mod/Path/PathScripts/PathCamoticsGui.py @@ -32,7 +32,6 @@ import PathScripts.PathPost as PathPost import camotics import io import json -import os import queue import subprocess @@ -245,7 +244,6 @@ class CamoticsSimulation(QtCore.QObject): def cancel(self): pass - def buildproject(self): # , files=[]): PathLog.track() diff --git a/src/Mod/Path/PathScripts/PathToolBit.py b/src/Mod/Path/PathScripts/PathToolBit.py index e657b7e315..8067d9f9ca 100644 --- a/src/Mod/Path/PathScripts/PathToolBit.py +++ b/src/Mod/Path/PathScripts/PathToolBit.py @@ -286,6 +286,9 @@ class ToolBit(object): break if doc is None: p = findToolShape(p, path if path else obj.File) + if p is None: + raise FileNotFoundError + if not path and p != obj.BitShape: obj.BitShape = p PathLog.debug("ToolBit {} using shape file: {}".format(obj.Label, p)) @@ -482,6 +485,9 @@ class ToolBitFactory(object): def CreateFrom(self, path, name="ToolBit"): PathLog.track(name, path) + + if not os.path.isfile(path): + raise FileNotFoundError(f"{path} not found") try: data = Declaration(path) bit = Factory.CreateFromAttrs(data, name, path) diff --git a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py index 902f6a7411..249ec437d2 100644 --- a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py @@ -73,7 +73,10 @@ def checkWorkingDir(): qm = PySide.QtGui.QMessageBox ret = qm.question( - None, "", translate("Path_ToolBit","Toolbit working directory not set up. Do that now?"), qm.Yes | qm.No + None, + "", + translate("Path_ToolBit", "Toolbit working directory not set up. Do that now?"), + qm.Yes | qm.No, ) if ret == qm.No: @@ -117,9 +120,10 @@ def checkWorkingDir(): ret = qm.question( None, "", - translate("Path_ToolBit","Toolbit Working directory {} needs these sudirectories:\n {} \n Create them?").format( - workingdir, needed - ), + translate( + "Path_ToolBit", + "Toolbit Working directory {} needs these sudirectories:\n {} \n Create them?", + ).format(workingdir, needed), qm.Yes | qm.No, ) @@ -136,7 +140,9 @@ def checkWorkingDir(): ret = qm.question( None, "", - translate("Path_ToolBit","Copy example files to new {} directory?").format(dir), + translate( + "Path_ToolBit", "Copy example files to new {} directory?" + ).format(dir), qm.Yes | qm.No, ) if ret == qm.Yes: @@ -788,12 +794,15 @@ class ToolBitLibrary(object): TooltableTypeJSON = translate("Path_ToolBit", "Tooltable JSON (*.fctl)") TooltableTypeLinuxCNC = translate("Path_ToolBit", "LinuxCNC tooltable (*.tbl)") + TooltableTypeCamotics = translate("Path_ToolBit", "Camotics tooltable (*.json)") filename = PySide.QtGui.QFileDialog.getSaveFileName( self.form, translate("Path_ToolBit", "Save toolbit library"), PathPreferences.lastPathToolLibrary(), - "{};;{}".format(TooltableTypeJSON, TooltableTypeLinuxCNC), + "{};;{};;{}".format( + TooltableTypeJSON, TooltableTypeLinuxCNC, TooltableTypeCamotics + ), ) if filename and filename[0]: if filename[1] == TooltableTypeLinuxCNC: @@ -803,6 +812,13 @@ class ToolBitLibrary(object): else "{}.tbl".format(filename[0]) ) self.libararySaveLinuxCNC(path) + elif filename[1] == TooltableTypeCamotics: + path = ( + filename[0] + if filename[0].endswith(".json") + else "{}.json".format(filename[0]) + ) + self.libararySaveCamotics(path) else: path = ( filename[0] @@ -878,3 +894,71 @@ class ToolBitLibrary(object): else: PathLog.error("Could not find tool #{} ".format(toolNr)) + + def libararySaveCamotics(self, path): + + SHAPEMAP = { + "ballend": "Ballnose", + "endmill": "Cylindrical", + "v-bit": "Conical", + "chamfer": "Snubnose", + } + + tooltemplate = { + "units": "metric", + "shape": "cylindrical", + "length": 10, + "diameter": 3.125, + "description": "", + } + toollist = {} + + unitstring = ( + "imperial" if FreeCAD.Units.getSchema() in [2, 3, 5, 7] else "metric" + ) + + for row in range(self.toolModel.rowCount()): + toolNr = self.toolModel.data( + self.toolModel.index(row, 0), PySide.QtCore.Qt.EditRole + ) + + toolPath = self.toolModel.data(self.toolModel.index(row, 0), _PathRole) + PathLog.debug(toolPath) + try: + bit = PathToolBit.Factory.CreateFrom(toolPath) + except FileNotFoundError as e: + FreeCAD.Console.PrintError(e) + continue + except Exception as e: + raise e + + if bit: + PathLog.track(bit) + + toolitem = tooltemplate.copy() + + toolitem["diameter"] = ( + float(bit.Diameter.getUserPreferred()[0].split()[0]) + if hasattr(bit, "Diameter") + else 2 + ) + toolitem["description"] = bit.Label + toolitem["length"] = ( + float(bit.Length.getUserPreferred()[0].split()[0]) + if hasattr(bit, "Length") + else 10 + ) + + if hasattr(bit, "Camotics"): + toolitem["shape"] = bit.Camotics + else: + toolitem["shape"] = SHAPEMAP.get(bit.ShapeName, "Cylindrical") + + toolitem["units"] = unitstring + FreeCAD.ActiveDocument.removeObject(bit.Name) + + toollist[toolNr] = toolitem + + if len(toollist) > 0: + with open(path, "w") as fp: + fp.write(json.dumps(toollist, indent=2)) From fa49c07840fd86d91c225fb3272f2ab987f6e84d Mon Sep 17 00:00:00 2001 From: sliptonic Date: Sat, 16 Jul 2022 12:16:14 -0500 Subject: [PATCH 2/2] Update src/Mod/Path/PathScripts/PathToolBitLibraryGui.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benjamin Bræstrup Sayoc --- .../Path/PathScripts/PathToolBitLibraryGui.py | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py index 249ec437d2..a4db74a907 100644 --- a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py @@ -932,32 +932,34 @@ class ToolBitLibrary(object): except Exception as e: raise e - if bit: - PathLog.track(bit) + if not bit: + continue + + PathLog.track(bit) - toolitem = tooltemplate.copy() + toolitem = tooltemplate.copy() - toolitem["diameter"] = ( - float(bit.Diameter.getUserPreferred()[0].split()[0]) - if hasattr(bit, "Diameter") - else 2 - ) - toolitem["description"] = bit.Label - toolitem["length"] = ( - float(bit.Length.getUserPreferred()[0].split()[0]) - if hasattr(bit, "Length") - else 10 - ) + toolitem["diameter"] = ( + float(bit.Diameter.getUserPreferred()[0].split()[0]) + if hasattr(bit, "Diameter") + else 2 + ) + toolitem["description"] = bit.Label + toolitem["length"] = ( + float(bit.Length.getUserPreferred()[0].split()[0]) + if hasattr(bit, "Length") + else 10 + ) - if hasattr(bit, "Camotics"): - toolitem["shape"] = bit.Camotics - else: - toolitem["shape"] = SHAPEMAP.get(bit.ShapeName, "Cylindrical") + if hasattr(bit, "Camotics"): + toolitem["shape"] = bit.Camotics + else: + toolitem["shape"] = SHAPEMAP.get(bit.ShapeName, "Cylindrical") - toolitem["units"] = unitstring - FreeCAD.ActiveDocument.removeObject(bit.Name) + toolitem["units"] = unitstring + FreeCAD.ActiveDocument.removeObject(bit.Name) - toollist[toolNr] = toolitem + toollist[toolNr] = toolitem if len(toollist) > 0: with open(path, "w") as fp: