From f4663cff7ac1d2dd5821db3bd0888a805a0a920a Mon Sep 17 00:00:00 2001 From: adam-urbanczyk <13981538+adam-urbanczyk@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:39:34 +0100 Subject: [PATCH 1/4] Query STEP model directly to collect additional subshape names for solids --- cadquery/occ_impl/importers/assembly.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/cadquery/occ_impl/importers/assembly.py b/cadquery/occ_impl/importers/assembly.py index f85ce5752..3bb438a3b 100644 --- a/cadquery/occ_impl/importers/assembly.py +++ b/cadquery/occ_impl/importers/assembly.py @@ -10,6 +10,8 @@ from OCP.TDocStd import TDocStd_Document from OCP.TDataStd import TDataStd_Name, TDataStd_TreeNode from OCP.STEPCAFControl import STEPCAFControl_Reader +from OCP.XSControl import XSControl_TransferReader +from OCP.StepRepr import StepRepr_RepresentationItem from OCP.XCAFDoc import ( XCAFDoc_ColorSurf, XCAFDoc_DocumentTool, @@ -165,7 +167,10 @@ def importStep(assy: AssemblyProtocol, path: str, unit: UnitLiterals = "MM"): # Transfer the contents of the STEP file to the document step_reader.Transfer(doc) - _importDoc(doc, assy) + # Collect the STEP model for possible subshape related workarounds + transfer_reader = step_reader.Reader().WS().TransferReader() + + _importDoc(doc, assy, transfer_reader) def importXbf(assy: AssemblyProtocol, path: str): @@ -228,7 +233,11 @@ def importXml(assy: AssemblyProtocol, path: str): _importDoc(doc, assy) -def _importDoc(doc: TDocStd_Document, assy: AssemblyProtocol): +def _importDoc( + doc: TDocStd_Document, + assy: AssemblyProtocol, + transfer_reader: XSControl_TransferReader | None = None, +): def _process_label(lbl: TDF_Label, parent: AssemblyProtocol): """ Recursive method to process the assembly in a top-down manner. @@ -348,6 +357,17 @@ def _process_label(lbl: TDF_Label, parent: AssemblyProtocol): # Save the color info via the assembly subshape mechanism current.addSubshape(Shape.cast(cur_shape), color=color) + # if provided, check the STEP data model to collect additional subshapes on solid level + if transfer_reader: + entity = cast( + StepRepr_RepresentationItem, + transfer_reader.EntityFromShapeResult(final_shape, 1), + ) + name = entity.Name().ToCString() + + if name: + current.addSubshape(cq_shape, name=name) + return parent # Shape and color tools for extracting XCAF data From 3cf8deb5960a5ba04cc64d8f81c076fdb0907b79 Mon Sep 17 00:00:00 2001 From: adam-urbanczyk <13981538+adam-urbanczyk@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:44:35 +0100 Subject: [PATCH 2/4] Add a test --- tests/test_assembly.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_assembly.py b/tests/test_assembly.py index d98514a81..9be3be0ab 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -2662,3 +2662,24 @@ def test_assembly_without_children_roundtrip(kind, tmpdir): loaded = cq.Assembly.load(path) assert loaded.toCompound().Volume() == approx(1) + + +@pytest.mark.parametrize("kind", ["step", "xml", "xbf"]) +def test_assembly_with_solid_subshape_roundtrip(kind, tmpdir): + """ + An assembly with a solid that is also tagged as a subshape. + """ + + assy = cq.Assembly(name="root").add(box(1, 1, 1), name="child") + + assy["child"].addSubshape(assy["child"].obj, name="SOLID_TAG") + assy["child"].addSubshape(assy["child"].obj.face(), name="FACE_TAG") + + path = os.path.join(tmpdir, f"no_children.{kind}") + assy.export(path) + + loaded = cq.Assembly.load(path) + child_subshape_names = loaded["child"]._subshape_names.values() + + assert "FACE_TAG" in child_subshape_names + assert "SOLID_TAG" in child_subshape_names From a1406a68fc8c529f29a606ac7f3e53f5329fcd0e Mon Sep 17 00:00:00 2001 From: adam-urbanczyk <13981538+adam-urbanczyk@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:10:58 +0100 Subject: [PATCH 3/4] Make the postproessing more robust --- cadquery/occ_impl/importers/assembly.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cadquery/occ_impl/importers/assembly.py b/cadquery/occ_impl/importers/assembly.py index 3bb438a3b..b594187aa 100644 --- a/cadquery/occ_impl/importers/assembly.py +++ b/cadquery/occ_impl/importers/assembly.py @@ -359,14 +359,14 @@ def _process_label(lbl: TDF_Label, parent: AssemblyProtocol): # if provided, check the STEP data model to collect additional subshapes on solid level if transfer_reader: - entity = cast( + if isinstance( + entity := transfer_reader.EntityFromShapeResult( + final_shape, 1 + ), StepRepr_RepresentationItem, - transfer_reader.EntityFromShapeResult(final_shape, 1), - ) - name = entity.Name().ToCString() - - if name: - current.addSubshape(cq_shape, name=name) + ): + if name := entity.Name().ToCString(): + current.addSubshape(cq_shape, name=name) return parent From 6619d513ffbbf42bd5637ad3d774c35f700eef6c Mon Sep 17 00:00:00 2001 From: AU Date: Sun, 8 Nov 2026 19:49:05 +0100 Subject: [PATCH 4/4] Fix wrong file name --- tests/test_assembly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_assembly.py b/tests/test_assembly.py index 9be3be0ab..b1b1960fa 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -2675,7 +2675,7 @@ def test_assembly_with_solid_subshape_roundtrip(kind, tmpdir): assy["child"].addSubshape(assy["child"].obj, name="SOLID_TAG") assy["child"].addSubshape(assy["child"].obj.face(), name="FACE_TAG") - path = os.path.join(tmpdir, f"no_children.{kind}") + path = os.path.join(tmpdir, f"solid_subshape.{kind}") assy.export(path) loaded = cq.Assembly.load(path)