Skip to content

Add assy imprint method - #2128

Open
adam-urbanczyk wants to merge 18 commits into
masterfrom
adam-assy-imprint-method
Open

adam-urbanczyk wants to merge 18 commits into
masterfrom
adam-assy-imprint-method

Conversation

@adam-urbanczyk

@adam-urbanczyk adam-urbanczyk commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #2075

  • cq.Assembly.imprint() method with subshape handling
  • tests
  • docs
  • check bool op for errors (this includes imprint)

I added (as it turns out without need) undoMove and undoMoved to Shape. Additionally reworked slightly how op history is handling default values. Nice to see that this feature can be used to develop new CQ features.

@adam-urbanczyk
adam-urbanczyk marked this pull request as draft November 6, 2026 17:46
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.20%. Comparing base (082b8de) to head (e054e3f).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2128      +/-   ##
==========================================
+ Coverage   95.85%   96.20%   +0.34%     
==========================================
  Files          30       30              
  Lines        9651    11323    +1672     
  Branches     1460     1877     +417     
==========================================
+ Hits         9251    10893    +1642     
- Misses        246      274      +28     
- Partials      154      156       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

shimwell added a commit to fusion-energy/paramak that referenced this pull request Nov 6, 2026
paramak's remove() matched parts with endswith('/<name>') on the names
yielded by iterating the assembly. CadQuery/cadquery#2128 changes those
names so direct children of the root no longer carry the root prefix,
which would make remove() silently return the full assembly.

