|
Issue
How do I convert planar AcDb3dPolyline to AcDb2dPolyline? The coordinates
obtained from an existing 3D polyline are in WCS, and the AcDb2dPolyline needs
vertex coordinates in an Object Coordinate System
Solution
If the AcDb3dPolylines are in the current UCS, then it is easier to convert the
vertex points obtained from the WCS to an Object Coordinate System by getting
the normal of the current UCS, which will be subsequently used in
acdbWcs2Ecs().
The following sample code demonstrates the same; the new AcDb2dPolyLines are
drawn in a blue color on layer "0".
void asdktest()
{
ads_name ss;
ACHAR* prompts[2]={L"\nSelect 3d polylines",L"\nUnselect objects"};
int res=acedSSGet(L":$",prompts,NULL,NULL,ss);
if (RTNORM != res)
{
acutPrintf(L"\nNo objects selected");
return;
}
long len;
res=acedSSLength(ss,&len);
if (RTNORM != res || len == 0)
{
acutPrintf(L"\nNo objects selected");
return;
}
//get coordinate system from current UCS
AcGeMatrix3d mat;
acdbUcsMatrix(mat);
AcGePoint3d origin;
AcGeVector3d xAxis;
AcGeVector3d yAxis;
AcGeVector3d norm;
mat.getCoordSystem(origin,xAxis,yAxis,norm);
Acad::ErrorStatus es;
for(int i=0 ; i < len ; i++)
{
ads_name eName;
acedSSName(ss,i,eName);
AcDbObjectId objId;
acdbGetObjectId(objId,eName);
AcDb3dPolyline *pPline=NULL;
es=acdbOpenObject(pPline, objId, AcDb::kForRead);
if (Acad::eOk == es)
{
AcDbObjectIterator *pVertIter= pPline->vertexIterator();
Adesk::Boolean isClosed=Adesk::kFalse;
isClosed=pPline->isClosed();
pPline->close();
// Finished with the pline header.
AcGePoint3d location;
AcDbObjectId vertexObjId;
AcGePoint3dArray aPts;
for (;!pVertIter->done();pVertIter->step())
{
AcDb3dPolylineVertex *pVertex;
vertexObjId = pVertIter->objectId();
acdbOpenObject(pVertex, vertexObjId, AcDb::kForRead);
aPts.append(pVertex->position());
pVertex->close();
}
delete pVertIter;
AcGePoint3dArray newPts;
AcGePoint3d newPt;
for (int i=0 ; i < aPts.length() ; i++)
{
acdbWcs2Ecs(asDblArray(aPts),asDblArray(newPt), asDblArray(norm),Adesk::kFalse);
newPts.append( newPt);
}
AcDb2dPolyline *pPoly = new AcDb2dPolyline(AcDb::k2dSimplePoly /*poly type*/,
newPts /* vertices */,
newPts.at(0)[Z] /* elevation */,
isClosed /* closed or open*/);
pPoly->setNormal(norm);
pPoly->setLayer(L"0");
pPoly->setColorIndex(5); //blue color
postToModelSpace(pPoly);
pPoly->close();
}//if
}//for
acedSSFree(ss);
}/* end of asktest*/ |
|