Skip to content

Commit edabe5e

Browse files
Call SetLocation to set top Assembly Location (#1623)
* Fix Assembly Location handling when applied at top level * Add comments * Call SetLocation to set top Assembly Location * Move the code into _toCAF --------- Co-authored-by: AU <[email protected]>
1 parent 9ee703d commit edabe5e

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,15 @@ def _toCAF(el, ancestor, color) -> TDF_Label:
235235
_toCAF(child, subassy, current_color)
236236

237237
if ancestor:
238-
# add the current subassy to the higher level assy
239238
tool.AddComponent(ancestor, subassy, el.loc.wrapped)
239+
rv = subassy
240+
else:
241+
# update the top level location
242+
rv = TDF_Label() # NB: additional label is needed to apply the location
243+
tool.SetLocation(subassy, assy.loc.wrapped, rv)
244+
setName(rv, assy.name, tool)
240245

241-
return subassy
246+
return rv
242247

243248
# process the whole assy recursively
244249
top = _toCAF(assy, None, None)

tests/test_assembly.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
exportVRML,
1515
)
1616
from cadquery.occ_impl.assembly import toJSON, toCAF, toFusedCAF
17-
from cadquery.occ_impl.shapes import Face
17+
from cadquery.occ_impl.shapes import Face, box
1818
from cadquery.occ_impl.geom import Location
1919

2020
from OCP.gp import gp_XYZ
@@ -286,6 +286,31 @@ def boxes7_assy():
286286
return assy
287287

288288

289+
@pytest.fixture
290+
def boxes8_assy():
291+
292+
b0 = box(1, 1, 1)
293+
294+
assy = cq.Assembly(loc=cq.Location(0, 10, 0))
295+
assy.add(b0, name="box0", color=cq.Color("red"))
296+
assy.add(b0, name="box1", color=cq.Color("green"), loc=cq.Location((1, 0, 0)))
297+
298+
return assy
299+
300+
301+
@pytest.fixture
302+
def boxes9_assy():
303+
304+
b0 = box(1, 1, 1)
305+
306+
assy = cq.Assembly(
307+
b0, name="box0", loc=cq.Location(0, 10, 0), color=cq.Color("red")
308+
)
309+
assy.add(b0, name="box1", color=cq.Color("green"), loc=cq.Location((1, 0, 0)))
310+
311+
return assy
312+
313+
289314
@pytest.fixture
290315
def spheres0_assy():
291316

@@ -587,13 +612,29 @@ def test_step_export(nested_assy, tmp_path_factory):
587612

588613
# check that locations were applied correctly
589614
c = cq.Compound.makeCompound(w.solids().vals()).Center()
590-
c.toTuple()
591615
assert pytest.approx(c.toTuple()) == (0, 4, 0)
592616
c2 = cq.Compound.makeCompound(o.solids().vals()).Center()
593-
c2.toTuple()
594617
assert pytest.approx(c2.toTuple()) == (0, 4, 0)
595618

596619

620+
@pytest.mark.parametrize(
621+
"assy_fixture, expected",
622+
[
623+
("boxes8_assy", {"nsolids": 2, "center": (0.5, 10, 0.5)},),
624+
("boxes9_assy", {"nsolids": 2, "center": (0.5, 10, 0.5)},),
625+
],
626+
)
627+
def test_step_export_loc(assy_fixture, expected, request, tmpdir):
628+
stepfile = Path(tmpdir, assy_fixture).with_suffix(".step")
629+
if not stepfile.exists():
630+
assy = request.getfixturevalue(assy_fixture)
631+
assy.save(str(stepfile))
632+
o = cq.importers.importStep(str(stepfile))
633+
assert o.solids().size() == expected["nsolids"]
634+
c = cq.Compound.makeCompound(o.solids().vals()).Center()
635+
assert pytest.approx(c.toTuple()) == expected["center"]
636+
637+
597638
def test_native_export(simple_assy):
598639

599640
exportCAF(simple_assy, "assy.xml")

0 commit comments

Comments
 (0)