Skip to content

Commit 8091750

Browse files
feat: imprint curves without a sketch (#1781)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 3bef736 commit 8091750

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

doc/changelog.d/1781.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
imprint curves without a sketch

src/ansys/geometry/core/designer/body.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
point3d_to_grpc_point,
6868
sketch_shapes_to_grpc_geometries,
6969
tess_to_pd,
70+
trimmed_curve_to_grpc_trimmed_curve,
7071
unit_vector_to_grpc_direction,
7172
)
7273
from ansys.geometry.core.designer.edge import CurveType, Edge
@@ -93,6 +94,7 @@
9394
min_backend_version,
9495
)
9596
from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Angle, Distance
97+
from ansys.geometry.core.shapes.curves.trimmed_curve import TrimmedCurve
9698
from ansys.geometry.core.sketch.sketch import Sketch
9799
from ansys.geometry.core.typing import Real
98100

@@ -1574,30 +1576,60 @@ def add_midsurface_offset( # noqa: D102
15741576
@protect_grpc
15751577
@ensure_design_is_active
15761578
def imprint_curves( # noqa: D102
1577-
self, faces: list[Face], sketch: Sketch
1579+
self, faces: list[Face], sketch: Sketch = None, trimmed_curves: list[TrimmedCurve] = None
15781580
) -> tuple[list[Edge], list[Face]]:
1581+
"""Imprint curves onto the specified faces using a sketch or edges.
1582+
1583+
Parameters
1584+
----------
1585+
faces : list[Face]
1586+
The list of faces to imprint the curves onto.
1587+
sketch : Sketch, optional
1588+
The sketch containing curves to imprint.
1589+
trimmed_curves : list[TrimmedCurve], optional
1590+
The list of curves to be imprinted. If sketch is provided, this parameter is ignored.
1591+
1592+
Returns
1593+
-------
1594+
tuple[list[Edge], list[Face]]
1595+
A tuple containing the list of new edges and faces created by the imprint operation.
1596+
"""
1597+
if sketch is None and self._template._grpc_client.backend_version < (25, 2, 0):
1598+
raise ValueError(
1599+
"A sketch must be provided for imprinting when using API versions below 25.2.0."
1600+
)
1601+
1602+
if sketch is None and trimmed_curves is None:
1603+
raise ValueError("Either a sketch or edges must be provided for imprinting.")
1604+
15791605
# Verify that each of the faces provided are part of this body
15801606
body_faces = self.faces
15811607
for provided_face in faces:
1582-
is_found = False
1583-
for body_face in body_faces:
1584-
if provided_face.id == body_face.id:
1585-
is_found = True
1586-
break
1587-
if not is_found:
1608+
if not any(provided_face.id == body_face.id for body_face in body_faces):
15881609
raise ValueError(f"Face with ID {provided_face.id} is not part of this body.")
15891610

15901611
self._template._grpc_client.log.debug(
1591-
f"Imprinting curves provided on {self.id} "
1592-
+ f"for faces {[face.id for face in faces]}."
1612+
f"Imprinting curves on {self.id} for faces {[face.id for face in faces]}."
15931613
)
15941614

1615+
curves = None
1616+
grpc_trimmed_curves = None
1617+
1618+
if sketch:
1619+
curves = sketch_shapes_to_grpc_geometries(sketch._plane, sketch.edges, sketch.faces)
1620+
1621+
if trimmed_curves:
1622+
grpc_trimmed_curves = [
1623+
trimmed_curve_to_grpc_trimmed_curve(curve) for curve in trimmed_curves
1624+
]
1625+
15951626
imprint_response = self._template._commands_stub.ImprintCurves(
15961627
ImprintCurvesRequest(
15971628
body=self._id,
1598-
curves=sketch_shapes_to_grpc_geometries(sketch._plane, sketch.edges, sketch.faces),
1629+
curves=curves,
15991630
faces=[face._id for face in faces],
1600-
plane=plane_to_grpc_plane(sketch.plane),
1631+
plane=plane_to_grpc_plane(sketch.plane) if sketch else None,
1632+
trimmed_curves=grpc_trimmed_curves,
16011633
)
16021634
)
16031635

tests/integration/test_design.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,57 @@ def test_project_and_imprint_curves(modeler: Modeler):
11711171
assert len(body_copy.faces) == 8
11721172

11731173

1174+
def test_imprint_trimmed_curves(modeler: Modeler):
1175+
"""
1176+
Test the imprinting of trimmed curves onto a specified face of a body.
1177+
"""
1178+
unit = DEFAULT_UNITS.LENGTH
1179+
1180+
wx = 1
1181+
wy = 1
1182+
wz = 1
1183+
design = modeler.create_design("test imprint")
1184+
1185+
# create box
1186+
start_at = Point3D([wx / 2, wy / 2, 0.0], unit=unit)
1187+
1188+
plane = Plane(
1189+
start_at,
1190+
UNITVECTOR3D_X,
1191+
UNITVECTOR3D_Y,
1192+
)
1193+
1194+
box_plane = Sketch(plane)
1195+
box_plane.box(Point2D([0.0, 0.0], unit=unit), width=wx, height=wy)
1196+
box = design.extrude_sketch("box", box_plane, wz)
1197+
1198+
assert len(box.faces) == 6
1199+
assert len(box.edges) == 12
1200+
1201+
# create cylinder
1202+
point = Point3D([0.5, 0.5, 0.5])
1203+
ortho_1, ortho_2 = UNITVECTOR3D_X, UNITVECTOR3D_Y
1204+
plane = Plane(point, ortho_1, ortho_2)
1205+
sketch_cylinder = Sketch(plane)
1206+
sketch_cylinder.circle(Point2D([0.0, 0.0], unit=unit), radius=0.1)
1207+
cylinder = design.extrude_sketch("cylinder", sketch_cylinder, 0.5)
1208+
1209+
edges = cylinder.faces[1].edges
1210+
trimmed_curves = [edges[0].shape]
1211+
new_edges, new_faces = box.imprint_curves(faces=[box.faces[1]], trimmed_curves=trimmed_curves)
1212+
1213+
# the new edge is coming from the circular top edge of the cylinder.
1214+
assert new_edges[0].start == new_edges[0].end
1215+
# verify that there is one new edge coming from the circle.
1216+
assert len(new_faces) == 1
1217+
# verify that there is one new face coming from the circle.
1218+
assert len(new_edges) == 1
1219+
# verify that there are 7 faces in total.
1220+
assert len(box.faces) == 7
1221+
# verify that there are 14 edges in total.
1222+
assert len(box.edges) == 13
1223+
1224+
11741225
def test_copy_body(modeler: Modeler):
11751226
"""Test copying a body."""
11761227
# Create your design on the server side

0 commit comments

Comments
 (0)