Skip to content

OpenCASCADE Cheats

Sandro Elsweijer edited this page Dec 5, 2021 · 10 revisions

OpenCASCADE recipies

The opencascade reference documentation: http://dev.opencascade.org/doc/refman/html/index.html

Geometry based operations (Geom)

Intersections

Task Function/Class
curve - surface GeomAPI_IntCS

Projections

Task Function/Class
point -> curve GeomAPI_ProjectPointOnCurve
ShapeAnalysis_Curve
point -> surface GeomAPI_ProjectPointOnSurf
ShapeAnalysis_Surface
curve -> surface GeomProjLib::Project

Downcast handles of geometric objects

Example:

Handle_Geom2d_Line line   = Handle_Geom2d_Line(new Geom2d_Line(gp_Pnt2d(u1, v2), gp_Dir2d(0, v1-v2)));
Handle_Geom2d_Curve::DownCast(line);

Get curve length

Functions: GCPnts_AbscissaPoint::Length

Example:

Handle_Geom_Curve curve = ...
Standard_Real umin = curve->FirstParamter();
Standard_Real umax = curve->LastParamter();
GeomAdaptor_Curve adaptorCurve(curve, umin, umax);
Standard_Real length = GCPnts_AbscissaPoint::Length(adaptorCurve, umin, umax);

Create a ruled surface

Handle(Geom_Curve) aCrv1 = ...;
Handle(Geom_Curve) aCrv2 = ...;
Handle(Geom_Surface) aSurf = GeomFill::Surface (aCrv1, aCrv2);

Topology operations (TopoDS_Shape)

Convert TopoDS_Shape

Functions: TopoDS::Face, TopoDS::Wire, TopoDS::Edge...

Examples:

TopoDS_Face& face = TopoDS::Face(faceShape);
TopoDS_Face& edge = TopoDS::Edge(edgeShape);

Create TopoDS_Compound from multiple TopoDS_Shape

Functions: BRep_Builder::Add

Examples:

TopoDS_Compound profiles;
BRep_Builder profileBuilder;
profileBuilder.MakeCompound(profiles);
profileBuilder.Add(profiles, myEdge3);
profileBuilder.Add(profiles, myEdge4);

Iterate through subshapes of TopoDS_Shape

Functions: BRepTools_WireExplorer, TopExp_Explorer, TopExp::MapShapes

Examples:

  1. Get edges of wire:
TopoDS_Wire myWire = ...;
for (BRepTools_WireExplorer wireExplorer(wire); wireExplorer.More(); wireExplorer.Next())
{
    const TopoDS_Edge& edge = wireExplorer.Current();
    // do something with edge
}
  1. Get face of shape:
TopoDS_Shape myShape= ...;
for(TopExp_Explorer faceExplorer(myShape, TopAbs_FACE); faceExplorer.More(); faceExplorer.Next())
{
    // get current face and convert to TopoDS_Face
    const TopoDS_Face& face = TopoDS::Face(faceExplorer.Current());
    // do something with face
}
  1. Get face of shape alternative:
TopoDS_Shape myShape= ...;
TopTools_IndexedMapOfShape faceMap;
TopExp::MapShapes(myShape, TopAbs_FACE, faceMap);
for(int i = 1; i <= faceMap.Extend(); ++i)
{
    // get current face and convert to TopoDS_Face
   const TopoDS_Face& face = TopoDS::Face(faceMap(i));
}

Access underlying geometry

Classes: BRep_Tool, BRep_Tools

Examples:

  1. Get Geom_Surface from TopoDS_Face:
TopoDS_Face face = ...
// get surface and set bounds
Handle_Geom_Surface surface = BRep_Tool::Surface(face);
  1. Get U,V bounds of TopoDS_Face:
TopoDS_Face face = ...
Standard_Real umin, umax, vmin, vmax;
BRepTools::UVBounds(face, umin, umax, vmin, vmax)
  1. Get bounded Geom_Curve from TopoDS_Edge:
TopoDS_Edge edge = ...;
Standard_Real umin, umax;
// get yet unbounded curve and parameter bounds (umin, umax)
Handle(Geom_Curve) curve = BRep_Tool::Curve(edge, umin, umax);
// trim curve to represent edge
curve = new Geom_TrimmedCurve(curve, umin, umax);
  1. Get parametric curve on TopoDS_Face. The resulting curve will be the curve projected into the u,v domain of the face
