From c6f14b98366b5fc3b62bfe2ecc5528ab22ca8da3 Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Sat, 9 Nov 2019 19:57:58 -0800 Subject: [PATCH] Added support for relative/absolute path mgmt for stored files. --- .../Path/Gui/Resources/preferences/PathJob.ui | 17 +++++---- src/Mod/Path/PathScripts/PathPreferences.py | 8 ++--- .../PathScripts/PathPreferencesPathJob.py | 4 +-- src/Mod/Path/PathScripts/PathToolBit.py | 36 ++++++++++++++++--- .../Path/PathScripts/PathToolBitLibraryGui.py | 5 ++- src/Mod/Path/PathTests/TestPathToolBit.py | 15 ++++++++ src/Mod/Path/Tools/Bit/t1.fctb | 10 +++--- src/Mod/Path/Tools/Bit/t2.fctb | 2 +- src/Mod/Path/Tools/Bit/t3.fctb | 10 +++--- src/Mod/Path/Tools/Bit/t4.fctb | 2 +- src/Mod/Path/Tools/Bit/t5.fctb | 2 +- src/Mod/Path/Tools/Bit/t6.fctb | 2 +- src/Mod/Path/Tools/Bit/t7.fctb | 2 +- src/Mod/Path/Tools/Bit/t8.fctb | 2 +- src/Mod/Path/Tools/Bit/t9.fctb | 2 +- src/Mod/Path/Tools/Library/endmills.fctl | 20 +++++------ 16 files changed, 94 insertions(+), 45 deletions(-) diff --git a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui index 7d88c71040..ea59592362 100644 --- a/src/Mod/Path/Gui/Resources/preferences/PathJob.ui +++ b/src/Mod/Path/Gui/Resources/preferences/PathJob.ui @@ -621,6 +621,14 @@ + + + 0 + 0 + 412 + 461 + + Tools @@ -636,15 +644,12 @@ - + - <html><head/><body><p>References to Tool Bits and their shapes can either be stored with an absolute path or with a relative path to the search path.</p><p><br/></p><p>Generally it is recommended to use relative paths due to their flexibility and robustness to layout changes.</p><p><br/></p><p>Should multiple tools or tool shapes with the same name exist in different directories it can be required to use absolute paths. </p></body></html> + <html><head/><body><p>References to Tool Bits and their shapes can either be stored with an absolute path or with a relative path to the search path.</p><p>Generally it is recommended to use relative paths due to their flexibility and robustness to layout changes.</p><p>Should multiple tools or tool shapes with the same name exist in different directories it can be required to use absolute paths. </p></body></html> - Store Relative Paths - - - true + Store Absolute Paths diff --git a/src/Mod/Path/PathScripts/PathPreferences.py b/src/Mod/Path/PathScripts/PathPreferences.py index 37e16d98ab..2fc5bf261b 100644 --- a/src/Mod/Path/PathScripts/PathPreferences.py +++ b/src/Mod/Path/PathScripts/PathPreferences.py @@ -46,7 +46,7 @@ LastPathToolLibrary = "LastPathToolLibrary" LastPathToolShape = "LastPathToolShape" UseLegacyTools = "UseLegacyTools" -UseRelativeToolPaths = "UseRelativeToolPaths" +UseAbsoluteToolPaths = "UseAbsoluteToolPaths" # Linear tolerance to use when generating Paths, eg when tessellating geometry GeometryTolerance = "GeometryTolerance" @@ -152,13 +152,13 @@ def searchPathsTool(sub='Bit'): def toolsUseLegacyTools(): return preferences().GetBool(UseLegacyTools, True) -def toolsStoreRelativePaths(): - return preferences().GetBool(UseRelativeToolPaths, True) +def toolsStoreAbsolutePaths(): + return preferences().GetBool(UseAbsoluteToolPaths, False) def setToolsSettings(legacy, relative): pref = preferences() pref.SetBool(UseLegacyTools, legacy) - pref.SetBool(UseRelativeToolPaths, relative) + pref.SetBool(UseAbsoluteToolPaths, relative) def defaultJobTemplate(): template = preferences().GetString(DefaultJobTemplate) diff --git a/src/Mod/Path/PathScripts/PathPreferencesPathJob.py b/src/Mod/Path/PathScripts/PathPreferencesPathJob.py index 525ba9c6e1..0782efd67b 100644 --- a/src/Mod/Path/PathScripts/PathPreferencesPathJob.py +++ b/src/Mod/Path/PathScripts/PathPreferencesPathJob.py @@ -109,7 +109,7 @@ class JobPreferencesPage: PathPreferences.setDefaultStockTemplate('') def saveToolsSettings(self): - PathPreferences.setToolsSettings(self.form.toolsUseLegacy.isChecked(), self.form.toolsRelativePaths.isChecked()) + PathPreferences.setToolsSettings(self.form.toolsUseLegacy.isChecked(), self.form.toolsAbsolutePaths.isChecked()) def selectComboEntry(self, widget, text): index = widget.findText(text, QtCore.Qt.MatchFixedString) @@ -251,7 +251,7 @@ class JobPreferencesPage: def loadToolSettings(self): self.form.toolsUseLegacy.setChecked(PathPreferences.toolsUseLegacyTools()) - self.form.toolsRelativePaths.setChecked(PathPreferences.toolsStoreRelativePaths()) + self.form.toolsAbsolutePaths.setChecked(PathPreferences.toolsStoreAbsolutePaths()) def getPostProcessor(self, name): if not name in self.processor.keys(): diff --git a/src/Mod/Path/PathScripts/PathToolBit.py b/src/Mod/Path/PathScripts/PathToolBit.py index 6dec07e999..b3c50c5484 100644 --- a/src/Mod/Path/PathScripts/PathToolBit.py +++ b/src/Mod/Path/PathScripts/PathToolBit.py @@ -96,6 +96,26 @@ def findLibrary(path, dbg=False): return _findTool(path, 'Library', dbg) return _findTool("{}.fctl".format(path), 'Library', dbg) +def _findRelativePath(path, typ): + relative = path + for p in PathPreferences.searchPathsTool(typ): + if path.startswith(p): + p = path[len(p):] + if '/' == p[0]: + p = p[1:] + if len(p) < len(relative): + relative = p + return relative + +def findRelativePathShape(path): + return _findRelativePath(path, 'Shape') + +def findRelativePathTool(path): + return _findRelativePath(path, 'Bit') + +def findRelativePathLibrary(path): + return _findRelativePath(path, 'Library') + def updateConstraint(sketch, name, value): for i, constraint in enumerate(sketch.Constraints): if constraint.Name.split(';')[0] == name: @@ -116,6 +136,7 @@ def updateConstraint(sketch, name, value): PathLog.track(name, constraint.Type, 'unchanged') break + PropertyGroupBit = 'Bit' PropertyGroupAttribute = 'Attribute' @@ -124,9 +145,9 @@ class ToolBit(object): def __init__(self, obj, shapeFile): PathLog.track(obj.Label, shapeFile) self.obj = obj - obj.addProperty('App::PropertyFile', 'BitShape', 'Base', translate('PathToolBit', 'Shape for bit shape')) - obj.addProperty('App::PropertyLink', 'BitBody', 'Base', translate('PathToolBit', 'The parametrized body representing the tool bit')) - obj.addProperty('App::PropertyFile', 'File', 'Base', translate('PathToolBit', 'The file of the tool')) + obj.addProperty('App::PropertyFile', 'BitShape', 'Base', translate('PathToolBit', 'Shape for bit shape')) + obj.addProperty('App::PropertyLink', 'BitBody', 'Base', translate('PathToolBit', 'The parametrized body representing the tool bit')) + obj.addProperty('App::PropertyFile', 'File', 'Base', translate('PathToolBit', 'The file of the tool')) if shapeFile is not None: obj.BitShape = shapeFile self._setupBitShape(obj) @@ -283,7 +304,10 @@ class ToolBit(object): attrs = {} attrs['version'] = 2 # Path.Tool is version 1 attrs['name'] = obj.Label - attrs['shape'] = obj.BitShape + if PathPreferences.toolsStoreAbsolutePaths(): + attrs['shape'] = obj.BitShape + else: + attrs['shape'] = findRelativePathShape(obj.BitShape) params = {} for name in self.propertyNamesBit(obj): params[name] = PathUtil.getPropertyValueString(obj, name) @@ -332,7 +356,9 @@ class ToolBitFactory(object): def CreateFrom(self, path, name='ToolBit'): try: data = Declaration(path) - return Factory.CreateFromAttrs(data, name) + bit = Factory.CreateFromAttrs(data, name) + bit.File = path + return bit except (OSError, IOError) as e: PathLog.error("%s not a valid tool file (%s)" % (path, e)) raise diff --git a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py index 68e159eff0..17b12638a2 100644 --- a/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py +++ b/src/Mod/Path/PathScripts/PathToolBitLibraryGui.py @@ -263,7 +263,10 @@ class ToolBitLibrary(object): 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) - tools.append({'nr': toolNr, 'path': toolPath}) + 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) diff --git a/src/Mod/Path/PathTests/TestPathToolBit.py b/src/Mod/Path/PathTests/TestPathToolBit.py index b7e27c1842..2eb2bbfa4b 100644 --- a/src/Mod/Path/PathTests/TestPathToolBit.py +++ b/src/Mod/Path/PathTests/TestPathToolBit.py @@ -43,3 +43,18 @@ class TestPathToolBit(PathTestUtils.PathTestBase): self.assertNotEqual(path, '/this/is/unlikely/a/valid/path/v-bit.fcstd') + def test10(self): + '''find the relative path of a tool bit''' + shape = 'endmill-straight.fcstd' + path = PathToolBit.findShape(shape) + self.assertIsNot(path, None) + self.assertGreater(len(path), len(shape)) + rel = PathToolBit.findRelativePathShape(path) + self.assertEqual(rel, shape) + + 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) + diff --git a/src/Mod/Path/Tools/Bit/t1.fctb b/src/Mod/Path/Tools/Bit/t1.fctb index 27f271ff99..0fe10f2e03 100644 --- a/src/Mod/Path/Tools/Bit/t1.fctb +++ b/src/Mod/Path/Tools/Bit/t1.fctb @@ -1,12 +1,12 @@ { - "version": 1, + "version": 2, "name": "T1", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", - "attribute": {}, + "shape": "endmill-straight.fcstd", "parameter": { "CuttingEdgeHeight": "30.000 mm", - "Diameter": "1.000 mm", + "Diameter": "5.000 mm", "Length": "50.000 mm", "ShankDiameter": "3.000 mm" - } + }, + "attribute": {} } diff --git a/src/Mod/Path/Tools/Bit/t2.fctb b/src/Mod/Path/Tools/Bit/t2.fctb index 931c833964..b947d7f4f5 100644 --- a/src/Mod/Path/Tools/Bit/t2.fctb +++ b/src/Mod/Path/Tools/Bit/t2.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T2", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t3.fctb b/src/Mod/Path/Tools/Bit/t3.fctb index b4e204e13f..0986533ddd 100644 --- a/src/Mod/Path/Tools/Bit/t3.fctb +++ b/src/Mod/Path/Tools/Bit/t3.fctb @@ -1,12 +1,12 @@ { - "version": 1, + "version": 2, "name": "T3", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", - "attribute": {}, + "shape": "endmill-straight.fcstd", "parameter": { "CuttingEdgeHeight": "30.000 mm", - "Diameter": "3.000 mm", + "Diameter": "5.000 mm", "Length": "50.000 mm", "ShankDiameter": "3.000 mm" - } + }, + "attribute": {} } diff --git a/src/Mod/Path/Tools/Bit/t4.fctb b/src/Mod/Path/Tools/Bit/t4.fctb index dc3042205f..df4edfaaa9 100644 --- a/src/Mod/Path/Tools/Bit/t4.fctb +++ b/src/Mod/Path/Tools/Bit/t4.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T4", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t5.fctb b/src/Mod/Path/Tools/Bit/t5.fctb index 9db3300c7a..9f8bdb0a6a 100644 --- a/src/Mod/Path/Tools/Bit/t5.fctb +++ b/src/Mod/Path/Tools/Bit/t5.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T5", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t6.fctb b/src/Mod/Path/Tools/Bit/t6.fctb index b1c2f58c61..61efb7d319 100644 --- a/src/Mod/Path/Tools/Bit/t6.fctb +++ b/src/Mod/Path/Tools/Bit/t6.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T6", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t7.fctb b/src/Mod/Path/Tools/Bit/t7.fctb index 10a3fc10a6..600de9ded1 100644 --- a/src/Mod/Path/Tools/Bit/t7.fctb +++ b/src/Mod/Path/Tools/Bit/t7.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T7", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t8.fctb b/src/Mod/Path/Tools/Bit/t8.fctb index 995b5012e2..b957c96871 100644 --- a/src/Mod/Path/Tools/Bit/t8.fctb +++ b/src/Mod/Path/Tools/Bit/t8.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T8", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Bit/t9.fctb b/src/Mod/Path/Tools/Bit/t9.fctb index ab49b17dbc..2135c0a9b6 100644 --- a/src/Mod/Path/Tools/Bit/t9.fctb +++ b/src/Mod/Path/Tools/Bit/t9.fctb @@ -1,7 +1,7 @@ { "version": 1, "name": "T9", - "shape": "src/Mod/Path/Tools/Shape/endmill-straight.fcstd", + "shape": "endmill-straight.fcstd", "attribute": {}, "parameter": { "CuttingEdgeHeight": "30.000 mm", diff --git a/src/Mod/Path/Tools/Library/endmills.fctl b/src/Mod/Path/Tools/Library/endmills.fctl index f3e9b37c34..c443e6cd10 100644 --- a/src/Mod/Path/Tools/Library/endmills.fctl +++ b/src/Mod/Path/Tools/Library/endmills.fctl @@ -2,40 +2,40 @@ "tools": [ { "nr": 1, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t1.fctb" + "path": "t1.fctb" }, { "nr": 2, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t2.fctb" + "path": "t2.fctb" }, { "nr": 3, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t3.fctb" + "path": "t3.fctb" }, { "nr": 4, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t4.fctb" + "path": "t4.fctb" }, { "nr": 5, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t5.fctb" + "path": "t5.fctb" }, { "nr": 6, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t6.fctb" + "path": "t6.fctb" }, { "nr": 7, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t7.fctb" + "path": "t7.fctb" }, { "nr": 8, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t8.fctb" + "path": "t8.fctb" }, { "nr": 9, - "path": "/media/sdb/projects/FreeCAD/dev/FreeCAD/src/Mod/Path/Tools/Bit/t9.fctb" + "path": "t9.fctb" } ], "version": 1 -} \ No newline at end of file +}