Skip to content

Commit

Permalink
Merge fb1c302 into 5f65afd
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr authored Dec 10, 2021
2 parents 5f65afd + fb1c302 commit 3cdb43c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cadquery/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = []
Expand All @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions tests/test_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3cdb43c

Please sign in to comment.