Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Working with Specific Entitites > Working with Multi-Polygons > Working with Multi-Polygon Loops
Working with Multi-Polygon Loops

A multi-polygon can have multiple loops that form the multi-polygon boundary. Each loop is a planar contour created from planar entities of the following types:

  • circle
  • ellipse
  • polyline
  • spline

The OdDbMPolygon class has the following methods to work with loops:

Get the number of loops

To find out the number of loops, use the numMPolygonLoops() method, which requires no parameters and returns the number of loops in the multi-polygon boundary as a positive integer value. If the number of loops is zero, the multi-polygon does not have loops. For example:

odPrintConsoleString(L"\nNumber of loops %i", pMPolygon->numMPolygonLoops());
odPrintConsoleString(L"\nNumber of invalid loops %i", pMPolygon->numMPolygonLoops() - pMPolygon->hatch()->numLoops());

Append Loops

To append loops to a multi-polygon, use the appendLoopFromBoundary() method, which returns an OdResult value and has three variants for using either an OdDbCircle, OdDbPolyline or OdDb2dPolyline. Input entities must have a closed boundary. A loop is not appended if it intersects with the existing loops. To append such a loop, specify the parameter: excludeCrossing = false.

To append a loop to a multi-polygon, you can also use the appendMPolygonLoop() method, which returns an OdResult value and has an array of points as an input parameter.

To create a multi-polygon with many loops at once, use one of the following two methods:

Note: These methods can be used only if an OdDbMPolygon object does not have loops.

Append loops using object IDs

To append loops to a multi-polygon using object IDs, use the appendLoopFromBoundary() method, which requires three parameters:

  • pointer to an entity (OdDbCircle, OdDbPolyline or OdDb2dPolyline) from which a loop is created
  • Boolean parameter to specify whether to exclude crossing loops
  • measurement accuracy tolerance for excluding crossing loops

For example:

OdDbBlockTableRecordPtr modelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr mpolygon = OdDbMPolygon::createObject();
  mpolygon->setDatabaseDefaults(pDb);
  mpolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  modelSpace->appendOdDbEntity(mpolygon);
  OdDbCirclePtr circle = OdDbCircle::createObject();
  circle->setCenter(OdGePoint3d(10, 10, 0));
  circle->setRadius(5.0);
  OdResult result = mpolygon->appendLoopFromBoundary(circle);

Append loops using an array of points

To append loops to a multi-polygon using an array of points, use the appendMPolygonLoop() method, which requires four parameters:

  • object of the OdGePoint2dArray class to specify an array of points (vertices)
  • object of the OdGeDoubledArray class to specify bulges corresponding to the vertices array
  • Boolean parameter to specify whether to exclude crossing loops
  • measurement accuracy tolerance for excluding crossing loops

For example:

OdDbBlockTableRecordPtr modelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr mpolygon = OdDbMPolygon::createObject();
  mpolygon->setDatabaseDefaults(pDb);
  mpolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  modelSpace->appendOdDbEntity(mpolygon);
  OdGePoint2dArray vertices;
  vertices.append(OdGePoint2d(-200, 200));
  vertices.append(OdGePoint2d(-200, -200));
  vertices.append(OdGePoint2d(200, -200));
  vertices.append(OdGePoint2d(200, 200));
  vertices.append(OdGePoint2d(-200, 200));
  OdGeDoubleArray bulges;
  bulges.resize(5, 0.0);
  OdResult result = mpolygon->appendMPolygonLoop(vertices, bulges);

Insert a loop at a specific index

To insert a loop at a specific index in the loop array of a multi-polygon, use the insertMPolygonLoopAt() method, which returns an OdResult value and requires five parameters:

  • integer value to specify the index of the loop in the loop array
  • object of the OdGePoint2dArray class to specify an array of points
  • object of the OdGeDoubledArray class to specify bulges corresponding to the vertices array
  • Boolean parameter to specify whether to exclude crossing loops
  • measurement accuracy tolerance for excluding crossing loops

For example:

OdDbBlockTableRecordPtr modelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr mpolygon = OdDbMPolygon::createObject();
  mpolygon->setDatabaseDefaults(pDb);
  mpolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  modelSpace->appendOdDbEntity(mpolygon);
