ODA Facet Modeler Developer Guide > Work with B-rep Data > Calculate Area and Volume
Calculate Area and Volume

This chapter provides information on how to calculate volume for bodies and how to calculate area for profiles.

In Facet Modeler there are only two entity types that can have area and volume properties: Profile2D and Body. Since a Profile2D is two-dimensional and a body is three-dimensional, the methods of calculations are different. It's important to note that if you want to see the value of the area or volume in a console window on Microsoft® Windows®, you should use the odPrintConsoleString() method.

Profile2D

As mentioned in the topic about profiles and contours, a profile is an array of contours. That means it's possible to calculate a contour's area; to do that you can use the area() method.

The following code snippet creates a square with side length dSide as a contour, calculates its area and prints it to the console window, and also it returns a square as a Profile2D. This method is also used as a base later in this topic.

Profile2D createSquare(const double dSide)
{
  // Create a contour
  Contour2D square;

  // Set points that form a square
  OdGePoint2dArray aPoints;
  aPoints.reserve(4);
  aPoints.push_back(OdGePoint2d(dSide, dSide));
  aPoints.push_back(OdGePoint2d(dSide, 0.0));
  aPoints.push_back(OdGePoint2d::kOrigin);
  aPoints.push_back(OdGePoint2d(0.0, dSide));

  // Insert points into the contour
  square.appendVertices(aPoints);

  // Form edges by connecting points and close the profile
  square.setOrientationAt(0, efoFront);
  square.setOrientationAt(1, efoFront);
  square.setOrientationAt(2, efoFront);
  square.setOrientationAt(3, efoFront);
  square.setClosed();

  // Make the contour outer by setting its direction counter-clockwise
  square.makeCCW();

  const OdString& indent = OdString::kEmpty;
  odPrintConsoleString(L"square area: %f\n", square.area());

  return Profile2D(square);
}

To calculate the area value for a Profile2D, use the signedArea() method:

double signedArea() const;

The following line calculates the area of a square with side length dSide:

double area = createSquare(dSide).signedArea();

Since a body's face is technically a profile, you can also calculate its area using the area() method.

The following code snippet calculates the face area of a given body by iterating through its faces and returns it as OdDouble. To get more information on iteration in Facet Modeler, see the corresponding topic.

OdDouble calcFaceArea(Body& body) 
{
  FacetModeler::FaceIterator iterator(&body);
  OdDouble faceArea = 0;
  for (; !iterator.done(); iterator.next())
  {
    faceArea += iterator.get()->area();
  }
  return faceArea;
}

Body

To calculate the volume value for a body, use the volume() method:

double volume() const;

The following line calculates the volume of a cube with side length edgeLen:

double volume = createCube(edgeLen).volume();

The createCube() function was implemented in the topic about creating basic objects.

See Also

Work With B-Rep Data in Facet Modeler
Create Basic Objects
Create Basic 2D Objects
Create Objects via Revolution
Create Objects via Extrusion
Iterate through Entities
Perform Boolean Operations
Perform Slicing Operations
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.