Added support for relative/absolute path mgmt for stored files.

This commit is contained in:
Markus Lampert
2019-11-09 19:57:58 -08:00
parent a2935d5ff0
commit c6f14b9836
16 changed files with 94 additions and 45 deletions

File diff suppressed because one or more lines are too long

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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": {}
}

View File

@@ -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",

View File

@@ -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": {}
}

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

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