Working with Geometry

Geometry is accessible through wrapped interfaces of the underlying geometry library. This example shows how to access the geometry of a shape and to work with the geometric library.

Get the Geometry

In out sample script, we want to sum up the areas of all faces of any given shape. We start our script with the retrieval of the selected body.

C#
using Macad.Interaction;

// ...

var body = Selection.SelectedEntities.FirstOrDefault() as Body;
if (body == null)
{
  Messages.Error("Please select a body.");
  return;
}

We now retrieve the geometry by accessing the shape function GetBRep. The result object holds our geometry in an instance of TopoDS_Shape, which is already a wrapped type of the geometry library.

C#
var bRep = body.Shape.GetBRep();
if (bRep == null)
{
  Messages.Error("Body shape is not valid.");
  return;
}

Use the Geometry

The BRep geometry consists of various types of geometry, including faces. They can be accessed from any TopoDS_Shape using the extension methods of TopoDSShapeExtensions.

The area of each face is a property which can be calculated using the static class BRepGProp and GProp_GProps. Please consult the documentation of the geometry library for usage of this classes.

C#
double sumArea = 0.0f;
GProp_GProps massProps = new GProp_GProps();
foreach (var face in bRep.Faces())
{
  BRepGProp.SurfaceProperties(face, massProps);
  sumArea += massProps.Mass();
}

Finally, let's print out the sum.

C#
Messages.Info(string.Format("The sum of all face areas is {0}", sumArea));

  Note

Working with geometric data demands knowledge of the OpenCASCADE Technology SDK. You can find the latest documentation following in the links section. Most of the SDK is wrapped in Occt namespace. Please note that most of the basic types (beginning with gp_) are wrapped as value types, omitting the prefix (e.g. gp_Pnt is wrapped as value type Pnt).

Complete Script

C#
using System.Linq;
using Macad.Interaction;

// Get selected body
var body = Selection.SelectedEntities.FirstOrDefault() as Body;
if (body == null)
{
    Messages.Error("Please select a body.");
    return;
}

// Get geometry from body
var bRep = body.Shape.GetBRep();
if (bRep == null)
{
    Messages.Error("Body shape is not valid.");
    return;
}

// Sum up face area
double sumArea = 0.0f;
GProp_GProps massProps = new GProp_GProps();
foreach (var face in bRep.Faces())
{
    // We could use face.Area() to get the area, but we want to show how interaction
    // with OCCT works, so we do it manually 
    BRepGProp.SurfaceProperties(face, massProps);
    sumArea += massProps.Mass();
}

// Printout
Messages.Info(string.Format("The sum of all face areas is {0}", sumArea));

See Also