OdGePoint2dArray vertices;
  vertices.append(OdGePoint2d(-200, 200));
  vertices.append(OdGePoint2d(-200, -200));
  vertices.append(OdGePoint2d(200, -200));
  vertices.append(OdGePoint2d(200, 200));
  vertices.append(OdGePoint2d(-200, 200));
  OdGeDoubleArray bulges;
  bulges.resize(5, 0.0);
  OdResult result = mpolygon->insertMPolygonLoopAt(1, vertices, bulges);

Create loops using an array of point arrays

To append loops to a multi-polygon using an array of points, use the createLoops() method, which requires five parameters:

  • object of the OdGePoint2dArrayArray class to specify an array of arrays of points (vertices)
  • object of the OdArray<OdGeDoubleArray> class to specify an array of bulges corresponding to the vertices array
  • object of the OdIntArray class to specify an array of indices that indicates the elements from the array of arrays of points that led to errors while creating loops
  • Boolean parameter to specify whether to exclude crossing loops
  • measurement accuracy tolerance for excluding crossing loops

For example:

OdDbBlockTableRecordPtr modelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr mpolygon = OdDbMPolygon::createObject();
  mpolygon->setDatabaseDefaults(pDb);
  mpolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  modelSpace->appendOdDbEntity(mpolygon);
OdGePoint2dArrayArray verticesArr;
OdGePoint2dArray vertices1, vertices2;
vertices1.append(OdGePoint2d(0, 0));
vertices1.append(OdGePoint2d(10, 0));
vertices1.append(OdGePoint2d(5, 10));
vertices2.append(OdGePoint2d(20, 0));
vertices2.append(OdGePoint2d(30, 0));
vertices2.append(OdGePoint2d(25, 10));
verticesArr.append(vertices1);
verticesArr.append(vertices2);

OdArray<OdGeDoubleArray> bulgesArr;
OdGeDoubleArray bulges1, bulges2;
bulges1.resize(3, 0.0);
bulges2.resize(3, 0.0);
bulgesArr.append(bulges1);
bulgesArr.append(bulges2);
OdIntArray rejectedObjs;
OdResult result = mpolygon->createLoops(verticesArr, bulgesArr, rejectedObjs);

Create loops using an array of object IDs

To append loops to a multi-polygon using an array of object IDs, use the createLoopsFromBoundaries() method, which requires four parameters:

  • object of the OdDbObjectIdArray class to specify the object IDs of the objects that comprise the loops
  • object of the OdIntArray class to specify indices from the array of arrays of points that led to errors while creating loops
  • Boolean parameter to specify whether to exclude crossing loops
  • measurement accuracy tolerance for excluding crossing loops

For example:

OdDbBlockTableRecordPtr modelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite);
  OdDbMPolygonPtr mpolygon = OdDbMPolygon::createObject();
  mpolygon->setDatabaseDefaults(pDb);
  mpolygon->setPattern(OdDbHatch::kPreDefined, L"SOLID");
  modelSpace->appendOdDbEntity(mpolygon);
  OdDbCirclePtr circle1 = OdDbCircle::createObject();
  circle1->setCenter(OdGePoint3d(10, 10, 0));
  circle1->setRadius(5.0);
  OdDbCirclePtr circle2 = OdDbCircle::createObject();
  circle2->setCenter(OdGePoint3d(200, 200, 0));
  circle2->setRadius(5.0);

OdDbObjectIdArray arrId;
OdIntArray rejectedObjs;
arrid.append(circle1->objectId());
arrid.append(circle2->objectId());
OdResult result = mpolygon-> createLoopsFromBoundaries (arrId, rejectedObjs);

Get a loop by using a specific index

To get a multi-polygon loop by using a specific index in the loop array, use the getMPolygonLoopAt() method. This method gets the loop as an array of vertices that the loop consists of and the array of bulges corresponding to the vertices array.

The getMPolygonLoopAt() method requires three parameters:

  • an input integer value to specify the loop index
  • an output pointer to an OdGePoint2dArray object to receive the placement of the output loop
  • an output pointer to an OdGeDoubleArray object to receive the placement of the output bulges

For example:

OdGePoint2dArray vertexPts;
OdGeDoubleArray vertexBulges;
OdResult result = pMPolygon->getMPolygonLoopAt (0, vertexPts, vertexBulges);

See Also

Working with Multi-Polygons

Overview of Multi-Polygons

Working with the Loop Direction

Examples of Working with Multi-Polygons

OdDbMPolygon class

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