Download this example
Download this example as a Jupyter Notebook or as a Python script.
Sketching: Basic usage#
This example shows how to use basic PyAnsys Geometry sketching capabilities.
Perform required imports#
Perform the required imports.
[1]:
from ansys.geometry.core.misc.units import UNITS as u
from ansys.geometry.core.sketch import Sketch
Create a sketch#
Sketches are fundamental objects for drawing basic shapes like lines, segments, circles, ellipses, arcs, and polygons.
You create a Sketch
instance by defining a drawing plane. To define a plane, you declare a point and two fundamental orthogonal directions.
[2]:
from ansys.geometry.core.math import Plane, Point2D, Point3D
Define a plane for creating a sketch.
[3]:
# Define the origin point of the plane
origin = Point3D([1, 1, 1])
# Create a plane located in previous point with desired fundamental directions
plane = Plane(
origin, direction_x=[1, 0, 0], direction_y=[0, -1, 1]
)
# Instantiate a new sketch object from previous plane
sketch = Sketch(plane)
Draw shapes#
To draw different shapes in the sketch, you use draw
methods.
Draw a circle#
You draw a circle in a sketch by specifying the center and radius.
[4]:
sketch.circle(Point2D([2, 1]), radius=30 * u.cm, tag="Circle")
sketch.select("Circle")
sketch.plot_selection()
Draw an ellipse#
You draw an ellipse in a sketch by specifying the center, major radius, and minor radius.
[5]:
sketch.ellipse(
Point2D([1, 1]), major_radius=2*u.m, minor_radius=1*u.m, tag="Ellipse"
)
sketch.select("Ellipse")
sketch.plot_selection()
Draw a polygon#
You draw a regular polygon by specifying the center, radius, and desired number of sides.
[6]:
sketch.polygon(
Point2D([1, 1]), inner_radius=3*u.m, sides=5, tag="Polygon"
)
sketch.select("Polygon")
sketch.plot_selection()
Draw an arc#
You draw an arc of circumference by specifying the center, starting point, and ending point.
[7]:
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
sketch.arc(start_point, end_point, Point2D([1,1]), tag="Arc")
sketch.select("Arc")
sketch.plot_selection()
There are also additional ways to draw arcs, such as by specifying the start, center point, and angle.
[8]:
start_point = Point2D([2, 1], unit=u.m)
center_point = Point2D([1, 1], unit=u.m)
angle = 90
sketch.arc_from_start_center_and_angle(
start_point, center_point, angle=90, tag="Arc_from_start_center_angle"
)
sketch.select("Arc_from_start_center_angle")
sketch.plot_selection()
Or by specifying the start, end point, and radius.
[9]:
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
radius = 1 * u.m
sketch.arc_from_start_end_and_radius(
start_point, end_point, radius, tag="Arc_from_start_end_radius"
)
sketch.select("Arc_from_start_end_radius")
sketch.plot_selection()
Draw a slot#
You draw a slot by specifying the center, width, and height.
[10]:
sketch.slot(Point2D([2, 0]), 4, 3, tag="Slot")
sketch.select("Slot")
sketch.plot_selection()
Draw a box#
You draw a box by specifying the center, width, and height.
[11]:
sketch.box(Point2D([2, 0]), 4, 5, tag="Box")
sketch.select("Box")
sketch.plot_selection()
Draw a segment#
You draw a segment by specifying the starting point and ending point.
[12]:
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
sketch.segment(start_point, end_point, "Segment")
sketch.select("Segment")
sketch.plot_selection()
Plot the sketch#
The Plotter
class provides capabilities for plotting different PyAnsys Geometry objects. PyAnsys Geometry uses PyVista as the visualization backend.
You use the plot_sketch
method to plot a sketch. This method accepts a Sketch
instance and some extra arguments to further customize the visualization of the sketch. These arguments include showing the plane of the sketch and its frame.
[13]:
# Plot the sketch in the whole scene
from ansys.geometry.core.plotting import GeometryPlotter
pl = GeometryPlotter()
pl.add_sketch(sketch, show_plane=True, show_frame=True)
pl.show()