Assembly cutaway/subtract()

24 views
Skip to first unread message

neri-engineering

unread,
Nov 16, 2025, 4:40:23 AMNov 16
to CadQuery
I believe that this was missing from CadQuery some time ago, and I've implemented a solution for myself.  It's the ability to do a cut() on an assembly, e.g. to create a nice colored cutaway drawing of your assembly/design.  Sorry, I don't have the time or internet resources to do a search to find where the appropriate place to post this is.

Code would look like the following.  This is only a general strategy that works at least in some cases.  An intersect() would be very similar; mathematically the intersect() strategy could be implemented by taking an infinitely large box, subtracting from it the intersection solid, and then doing a subtract() with the remaining object.

def subtract(whole_assy, subtrahend):
    if not isinstance(whole_assy, Assembly):
        raise TypeError(
            "subtract(): expected 'whole_assy' to be Assembly object type")
    # Because we don't really want to apply the 'whole_assy.loc' transformation
    # to all children of the Assembly object (and then we'd set that transform
    # to identity), we instead choose to compute the inverse transformation and
    # we pass 'subtrahend' through that inverse.
    inverse = whole_assy.loc.inverse.wrapped.Transformation()
    # We could also minimize the number of times 'subtrahend' is passed through
    # a transformation by keeping a concatenated/cumulative inverse
    # transformation and pass that around.  We choose the more obvious course
    # of action, which may be less optimal but requires less bookkeeping.
    if isinstance(subtrahend, Shape):
        subtrahend = subtrahend._apply_transform(inverse)
    elif isinstance(subtrahend, Workplane):
        subtrahend = subtrahend.newObject(
            [
                obj._apply_transform(inverse)
                if isinstance(obj, Shape)
                else obj
                for obj in subtrahend.objects
            ]
        )
    else:
        raise TypeError(
            "subtract(): expected 'subtrahend' to be Shape or Workplane")
    if whole_assy.obj is not None:
        whole_assy.obj = whole_assy.obj.cut(subtrahend)
    for child in whole_assy.children:
        subtract(child, subtrahend)
    return whole_assy

Sent with Proton Mail secure email.
subtract.py

Jeremy Wright

unread,
Nov 16, 2025, 8:59:39 AMNov 16
to neri-engineering, CadQuery
Thanks for posting this. Do you have a screenshot somewhere of what the result looks like?

--
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 visit https://groups.google.com/d/msgid/cadquery/jD-lfeE991K_kYu7uqUqaUegtZfuFfHfvX2DUF1ooZ-fiTYe5nh8P-ywJEuhENERX7cnhV_5jqdmtnLYsUg4sxSR9iuFqG9yRN3MqJjeUHw%3D%40protonmail.com.

neri-engineering

unread,
Nov 17, 2025, 5:29:03 AMNov 17
to Jeremy Wright, CadQuery
I can't post much, and I cannot include much code, because the design isn't protected by patent.

(In fact I'm searching for potential collaborators for my efforts.)

However suppose you have an Assembly with nested objects (other Assemblies in 'children' array), where each child is put into position with a translation and/or rotation (the 'loc' member of Assembly), then the cutaway() function handles all the children and honors all of the nested transformations which define the children positions.  Furthermore cutaway() does not touch the 'loc' transformations.  It only causes the cut() on the leaf (Workplane, Shape) nodes, in the correct manner.  The 'obj' member of Assembly is the leaf node ('None' if not leaf), from what I recall.

This single subtract() function has saved me TONS of effort.  An intersect() could also be written, which is almost identical.

cutaway.png

Sent with Proton Mail secure email.

Jeremy Wright

unread,
Nov 21, 2025, 11:00:29 AM (12 days ago) Nov 21
to neri-engineering, CadQuery
Reply all
Reply to author
Forward
0 new messages