Skip to content

Commit 84edaf5

Browse files
#926 Initialize assy metadata (#928)
* #926 Initialize assy metadata * Rework typing * Use ubuntu-20.04 * Back to 18.04 Co-authored-by: AU <[email protected]>
1 parent 5f65afd commit 84edaf5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

cadquery/assembly.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,16 @@ def __init__(
196196
loc: Optional[Location] = None,
197197
name: Optional[str] = None,
198198
color: Optional[Color] = None,
199+
metadata: Optional[Dict[str, Any]] = None,
199200
):
200201
"""
201202
construct an assembly
202203
203204
:param obj: root object of the assembly (default: None)
204205
:param loc: location of the root object (default: None, interpreted as identity transformation)
205-
:param name: unique name of the root object (default: None, reasulting in an UUID being generated)
206+
:param name: unique name of the root object (default: None, resulting in an UUID being generated)
206207
:param color: color of the added object (default: None)
208+
:param metadata: a store for user-defined metadata (default: None)
207209
:return: An Assembly object.
208210
209211
@@ -222,6 +224,7 @@ def __init__(
222224
self.loc = loc if loc else Location()
223225
self.name = name if name else str(uuid())
224226
self.color = color if color else None
227+
self.metadata = metadata if metadata else {}
225228
self.parent = None
226229

227230
self.children = []
@@ -235,7 +238,7 @@ def _copy(self) -> "Assembly":
235238
Make a deep copy of an assembly
236239
"""
237240

238-
rv = self.__class__(self.obj, self.loc, self.name, self.color)
241+
rv = self.__class__(self.obj, self.loc, self.name, self.color, self.metadata)
239242

240243
for ch in self.children:
241244
ch_copy = ch._copy()

tests/test_assembly.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ def box_and_vertex():
8787
return assy
8888

8989

90+
@pytest.fixture
91+
def metadata_assy():
92+
93+
b1 = cq.Solid.makeBox(1, 1, 1)
94+
b2 = cq.Workplane().box(1, 1, 2)
95+
96+
assy = cq.Assembly(
97+
b1,
98+
loc=cq.Location(cq.Vector(2, -5, 0)),
99+
name="base",
100+
metadata={"b1": "base-data"},
101+
)
102+
sub_assy = cq.Assembly(
103+
b2, loc=cq.Location(cq.Vector(1, 1, 1)), name="sub", metadata={"b2": "sub-data"}
104+
)
105+
assy.add(sub_assy)
106+
return assy
107+
108+
109+
def test_metadata(metadata_assy):
110+
"""Verify the metadata is present in both the base and sub assemblies"""
111+
assert metadata_assy.metadata["b1"] == "base-data"
112+
# The metadata should be able to be modified
113+
metadata_assy.metadata["b2"] = 0
114+
assert len(metadata_assy.metadata) == 2
115+
# Test that metadata was copied by _copy() during the processing of adding the subassembly
116+
assert metadata_assy.children[0].metadata["b2"] == "sub-data"
117+
118+
90119
def solve_result_check(solve_result: dict) -> bool:
91120
checks = [
92121
solve_result["status"] == nlopt.XTOL_REACHED,

0 commit comments

Comments
 (0)