Segmented Path for Sweep_Path Method #2418
Unanswered
JonathonMcMullan
asked this question in
Q&A
Replies: 1 comment 1 reply
-
|
Hi @JonathonMcMullan - I gave it a try to your request and came up with the following script that demos how I would do it personally. See the following code: """Script to create a box sketch and sweep it along a multi-point straight line path."""
from ansys.geometry.core import launch_modeler
from ansys.geometry.core.math import Plane, Point2D, Point3D, Vector3D
from ansys.geometry.core.shapes import Interval, Line
from ansys.geometry.core.sketch import Sketch
# Start the modeler
modeler = launch_modeler()
# Create a new design
design = modeler.create_design("BoxSweepExample")
# Define the box sketch on the XY plane at the first path point
# The box will be a 0.1 x 0.1 meter square centered at the origin
box_sketch = Sketch(plane=Plane(origin=Point3D([0, 0, 0])))
box_sketch.box(
center=Point2D([0, 0]),
width=0.1, # 0.1 meters
height=0.1, # 0.1 meters
)
# Create a multi-point path with straight line segments
# Define path points in 3D space
# Note: Each segment must move in Z direction since the sketch is on the XY plane
path_points = [
Point3D([0, 0, 0]),
Point3D([-0.5, 0, 0.3]), # Move in -X and Z
Point3D([-0.5, -0.5, 0.6]), # Move in -Y and Z
Point3D([-1.0, -0.5, 0.9]), # Move in -X and Z
Point3D([-1.0, -1.0, 1.2]), # Move in -Y and Z
]
# Create trimmed line segments between consecutive points
path = []
for i in range(len(path_points) - 1):
start_point = path_points[i]
end_point = path_points[i + 1]
start_to_end_vec = Vector3D.from_points(start_point, end_point)
# Create a line from start to end
line = Line(origin=start_point, direction=start_to_end_vec)
# Trim the line to the interval [0, distance]
# The parameter goes from 0 (at origin) to distance (at end_point)
trimmed_line = line.trim(Interval(0, start_to_end_vec.magnitude))
path.append(trimmed_line)
# Sweep the box sketch along the path
swept_body = design.sweep_sketch(
name="SweptBox",
sketch=box_sketch,
path=path,
)
print(f"Created swept body: {swept_body.name}")
print(f"Body ID: {swept_body.id}")
print(f"Is surface: {swept_body.is_surface}")
print(f"Number of faces: {len(swept_body.faces)}")
print(f"Number of edges: {len(swept_body.edges)}")
print(f"Volume: {swept_body.volume}")
# Optionally plot the result (requires PyVista)
try:
design.plot()
except Exception as e:
print(f"Could not plot the design: {e}")This is an example output of the plot:
You can see how the box was propagated along the Z-direction in a zig-zag fashion. Hope this helps! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
I suspect I'm missing something obvious but what is the suggested way to make a line with multiple sections for a path? The sweep_path takes a "TrimmedCurve" object but all of the examples use a geometry primitive first rather than line type objects. I have a square that I want to sweep along a line defined by a sequence of points. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions