Source code for pyvcad_metamaterials.occ
"""Lazy raw OCP shape adapters using in-memory ASCII BREP streams."""
# STD
from io import BytesIO
# OpenVCAD
import pyvcad as pv
def _ocp():
try:
from OCP.BRepTools import BRepTools
from OCP.TopoDS import TopoDS_Shape
except ImportError as error:
raise ImportError(
"OCP support is optional. Install OpenVCAD[cad] or "
"pyvcad-metamaterials[cad] to use mm.occ."
) from error
return BRepTools, TopoDS_Shape
def _model(shape):
brep_tools, shape_type = _ocp()
if not isinstance(shape, shape_type):
raise TypeError("expected an OCP TopoDS_Shape")
with BytesIO() as stream:
brep_tools.Write_s(shape, stream)
return pv.CADModel.from_brep_bytes(stream.getvalue())
[docs]
def body(shape):
"""Convert a raw OCP shape into a topology-preserving CADModel."""
return _model(shape)
[docs]
def face(shape):
"""Convert one raw OCP face into a CADFace."""
model = _model(shape)
if len(model.faces) != 1:
raise ValueError("OCP face adapter requires a shape containing exactly one face")
return model.faces[0]
[docs]
def faces(shapes):
"""Convert raw OCP faces into independent CADFace handles."""
return [face(shape) for shape in shapes]