Changed tool-bit search path to either absolute, local install or relative to context path

This commit is contained in:
Markus Lampert
2021-01-14 23:16:15 -08:00
parent b0aa6f32fe
commit df6de8693e
7 changed files with 203 additions and 79 deletions

View File

@@ -212,6 +212,9 @@ SET(PathTests_SRCS
PathTests/TestPathUtil.py
PathTests/TestPathVcarve.py
PathTests/TestPathVoronoi.py
PathTests/Tools/Bit/test-path-tool-bit-bit-00.fctb
PathTests/Tools/Library/test-path-tool-bit-library-00.fctl
PathTests/Tools/Shape/test-path-tool-bit-shape-00.fcstd
PathTests/boxtest.fcstd
PathTests/test_centroid_00.ngc
PathTests/test_geomop.fcstd

View File

@@ -151,26 +151,9 @@ def searchPathsPost():
return paths
def searchPathsTool(sub='Bit'):
def searchPathsTool(sub):
paths = []
def appendPath(p, sub):
if p:
paths.append(os.path.join(p, 'Tools', sub))
paths.append(os.path.join(p, sub))
paths.append(p)
appendPath(defaultFilePath(), sub)
appendPath(macroFilePath(), sub)
appendPath(os.path.join(FreeCAD.getHomePath(), "Mod/Path/"), sub)
if 'Bit' == sub:
paths.append("{}/Bit".format(os.path.dirname(lastPathToolLibrary())))
paths.append(lastPathToolBit())
if 'Library' == sub:
paths.append(lastPathToolLibrary())
if 'Shape' == sub:
paths.append(lastPathToolShape())
paths.append(os.path.join(FreeCAD.getHomePath(), 'Mod', 'Path', 'Tools', sub))
return paths

View File

@@ -54,56 +54,56 @@ PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
def translate(context, text, disambig=None):
return PySide.QtCore.QCoreApplication.translate(context, text, disambig)
def _findTool(path, typ, dbg=_DebugFindTool):
PathLog.track(path)
if os.path.exists(path): # absolute reference
def _findToolFile(name, containerFile, typ, dbg=_DebugFindTool):
PathLog.track(name)
if os.path.exists(name): # absolute reference
if dbg:
PathLog.debug("Found {} at {}".format(typ, path))
return path
PathLog.debug("Found {} at {}".format(typ, name))
return name
def searchFor(pname, fname):
# PathLog.debug("pname: {} fname: {}".format(pname, fname))
if dbg:
PathLog.debug("Looking for {} in {}".format(pname, fname))
if fname:
for p in PathPreferences.searchPathsTool(typ):
PathLog.track(p)
f = os.path.join(p, fname)
if dbg:
PathLog.debug(" Checking {}".format(f))
if os.path.exists(f):
if dbg:
PathLog.debug(" Found {} at {}".format(typ, f))
return f
if pname and os.path.sep != pname:
PathLog.track(pname)
ppname, pfname = os.path.split(pname)
ffname = os.path.join(pfname, fname) if fname else pfname
return searchFor(ppname, ffname)
return None
if containerFile:
rootPath = os.path.dirname(os.path.dirname(containerFile))
paths = [os.path.join(rootPath, typ)]
else:
paths = []
paths.extend(PathPreferences.searchPathsTool(typ))
return searchFor(path, '')
def _findFile(path, name):
PathLog.track(path, name)
fullPath = os.path.join(path, name)
if os.path.exists(fullPath):
return (True, fullPath)
for root, ds, fs in os.walk(path):
for d in ds:
found, fullPath = _findFile(d, name)
if found:
return (True, fullPath)
return (False, None)
def findShape(path):
'''
findShape(path) ... search for path, full and partially
in all known shape directories.
'''
return _findTool(path, 'Shape')
for p in paths:
found, path = _findFile(p, name)
if found:
return path
return None
def findBit(path):
PathLog.track(path)
if path.endswith('.fctb'):
return _findTool(path, 'Bit')
return _findTool("{}.fctb".format(path), 'Bit')
def _findShape(name, path=None):
'''_findShape(name, path) ... search for name, full and partially starting with path in known shape directories'''
return _findToolFile(name, path, 'Shape')
def findLibrary(path, dbg=False):
if path.endswith('.fctl'):
return _findTool(path, 'Library', dbg)
return _findTool("{}.fctl".format(path), 'Library', dbg)
def findBit(name, path=None):
PathLog.track(name)
if name.endswith('.fctb'):
return _findToolFile(name, path, 'Bit')
return _findToolFile("{}.fctb".format(name), path, 'Bit')
def findLibrary(name, path=None, dbg=False):
if name.endswith('.fctl'):
return _findToolFile(name, path, 'Library', dbg)
return _findToolFile("{}.fctl".format(name), path, 'Library', dbg)
def _findRelativePath(path, typ):
@@ -222,7 +222,7 @@ class ToolBit(object):
doc = FreeCAD.getDocument(d)
break
if doc is None:
p = findShape(p)
p = _findShape(p)
if not path and p != obj.BitShape:
obj.BitShape = p
PathLog.debug("ToolBit {} using shape file: {}".format(obj.Label, p))
@@ -330,7 +330,7 @@ class ToolBit(object):
def getBitThumbnail(self, obj):
if obj.BitShape:
path = findShape(obj.BitShape)
path = _findShape(obj.BitShape)
if path:
with open(path, 'rb') as fd:
try:

View File

@@ -22,37 +22,122 @@
import PathScripts.PathToolBit as PathToolBit
import PathTests.PathTestUtils as PathTestUtils
import os
TestToolDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Tools')
TestInvalidDir = os.path.join(TestToolDir, 'some', 'silly', 'path', 'that', 'should', 'not', 'exist')
TestToolBitName = 'test-path-tool-bit-bit-00.fctb'
TestToolShapeName = 'test-path-tool-bit-shape-00.fcstd'
TestToolLibraryName = 'test-path-tool-bit-library-00.fctl'
def testToolShape(path = TestToolDir, name = TestToolShapeName):
return os.path.join(path, 'Shape', name)
def testToolBit(path = TestToolDir, name = TestToolBitName):
return os.path.join(path, 'Bit', name)
def testToolLibrary(path = TestToolDir, name = TestToolLibraryName):
return os.path.join(path, 'Library', name)
class TestPathToolBit(PathTestUtils.PathTestBase):
def test00(self):
'''Find a tool shapee from file name'''
path = PathToolBit.findShape('endmill.fcstd')
'''Find a tool shape from file name'''
path = PathToolBit._findShape('endmill.fcstd')
self.assertIsNot(path, None)
self.assertNotEqual(path, 'endmill.fcstd')
def test01(self):
'''Find a tool shapee from an invalid absolute path.'''
path = PathToolBit.findShape('/this/is/unlikely/a/valid/path/v-bit.fcstd')
def test01(self):
'''Not find a relative path shape if not stored in default location'''
path = PathToolBit._findShape(TestToolShapeName)
self.assertIsNone(path)
def test02(self):
'''Find a relative path shape if it's local to a bit path'''
path = PathToolBit._findShape(TestToolShapeName, testToolBit())
self.assertIsNot(path, None)
self.assertNotEqual(path, '/this/is/unlikely/a/valid/path/v-bit.fcstd')
self.assertEqual(path, testToolShape())
def test03(self):
'''Not find a tool shape from an invalid absolute path.'''
path = PathToolBit._findShape(testToolShape(TestInvalidDir))
self.assertIsNone(path)
def test04(self):
'''Find a tool shape from a valid absolute path.'''
path = PathToolBit._findShape(testToolShape())
self.assertIsNot(path, None)
self.assertEqual(path, testToolShape())
def test10(self):
'''find the relative path of a tool bit'''
shape = 'endmill.fcstd'
path = PathToolBit.findShape(shape)
'''Find a tool bit from file name'''
path = PathToolBit.findBit('5mm_Endmill.fctb')
self.assertIsNot(path, None)
self.assertGreater(len(path), len(shape))
rel = PathToolBit.findRelativePathShape(path)
self.assertEqual(rel, shape)
self.assertNotEqual(path, '5mm_Endmill.fctb')
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)
'''Not find a relative path bit if not stored in default location'''
path = PathToolBit.findBit(TestToolBitName)
self.assertIsNone(path)
def test12(self):
'''Find a relative path bit if it's local to a library path'''
path = PathToolBit.findBit(TestToolBitName, testToolLibrary())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())
def test13(self):
'''Not find a tool bit from an invalid absolute path.'''
path = PathToolBit.findBit(testToolBit(TestInvalidDir))
self.assertIsNone(path)
def test14(self):
'''Find a tool bit from a valid absolute path.'''
path = PathToolBit.findBit(testToolBit())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())
def test20(self):
'''Find a tool library from file name'''
path = PathToolBit.findBit('5mm_Endmill.fctb')
self.assertIsNot(path, None)
self.assertNotEqual(path, '5mm_Endmill.fctb')
def test21(self):
'''Not find a relative path library if not stored in default location'''
path = PathToolBit.findBit(TestToolBitName)
self.assertIsNone(path)
def test22(self):
'''[skipped] Find a relative path library if it's local to <what?>'''
# this is not a valid test for libraries because t
self.assertTrue(True)
def test23(self):
'''Not find a tool library from an invalid absolute path.'''
path = PathToolBit.findBit(testToolBit(TestInvalidDir))
self.assertIsNone(path)
def test24(self):
'''Find a tool library from a valid absolute path.'''
path = PathToolBit.findBit(testToolBit())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())

View File

@@ -0,0 +1,12 @@
{
"version": 2,
"name": "5mm Endmill",
"shape": "endmill.fcstd",
"parameter": {
"CuttingEdgeHeight": "30.0000 mm",
"Diameter": "5.0000 mm",
"Length": "50.0000 mm",
"ShankDiameter": "3.0000 mm"
},
"attribute": {}
}

View File

@@ -0,0 +1,41 @@
{
"tools": [
{
"nr": 1,
"path": "5mm_Endmill.fctb"
},
{
"nr": 2,
"path": "5mm_Drill.fctb"
},
{
"nr": 3,
"path": "6mm_Ball_End.fctb"
},
{
"nr": 4,
"path": "6mm_Bullnose.fctb"
},
{
"nr": 5,
"path": "60degree_Vbit.fctb"
},
{
"nr": 6,
"path": "45degree_chamfer.fctb"
},
{
"nr": 7,
"path": "slittingsaw.fctb"
},
{
"nr": 8,
"path": "probe.fctb"
},
{
"nr": 9,
"path": "5mm-thread-cutter.fctb"
}
],
"version": 1
}