ARX合并多线段(部分内容参考其他人)
展开
void CCommonModel::AppendPoly( const AcDbObjectIdArray& objArray,AcDbPolyline *pPolyNew )
{
AcDbObjectIdArray objIdArray = objArray;
std::multimap<AcGePoint3d,int> mapPoints; // AcGePoint3d不能排序,要重载符号“<”;
std::multimap<AcGePoint3d,int>::iterator ite1;
std::multimap<AcGePoint3d,int>::iterator ite2;
for (int i = 0; i < objIdArray.length(); i++)
{
AcDbObjectId objId;
objId = objIdArray.at(i);
AcGePoint3d ptStart;
AcGePoint3d ptEnd;
AcDbObjectPointer<AcDbPolyline> pPoly(objId,AcDb::kForRead);
pPoly->getStartPoint(ptStart);
pPoly->getEndPoint(ptEnd);
if (i == 0)
{
mapPoints.insert(std::make_pair(ptStart,1));
mapPoints.insert(std::make_pair(ptEnd,1));
}
else
{
ite1 = mapPoints.find(ptStart);
ite2 = mapPoints.find(ptEnd);
if (ite1 != mapPoints.end())
{
(ite1->second)++;
}
else
{
mapPoints.insert(std::make_pair(ptStart,1));
}
ite2 = mapPoints.find(ptEnd);
if (ite2 != mapPoints.end())
{
(ite2->second)++;
}
else
{
mapPoints.insert(std::make_pair(ptEnd,1));
}
}
}
AcGePoint3d ptStartOk;
for (ite1 = mapPoints.begin(); ite1 != mapPoints.end(); ite1++)
{
if (ite1->second == 1)
{
ptStartOk = ite1->first;
break;
}
}
int plIndex = 0;
while(objIdArray.length()>0)
{
for (int i = 0; i < objIdArray.length(); i++) // 问题,如果几条多线段不是首尾相接的,那么死循环。
{
AcDbObjectId objId;
objId = objIdArray.at(i);
AcGePoint3d ptStart;
AcGePoint3d ptEnd;
AcDbObjectPointer<AcDbPolyline> pPoly(objId,AcDb::kForRead);
pPoly->getStartPoint(ptStart);
pPoly->getEndPoint(ptEnd);
AcGePoint2d pt ;
double bulge = 0.0;
if (ptStartOk == ptStart) // 判断点相等,用自带的函数或者用点距离
{
for (int j= 0; j < pPoly->numVerts(); j++ ) // 问题:最后点会被下一个加入点给覆盖掉,即有重合点
{
pPoly->getPointAt(j,pt);
pPoly->getBulgeAt(j,bulge);
pPolyNew->addVertexAt(plIndex,pt,bulge);
plIndex++;
}
ptStartOk = ptEnd;
objIdArray.removeAt(i);
}
else if(ptStartOk == ptEnd) // 判断点相等,用自带的函数或者用点距离
{
for(int k = pPoly->numVerts() - 1;k > 0; k--)
{
pPoly->getPointAt(k,pt);
if(k > 0)
{
pPoly->getBulgeAt(k - 1,bulge);
}
else
{
pPoly->getBulgeAt(0,bulge);
}
pPolyNew->addVertexAt(plIndex,pt,-bulge);
plIndex++;
}
ptStartOk = ptStart;
objIdArray.removeAt(i);
}
else
{
}
}
}
}
bool operator < (AcGePoint3d pt3dS,AcGePoint3d pt3dE)
{
if (pt3dS.x < pt3dE.x)
{
return true;
}
else if (pt3dS.x == pt3dE.x)
{
if (pt3dS.y < pt3dE.y)
{
return true;
}
else if (pt3dS.y == pt3dE.y)
{
return false;
}
else
{
return false;
}
}
else
{
return false;
}
}
改进方法:1.判断map中点出现次数为1次的点有多少个,如果大于2个,提示错误。
2.如果几条多线段是从一个闭合的多线段中截取出来的,那么map中点出现次数为1次的点有2或者4个,如果是4个,判断其中2个点是否为原多线段的起始点。然后闭合连接起来。