Merge pull request #7168 from sliptonic/feature/camoticstooltable

[Path] export a camotics tooltable
This commit is contained in:
sliptonic
2022-07-17 08:42:43 -05:00
committed by GitHub
3 changed files with 98 additions and 8 deletions

View File

@@ -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()

View File

@@ -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)

View File

@@ -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,73 @@ 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 not bit:
continue
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))