mirror of
https://github.com/gumyr/build123d.git
synced 2025-12-06 02:30:55 -08:00
Combined project and project_points, added docs
This commit is contained in:
parent
f89fbfce79
commit
ebbf6a7464
5 changed files with 84 additions and 111 deletions
|
|
@ -21,39 +21,43 @@ The following table summarizes all of the available operations. Operations marke
|
|||
applicable to BuildLine and Algebra Curve, 2D to BuildSketch and Algebra Sketch, 3D to
|
||||
BuildPart and Algebra Part.
|
||||
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| Operation | Description | 1D | 2D | 3D | Example |
|
||||
+==========================================+==================================+====+====+====+================================+
|
||||
| :func:`~operations_generic.add` | Add object to builder | ✓ | ✓ | ✓ | :ref:`16 <ex 16>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.bounding_box` | Add bounding box as Shape | ✓ | ✓ | ✓ | |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.chamfer` | Bevel Vertex or Edge | | ✓ | ✓ | :ref:`9 <ex 9>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.extrude` | Draw 2D Shape into 3D | | | ✓ | :ref:`3 <ex 3>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.fillet` | Radius Vertex or Edge | | ✓ | ✓ | :ref:`9 <ex 9>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.mirror` | Mirror about Plane | ✓ | ✓ | ✓ | :ref:`15 <ex 15>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.offset` | Inset or outset Shape | ✓ | ✓ | ✓ | :ref:`25 <ex 25>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.loft` | Create 3D Shape from sections | | | ✓ | :ref:`24 <ex 24>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_sketch.make_face` | Create a Face from Edges | | ✓ | | :ref:`4 <ex 4>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_sketch.make_hull` | Create Convex Hull from Edges | | ✓ | | |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.revolve` | Swing 2D Shape about Axis | | | ✓ | :ref:`23 <ex 23>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.scale` | Change size of Shape | ✓ | ✓ | ✓ | |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.section` | Generate 2D slices from 3D Shape | | | ✓ | |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.split` | Divide object by Plane | ✓ | ✓ | ✓ | :ref:`27 <ex 27>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.sweep` | Extrude 2D section(s) along path | | | ✓ | :ref:`14 <ex 14>` |
|
||||
+------------------------------------------+----------------------------------+----+----+----+--------------------------------+
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| Operation | Description | 0D | 1D | 2D | 3D | Example |
|
||||
+============================================+==================================+====+====+====+====+================================+
|
||||
| :func:`~operations_generic.add` | Add object to builder | | ✓ | ✓ | ✓ | :ref:`16 <ex 16>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.bounding_box` | Add bounding box as Shape | | ✓ | ✓ | ✓ | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.chamfer` | Bevel Vertex or Edge | | | ✓ | ✓ | :ref:`9 <ex 9>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.extrude` | Draw 2D Shape into 3D | | | | ✓ | :ref:`3 <ex 3>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.fillet` | Radius Vertex or Edge | | | ✓ | ✓ | :ref:`9 <ex 9>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.loft` | Create 3D Shape from sections | | | | ✓ | :ref:`24 <ex 24>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_sketch.make_face` | Create a Face from Edges | | | ✓ | | :ref:`4 <ex 4>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_sketch.make_hull` | Create Convex Hull from Edges | | | ✓ | | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.mirror` | Mirror about Plane | | ✓ | ✓ | ✓ | :ref:`15 <ex 15>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.offset` | Inset or outset Shape | | ✓ | ✓ | ✓ | :ref:`25 <ex 25>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.project` | Project points, lines or Faces | ✓ | ✓ | ✓ | | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.project_workplane` | Create workplane for projection | | | | | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.revolve` | Swing 2D Shape about Axis | | | | ✓ | :ref:`23 <ex 23>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.scale` | Change size of Shape | | ✓ | ✓ | ✓ | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.section` | Generate 2D slices from 3D Shape | | | | ✓ | |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_generic.split` | Divide object by Plane | | ✓ | ✓ | ✓ | :ref:`27 <ex 27>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
| :func:`~operations_part.sweep` | Extrude 2D section(s) along path | | | | ✓ | :ref:`14 <ex 14>` |
|
||||
+--------------------------------------------+----------------------------------+----+----+----+----+--------------------------------+
|
||||
|
||||
Reference
|
||||
^^^^^^^^^
|
||||
|
|
@ -67,6 +71,8 @@ Reference
|
|||
.. autoclass:: operations_sketch.make_hull
|
||||
.. autoclass:: operations_generic.mirror
|
||||
.. autoclass:: operations_generic.offset
|
||||
.. autoclass:: operations_generic.project
|
||||
.. autoclass:: operations_part.project_workplane
|
||||
.. autoclass:: operations_part.revolve
|
||||
.. autoclass:: operations_generic.scale
|
||||
.. autoclass:: operations_part.section
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ __all__ = [
|
|||
"mirror",
|
||||
"offset",
|
||||
"project",
|
||||
"project_points",
|
||||
# "project_points",
|
||||
"project_workplane",
|
||||
"revolve",
|
||||
"scale",
|
||||
|
|
|
|||
|
|
@ -105,7 +105,6 @@ operations_apply_to = {
|
|||
"mirror": ["BuildPart", "BuildSketch", "BuildLine"],
|
||||
"offset": ["BuildPart", "BuildSketch", "BuildLine"],
|
||||
"project": ["BuildSketch", "BuildLine"],
|
||||
"project_points": ["BuildSketch", "BuildLine"],
|
||||
"project_workplane": ["BuildPart"],
|
||||
"revolve": ["BuildPart"],
|
||||
"scale": ["BuildPart", "BuildSketch", "BuildLine"],
|
||||
|
|
|
|||
|
|
@ -553,33 +553,39 @@ def offset(
|
|||
|
||||
|
||||
#:TypeVar("ProjectType"): Type of objects which can be projected
|
||||
ProjectType = Union[Edge, Face, Wire]
|
||||
ProjectType = Union[Edge, Face, Wire, VectorLike, Vertex]
|
||||
|
||||
|
||||
def project(
|
||||
objects: Union[ProjectType, Iterable[ProjectType]],
|
||||
workplane: Plane = None,
|
||||
mode: Mode = Mode.ADD,
|
||||
) -> Union[Curve, Sketch, Compound]:
|
||||
) -> Union[Curve, Sketch, Compound, ShapeList[Vector]]:
|
||||
"""Generic Operation: project
|
||||
|
||||
Applies to 1, 2 dimensional objects.
|
||||
Applies to 0, 1, and 2 dimensional objects.
|
||||
|
||||
Project the given objects onto a BuildLine or BuildSketch workplane in
|
||||
Project the given objects or points onto a BuildLine or BuildSketch workplane in
|
||||
the direction of the normal of that workplane. When projecting onto a
|
||||
sketch a Face(s) are generated while Edges are generated for BuildLine.
|
||||
Will only use the first if BuildSketch has multiple active workplanes.
|
||||
In algebra mode a workplane must be provided and the output is either
|
||||
a Face or the same type as the input.
|
||||
a Face, Curve, Sketch, Compound, or ShapeList[Vector].
|
||||
|
||||
Note that only if mode is not Mode.PRIVATE only Faces can be projected into
|
||||
BuildSketch and Edge/Wires into BuildLine.
|
||||
|
||||
Args:
|
||||
objects (Union[Edge, Face, Wire] or Iterable of): objects to project
|
||||
objects (Union[Edge, Face, Wire, VectorLike, Vertex] or Iterable of):
|
||||
objects or points to project
|
||||
workplane (Plane, optional): screen workplane
|
||||
mode (Mode, optional): combination mode. Defaults to Mode.ADD.
|
||||
|
||||
Raises:
|
||||
ValueError: missing objects
|
||||
ValueError: BuildSketch has multiple active workplanes
|
||||
ValueError: project doesn't accept group_by
|
||||
ValueError: Either a workplane must be provided or a builder must be active
|
||||
ValueError: Points and faces can only be projected in PRIVATE mode
|
||||
ValueError: Edges, wires and points can only be projected in PRIVATE mode
|
||||
RuntimeError: BuildPart doesn't have a project operation
|
||||
"""
|
||||
context: Builder = Builder._get_context("project")
|
||||
|
|
@ -590,6 +596,13 @@ def project(
|
|||
object_list = (
|
||||
[*objects] if isinstance(objects, (list, tuple, filter)) else [objects]
|
||||
)
|
||||
point_list = [o for o in object_list if isinstance(o, (tuple, Vector, Vertex))]
|
||||
point_list = [
|
||||
pnt.to_vector() if isinstance(pnt, Vertex) else Vector(pnt)
|
||||
for pnt in point_list
|
||||
]
|
||||
face_list = [o for o in object_list if isinstance(o, Face)]
|
||||
line_list = [o for o in object_list if isinstance(o, (Edge, Wire))]
|
||||
|
||||
if workplane is None:
|
||||
if context is None:
|
||||
|
|
@ -598,12 +611,16 @@ def project(
|
|||
)
|
||||
if isinstance(context, BuildLine):
|
||||
workplane = context.initial_plane
|
||||
if not all([isinstance(o, (Edge, Wire)) for o in object_list]):
|
||||
raise ValueError("Only Edges and Wires can be projected into BuildLine")
|
||||
if mode != Mode.PRIVATE and (face_list or point_list):
|
||||
raise ValueError(
|
||||
"Points and faces can only be projected in PRIVATE mode"
|
||||
)
|
||||
elif isinstance(context, BuildSketch):
|
||||
workplane = context.workplanes[0]
|
||||
if not all([isinstance(o, Face) for o in object_list]):
|
||||
raise ValueError("Only Faces can be projected into BuildSketch")
|
||||
if mode != Mode.PRIVATE and (line_list or point_list):
|
||||
raise ValueError(
|
||||
"Edges, wires and points can only be projected in PRIVATE mode"
|
||||
)
|
||||
elif isinstance(context, BuildPart):
|
||||
raise RuntimeError("project isn't available for BuildPart")
|
||||
|
||||
|
|
@ -612,7 +629,7 @@ def project(
|
|||
screen = Face.make_rect(1e9, 1e9, plane=workplane)
|
||||
projected_shapes = []
|
||||
obj: Shape
|
||||
for obj in object_list:
|
||||
for obj in face_list + line_list:
|
||||
obj_to_screen = (workplane.origin - obj.center()).normalized()
|
||||
if workplane.to_local_coords(obj_to_screen).Z > 0:
|
||||
projection_direction = -workplane.z_dir
|
||||
|
|
@ -625,67 +642,6 @@ def project(
|
|||
else: # BuildLine
|
||||
projected_shapes.append(projection[0])
|
||||
|
||||
if context is not None:
|
||||
context._add_to_context(*projected_shapes, mode=mode)
|
||||
|
||||
result = Compound.make_compound(projected_shapes)
|
||||
if all([obj._dim == 2 for obj in object_list]):
|
||||
result = Sketch(result.wrapped)
|
||||
elif all([obj._dim == 1 for obj in object_list]):
|
||||
result = Curve(result.wrapped)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
#:TypeVar("ProjectPointsType"): Type of objects which can be projected
|
||||
ProjectPointsType = Union[VectorLike, Vertex]
|
||||
|
||||
|
||||
def project_points(
|
||||
points: Union[ProjectPointsType, Iterable[ProjectPointsType]],
|
||||
workplane: Plane = None,
|
||||
) -> ShapeList[Vector]:
|
||||
"""Generic Operation: project_points
|
||||
|
||||
Applies to 0 dimensional objects.
|
||||
|
||||
Project the given points onto a BuildLine or BuildSketch workplane in
|
||||
the direction of the normal of that workplane generating a ShapeList of
|
||||
Vectors. Will only use the first if BuildSketch has multiple active workplanes.
|
||||
In algebra mode a workplane must be provided.
|
||||
|
||||
Args:
|
||||
points (Union[VectorLike, Vertex] or Iterable of): points to project
|
||||
workplane (Plane, optional): screen workplane
|
||||
|
||||
Raises:
|
||||
ValueError: BuildSketch has multiple active workplanes
|
||||
RuntimeError: BuildPart doesn't have a project operation
|
||||
"""
|
||||
context: Builder = Builder._get_context("project_points")
|
||||
|
||||
if isinstance(points, GroupBy):
|
||||
raise ValueError("project points doesn't accept group_by, did you miss [n]?")
|
||||
|
||||
point_list = [*points] if isinstance(points, (list, tuple, filter)) else [points]
|
||||
point_list = [
|
||||
pnt.to_vector() if isinstance(pnt, Vertex) else Vector(pnt)
|
||||
for pnt in point_list
|
||||
]
|
||||
|
||||
if workplane is None:
|
||||
if context is None:
|
||||
raise ValueError(
|
||||
"Either a workplane must be provided or a builder must be active"
|
||||
)
|
||||
if isinstance(context, BuildLine):
|
||||
workplane = context.initial_plane
|
||||
elif isinstance(context, BuildSketch):
|
||||
workplane = context.workplanes[0]
|
||||
elif isinstance(context, BuildPart):
|
||||
raise RuntimeError("project isn't available for BuildPart")
|
||||
|
||||
screen = Face.make_rect(1e9, 1e9, plane=workplane)
|
||||
projected_points = []
|
||||
for pnt in point_list:
|
||||
pnt_to_screen = (workplane.origin - pnt).normalized()
|
||||
|
|
@ -700,7 +656,19 @@ def project_points(
|
|||
else: # BuildLine
|
||||
projected_points.append(projection[0][0])
|
||||
|
||||
return ShapeList(projected_points)
|
||||
if context is not None:
|
||||
context._add_to_context(*projected_shapes, mode=mode)
|
||||
|
||||
if projected_points:
|
||||
result = ShapeList(projected_points)
|
||||
else:
|
||||
result = Compound.make_compound(projected_shapes)
|
||||
if all([obj._dim == 2 for obj in object_list]):
|
||||
result = Sketch(result.wrapped)
|
||||
elif all([obj._dim == 1 for obj in object_list]):
|
||||
result = Curve(result.wrapped)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def scale(
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ def project_workplane(
|
|||
"""
|
||||
context: BuildPart = BuildPart._get_context("project_workplane")
|
||||
|
||||
if context is None or not isinstance(context, BuildPart):
|
||||
if context is not None and not isinstance(context, BuildPart):
|
||||
raise RuntimeError(
|
||||
"projection_workplane can only be used from a BuildPart context or algebra"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue