Download this example

Download this example as a Jupyter Notebook or as a Python script.


PyAnsys Geometry 101: Curve and Surface Plotting#

This example provides an overview of how to visualize PyAnsys Geometry’s Curve and Surface objects. After constructing some of the Curve and Surface objects, it shows how to plot them.

Perform required imports#

Perform the required imports.

[1]:
from ansys.geometry.core.math import Point3D
from ansys.geometry.core.plotting import GeometryPlotter

Create curve shapes#

PyAnsys Geometry provides several curve types. Let’s create instances of each.

Create a Circle curve#

[2]:
from ansys.geometry.core.shapes.curves.circle import Circle

circle = Circle(origin=Point3D([0, 0, 0]), radius=1)

Create a Line curve#

[3]:
from ansys.geometry.core.shapes.curves.line import Line
from ansys.geometry.core.math import UnitVector3D

line = Line(origin=Point3D([5, 0, 0]), direction=UnitVector3D([1, 1, 0]))

Create an Ellipse curve#

[4]:
from ansys.geometry.core.shapes.curves.ellipse import Ellipse

ellipse = Ellipse(origin=Point3D([10, 0, 0]), major_radius=1.5, minor_radius=0.8)

Create a NURBSCurve#

[5]:
from ansys.geometry.core.shapes.curves.nurbs import NURBSCurve
import numpy as np

# Create a simple NURBS curve with 4 control points
points = [
    Point3D([0, 5, 0]),
    Point3D([1, 6, 0]),
    Point3D([2, 5, 0]),
    Point3D([3, 6, 0])
]
nurbs = NURBSCurve.fit_curve_from_points(points=points, degree=3)

Plot individual curves with colors#

Now let’s plot each curve individually with different colors.

Plot the circle in blue#

[6]:
plotter = GeometryPlotter()
plotter.plot(circle, color="blue", opacity=1)
plotter.show()

Plot the line in red#

[7]:
plotter = GeometryPlotter()
plotter.plot(line, color="red", opacity=1)
plotter.show()

Plot the ellipse in green#

[8]:
plotter = GeometryPlotter()
plotter.plot(ellipse, color="green", opacity=1)
plotter.show()

Plot the NURBS curve in magenta#

[9]:
plotter = GeometryPlotter()
plotter.plot(nurbs, color="magenta")
plotter.show()

Plot multiple curves together#

You can also plot multiple curves together in a single scene with different colors.

[10]:
plotter = GeometryPlotter()

# Add all curves with different colors
plotter.plot(circle, color="blue", opacity=1)
plotter.plot(line, color="red", opacity=1)
plotter.plot(ellipse, color="green", opacity=1)
plotter.plot(nurbs, color="magenta", opacity=1)

# Show all curves together
plotter.show()

You can also plot multiple curves with the same settings using a list.

[11]:
plotter = GeometryPlotter()

# Add all curves with the same color
plotter.plot([circle, line, ellipse], color="magenta", opacity=1)

# Show all curves together
plotter.show()

Create surface shapes#

PyAnsys Geometry provides several surface types. Let’s create instances of each.

Create a Sphere shape#

[12]:
from ansys.geometry.core.shapes.surfaces.sphere import Sphere

sphere = Sphere(origin=Point3D([0, 10, 0]), radius=1)

Create a Cylinder shape#

[13]:
from ansys.geometry.core.shapes.surfaces.cylinder import Cylinder

cylinder = Cylinder(origin=Point3D([5, 10, 0]), radius=0.8)

Create a Cone shape#

[14]:
from ansys.geometry.core.shapes.surfaces.cone import Cone
import numpy as np

cone = Cone(origin=Point3D([10, 10, 0]), radius=1, half_angle=np.pi / 6)

Create a Torus shape#

[15]:
from ansys.geometry.core.shapes.surfaces.torus import Torus

torus = Torus(origin=Point3D([0, 15, 0]), major_radius=1.5, minor_radius=0.5)

Create a PlaneSurface shape#

[16]:
from ansys.geometry.core.shapes.surfaces.plane import PlaneSurface

plane = PlaneSurface(origin=Point3D([5, 15, 0]))

Plot individual surfaces with colors#

Now let’s plot each surface individually with different colors.

Plot the sphere in blue#

[17]:
plotter = GeometryPlotter()
plotter.plot(sphere, color="blue", opacity=1)
plotter.show()

Plot the cylinder in red#

[18]:
plotter = GeometryPlotter()
plotter.plot(cylinder, color="red", opacity=1)
plotter.show()

Plot the cone in green#

[19]:
plotter = GeometryPlotter()
plotter.plot(cone, color="green", opacity=1)
plotter.show()

Plot the torus in yellow#

[20]:
plotter = GeometryPlotter()
plotter.plot(torus, color="yellow", opacity=1)
plotter.show()

Plot the plane in cyan#

[21]:
plotter = GeometryPlotter()
plotter.plot(plane, color="cyan", opacity=1)
plotter.show()

Plot multiple surfaces together#

You can also plot multiple surfaces together in a single scene with different colors.

[22]:
plotter = GeometryPlotter()

# Add all surfaces with different colors
plotter.plot(sphere, color="blue", opacity=1)
plotter.plot(cylinder, color="red", opacity=1)
plotter.plot(cone, color="green", opacity=1)
plotter.plot(torus, color="yellow", opacity=1)
plotter.plot(plane, color="cyan", opacity=1)

# Show all surfaces together
plotter.show()

You can also plot multiple surfaces with the same settings using a list

[23]:
plotter = GeometryPlotter()

# Add all surfaces with different colors
plotter.plot([sphere, cylinder, cone], color="orange", opacity=1)

# Show all surfaces together
plotter.show()

Download this example

Download this example as a Jupyter Notebook or as a Python script.