| name | opencascade-python |
| description | Access OpenCASCADE geometry kernel through Python bindings (pythonocc-core or OCP). Use for B-rep modeling, NURBS surfaces, boolean operations, and STEP/IGES file handling. Foundation for CadQuery and build123d. |
OpenCASCADE Python
Python bindings for the OpenCASCADE CAD kernel.
Installation
pip install cadquery-ocp # OCP bindings
# or
conda install -c conda-forge pythonocc-core
Basic Shapes with OCP
from OCP.BRepPrimAPI import (
BRepPrimAPI_MakeBox,
BRepPrimAPI_MakeCylinder,
BRepPrimAPI_MakeSphere
)
from OCP.gp import gp_Pnt, gp_Vec, gp_Ax2, gp_Dir
# Create box
box = BRepPrimAPI_MakeBox(10, 20, 30).Shape()
# Create cylinder
cylinder = BRepPrimAPI_MakeCylinder(5, 20).Shape()
# Create sphere
sphere = BRepPrimAPI_MakeSphere(10).Shape()
Boolean Operations
from OCP.BRepAlgoAPI import (
BRepAlgoAPI_Fuse,
BRepAlgoAPI_Cut,
BRepAlgoAPI_Common
)
# Union
fused = BRepAlgoAPI_Fuse(shape1, shape2).Shape()
# Subtraction
cut = BRepAlgoAPI_Cut(base, tool).Shape()
# Intersection
common = BRepAlgoAPI_Common(shape1, shape2).Shape()
Transformations
from OCP.gp import gp_Trsf, gp_Vec
from OCP.BRepBuilderAPI import BRepBuilderAPI_Transform
# Create transformation
trsf = gp_Trsf()
trsf.SetTranslation(gp_Vec(10, 0, 0))
# Apply to shape
transform = BRepBuilderAPI_Transform(shape, trsf)
moved_shape = transform.Shape()
# Rotation
trsf.SetRotation(gp_Ax1(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 3.14159/4)
NURBS and Curves
from OCP.Geom import Geom_BSplineCurve
from OCP.TColgp import TColgp_Array1OfPnt
from OCP.TColStd import TColStd_Array1OfReal, TColStd_Array1OfInteger
# Define control points
points = TColgp_Array1OfPnt(1, 4)
points.SetValue(1, gp_Pnt(0, 0, 0))
points.SetValue(2, gp_Pnt(1, 2, 0))
points.SetValue(3, gp_Pnt(3, 2, 0))
points.SetValue(4, gp_Pnt(4, 0, 0))
# Create B-spline
curve = Geom_BSplineCurve(points, knots, multiplicities, degree)
File I/O
from OCP.STEPControl import STEPControl_Reader, STEPControl_Writer
# Read STEP
reader = STEPControl_Reader()
reader.ReadFile("input.step")
reader.TransferRoots()
shape = reader.OneShape()
# Write STEP
writer = STEPControl_Writer()
writer.Transfer(shape, STEPControl_AsIs)
writer.Write("output.step")
Topology Exploration
from OCP.TopExp import TopExp_Explorer
from OCP.TopAbs import TopAbs_FACE, TopAbs_EDGE
# Iterate faces
explorer = TopExp_Explorer(shape, TopAbs_FACE)
faces = []
while explorer.More():
faces.append(explorer.Current())
explorer.Next()