CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pygmsh

Python frontend for Gmsh mesh generator providing intuitive abstractions for creating complex geometric models and generating high-quality meshes

Pending
Overview
Eval results
Files

occ-geometry.mddocs/

OpenCASCADE Solid Modeling

The OpenCASCADE geometry kernel (pygmsh.occ) provides advanced solid modeling capabilities with boolean operations, primitive shapes, BREP operations, and CAD file import/export. This kernel leverages the full power of OpenCASCADE for complex geometric operations and robust solid modeling workflows.

Capabilities

Primitive Shape Creation

Create fundamental 3D shapes that serve as building blocks for complex geometries. All primitive shapes support optional mesh size specification and return geometry objects suitable for boolean operations.

def add_ball(*args, mesh_size=None, **kwargs):
    """
    Create a sphere with optional angular cuts.
    
    Parameters from Ball constructor:
    - center: list[float], sphere center coordinates [x, y, z]
    - radius: float, sphere radius
    - angle1: float, minimum polar angle (default: -π/2)
    - angle2: float, maximum polar angle (default: π/2)  
    - angle3: float, azimuthal angle span (default: 2π)
    - mesh_size: float, optional mesh size at sphere surface
    
    Returns:
    Ball object representing the sphere geometry
    """

def add_box(*args, mesh_size=None, **kwargs):
    """
    Create a rectangular box.
    
    Parameters from Box constructor:
    - x0: list[float], corner point coordinates [x, y, z]
    - extents: list[float], box dimensions [dx, dy, dz]
    - char_length: float, optional characteristic length (deprecated, use mesh_size)
    - mesh_size: float, optional mesh size at box surfaces
    
    Returns:
    Box object representing the rectangular box geometry
    """

def add_cone(*args, mesh_size=None, **kwargs):
    """
    Create a cone with different top and bottom radii.
    
    Parameters from Cone constructor:
    - center: list[float], cone base center coordinates [x, y, z]
    - axis: list[float], cone axis direction vector [dx, dy, dz]
    - radius0: float, base radius
    - radius1: float, top radius
    - angle: float, angular opening (default: 2π for full cone)
    - mesh_size: float, optional mesh size at cone surface
    
    Returns:
    Cone object representing the cone geometry
    """

def add_cylinder(*args, mesh_size=None, **kwargs):
    """
    Create a cylinder with optional angular opening.
    
    Parameters from Cylinder constructor:
    - x0: list[float], cylinder base center coordinates [x, y, z]
    - axis: list[float], cylinder axis direction vector [dx, dy, dz]
    - radius: float, cylinder radius
    - angle: float, angular opening (default: 2π for full cylinder)
    - mesh_size: float, optional mesh size at cylinder surface
    
    Returns:
    Cylinder object representing the cylinder geometry
    """

def add_disk(*args, mesh_size=None, **kwargs):
    """
    Create a disk or ellipse.
    
    Parameters from Disk constructor:
    - x0: list[float], disk center coordinates [x, y, z]
    - radius0: float, first radius (for circle) or X-axis radius (for ellipse)
    - radius1: float, optional second radius for ellipse (default: None for circle)
    - mesh_size: float, optional mesh size at disk boundary
    
    Returns:
    Disk object representing the disk or ellipse geometry
    """

def add_rectangle(*args, mesh_size=None, **kwargs):
    """
    Create a rectangle with optional rounded corners.
    
    Parameters from Rectangle constructor:
    - x0: list[float], rectangle corner coordinates [x, y, z]
    - a: float, width in X direction
    - b: float, height in Y direction  
    - corner_radius: float, optional corner radius for rounded rectangle
    - mesh_size: float, optional mesh size at rectangle boundary
    
    Returns:
    Rectangle object representing the rectangle geometry
    """

def add_torus(*args, mesh_size=None, **kwargs):
    """
    Create a torus with inner and outer radii.
    
    Parameters from Torus constructor:
    - center: list[float], torus center coordinates [x, y, z]
    - radius0: float, minor radius (tube radius)
    - radius1: float, major radius (distance from center to tube center)
    - alpha: float, angular span (default: 2π for full torus)
    - mesh_size: float, optional mesh size at torus surface
    
    Returns:
    Torus object representing the torus geometry
    """

def add_wedge(*args, mesh_size=None, **kwargs):
    """
    Create a right angular wedge.
    
    Parameters from Wedge constructor:
    - x0: list[float], wedge corner coordinates [x, y, z]
    - extents: list[float], wedge dimensions [dx, dy, dz]
    - top_extent: list[float], optional different top dimensions for tapered wedge
    - mesh_size: float, optional mesh size at wedge surfaces
    
    Returns:
    Wedge object representing the wedge geometry
    """

def add_ellipsoid(center, radii, mesh_size=None):
    """
    Create an ellipsoid with specified radii in each direction.
    
    Parameters:
    - center: list[float], ellipsoid center coordinates [x, y, z]
    - radii: list[float], radii in [x, y, z] directions [rx, ry, rz]
    - mesh_size: float, optional mesh size at ellipsoid surface
    
    Returns:
    Ellipsoid object representing the ellipsoid geometry
    """

