CAM: Replace complete tool management (PR 21425)
This commit is contained in:
@@ -20,134 +20,62 @@
|
||||
# * *
|
||||
# ***************************************************************************
|
||||
|
||||
import Path.Tool.Bit as PathToolBit
|
||||
import CAMTests.PathTestUtils as PathTestUtils
|
||||
import glob
|
||||
from typing import cast
|
||||
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"
|
||||
import uuid
|
||||
import pathlib
|
||||
import FreeCAD
|
||||
from CAMTests.PathTestUtils import PathTestWithAssets
|
||||
from Path.Tool.library import Library
|
||||
from Path.Tool.shape import ToolBitShapeBullnose
|
||||
from Path.Tool.toolbit import ToolBitEndmill, ToolBitBullnose
|
||||
|
||||
|
||||
def testToolShape(path=TestToolDir, name=TestToolShapeName):
|
||||
return os.path.join(path, "Shape", name)
|
||||
TOOL_DIR = pathlib.Path(os.path.realpath(__file__)).parent.parent / "Tools"
|
||||
SHAPE_DIR = TOOL_DIR / "Shape"
|
||||
BIT_DIR = TOOL_DIR / "Bit"
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def printTree(path, indent):
|
||||
print("{} {}".format(indent, os.path.basename(path)))
|
||||
if os.path.isdir(path):
|
||||
if os.path.basename(path).startswith("__"):
|
||||
print("{} ...".format(indent))
|
||||
else:
|
||||
for foo in sorted(glob.glob(os.path.join(path, "*"))):
|
||||
printTree(foo, "{} ".format(indent))
|
||||
|
||||
|
||||
class TestPathToolBit(PathTestUtils.PathTestBase):
|
||||
def test(self):
|
||||
"""Log test setup directory structure"""
|
||||
# Enable this test if there are errors showing up in the build system with the
|
||||
# paths that work OK locally. It'll print out the directory tree, and if it
|
||||
# doesn't look right you know where to look for it
|
||||
print()
|
||||
print("realpath : {}".format(os.path.realpath(__file__)))
|
||||
print(" Tools : {}".format(TestToolDir))
|
||||
print(" dir : {}".format(os.path.dirname(os.path.realpath(__file__))))
|
||||
printTree(os.path.dirname(os.path.realpath(__file__)), " :")
|
||||
|
||||
def test00(self):
|
||||
"""Find a tool shape from file name"""
|
||||
path = PathToolBit.findToolShape("endmill.fcstd")
|
||||
self.assertIsNot(path, None)
|
||||
self.assertNotEqual(path, "endmill.fcstd")
|
||||
|
||||
def test01(self):
|
||||
"""Not find a relative path shape if not stored in default location"""
|
||||
path = PathToolBit.findToolShape(TestToolShapeName)
|
||||
self.assertIsNone(path)
|
||||
|
||||
def test02(self):
|
||||
"""Find a relative path shape if it's local to a bit path"""
|
||||
path = PathToolBit.findToolShape(TestToolShapeName, testToolBit())
|
||||
self.assertIsNot(path, None)
|
||||
self.assertEqual(path, testToolShape())
|
||||
|
||||
def test03(self):
|
||||
"""Not find a tool shape from an invalid absolute path."""
|
||||
path = PathToolBit.findToolShape(testToolShape(TestInvalidDir))
|
||||
self.assertIsNone(path)
|
||||
|
||||
def test04(self):
|
||||
"""Find a tool shape from a valid absolute path."""
|
||||
path = PathToolBit.findToolShape(testToolShape())
|
||||
self.assertIsNot(path, None)
|
||||
self.assertEqual(path, testToolShape())
|
||||
|
||||
def test10(self):
|
||||
class TestPathToolBit(PathTestWithAssets):
|
||||
def testGetToolBit(self):
|
||||
"""Find a tool bit from file name"""
|
||||
path = PathToolBit.findToolBit("5mm_Endmill.fctb")
|
||||
self.assertIsNot(path, None)
|
||||
self.assertNotEqual(path, "5mm_Endmill.fctb")
|
||||
toolbit = self.assets.get("toolbit://5mm_Endmill")
|
||||
self.assertIsInstance(toolbit, ToolBitEndmill)
|
||||
self.assertEqual(toolbit.id, "5mm_Endmill")
|
||||
|
||||
def test11(self):
|
||||
"""Not find a relative path bit if not stored in default location"""
|
||||
path = PathToolBit.findToolBit(TestToolBitName)
|
||||
self.assertIsNone(path)
|
||||
|
||||
def test12(self):
|
||||
"""Find a relative path bit if it's local to a library path"""
|
||||
path = PathToolBit.findToolBit(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.findToolBit(testToolBit(TestInvalidDir))
|
||||
self.assertIsNone(path)
|
||||
|
||||
def test14(self):
|
||||
"""Find a tool bit from a valid absolute path."""
|
||||
path = PathToolBit.findToolBit(testToolBit())
|
||||
self.assertIsNot(path, None)
|
||||
self.assertEqual(path, testToolBit())
|
||||
|
||||
def test20(self):
|
||||
def testGetLibrary(self):
|
||||
"""Find a tool library from file name"""
|
||||
path = PathToolBit.findToolLibrary("Default.fctl")
|
||||
self.assertIsNot(path, None)
|
||||
self.assertNotEqual(path, "Default.fctl")
|
||||
library = self.assets.get("toolbitlibrary://Default")
|
||||
self.assertIsInstance(library, Library)
|
||||
self.assertEqual(library.id, "Default")
|
||||
|
||||
def test21(self):
|
||||
"""Not find a relative path library if not stored in default location"""
|
||||
path = PathToolBit.findToolLibrary(TestToolLibraryName)
|
||||
self.assertIsNone(path)
|
||||
def testBullnose(self):
|
||||
"""Test ToolBitBullnose basic parameters"""
|
||||
shape = self.assets.get("toolbitshape://bullnose")
|
||||
shape = cast(ToolBitShapeBullnose, shape)
|
||||
|
||||
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)
|
||||
bullnose_bit = ToolBitBullnose(shape, id="mybullnose")
|
||||
self.assertEqual(bullnose_bit.get_id(), "mybullnose")
|
||||
|
||||
def test23(self):
|
||||
"""Not find a tool library from an invalid absolute path."""
|
||||
path = PathToolBit.findToolLibrary(testToolLibrary(TestInvalidDir))
|
||||
self.assertIsNone(path)
|
||||
bullnose_bit = ToolBitBullnose(shape)
|
||||
uuid.UUID(bullnose_bit.get_id()) # will raise if not valid UUID
|
||||
|
||||
def test24(self):
|
||||
"""Find a tool library from a valid absolute path."""
|
||||
path = PathToolBit.findToolBit(testToolBit())
|
||||
self.assertIsNot(path, None)
|
||||
self.assertEqual(path, testToolBit())
|
||||
# Parameters should be loaded from the shape file and set on the tool bit's object
|
||||
self.assertEqual(bullnose_bit.obj.Diameter, FreeCAD.Units.Quantity("5.0 mm"))
|
||||
self.assertEqual(bullnose_bit.obj.FlatRadius, FreeCAD.Units.Quantity("1.5 mm"))
|
||||
|
||||
def testToolBitPickle(self):
|
||||
"""Test if ToolBit is picklable"""
|
||||
import pickle
|
||||
|
||||
shape = self.assets.get("toolbitshape://bullnose")
|
||||
shape = cast(ToolBitShapeBullnose, shape)
|
||||
bullnose_bit = ToolBitBullnose(shape, id="mybullnose")
|
||||
try:
|
||||
pickled_bit = pickle.dumps(bullnose_bit)
|
||||
unpickled_bit = pickle.loads(pickled_bit)
|
||||
self.assertIsInstance(unpickled_bit, ToolBitBullnose)
|
||||
self.assertEqual(unpickled_bit.get_id(), "mybullnose")
|
||||
# Add more assertions here to check if other attributes are preserved
|
||||
except Exception as e:
|
||||
self.fail(f"ToolBit is not picklable: {e}")
|
||||
|
||||
Reference in New Issue
Block a user