cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

How to constrain an object with an object added to a different assembly?

Open Vishnu-C opened this issue 8 months ago • 3 comments

import cadquery as cq

# create an assembly 
assmebly1 = cq.Assembly(name="assembly1")
# add box to assembly1
box = cq.Workplane().box(1, 1, 1)

assmebly1.add(box, name="box1", color=cq.Color("green"))
assmebly1.add(box, name="box2", color=cq.Color("yellow"))

assmebly1.constrain("box1@faces@<X", "box2@faces@<X", "Axis", param=0.0)
assmebly1.constrain("box1@faces@<Y", "box2@faces@<Y", "Axis", param=0.0)
assmebly1.constrain("box1@faces@<Z", "box2@faces@<Z", "PointInPlane", param=10.0)
assmebly1.solve()

assmebly2 = cq.Assembly(name="assembly2")
# add box to assembly2
assmebly2.add(box, name="box3", color=cq.Color("purple"))

# -----> how to constrain box3 in assembly2 with box1 from assembly1?
# I should not add box1 to assembly2

# assmebly2.solve()

https://github.com/user-attachments/assets/5d9a8685-ba41-4578-b74b-65e0d071fc67

Vishnu-C avatar May 03 '25 08:05 Vishnu-C

Related: https://github.com/CadQuery/cadquery/issues/1239

It is possible to reference the location of the object in the other assy:

assembly2.add(box, name="box3", loc=assembly1.objects['box2'].loc)

lorenzncode avatar May 03 '25 21:05 lorenzncode

In general you cannot reference unrelated assembly. The example above will not update if assy1 changes. You could add assy1 to assy2 as a sub-assembly.

adam-urbanczyk avatar Jun 03 '25 05:06 adam-urbanczyk

For my use case I need the object only to be referred and not add to the current assembly. Did a trick by adding the object with its solved location, solve the current assembly and use remove() function, which fortunately does not re-solve constrains after remove.

assembly2.add(box, name="box3", loc=assembly1.objects['box2'].loc)
// add constraints
...
assembly2.solve()
assembly2.remove("box3")

Cheers!

Vishnu-C avatar Jun 08 '25 15:06 Vishnu-C