Boolean Operations

Perform robust solid modeling operations to combine, intersect, and subtract geometries. These operations form the foundation of complex solid modeling workflows.

def boolean_intersection(entities, delete_first=True, delete_other=True):
    """
    Compute boolean intersection of multiple entities.
    
    Parameters:
    - entities: list, geometry entities to intersect
    - delete_first: bool, delete first entity after operation (default: True)
    - delete_other: bool, delete other entities after operation (default: True)
    
    Returns:
    New geometry object representing the intersection
    """

def boolean_union(entities, delete_first=True, delete_other=True):
    """
    Compute boolean union of multiple entities.
    
    Parameters:
    - entities: list, geometry entities to unite
    - delete_first: bool, delete first entity after operation (default: True)
    - delete_other: bool, delete other entities after operation (default: True)
    
    Returns:
    New geometry object representing the union
    """

def boolean_difference(d0, d1, delete_first=True, delete_other=True):
    """
    Compute boolean difference (d0 - d1).
    
    Parameters:
    - d0: geometry entity (minuend)
    - d1: geometry entity or list of entities (subtrahend)
    - delete_first: bool, delete first entity after operation (default: True)
    - delete_other: bool, delete other entities after operation (default: True)
    
    Returns:
    New geometry object representing d0 with d1 subtracted
    """

def boolean_fragments(d0, d1, delete_first=True, delete_other=True):
    """
    Compute boolean fragments (partition entities into non-overlapping pieces).
    
    Parameters:
    - d0: geometry entity or list of entities
    - d1: geometry entity or list of entities
    - delete_first: bool, delete first entities after operation (default: True)
    - delete_other: bool, delete other entities after operation (default: True)
    
    Returns:
    List of geometry objects representing the fragments
    """

Mesh Control Properties

Control mesh generation parameters specific to the OpenCASCADE kernel through geometry properties.

@property
def characteristic_length_min():
    """
    Get minimum characteristic length for mesh generation.
    
    Returns:
    float: Current minimum characteristic length value
    """

@characteristic_length_min.setter
def characteristic_length_min(val):
    """
    Set minimum characteristic length for mesh generation.
    
    Parameters:
    - val: float, minimum characteristic length
    """

@property  
def characteristic_length_max():
    """
    Get maximum characteristic length for mesh generation.
    
    Returns:
    float: Current maximum characteristic length value
    """

@characteristic_length_max.setter
def characteristic_length_max(val):
    """
    Set maximum characteristic length for mesh generation.
    
    Parameters:
    - val: float, maximum characteristic length
    """

Advanced Operations

Perform advanced solid modeling operations including revolution, CAD import/export, and surface normal correction.

def revolve(*args, **kwargs):
    """
    Revolve entity around axis to create surfaces or volumes.
    
    Note: For occ kernel, revolution angle must be < 2π (360 degrees).
    
    Parameters depend on input entity type:
    - input_entity: curve, surface, or volume to revolve
    - point_on_axis: point on revolution axis
    - axis_direction: revolution axis direction vector
    - angle: revolution angle in radians (must be < 2π)
    
    Returns:
    Revolved entity (surface from curve, volume from surface)
    """

def import_shapes(filename):
    """
    Import shapes from CAD files (STEP, IGES, BREP, etc.).
    
    Parameters:
    - filename: str, path to CAD file to import
    
    Returns:
    List of imported geometry objects
    """

def force_outward_normals(tag):
    """
    Force outward orientation of surface normals.
    
    Parameters:
    - tag: int, surface tag to correct normals
    
    Returns:
    None (modifies surface in-place)
    """

Usage Examples

Creating Complex Geometry with Boolean Operations

import pygmsh

with pygmsh.occ.Geometry() as geom:
    # Create primary shapes
    main_box = geom.add_box([0, 0, 0], [2, 2, 2])
    
    # Create holes
    hole1 = geom.add_cylinder([0.5, 0.5, -0.1], [0, 0, 1], 0.2)
    hole2 = geom.add_cylinder([1.5, 1.5, -0.1], [0, 0, 1], 0.2)
    
    # Create final geometry with boolean operations
    box_with_holes = geom.boolean_difference(main_box, [hole1, hole2])
    
    # Add a protruding feature
    protrusion = geom.add_cylinder([1, 1, 2], [0, 0, 1], 0.3)
    final_shape = geom.boolean_union([box_with_holes, protrusion])
    
    mesh = geom.generate_mesh(dim=3)

Working with Imported CAD Geometry

import pygmsh

