Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#926 Initialize assy metadata #928

Merged
merged 4 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- template: conda-build.yml@templates
parameters:
name: Linux_38
vmImage: 'ubuntu-18.04'
vmImage: 'ubuntu-20.04'
py_maj: 3
py_min: 8
conda_bld: 3.21.6
Expand All @@ -42,7 +42,7 @@ jobs:
- template: conda-build.yml@templates
parameters:
name: Linux_39
vmImage: 'ubuntu-18.04'
vmImage: 'ubuntu-20.04'
py_maj: 3
py_min: 9
conda_bld: 3.21.6
Expand Down
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