-
Notifications
You must be signed in to change notification settings - Fork 423
(Part 2/3) Project 3D part to plane #2002
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
antonysigma
wants to merge
1
commit into
CadQuery:master
Choose a base branch
from
antonysigma:project-to-viewpoint
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.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from typing import Optional | ||
|
|
||
| from OCP.BRepLib import BRepLib | ||
| from OCP.gp import gp_Ax2, gp_Dir, gp_Pnt | ||
| from OCP.HLRAlgo import HLRAlgo_Projector | ||
| from OCP.HLRBRep import HLRBRep_Algo, HLRBRep_HLRToShape | ||
|
|
||
| from .shapes import TOLERANCE, Edge, Shape | ||
|
|
||
| DISCRETIZATION_TOLERANCE = 1e-3 | ||
|
|
||
|
|
||
| def projectToViewpoint( | ||
| shape, | ||
| projectionDir: tuple[float, float, float], | ||
| focus: Optional[float] = None, | ||
| ) -> tuple[list[Edge], list[Edge]]: | ||
| hlr = HLRBRep_Algo() | ||
| hlr.Add(shape.wrapped) | ||
|
|
||
| coordinate_system = gp_Ax2(gp_Pnt(), gp_Dir(*projectionDir)) | ||
|
|
||
| if focus is not None: | ||
| projector = HLRAlgo_Projector(coordinate_system, focus) | ||
| else: | ||
| projector = HLRAlgo_Projector(coordinate_system) | ||
|
|
||
| hlr.Projector(projector) | ||
| hlr.Update() | ||
| hlr.Hide() | ||
|
|
||
| hlr_shapes = HLRBRep_HLRToShape(hlr) | ||
|
|
||
| visible = [] | ||
|
|
||
| visible_sharp_edges = hlr_shapes.VCompound() | ||
| if not visible_sharp_edges.IsNull(): | ||
| visible.append(visible_sharp_edges) | ||
|
|
||
| visible_smooth_edges = hlr_shapes.Rg1LineVCompound() | ||
| if not visible_smooth_edges.IsNull(): | ||
| visible.append(visible_smooth_edges) | ||
|
|
||
| visible_contour_edges = hlr_shapes.OutLineVCompound() | ||
| if not visible_contour_edges.IsNull(): | ||
| visible.append(visible_contour_edges) | ||
|
|
||
| hidden = [] | ||
|
|
||
| hidden_sharp_edges = hlr_shapes.HCompound() | ||
| if not hidden_sharp_edges.IsNull(): | ||
| hidden.append(hidden_sharp_edges) | ||
|
|
||
| hidden_contour_edges = hlr_shapes.OutLineHCompound() | ||
| if not hidden_contour_edges.IsNull(): | ||
| hidden.append(hidden_contour_edges) | ||
|
|
||
| # Fix the underlying geometry - otherwise we will get segfaults | ||
| for el in visible: | ||
| BRepLib.BuildCurves3d_s(el, TOLERANCE) | ||
| for el in hidden: | ||
| BRepLib.BuildCurves3d_s(el, TOLERANCE) | ||
|
|
||
| # convert to native CQ objects | ||
| visible = [Shape.cast(s) for s in visible] # s is a TopoDS_Shape (Compound) | ||
| hidden = [Shape.cast(s) for s in hidden] | ||
|
|
||
| # Extract edges | ||
| visible_edges = [e for c in visible for e in c.Edges()] | ||
| hidden_edges = [e for c in hidden for e in c.Edges()] | ||
|
|
||
| return visible_edges, hidden_edges | ||
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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import cadquery as cq | ||
| from cadquery.occ_impl.projection import projectToViewpoint | ||
| from cadquery.occ_impl.exporters.svg import exportSVG | ||
| from cadquery.occ_impl.shapes import Compound | ||
| from cadquery import Workplane | ||
|
|
||
| viewpoint = { | ||
| "top": (0, 0, 1), | ||
| "left": (1, 0, 0), | ||
| "front": (0, 1, 0), | ||
| "ortho": (1, 1, 1), | ||
| } | ||
|
|
||
|
|
||
| def exportDXF3rdAngleProjection(my_part: Workplane, prefix: str) -> None: | ||
| for name, direction in viewpoint.items(): | ||
| visible_edges, hidden_edges = projectToViewpoint(my_part.val(), direction) | ||
| cq.exporters.exportDXF( | ||
| Compound.makeCompound(visible_edges), | ||
| f"{prefix}{name}.dxf", | ||
| doc_units=6, | ||
| ) | ||
|
Comment on lines
18
to
22
Author
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. |
||
|
|
||
|
|
||
| def exportSVG3rdAngleProjection(my_part, prefix: str) -> None: | ||
| for name, direction in viewpoint.items(): | ||
| exportSVG( | ||
| my_part, | ||
| f"{prefix}{name}.svg", | ||
| opts={ | ||
| "projectionDir": direction, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Build the part | ||
| width = 10 | ||
| depth = 10 | ||
| height = 10 | ||
|
|
||
| # !!! Test projection of fillets to arc segments in DXF. !!! | ||
| baseplate = ( | ||
| cq.Workplane("XY") # | ||
| .box(width, depth, height) | ||
| .edges("|Z") | ||
| .fillet(2.0) | ||
| ) | ||
|
|
||
| hole_dia = 3.0 | ||
|
|
||
| # !!! Test projection of countersunk to arc segments in DXF. !!! | ||
| drilled = ( | ||
| baseplate.faces(">Z") # | ||
| .workplane() | ||
| .cskHole(hole_dia, hole_dia * 2, 82.0) | ||
| ) | ||
|
|
||
| # Expected DXF output to be identical to SVG output | ||
| exportSVG3rdAngleProjection(drilled, "") | ||
| exportDXF3rdAngleProjection(drilled, "") | ||
|
Comment on lines
+59
to
+61
Author
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. Help wanted to move this example code to |
||
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.
Help wanted to reduce duplicated code.. This part is 90% similar to the code in
exporters/svg.py, so it would be better if we can consolidate it.Also, should I create a new file
occ_impl/projection.py? Or, should I move it toocc_impl/geom.py?