with pygmsh.occ.Geometry() as geom:
    # Import shapes from CAD file
    imported_shapes = geom.import_shapes("mechanical_part.step")
    
    # Create additional geometry
    mounting_holes = [
        geom.add_cylinder([x, y, -1], [0, 0, 1], 0.1)
        for x, y in [(10, 10), (50, 10), (50, 50), (10, 50)]
    ]
    
    # Subtract mounting holes from imported geometry
    final_part = geom.boolean_difference(imported_shapes[0], mounting_holes)
    
    # Force outward normals for proper meshing
    geom.force_outward_normals(final_part.id)
    
    mesh = geom.generate_mesh(dim=3)

Precision Control for Complex Geometries

import pygmsh

with pygmsh.occ.Geometry() as geom:
    # Set mesh size bounds for complex geometry
    geom.characteristic_length_min = 0.01
    geom.characteristic_length_max = 1.0
    
    # Create geometry with varying feature sizes
    large_sphere = geom.add_ball([0, 0, 0], 10.0, mesh_size=1.0)
    
    small_features = [
        geom.add_ball([x, 0, 0], 0.5, mesh_size=0.05)
        for x in [-8, -4, 4, 8]
    ]
    
    # Boolean union with automatic mesh size transition
    complex_geometry = geom.boolean_union([large_sphere] + small_features)
    
    mesh = geom.generate_mesh(dim=3)

Primitive Shape Classes

Ball Class

class Ball:
    """Sphere with optional angular cuts."""
    
    def __init__(self, center, radius, angle1=-π/2, angle2=π/2, angle3=2π):
        """
        Initialize sphere.
        
        Parameters:
        - center: list[float], sphere center [x, y, z]
        - radius: float, sphere radius
        - angle1: float, minimum polar angle (default: -π/2)
        - angle2: float, maximum polar angle (default: π/2)
        - angle3: float, azimuthal span (default: 2π)
        """

Box Class

class Box:
    """Rectangular box."""
    
    def __init__(self, x0, extents, char_length=None):
        """
        Initialize rectangular box.
        
        Parameters:
        - x0: list[float], corner coordinates [x, y, z]
        - extents: list[float], dimensions [dx, dy, dz]
        - char_length: float, optional characteristic length (deprecated)
        """

Cone Class

class Cone:
    """Cone with different top and bottom radii."""
    
    def __init__(self, center, axis, radius0, radius1, angle=2π):
        """
        Initialize cone.
        
        Parameters:
        - center: list[float], base center [x, y, z]
        - axis: list[float], axis direction [dx, dy, dz]
        - radius0: float, base radius
        - radius1: float, top radius
        - angle: float, angular opening (default: 2π)
        """

Cylinder Class

class Cylinder:
    """Cylinder with optional angular opening."""
    
    def __init__(self, x0, axis, radius, angle=2π):
        """
        Initialize cylinder.
        
        Parameters:
        - x0: list[float], base center [x, y, z]
        - axis: list[float], axis direction [dx, dy, dz]
        - radius: float, cylinder radius
        - angle: float, angular opening (default: 2π)
        """

Disk Class

class Disk:
    """Disk or ellipse."""
    
    def __init__(self, x0, radius0, radius1=None):
        """
        Initialize disk or ellipse.
        
        Parameters:
        - x0: list[float], center coordinates [x, y, z]
        - radius0: float, first radius
        - radius1: float, optional second radius for ellipse
        """

Rectangle Class

class Rectangle:
    """Rectangle with optional rounded corners."""
    
    def __init__(self, x0, a, b, corner_radius=None):
        """
        Initialize rectangle.
        
        Parameters:
        - x0: list[float], corner coordinates [x, y, z]
        - a: float, width in X direction
        - b: float, height in Y direction
        - corner_radius: float, optional corner radius
        """

Torus Class

class Torus:
    """Torus with inner and outer radii."""
    
    def __init__(self, center, radius0, radius1, alpha=2π):
        """
        Initialize torus.
        
        Parameters:
        - center: list[float], torus center [x, y, z]
        - radius0: float, minor radius (tube radius)
        - radius1: float, major radius (center to tube center)
        - alpha: float, angular span (default: 2π)
        """

Wedge Class

class Wedge:
    """Right angular wedge."""
    
    def __init__(self, x0, extents, top_extent=None):
        """
        Initialize wedge.
        
        Parameters:
        - x0: list[float], corner coordinates [x, y, z]
        - extents: list[float], base dimensions [dx, dy, dz]
        - top_extent: list[float], optional top dimensions for tapered wedge
        """

Common Shape Attributes

All OCC shape classes share the following attributes after creation:

# Common attributes for all shapes
shape.dim: int          # Shape dimension (0=point, 1=curve, 2=surface, 3=volume) 
shape._id: int          # Internal GMSH entity ID
shape.dim_tag: tuple    # (dimension, tag) tuple for GMSH operations
shape.dim_tags: list    # List of (dimension, tag) tuples for compound shapes

Install with Tessl CLI

npx tessl i tessl/pypi-pygmsh

docs

geo-geometry.md

helpers-and-utilities.md

index.md

mesh-control.md

occ-geometry.md

shape-classes.md

tile.json