Using assemblies in assemblies to reuse components

25 views
Skip to first unread message

Oliver

unread,
May 26, 2024, 4:29:40 AMMay 26
to CadQuery
Hello all,

I just started with Cadquery and tried to create my first model. I got at a point where I needed to use the some more complex geometry several times. I think this is a common thing you are facing during the creation of a 3D model. And I think it is also useful if you want to reuse the same geometry (let's say a holder) which somehow standard in several models.
As far as I have understood, assemblies should be used for this, correct?
I tried this, but it only worked partly. My main problem is currently to setup the constrains of the final assembly. I found that in the examples often tags are used, so I started with that. But I found that I cannot reference the tags from a subassembly. I made a short simple example which hopefully says more than thousand words:

    import cadquery as cq
   
    #### first part as subassembly ###
    subpart1 = cq.Workplane("XY").box(10,10,10)
    subpart1.faces(">Z").tag("top")
   
    subpart2 = cq.Workplane("XY").box(3,3,3)
   
   
    subassembly = (cq.Assembly()
        .add(subpart1, name="subpartX1")
        .add(subpart2, name="subpartX2")
       
        .constrain("subpartX1@faces@>X", "subpartX2@faces@>X", "Plane")
    )
    subassembly.solve()
    #show_object(subassembly)
   
   
    ### second part ###
    part2 = cq.Workplane("XY").box(5,15,5)
    part2.faces(">Y").tag("left")
    part2.faces(">X").tag("right")
   
   
    ### the assemblies ###
   
    #adding the subassembly as compound works, but I cannot reference the tag from the subassembly
    assy1 = (
            cq.Assembly()
            .add(subassembly.toCompound(), name="partX", color=cq.Color("orange"))
            .add(part2, name="part2", color=cq.Color("red"))
           
            #this works
            .constrain("partX@faces@>X", "part2?right", "Plane")
           
            #this does not work
            #.constrain("partX?top", "part2?right", "Plane")
    )
    assy1.solve()  
    show_object(assy1)
   
    #adding the subassembly as assembly works, but the constrain is not working
    assy2 = (
            cq.Assembly()
            .add(subassembly, name="partX", color=cq.Color("orange"))
            .add(part2, name="part2", color=cq.Color("red"))
           
            #this does not work
            #.constrain("partX@faces@>X", "part2?right", "Plane")
    )
    #assy2.solve()  
    #show_object(assy2)

Lorenz

unread,
May 26, 2024, 11:38:19 AMMay 26
to CadQuery
Thanks for sharing the example.  A related open issue is:

Allow constraints involving complete subassys
https://github.com/CadQuery/cadquery/issues/1239

Adam Urbanczyk

unread,
May 26, 2024, 3:03:13 PMMay 26
to CadQuery
You need to indicate specific subpart like so:

assy2 = (
        cq.Assembly()
        .add(subassembly, name="partX", color=cq.Color("orange"))
        .add(part2, name="part2", color=cq.Color("red"))
       
        #NB: partX/subpartX1
        .constrain("partX/subpartX1@faces@>X", "part2?right", "Plane")
)
assy2.solve()

Oliver

unread,
May 28, 2024, 2:49:55 PMMay 28
to CadQuery
Perfect, that is working now!
Do I have missed this method to reference parts by using "/" in the documentation?
If not, I think it would be very helpful to add an example for it.

Thanks a lot for your support!!!

Adam Urbanczyk

unread,
May 28, 2024, 3:04:59 PMMay 28
to CadQuery
I think it is indeed not mentioned. A PR would be nice, for now I'm opening an issue.
Reply all
Reply to author
Forward
0 new messages