TopoDS_Face face=...
// Edge must be an edge on the face!!!
TopoDS_Edge egde=...
Standard_Real umin, umax;
// 2d because curve is stored in UV space of surface
// get curve handle and paramter bounds
Handle_Geom2d_Curve hcurve = BRep_Tool::CurveOnSurface(edge, face, umin, umax);
// make bounded curve, else hcurve is inifinte
hcurve = new Geom2d_TrimmedCurve(hcurve, umin, umax);

Read and Save TopoDS_Shape to file

Classes: BRepTools::Write, BRepTools::Read

Example:

TopoDS_Shape myShape=...
///store to disc
BRepTools::Write(myShape, "shape.brep");

///read shape from disc
BRep_Builder b;
BRepTools::Read(myShape, "otherShape.brep", b);

Create a TopoDS_Shell from a collection of TopoDS_Faces

Classes: BRepBuilderAPI_Sewing

Example:

BRepBuilderAPI_Sewing shellMaker;
shellMaker.Add(face1);
shellMaker.Add(face2);
shellMAker.Add(face3);
...
// The resulting shape may consist of multiple shells!
// Use TopExp_Explorer to iterate through shells
TopoDS_Shape shell = shellMaker.SewedShape();

Create Solid from (multiple) TopoDS_Shell(s)

Classes: BRepBuilderAPI_MakeSolid

Example:

// Shape that contains at least one shell
TopoDS_Shape shellShape = ...

// Build solid
BRepBuilderAPI_MakeSolid solidmaker;
TopTools_IndexedMapOfShape shellMap;
TopExp::MapShapes(shellShape, TopAbs_SHELL, shellMap);
for(int ishell = 1; ishell <= shellMap.Extent(); ++ishell) {
    const TopoDS_Shell& shell = TopoDS::Shell(shellMap(ishell));
    solidmaker.Add(shell);
}
TopoDS_Shape solid = solidmaker.Solid();

Boolean Operations

Classes: BRepAlgoAPI_Common, BRepAlgoAPI_Fuse, BRepAlgoAPI_Cut, BRepAlgoAPI_Section

Examples:

  1. Fuse/boolean add two shapes:
TopoDS_Shape shape1 = ...
TopoDS_Shape shape2 = ...
TopoDS_Shape result = BRepAlgoAPI_Fuse(shape1, shape2);
  1. Multiple different boolean operations on same shapes (efficiently):
TopoDS_Shape shape1 = ...
TopoDS_Shape shape2 = ...

// create intermediate result for boolean ops
BOPCol_ListOfShape aLS;
aLS.Append(shape1);
aLS.Append(shape2);
BOPAlgo_PaveFiller dsFill;
dsFill.SetArguments(aLS);
dsFill.Perform();

// boolean add
TopoDS_Shape fusedResult = BRepAlgoAPI_Fuse(shape1, shape2, dsFill);
// boolean subtract
TopoDS_Shape cut1Result  = BRepAlgoAPI_Cut(shape1, shape2, dsFill);
// we can even swap the arguments to create the other cut
TopoDS_Shape cut2Result  = BRepAlgoAPI_Cut(shape2, shape1, dsFill);

Rotate TopoDS_Shape around gp_Lin

Classes: gce_MakeRotation, BRepBuilderAPI_Transform

Example:

// rotation 
double rad = ...

TopoDS_Shape shape = ...

// rotation Axis
gp_Lin line( ... , ... );

gp_Trsf trans = gce_MakeRotation(line,rad);

// apply transformation
BRepBuilderAPI_Transform form(shape,trans);

Get iso parametric lines of a TopoDS_Face

Classes: DBRep_IsoBuilder, Geom2dHatch_Hatcher

Example:

TopoDS_Face face = ...

// create 2 u and v iso curves
DBrep_IsoBuilder isoBuilder(face, DBL_MAX, 2);
// Get first u iso curve
Geom2d_AdapterCurve curve = isoBuilder.HatchingCurve(1);

Split shapes

Use Salomes splitting routine: Example:

GEOMAlgo_Splitter splitter;
splitter.AddArgument(face);
splitter.AddTool(edge);
splitter.AddTool(edge2);
splitter.Perform();
TopoDS_Shape res = splitter.Shape();

If one of the shapes is constructed out of 2d Geometries, it is imperative to add 3d attributes by

// add 3d attributes to an edge constructed out of 2d geometry
BRepLib::BuildCurves3d(edge);
TopoDS_Shape res = splitter.Shape();