Assembly Constraints

356 views
Skip to first unread message

Anthony Sokolowski

unread,
Mar 29, 2021, 7:25:41 AM3/29/21
to CadQuery

I am having trouble understanding assembly constraints.
The assembly that I want to create is below.
part.jpg
My experience is in Autodesk Inventor and this part is created by a mate, flush and flush with offset on each side plate.
With the same thinking I have created the following code and the failed result. It seems as though after I add a third constraint between two objects the original constraints are no longer valid.

import cadquery as cq

base_pl = cq.Workplane().box(600, 1000, 6)
side_pl = cq.Workplane().box(1000, 200, 6)

trough = (
    cq.Assembly()
    .add(base_pl, name='base_pl')
    .add(side_pl, name='side_pl_lhs',color=cq.Color('blue'))
    .add(side_pl, name='side_pl_rhs',color=cq.Color('red'))
    .constrain('base_pl@faces@>Z','side_pl_lhs@faces@>Y','Axis')
    .constrain('base_pl@faces@>X','side_pl_lhs@faces@>Z','Axis')
    .constrain('base_pl@faces@>X','side_pl_lhs@faces@>X','Axis')
    .constrain('base_pl@faces@>Z','side_pl_rhs@faces@>Y','Axis')
    .constrain('base_pl@faces@>X','side_pl_rhs@faces@>Z','Axis')
    .constrain('base_pl@faces@>X','side_pl_rhs@faces@<X','Axis')
    .solve()
)

show_object(trough)
cq-part.png

Can anybody offer assistance?

Jeremy Wright

unread,
Mar 29, 2021, 9:15:31 PM3/29/21
to CadQuery
You might want to have a look at this spindle assembly, which includes constraints with offsets.


I come from more of a mouse-driven background as well, and CQ's assembly constraints can take some getting used to. I only had a few minutes to tinker with your code, but switching to Plane and Point constraints instead of using all Axis constraints started to yield better results.

Marcus Boyd

unread,
Mar 30, 2021, 2:04:35 AM3/30/21
to Anthony Sokolowski, CadQuery
I've only done one side, and I've added some comments about what each constraint is doing that might help.

Keep in mind "Axis" by default sets the normal of the two objects to be pi radians apart. You can set it to zero radians (ie. aligned) by using param=0.


import cadquery as cq

base_pl = cq.Workplane().box(600, 1000, 6)
side_pl = cq.Workplane().box(1000, 200, 6)

trough = (
    cq.Assembly()
    .add(base_pl, name='base_pl')
    .add(side_pl, name='side_pl_lhs',color=cq.Color('blue'))
    # normal of base_pl@faces@>Z 0 degrees to side_pl_lhs@faces@>Y
    .constrain('base_pl@faces@>Z','side_pl_lhs@faces@>Y','Axis', param=0)
    # normal of base_pl@faces@>X 180 degrees to side_pl_lhs@faces@>Z

    .constrain('base_pl@faces@>X','side_pl_lhs@faces@>Z','Axis')
    # offset the center of side_pl_lhs@edges@<Y and >Z from the center of base_pl@edges@>X and >Z
    .constrain(
        'base_pl',
        base_pl.edges(">X and >Z").val().translate((-100, 0, 0)),
        'side_pl_lhs',
        side_pl.edges("<Y and >Z").val(),
        "Point"
    )
    .solve()
)
if "show_object" in locals():
    show_object(trough)

screenshot2021-03-30-163324.png

--
cadquery home: https://github.com/CadQuery/cadquery
post issues at https://github.com/CadQuery/cadquery/issues
run it at home at : https://github.com/CadQuery/CQ-editor
---
You received this message because you are subscribed to the Google Groups "CadQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cadquery+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cadquery/8e661032-7df3-4c75-b73a-095cc8044f23n%40googlegroups.com.

Anthony Sokolowski

unread,
Mar 31, 2021, 6:08:20 AM3/31/21
to CadQuery
Thank you. It will take me some time to work out these assemblies but I am confident that the code example and spindle reference will help greatly!

Craig Trader

unread,
Mar 31, 2021, 8:02:58 AM3/31/21
to CadQuery
Since we're on the topic of Assembly, when will Assembly get the ability to export models? 

Constraint-based modeling is all fine and well, but if I can't get the resulting model out to other programs and tools (eg: my 3D printer) all they are are pretty pictures. On the flip side, I would love to be able to specify colors / materials for parts of my model, particularly if I can export that information in my models (eg: .3MF) for CAM. As it is now, I have to model each component individually and assemble them in a separate program.

- Craig -

Jeremy Wright

unread,
Mar 31, 2021, 9:32:25 AM3/31/21
to CadQuery
https://cadquery.readthedocs.io/en/latest/classreference.html?highlight=save#cadquery.Assembly.save

You can set colors for assembly objects, and I think (maybe) they are exported with STEP. Materials and 3MF support could be feature requests.

Adam Urbanczyk

unread,
Mar 31, 2021, 12:44:44 PM3/31/21
to CadQuery
cq.Assembly has STEP export with colors from day 1: https://cadquery.readthedocs.io/en/latest/assy.html#data-export

Craig Trader

unread,
Apr 1, 2021, 7:28:41 AM4/1/21
to CadQuery
Good to know.
Reply all
Reply to author
Forward
0 new messages