Body#
The Body class represents a 3D solid or
surface body within a component. Bodies are the leaf-level geometry objects in the
assembly hierarchy and are the primary objects used for analysis, visualization, and
export.
Bodies are created through component methods such as
extrude_sketch(),
sweep_sketch(), and
revolve_sketch().
For more information, see Component.
Purpose and responsibilities#
A Body object is responsible for:
Representing a solid or surface geometry entity with faces, edges, and vertices.
Supporting geometric transformations (translate, rotate, scale, mirror).
Supporting boolean operations (unite, subtract, intersect).
Providing visualization and tessellation of the geometry.
Allowing material assignment.
Exposing physical properties such as volume and bounding box.
Solid bodies versus surface bodies#
A body can be either a solid body (a closed, watertight 3D volume) or a
surface body (an open, 2D surface embedded in 3D space). You can check the type
by inspecting the is_surface property:
if body.is_surface:
print("This is a surface body.")
else:
print("This is a solid body.")
Surface bodies have additional properties:
surface_thickness: The thickness assigned to the mid-surface.surface_offset: The offset type for the mid-surface.
Key properties#
The following are the most commonly used properties on a Body object.
nameThe user-defined name of the body.
idThe unique identifier of the body within the Geometry service.
facesA list of
Faceobjects bounding the body.edgesA list of
Edgeobjects on the body.verticesA list of
Vertexobjects on the body.volumeThe computed volume of the body (only applicable for solid bodies).
bounding_boxAn axis-aligned bounding box enclosing the body.
is_aliveTrueif the body still exists in the Geometry service (it may have been deleted or consumed by a boolean operation).is_suppressedTrueif the body is currently suppressed (hidden) in the design.materialThe
Materialassigned to this body, orNoneif no material has been assigned.
Renaming and styling#
from ansys.geometry.core.designer.body import FillStyle
# Rename the body
body.set_name("MyRenamedBody")
# Set the display color (RGB tuple with values between 0 and 255)
body.set_color((255, 0, 0)) # Red
# Control transparency
body.set_fill_style(FillStyle.TRANSPARENT)
# Suppress (hide) the body
body.set_suppressed(True)
# Unsuppress (show) the body
body.set_suppressed(False)
Assigning materials#
# Assign a material (the material must exist in the design)
body.assign_material(steel)
# Retrieve the assigned material
assigned = body.get_assigned_material()
# Remove the material assignment
body.remove_assigned_material()
Geometric transformations#
Bodies can be moved, rotated, scaled, and mirrored within their parent component.
Translating a body
from ansys.geometry.core.math import UnitVector3D
from ansys.geometry.core.misc import UNITS
from pint import Quantity
# Translate 20 mm in the X direction
body.translate(UnitVector3D([1, 0, 0]), Quantity(20, UNITS.mm))
Rotating a body
from ansys.geometry.core.math import Point3D, UnitVector3D
from ansys.geometry.core.misc import UNITS
from pint import Quantity
axis_origin = Point3D([0, 0, 0])
axis_direction = UnitVector3D([0, 0, 1])
# Rotate 45 degrees around the Z axis
body.rotate(axis_origin, axis_direction, Quantity(45, UNITS.deg))
Scaling a body
# Scale uniformly by a factor of 2
body.scale(2.0)
Mirroring a body
from ansys.geometry.core.math import Plane, Point3D, UnitVector3D
# Mirror across the XY plane
mirror_plane = Plane(Point3D([0, 0, 0]), UnitVector3D([1, 0, 0]), UnitVector3D([0, 1, 0]))
body.mirror(mirror_plane)
Boolean operations#
Boolean operations combine or subtract volumes to produce new geometry. After a boolean
operation, the tool body is consumed (is_alive becomes False), while the target
body is modified in place.
Unite (union)
Joins two bodies into a single body:
# Unite `body` with `other_body`; `other_body` is consumed
body.unite(other_body)
Subtract
Removes the volume of one body from another:
# Subtract `tool_body` from `body`; `tool_body` is consumed
body.subtract(tool_body)
Intersect
Keeps only the volume common to both bodies:
# Intersect `body` with `other_body`; `other_body` is consumed
body.intersect(other_body)
Note
After a boolean operation, always check body.is_alive before accessing the result.
The tool body is consumed by the operation. If the result is an empty solid (for example,
when there is no intersection), the target body may also be removed.
Copying a body#
# Create an independent copy of the body in the same component
body_copy = body.copy(parent_component, "CopiedBody")
Curve imprinting and projection#
You can imprint 2D sketch curves onto the faces of a body to create new edges:
from ansys.geometry.core.sketch import Sketch
from ansys.geometry.core.math import Point2D
from ansys.geometry.core.misc import UNITS
from pint import Quantity
imprint_sketch = Sketch()
imprint_sketch.circle(Point2D([0, 0]), Quantity(3, UNITS.mm))
# Imprint the sketch curves onto the body, creating new face edges
body.imprint_curves([body.faces[0]], imprint_sketch)
Checking collision#
from ansys.geometry.core.designer.body import CollisionType
collision_result = body.get_collision(other_body)
if collision_result == CollisionType.INTERFERENCE:
print("Bodies interfere.")
elif collision_result == CollisionType.TOUCHING:
print("Bodies are touching.")
else:
print("No collision detected.")
Tessellation and visualization#
Tessellating a body generates a mesh representation (triangulation) of its surface, which is useful for visualization and downstream analysis.
# Visualize the body in an interactive 3D plot
body.plot()
# Get a PyVista PolyData object for custom visualization
poly_data = body.tessellate()
# Get raw tessellation data
raw_tess = body.get_raw_tessellation()
# Get VTK tessellation data
vtk_tess = body.get_vtk_tessellation()
For the full API reference, see
Body.