Skip to content

Commit 436ab58

Browse files
authored
Merge pull request #1270 from lorenzncode/assy-hier
Remove one level of hierarchy in STEP export
2 parents 23b4797 + 45a4f30 commit 436ab58

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,12 @@ def toCAF(
152152
tool.SetAutoNaming_s(False)
153153
ctool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
154154

155-
# add root
156-
top = tool.NewShape()
157-
TDataStd_Name.Set_s(top, TCollection_ExtendedString("CQ assembly"))
158-
159155
# used to store labels with unique part-color combinations
160156
unique_objs: Dict[Tuple[Color, AssemblyObjects], TDF_Label] = {}
161157
# used to cache unique, possibly meshed, compounds; allows to avoid redundant meshing operations if same object is referenced multiple times in an assy
162158
compounds: Dict[AssemblyObjects, Compound] = {}
163159

164-
def _toCAF(el, ancestor, color):
160+
def _toCAF(el, ancestor, color) -> TDF_Label:
165161

166162
# create a subassy
167163
subassy = tool.NewShape()
@@ -207,11 +203,14 @@ def _toCAF(el, ancestor, color):
207203
for child in el.children:
208204
_toCAF(child, subassy, current_color)
209205

210-
# add the current subassy to the higher level assy
211-
tool.AddComponent(ancestor, subassy, el.loc.wrapped)
206+
if ancestor:
207+
# add the current subassy to the higher level assy
208+
tool.AddComponent(ancestor, subassy, el.loc.wrapped)
209+
210+
return subassy
212211

213212
# process the whole assy recursively
214-
_toCAF(assy, top, None)
213+
top = _toCAF(assy, None, None)
215214

216215
tool.UpdateAssemblies()
217216

tests/test_assembly.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,21 @@ def test_assembly(simple_assy, nested_assy):
479479
assert kvs[-1][0] == "TOP"
480480

481481

482+
@pytest.mark.parametrize(
483+
"assy_fixture, root_name", [("simple_assy", None), ("nested_assy", "TOP")],
484+
)
485+
def test_assy_root_name(assy_fixture, root_name, request):
486+
assy = request.getfixturevalue(assy_fixture)
487+
_, doc = toCAF(assy, True)
488+
root = get_doc_nodes(doc, False)[0]
489+
if root_name:
490+
assert root["name"] == root_name
491+
else:
492+
# When a name is not user-specifed, the name is assigned a UUID
493+
m = re.findall(r"[0-9a-f]+", root["name"])
494+
assert list(map(len, m)) == [8, 4, 4, 4, 12]
495+
496+
482497
def test_step_export(nested_assy, tmp_path_factory):
483498
# Use a temporary directory
484499
tmpdir = tmp_path_factory.mktemp("out")

0 commit comments

Comments
 (0)