-
-
Notifications
You must be signed in to change notification settings - Fork 539
Fix history handling for solid(...) #2089
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
9
commits into
master
Choose a base branch
from
adam-reshape-history-fix
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.
+191
−16
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc42a4e
Rework solid history handling
adam-urbanczyk 1b97f52
Merge branch 'master' into adam-reshape-history-fix
adam-urbanczyk fe7a9e5
Add and fix tests
adam-urbanczyk 762cb02
Add a test case
adam-urbanczyk 6fe1677
Another case
adam-urbanczyk 9b4f1fe
Update history handling for shell args
adam-urbanczyk cdcf38a
History/Op construction tweaks
adam-urbanczyk ee1813a
Merge branch 'master' into adam-reshape-history-fix
adam-urbanczyk 7691860
Coverage tweak
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 |
|---|---|---|
|
|
@@ -5496,6 +5496,19 @@ def _compound_or_shape(s: TopoDS_Shape | Sequence[TopoDS_Shape]) -> Shape: | |
| return rv | ||
|
|
||
|
|
||
| def _compound(s: TopoDS_Shape | Sequence[TopoDS_Shape]) -> Compound: | ||
| """ | ||
| Convert a list of TopoDS_Shape to a Compound. | ||
| """ | ||
|
|
||
| if isinstance(s, TopoDS_Shape): | ||
| rv = Compound.makeCompound([_normalize(Shape.cast(s))]) | ||
| else: | ||
| rv = Compound.makeCompound([_normalize(Shape.cast(el)) for el in s]) | ||
|
|
||
| return rv | ||
|
|
||
|
|
||
| def _shape(s: TopoDS_Shape, _: type[T]) -> T: | ||
| """ | ||
| Cast a TopoDS_Shape to a Shape of the specfied type. | ||
|
|
@@ -5805,6 +5818,54 @@ def _combine_ops(op: Op, *ops: Op) -> Op: | |
| return op | ||
|
|
||
|
|
||
| def _apply_reshape(op: Op, ctx: ShapeBuild_ReShape) -> Op: | ||
| """ | ||
| Apply (if applicable) additional ReShape history to an exisitn Op. Used by solid. | ||
| """ | ||
|
|
||
| hist = ctx.History() | ||
|
|
||
| if hist.HasModified(): | ||
|
|
||
| # update modified | ||
| for key, val in op._modified.items(): | ||
|
|
||
| processed = [] | ||
|
|
||
| for subshape in val: | ||
| modified = hist.Modified(subshape.wrapped) | ||
|
|
||
| if modified: | ||
|
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. It looks like an empty OCP list is |
||
| processed.extend([Shape.cast(el) for el in modified]) | ||
| else: | ||
| processed.append(subshape) | ||
|
|
||
| op._modified[key] = compound(processed) | ||
|
|
||
| # update images | ||
| for key, val in op._images.items(): | ||
|
|
||
| mod = hist.Modified(val.wrapped) | ||
| if not mod.IsEmpty(): | ||
| op._images[key] = Shape.cast(mod.First()) | ||
|
|
||
| return op | ||
|
|
||
|
|
||
| def _polish_images(op: Op, s: Shape) -> Op: | ||
| """ | ||
| Workaround for Reshape context not tracking (face, face) relations when fixing solids. | ||
| """ | ||
|
|
||
| face_dict = {f: f for f in s.Faces()} | ||
|
|
||
| for k, v in op._images.items(): | ||
| if isinstance(k, Face): | ||
| op._images[k] = compound([face_dict[f] for f in v.Faces()]) | ||
|
|
||
| return op | ||
|
|
||
|
|
||
| class History: | ||
| """ | ||
| Operation history. | ||
|
|
@@ -5940,7 +6001,7 @@ def _update_history( | |
|
|
||
| if has_modifidied: | ||
| try: | ||
| mod = _compound_or_shape(list(builder.Modified(wrapped))) | ||
| mod = _compound(list(builder.Modified(wrapped))) | ||
| if mod: | ||
| if el in op._modified: | ||
| op._modified[el] |= mod | ||
|
|
@@ -5983,12 +6044,12 @@ def _remap_history_values(history: History | None, aux: History,) -> None: | |
|
|
||
| # handle last shape | ||
| last_op._last_shape = compound( | ||
| [last_aux._modified.get(el, el) for el in last_op._last_shape] | ||
| [_normalize(last_aux._modified.get(el, el)) for el in last_op._last_shape] | ||
| ) | ||
|
|
||
| # handle first shape | ||
| last_op._first_shape = compound( | ||
| [last_aux._modified.get(el, el) for el in last_op._first_shape] | ||
| [_normalize(last_aux._modified.get(el, el)) for el in last_op._first_shape] | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -6365,25 +6426,49 @@ def solid( | |
| Build solid from faces or shells. | ||
| """ | ||
|
|
||
| ctx = ShapeBuild_ReShape() | ||
| builder = ShapeFix_Solid() | ||
| builder.SetContext(ctx) | ||
|
|
||
| # get both Shells and Faces | ||
| s = [s1, *sn] | ||
| shells_faces = [f for el in s for f in _get(el, (Shell, Face))] | ||
| # try to collect shells | ||
| shells = [el.wrapped for el in shells_faces if isinstance(el, Shell)] | ||
|
|
||
| # if no shells are present, use faces to construct them | ||
| shells = [el.wrapped for el in shells_faces if isinstance(el, Shell)] | ||
| if not shells: | ||
| faces = [el for el in shells_faces if isinstance(el, Face)] | ||
| shells = [ | ||
| tcast( | ||
| TopoDS_Shell, shell(*faces, tol=tol, history=history, name=name).wrapped | ||
| rvs = [ | ||
| builder.SolidFromShell( | ||
| TopoDS.Shell(shell(*faces, tol=tol, history=history, name=name).wrapped) | ||
| ) | ||
| ] | ||
|
|
||
| rvs = [builder.SolidFromShell(sh) for sh in shells] | ||
| if history: | ||
| # NB: reusing the history from shell() | ||
| _apply_reshape(history.ops[-1], ctx) | ||
|
|
||
| rv = tcast(Compound | Solid, _compound_or_shape(rvs)) | ||
|
|
||
| return tcast(Compound | Solid, _compound_or_shape(rvs)) | ||
| # otherwise construct solids with provided shells | ||
| else: | ||
| rvs = [builder.SolidFromShell(sh) for sh in shells] | ||
| rv = tcast(Compound | Solid, _compound_or_shape(rvs)) | ||
|
|
||
| if history: | ||
| # update history - this is likely a noop for this branch | ||
| _update_history(history, name, shells_faces, ctx.History()) | ||
| # update images by hand | ||
| op = history.ops[-1] | ||
| for el in op._tracked: | ||
| if isSubshape(el, rv): | ||
| op._images[el] = el | ||
|
|
||
| if history: | ||
| _polish_images(history.ops[-1], rv) | ||
|
|
||
| return rv | ||
|
|
||
|
|
||
| @multidispatch | ||
|
|
@@ -6401,8 +6486,6 @@ def solid( | |
| builder = BRepBuilderAPI_MakeSolid() | ||
| builder.Add(_get_one(shell(*s, tol=tol, history=history, name=name), Shell).wrapped) | ||
|
|
||
| n_inner = 0 | ||
|
|
||
| if inner: | ||
| for sh in _get(shell(*inner, tol=tol, history=history), Shell): | ||
| builder.Add(sh.wrapped) | ||
|
|
@@ -6414,12 +6497,18 @@ def solid( | |
| sf.SetContext(ctx) | ||
| sf.Perform() | ||
|
|
||
| rv = _shape(sf.Solid(), Solid) | ||
|
|
||
| # combine histories of all shell operations if needed | ||
| if history and inner: | ||
| inner_op = history.pop() | ||
| _combine_ops(history.ops[-1], inner_op) | ||
| if history: | ||
| if inner: | ||
| inner_op = history.pop() | ||
| _combine_ops(history.ops[-1], inner_op) | ||
|
|
||
| _apply_reshape(history.ops[-1], ctx) | ||
| _polish_images(history.ops[-1], rv) | ||
|
|
||
| return _shape(sf.Solid(), Solid) | ||
| return rv | ||
|
|
||
|
|
||
| @multimethod | ||
|
|
||
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
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.
typo: "exisitn" -> "existing"