Skip to content

Commit 0fc683c

Browse files
jmwrightadam-urbanczyklorenzncode
authored
Added metadata handling to Assembly.add method (#1327)
* Added metadata handling to Assembly.add method * Remove one overload * Update assembly metadata test --------- Co-authored-by: AU <[email protected]> Co-authored-by: Lorenz Neureuter <[email protected]>
1 parent 95f37fd commit 0fc683c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

cadquery/assembly.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def add(
184184
loc: Optional[Location] = None,
185185
name: Optional[str] = None,
186186
color: Optional[Color] = None,
187+
metadata: Optional[Dict[str, Any]] = None,
187188
) -> "Assembly":
188189
"""
189190
Add a subassembly to the current assembly with explicit location and name.
@@ -194,6 +195,7 @@ def add(
194195
:param name: unique name of the root object (default: None, resulting in an UUID being
195196
generated)
196197
:param color: color of the added object (default: None)
198+
:param metadata: a store for user-defined metadata (default: None)
197199
"""
198200
...
199201

@@ -214,6 +216,9 @@ def add(self, arg, **kwargs):
214216
subassy.loc = kwargs["loc"] if kwargs.get("loc") else arg.loc
215217
subassy.name = kwargs["name"] if kwargs.get("name") else arg.name
216218
subassy.color = kwargs["color"] if kwargs.get("color") else arg.color
219+
subassy.metadata = (
220+
kwargs["metadata"] if kwargs.get("metadata") else arg.metadata
221+
)
217222
subassy.parent = self
218223

219224
self.children.append(subassy)

tests/test_assembly.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ def metadata_assy():
132132
)
133133
assy.add(sub_assy)
134134

135+
sub_assy2 = cq.Assembly(name="sub2", metadata={"mykey": "sub2-data"})
136+
sub_assy2.add(
137+
b1, name="sub2-0", loc=cq.Location((1, 0, 0)), metadata={"mykey": "sub2-0-data"}
138+
)
139+
sub_assy2.add(
140+
b1, name="sub2-1", loc=cq.Location((2, 0, 0)), metadata={"mykey": "sub2-1-data"}
141+
)
142+
assy.add(
143+
sub_assy2, metadata={"mykey": "sub2-data-add"}
144+
) # override metadata mykey:sub2-data
145+
135146
return assy
136147

137148

@@ -488,6 +499,9 @@ def test_metadata(metadata_assy):
488499
assert len(metadata_assy.metadata) == 2
489500
# Test that metadata was copied by _copy() during the processing of adding the subassembly
490501
assert metadata_assy.children[0].metadata["b2"] == "sub-data"
502+
assert metadata_assy.children[1].metadata["mykey"] == "sub2-data-add"
503+
assert metadata_assy.children[1].children[0].metadata["mykey"] == "sub2-0-data"
504+
assert metadata_assy.children[1].children[1].metadata["mykey"] == "sub2-1-data"
491505

492506

493507
def solve_result_check(solve_result: dict) -> bool:

0 commit comments

Comments
 (0)