Repository navigation
Add assy imprint method #2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adam-urbanczyk
wants to merge
18
commits into
master
Choose a base branch
from
adam-assy-imprint-method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−15
Open
Add assy imprint method #2128
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6482cf9
move/moved undo and solid/shell history tracking
adam-urbanczyk add645c
Initial impl of Assembly.imprint
adam-urbanczyk cef6929
Add inverted() for consistency
adam-urbanczyk a17ba1d
Optional default value for modified
adam-urbanczyk 93ac7a6
Some Assembly.imprint() fixes
adam-urbanczyk 8382b6d
Fix subshape handling
adam-urbanczyk 56b385a
Add a test
adam-urbanczyk f0c087a
Merge branch 'master' into adam-assy-imprint-method
adam-urbanczyk 337225a
Fix subassy iteration
adam-urbanczyk 44d4900
Test subassy imprinting
adam-urbanczyk 93f16af
Rework default handling
adam-urbanczyk 2cefad6
Extend coverage to other obj types
adam-urbanczyk 27b5290
Update docs
adam-urbanczyk d77f730
x-ref fix
adam-urbanczyk 4c49a3a
Test undoMove(d)
adam-urbanczyk 8114a83
Check bool ops for errors
adam-urbanczyk dfeb5f5
Revert unintended changes
adam-urbanczyk e054e3f
Fix subshape handling and extend the test
adam-urbanczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,15 @@ | |||||
| from itertools import chain | ||||||
|
|
||||||
| from .cq import Workplane | ||||||
| from .occ_impl.shapes import Shape, Compound, isSubshape, compound | ||||||
| from .occ_impl.shapes import ( | ||||||
| Shape, | ||||||
| Compound, | ||||||
| isSubshape, | ||||||
| compound, | ||||||
| GlueLiteral, | ||||||
| imprint, | ||||||
| History, | ||||||
| ) | ||||||
| from .occ_impl.geom import Location | ||||||
| from .occ_impl.assembly import Color, Material | ||||||
| from .occ_impl.solver import ( | ||||||
|
|
@@ -713,12 +721,17 @@ def __iter__( | |||||
| loc: Optional[Location] = None, | ||||||
| name: Optional[str] = None, | ||||||
| color: Optional[Color] = None, | ||||||
| depth: int = 0, | ||||||
| ) -> Iterator[Tuple[Shape, str, Location, Optional[Color]]]: | ||||||
| """ | ||||||
| Assembly iterator yielding shapes, names, locations and colors. | ||||||
| """ | ||||||
|
|
||||||
| name = f"{name}/{self.name}" if name else self.name | ||||||
| if depth <= 1: | ||||||
| name = self.name | ||||||
| else: | ||||||
| name = f"{name}/{self.name}" | ||||||
|
|
||||||
| loc = loc * self.loc if loc else self.loc | ||||||
| color = self.color if self.color else color | ||||||
|
|
||||||
|
|
@@ -728,7 +741,7 @@ def __iter__( | |||||
| ), name, loc, color | ||||||
|
|
||||||
| for ch in self.children: | ||||||
| yield from ch.__iter__(loc, name, color) | ||||||
| yield from ch.__iter__(loc, name, color, depth + 1) | ||||||
|
|
||||||
| def toCompound(self) -> Compound: | ||||||
| """ | ||||||
|
|
@@ -859,3 +872,48 @@ def __setstate__(self, d): | |||||
| """ | ||||||
|
|
||||||
| self.__dict__ = d | ||||||
|
|
||||||
| def imprint(self, tol: float = 0.0, glue: GlueLiteral = "partial") -> Self: | ||||||
| """ | ||||||
| Imprint assembly in-place. | ||||||
|
|
||||||
| NB: this function will tranform all non-Shape obj (e.g. Workplane) fields into | ||||||
| Shape obj fields. | ||||||
| """ | ||||||
|
|
||||||
| hist = History() | ||||||
|
|
||||||
| objs = [] | ||||||
| names = [] | ||||||
| locs = [] | ||||||
|
|
||||||
| for obj, name, loc, _ in self: | ||||||
| objs.append(obj.moved(loc)) | ||||||
| names.append(name) | ||||||
| locs.append(loc) | ||||||
|
|
||||||
| result = imprint(*objs, tol=tol, glue=glue, history=hist) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||||||
| op = hist[-1] | ||||||
|
|
||||||
| 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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| subassy = self.objects[name] | ||||||
| subassy.obj = obj_imp.moved(loc.inverted()) | ||||||
|
|
||||||
| # update all subshape dicts | ||||||
| for subx_dict in ( | ||||||
| subassy._subshape_names, | ||||||
| subassy._subshape_colors, | ||||||
| subassy._subshape_layers, | ||||||
| ): | ||||||
| key_subs = {} | ||||||
| for k in subx_dict: | ||||||
| k_moved = k.moved(loc) | ||||||
| key_subs[k] = op.modified(k_moved, True).moved(loc.inverted()) | ||||||
|
|
||||||
| for k, k_new in key_subs.items(): | ||||||
| subx_dict[k_new] = subx_dict.pop(k) # type: ignore | ||||||
|
|
||||||
| return self | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1411,3 +1411,35 @@ listed along with a color sample below: | |||||
| <div style="background-color:rgba(65,65,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">yellow4</div> | ||||||
| <div style="background-color:rgba(82,155,8,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">yellowgreen</div> | ||||||
| </div> | ||||||
|
|
||||||
|
|
||||||
| Imprinting | ||||||
| ---------- | ||||||
|
|
||||||
| 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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| metadata, but due to how the operation is implemented on the kernel level, subshape keys are modified in place. | ||||||
|
|
||||||
|
|
||||||
| Here is a simple example of imprinting in action. Note that this operation changes the number of unique faces. The touching faces are merged into one modulo their orientation. Additionally, it can | ||||||
| mutate the type of values stored in the :py:attr:`cadquery.Assembly.obj` field. | ||||||
|
|
||||||
| .. cadquery:: | ||||||
| :height: 600px | ||||||
|
|
||||||
| from cadquery import Assembly, Color, Location | ||||||
| from cadquery.func import box | ||||||
|
|
||||||
| assy = cq.Assembly() | ||||||
| assy.add(box(1,1,1), color=Color("cyan")) | ||||||
| assy.add(box(1,1,1), loc=Location(x=1), color=Color("red")) | ||||||
|
|
||||||
| assert assy.toCompound().faces().size() == 12 | ||||||
|
|
||||||
| assy.imprint() | ||||||
|
|
||||||
| assert assy.toCompound().faces().size() == 11 | ||||||
|
|
||||||
| show_object(assy) | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.