Skip to content

Commit 392e6f4

Browse files
committed
Remove one level of hierarchy in STEP export
Do not hard code root name if unspecified
1 parent 2997a3a commit 392e6f4

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def toCAF(
140140
mesh: bool = False,
141141
tolerance: float = 1e-3,
142142
angularTolerance: float = 0.1,
143-
) -> Tuple[TDF_Label, TDocStd_Document]:
143+
) -> TDocStd_Document:
144144

145145
# prepare a doc
146146
app = XCAFApp_Application.GetApplication_s()
@@ -152,10 +152,6 @@ 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
@@ -207,15 +203,16 @@ 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)
212209

213210
# process the whole assy recursively
214-
_toCAF(assy, top, None)
211+
_toCAF(assy, None, None)
215212

216213
tool.UpdateAssemblies()
217214

218-
return top, doc
215+
return doc
219216

220217

221218
def toVTK(

cadquery/occ_impl/exporters/assembly.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def exportAssembly(assy: AssemblyProtocol, path: str, **kwargs) -> bool:
5050
pcurves = 0
5151
precision_mode = kwargs["precision_mode"] if "precision_mode" in kwargs else 0
5252

53-
_, doc = toCAF(assy, True)
53+
doc = toCAF(assy, True)
5454

5555
session = XSControl_WorkSession()
5656
writer = STEPCAFControl_Writer(session, False)
@@ -75,7 +75,7 @@ def exportCAF(assy: AssemblyProtocol, path: str) -> bool:
7575
name, ext = os.path.splitext(fname)
7676
ext = ext[1:] if ext[0] == "." else ext
7777

78-
_, doc = toCAF(assy)
78+
doc = toCAF(assy)
7979
app = XCAFApp_Application.GetApplication_s()
8080

8181
store = XmlDrivers_DocumentStorageDriver(
@@ -167,7 +167,7 @@ def exportGLTF(
167167
orig_loc = assy.loc
168168
assy.loc *= Location((0, 0, 0), (1, 0, 0), -90)
169169

170-
_, doc = toCAF(assy, True, True, tolerance, angularTolerance)
170+
doc = toCAF(assy, True, True, tolerance, angularTolerance)
171171

172172
writer = RWGltf_CafWriter(TCollection_AsciiString(path), binary)
173173
result = writer.Perform(

tests/test_assembly.py

Lines changed: 18 additions & 3 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")
@@ -600,7 +615,7 @@ def test_save_raises(nested_assy):
600615
def test_leaf_node_count(assy_fixture, count, request):
601616

602617
assy = request.getfixturevalue(assy_fixture)
603-
_, doc = toCAF(assy, True)
618+
doc = toCAF(assy, True)
604619

605620
assert len(get_doc_nodes(doc, True)) == count
606621

@@ -659,7 +674,7 @@ def check_nodes(doc, expected):
659674
assert pytest.approx(n[k], abs=1e-3) == v
660675

661676
assy = request.getfixturevalue(assy_fixture)
662-
_, doc = toCAF(assy, False)
677+
doc = toCAF(assy, False)
663678
check_nodes(doc, expected)
664679

665680

@@ -807,7 +822,7 @@ def check_nodes(doc, expected, is_STEP=False):
807822
assert pytest.approx(n[k], abs=1e-3) == v
808823

809824
assy = request.getfixturevalue(assy_fixture)
810-
_, doc = toCAF(assy, True)
825+
doc = toCAF(assy, True)
811826
check_nodes(doc, expected)
812827

813828
# repeat color check again - after STEP export round trip

0 commit comments

Comments
 (0)