diff --git a/cadquery/assembly.py b/cadquery/assembly.py index 494dc53d3..fbc29b7f9 100644 --- a/cadquery/assembly.py +++ b/cadquery/assembly.py @@ -196,14 +196,16 @@ def __init__( loc: Optional[Location] = None, name: Optional[str] = None, color: Optional[Color] = None, + metadata: Optional[Dict[str, Any]] = None, ): """ construct an assembly :param obj: root object of the assembly (default: None) :param loc: location of the root object (default: None, interpreted as identity transformation) - :param name: unique name of the root object (default: None, reasulting in an UUID being generated) + :param name: unique name of the root object (default: None, resulting in an UUID being generated) :param color: color of the added object (default: None) + :param metadata: a store for user-defined metadata (default: None) :return: An Assembly object. @@ -222,6 +224,7 @@ def __init__( self.loc = loc if loc else Location() self.name = name if name else str(uuid()) self.color = color if color else None + self.metadata = metadata if metadata else {} self.parent = None self.children = [] @@ -235,7 +238,7 @@ def _copy(self) -> "Assembly": Make a deep copy of an assembly """ - rv = self.__class__(self.obj, self.loc, self.name, self.color) + rv = self.__class__(self.obj, self.loc, self.name, self.color, self.metadata) for ch in self.children: ch_copy = ch._copy() diff --git a/tests/test_assembly.py b/tests/test_assembly.py index ca44db041..c7cc0335f 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -87,6 +87,35 @@ def box_and_vertex(): return assy +@pytest.fixture +def metadata_assy(): + + b1 = cq.Solid.makeBox(1, 1, 1) + b2 = cq.Workplane().box(1, 1, 2) + + assy = cq.Assembly( + b1, + loc=cq.Location(cq.Vector(2, -5, 0)), + name="base", + metadata={"b1": "base-data"}, + ) + sub_assy = cq.Assembly( + b2, loc=cq.Location(cq.Vector(1, 1, 1)), name="sub", metadata={"b2": "sub-data"} + ) + assy.add(sub_assy) + return assy + + +def test_metadata(metadata_assy): + """Verify the metadata is present in both the base and sub assemblies""" + assert metadata_assy.metadata["b1"] == "base-data" + # The metadata should be able to be modified + metadata_assy.metadata["b2"] = 0 + assert len(metadata_assy.metadata) == 2 + # Test that metadata was copied by _copy() during the processing of adding the subassembly + assert metadata_assy.children[0].metadata["b2"] == "sub-data" + + def solve_result_check(solve_result: dict) -> bool: checks = [ solve_result["status"] == nlopt.XTOL_REACHED,