Issue
I can't iterate through a modified AcDb2dPolyline or AcDb3dPolyline that has its first vertex marked as erased. How can I solve this problem?
Solution
This is a known issue with the iterator. The iterator for 2D and 3D polylines will not skip an erased first vertex when the iterator is first created. To work around this, get the objectId of the first vertex from the iterator, then use AcDbObjectId::isErased() to check to see if the vertex is erased before starting any processing using the iterator. If the vertex is erased, then step the iterator to the next vertex. This will automatically skip any erased vertices
immediately after the first vertex.
The code demo below shows the issue and solution.
static void iteratePLVer()
{
// select the 2d polyline
ads_name eName;
ads_point pt;
int res = ads_entsel(_T("nPlease pick a polyline"), eName, pt);
if (res != RTNORM)
return;
// convert the ename to an ObjectARX objectid
AcDbObjectId id;
acdbGetObjectId( id, eName );
// AcDb2dPolyline or AcDb3dPolyline
AcDbObjectPointer pPoly(id,
AcDb::kForWrite);
//AcDbObjectPointer pPoly(id,
// AcDb::kForWrite);
if (pPoly.openStatus() == Acad::eOk)
{
AcDbObjectIterator *pIter =
pPoly->vertexIterator();
int count = 0;
for(; !pIter->done(); pIter->step() )
{
count++;
}
acutPrintf(
_T("nBefore erase, there are %d vertex:"),
count);
acutPrintf(
_T("n erase one vertex such as the first one"));
pIter->start();
AcDbObjectId vid = pIter->objectId();
AcDbObjectPointer pVertex(vid,
AcDb::kForWrite);
if(pVertex.openStatus() ==
Acad::eOk)
{
pVertex->erase();
}
else
{
return;
}
delete pIter;
pIter = pPoly->vertexIterator();
count = 0;
acutPrintf(_T("nIterate vertex again:"));
for(; !pIter->done();
pIter->step() )
{
count++;
}
acutPrintf(_T("nWithout checking status of erase,
there are %d vertex:"),count);
acutPrintf(_T("Now nIterate vertex again,
checking status of erase"));
count = 0;
pIter->start();
for(; !pIter->done();
pIter->step() )
{
vid = pIter->objectId();
if(!vid.isErased())
count++;
}
acutPrintf(_T("nchecking status of erase,
there are %d valid vertex:"),count);
delete pIter;
}
}

Leave a Reply