cadquery has had Assembly.remove since 2.6.0 (paramak's floor). It
removes the part in place by its key, keeps nested structure and raises
ValueError for an unknown name. Drop the override and test cadquery's
method on plain assemblies, nested sub-assemblies and a full reactor.
@shimwell

shimwell commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

this looks useful for a few workflows in cad-to-dagmc where i have functions that take assemblies and imprint them.

It might be useful to allow the assembly to know if it has been imprinted or know so that if imprinted assemblies are passed around the receiver does not accidentally imprint them again

I was looking into how this improves my upstream codes and got a few keyerrors that might be worth considering

@shimwell

shimwell commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

KeyError when a part (or a named sub-shape) is not changed by the imprint

import cadquery as cq

a = cq.Assembly()
a.add(cq.Workplane().box(10, 10, 10).val(), name="p0")
a.add(cq.Workplane().transformed(offset=(50, 0, 0)).box(10, 10, 10).val(), name="p1")
a.imprint()  # KeyError

@shimwell

shimwell commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Parts with a loc are moved twice and lose their shared faces

a = cq.Assembly()
a.add(cq.Workplane().box(10, 10, 10).val(), name="a")
a.add(cq.Workplane().box(10, 10, 10).val(), name="b", loc=cq.Location((10, 0, 0)))
a.imprint()
# "b" now spans x 15..25 (should be 5..15), and the compound has 12 faces instead of 11

@adam-urbanczyk

Copy link
Copy Markdown
Member Author

Thanks @shimwell , this is still a draft. I would not yet recommend testing.

@shimwell

shimwell commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the update. I tested 44d4900a with cad_to_dagmc (fusion-energy/cad_to_dagmc#247, which switches our imprinting over to Assembly.imprint). it got further than first time i tried

One case still fails

a Workplane or Compound part that the imprint leaves untouched still raises KeyError.

import cadquery as cq

a = cq.Assembly()
a.add(cq.Workplane().box(10, 10, 10), name="a")
a.add(cq.Workplane().box(10, 10, 10).translate((50, 0, 0)), name="b")
a.imprint()  # KeyError

The same thing happens with cq.Compound.makeCompound([box]) parts. The same two parts as Solids imprint fine.

I did a local fix in which appears to help

val = d[el] if default is None else d.get(el, el)

shimwell added a commit to fusion-energy/cad_to_dagmc that referenced this pull request Nov 20, 2026
Brings in main up to #255. The conflicts were the tag length check that
main added beside the material tag count check, which now counts parts
since the remapping helpers are gone, and main's rewrite of the imprinting
docs.

The overlaps docs said the Gmsh backend writes overlapping solids without
an error. Its imprint now refuses the same overlaps as the mesher's, as
each one splits a part into several solids.

CadQuery 2.8.0, the current release, has no Assembly.imprint, so
imprint_assembly falls back to the functional imprint when it is missing.
The fallback puts the imprinted solids back in part order and raises the
same overlapping solids error, so the gmsh paths behave the same either
way. On the legacy imprint, "full" and None glue raise RuntimeError again,
as they did before this branch. The requirement goes back to
cadquery>=2.8.0, CI tests stable, master and the CadQuery/cadquery#2128
branch, and the imprint_assembly tests run against both implementations.
TODOs mark the fallback for removal once a CadQuery release includes
Assembly.imprint.
@shimwell

Copy link
Copy Markdown
Contributor

This now works for my use cases. Many thanks

@shimwell

Copy link
Copy Markdown
Contributor

Found one more edge case imprinting an assembly with a single part raises an error, is this desired, perhaps it should just pass unless the solid connects back on itself when i guess it should do something (like the 360 degree plasma on paramak)

import cadquery as cq

assy = cq.Assembly().add(cq.Workplane().box(1, 1, 1), name="box")
assy.imprint()  # ValueError: Null TopoDS_Shape object

@adam-urbanczyk

adam-urbanczyk commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

I added error checks to the bool ops, so now you'll get:

ValueError: Bool operation failed: BOPAlgo_AlertTooFewArguments

Comment thread setup.py Resolved

@lorenzncode lorenzncode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from cadquery import Assembly, Location, Color
from cadquery.func import box

box_bottom = box(1, 1, 1)
box_top = box(1, 1, 1)

assy = Assembly()
assy.add(box_bottom, name="box_bottom")
assy.add(box_top, name="box_top", loc=Location(z=1))

assy["box_bottom"].addSubshape(box_bottom.face("<Z"), name="bottom_face")
myface = assy["box_bottom"]["bottom_face"]
assy.imprint() # KeyError

I tested with addSubShape to name an unchanged face. imprint raises KeyError in this case.

@adam-urbanczyk

Copy link
Copy Markdown
Member Author

Thanks, should be fixed now.

@jmwright jmwright left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adam-urbanczyk there are just a few comments.

Comment thread cadquery/assembly.py
for obj, name, loc in zip(objs, names, locs):
obj_imp = op.modified(obj, True)

# NB: loc.inverted() and not undMoved() due to how imprinting bool op works

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# NB: loc.inverted() and not undMoved() due to how imprinting bool op works
# NB: loc.inverted() and not undoMoved() due to how imprinting bool op works

Comment thread cadquery/assembly.py
"""
Imprint assembly in-place.

NB: this function will tranform all non-Shape obj (e.g. Workplane) fields into

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NB: this function will tranform all non-Shape obj (e.g. Workplane) fields into
NB: this function will transform all non-Shape obj (e.g. Workplane) fields into

Comment thread doc/assy.rst

For downstream processing including meshing and simulations it might be needed to imprint assembly objects, i.e. to make sure that faces that are geometrically
connected are also topologically connected. This can be achieved using the :py:meth:`cadquery.Assembly.imprint` method. For complex models imprinting can be very time-consuming so
it often beneficial to only imprint subassemblies that are known to have common faces, or imprint different subassemblies with different settings. Last, but not least, imprinting handles subshape

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it often beneficial to only imprint subassemblies that are known to have common faces, or imprint different subassemblies with different settings. Last, but not least, imprinting handles subshape
it is often beneficial to only imprint subassemblies that are known to have common faces, or imprint different subassemblies with different settings. Last, but not least, imprinting handles subshape

Comment thread cadquery/assembly.py
names.append(name)
locs.append(loc)

result = imprint(*objs, tol=tol, glue=glue, history=hist)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is result ever used?

@lorenzncode lorenzncode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from cadquery import Assembly, Location
from cadquery.func import box

box_bottom = box(2, 2, 1)
box_top = box(1, 1, 1)

assy = Assembly()
assy.add(box_bottom, name="box_bottom")
assy.add(box_top, name="box_top", loc=Location(z=1))

assy["box_bottom"].addSubshape(box_bottom.face(">Z"), name="face_at_z1")
assy.imprint()
assy.export("split_face.step")  # AssertionError: Invalid subshape

Here STEP export fails after imprint splits a named face.

@lorenzncode lorenzncode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from cadquery import Assembly, Location
from cadquery.func import box, compound

box_bottom = box(1, 1, 1)
box_top = box(1, 1, 1)

assy = Assembly()
assy.add(compound(compound(box_bottom)), name="box_bottom")
assy.add(box_top, name="box_top", loc=Location(z=1))
assy.imprint()

# The boxes touch at z=1, so should count as one unique face.
result = assy.toCompound()
assert len(result.Solids()) == 2
face_count = len(result.Faces())
assert face_count == 11, f"Expected 11 unique faces, got {face_count}"

The resulting number of faces is unexpected with a nested compound.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement cq.Assembly.imprint

4 participants