diff --git a/cadquery/occ_impl/assembly.py b/cadquery/occ_impl/assembly.py index d76422bb7..e08c4a6c1 100644 --- a/cadquery/occ_impl/assembly.py +++ b/cadquery/occ_impl/assembly.py @@ -413,18 +413,27 @@ def _toCAF(el: AssemblyProtocol, ancestor: TDF_Label | None) -> TDF_Label: for child in el.children: _toCAF(child, subassy) + # final rv construction if ancestor and el.children: tool.AddComponent(ancestor, subassy, el.loc.wrapped) rv = subassy elif ancestor: rv = ancestor - else: + elif el.children: # update the top level location rv = TDF_Label() # NB: additional label is needed to apply the location - # set location, is location is identity return subassy + # set location, if location is identity return subassy tool.SetLocation(subassy, assy.loc.wrapped, rv) setName(rv, assy.name, tool) + elif el.obj: + # only root with an object + rv = tool.NewShape() + + lab = tool.AddComponent(rv, lab, el.loc.wrapped) + setName(lab, f"{el.name}", tool) + else: + raise ValueError("Cannot convert an empty assembly to CAF") return rv diff --git a/tests/test_assembly.py b/tests/test_assembly.py index 4e73b518d..5d0597c18 100644 --- a/tests/test_assembly.py +++ b/tests/test_assembly.py @@ -2328,3 +2328,15 @@ def test_special_methods(subshape_assy): with pytest.raises(AttributeError): subshape_assy.cube_123456 + + +def test_shallow_assy(): + """ + toCAF edge case. + """ + + # shallow assy + toCAF(cq.Assembly(cq.Workplane().box(1, 1, 1))) + + with pytest.raises(ValueError): + toCAF(cq.Assembly())