Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Working with Specific Entitites > Working with Multi-Polygons > Working with the Loop Direction
Working with the Loop Direction

Loop area and direction

The direction of a multi-polygon loop matters because the area calculation takes into account the direction of the loop. A loop with counterclockwise direction has a positive area value. Clockwise direction results in a negative value. To find out the direction of a loop, use the getLoopDirection() method. The method requires two parameters:

  • input integer value to specify the loop index
  • output enumeration of the loopDir type to indicate the loop direction

The loopDir enumeration defines the following values for loop directions:

enum loopDir {
  kExterior   = 0,   // Loop direction is counterclockwise
  kInterior   = 1,   // Loop direction is clockwise
  kAnnotation = 2    // Text
};

Example of a multi-polygon with a negative area:

OdDbDatabasePtr pDb = pHostApp->createDatabase();
  OdDbBlockTableRecordPtr pModelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr pMPolygon = OdDbMPolygon::createObject();
  pMPolygon->setDatabaseDefaults(pDb);
  pMPolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  pModelSpace->appendOdDbEntity(pMPolygon);

  OdGePoint2dArray vertexArr;
  vertexArr.append(OdGePoint2d(0, 0));
  vertexArr.append(OdGePoint2d(5, 5));
  vertexArr.append(OdGePoint2d(5, 0));
  OdGeDoubleArray bugles;
  bugles.resize(3, 0);
  pMPolygon->appendMPolygonLoop(vertexArr, bugles);
  double area = 0;
  pMPolygon->getArea(area); // area == -12.50
  OdDbMPolygon::loopDir loopDirection;
  pMPolygon->getLoopDirection(0, loopDirection); // loopDirection == kInterior

Change the loop direction manually

To change the loop direction of a multi-polygon, use the setLoopDirection() method. It requires two parameters:

  • input integer value to specify the loop index
  • input value of the loopDir type to specify the loop direction

Continued previous example:

loopDirection = OdDbMPolygon::kExterior;
  pMPolygon->setLoopDirection(0, loopDirection); // change the loop direction from clockwise to counter-clockwise
  pMPolygon->getArea(area); // area == 12.5

Automatic correction of the loop direction

To change a loop direction automatically, use the balanceTree() method. The direction of the loops is changed to the correct one.

Note: If there are loops that intersect one another, one of them is marked as invalid and no longer participates in rendering of the multi-polygon as a filled loop. Depending on the POLYDISPLAY system variable, the outline of the invalid loop can be drawn or not.

Continued from the first example:

pMPolygon->balanceTree();
pMPolygon->getArea(area); // area == 12.5

The figure below illustrates the result of the balanceTree() method when loops intersect each other.

The next figure illustrates the result of executing the balanceTree() method if the system variable POLYDISPLAY == 0.

Validate the loop direction and intersection

To validate the correct direction of multi-polygon loops and their intersections, use the balanceDisplay() method. If the loop that is drawn has intersections or the wrong direction, it is marked as invalid. If among the invalid loops there are loops that no longer intersect or have the correct direction, they are marked as valid.

Note: The balanceDisplay() method does not correct the direction of loops. To do this, use the balanceTree() or setLoopDirection() method.

See Also

Working with Multi-Polygons

Working with Multi-Polygon Loops

Examples of Working with Multi-Polygons

Copyright © 2002 – 2021. Open Design Alliance. All rights reserved.