一、本节课程
c++ Arx二次开发创建多段线
二、本节要讲解的知识点
如何应用C++ ARX二次开发创建多段线(AcDbPolyline、AcDb2dPolyLine、AcDb3dPolyline的使用)
三、具体内容
1、ARX提供了三种多段线相关的类:AcDbPolyline、AcDb2dPolyLine、AcDb3dPolyline。PLINE 创建的是AcDbPolyline,3DPOLY创建AcDb3dPolyline。创建一般的轻量多段线只需要调用AcDbPolyline的构造函数即可,创建矩形、正多边形、圆形、圆环对象,关键在于顶点和凸度的确定。
2、添加多段线对象的函数实现:
AcDbObjectId CPolylineUtil::Add(const AcGePoint2dArray &points,double width) { int numVertices=points.length(); AcDbPolyline *pPoly=new AcDbPolyline(numVertices); for (int i=0;i<numVertices;i++) { pPoly->addVertexAt(i,points.at(i),0,width,width); } return CDwgDatabaseUtil::PostToModelSpace(pPoly); }
AcDbObjectId CPolylineUtil::Add(const AcGePoint2d &ptStart,const AcGePoint2d &ptEnd,double width) { AcGePoint2dArray points; points.append(ptStart); points.append(ptEnd); return Add(points,width); } AcDbObjectId CPolylineUtil::Add3dPolyline(const AcGePoint3dArray &points) { AcGePoint3dArray verts=points; AcDb3dPolyline *pPoly3d=new AcDb3dPolyline(AcDb::k3dSimplePoly,verts); return CDwgDatabaseUtil::PostToModelSpace(pPoly3d); } AcDbObjectId CPolylineUtil::AddPolygon(const AcGePoint2d &ptCenter,int number,double radius,double rotation,double width) { double angle=2*CMathUtil::PI()/(double)number; AcGePoint2dArray points; for (int i=0;i<number;i++) { AcGePoint2d pt; pt.x=ptCenter.x+radius*cos(i*angle); pt.y=ptCenter.y+radius*sin(i*angle); points.append(pt);
} AcDbObjectId polyId=Add(points,width);
AcDbEntity *pEnt=NULL; if (acdbOpenObject(pEnt,polyId,AcDb::kForWrite)==Acad::eOk) { AcDbPolyline *pPoly=AcDbPolyline::cast(pEnt); if (pPoly!=NULL) { pPoly->setClosed(Adesk::kTrue); } pEnt->close();
} EntityUtil::Rotate(polyId,ptCenter,rotation); return polyId; } AcDbObjectId CPolylineUtil::AddRectangle(const AcGePoint2d &pt1,const AcGePoint2d &pt2,double width) {
double x1=pt1.x,x2=pt2.x; double y1=pt1.y,y2=pt2.y;
AcGePoint2d ptLeftBottom(min(x1,x2),min(y1,y2)); AcGePoint2d ptRightBottom(max(x1,x2),min(y1,y2)); AcGePoint2d ptRightTop(max(x1,x2),max(y1,y2)); AcGePoint2d ptLeftTop(min(x1,x2),max(y1,y2));
AcDbPolyline *pPoly=new AcDbPolyline(4); pPoly->addVertexAt(0,ptLeftBottom,0,width,width); pPoly->addVertexAt(1,ptRightBottom,0,width,width); pPoly->addVertexAt(2,ptRightTop,0,width,width); pPoly->addVertexAt(3,ptLeftTop,0,width,width); pPoly->setClosed(true); return CDwgDatabaseUtil::PostToModelSpace(pPoly);
} AcDbObjectId CPolylineUtil::AddPolyCircle(const AcGePoint2d &ptCenter,double radius,double width) { AcGePoint2d pt1,pt2,pt3; pt1.x=ptCenter.x+radius; pt1.y=ptCenter.y; pt2.x=ptCenter.x-radius; pt2.y=ptCenter.y; pt3.x=ptCenter.x+radius; pt3.y=ptCenter.y;
AcDbPolyline *pPoly=new AcDbPolyline(3); pPoly->addVertexAt(0,pt1,1,width,width); pPoly->addVertexAt(1,pt2,1,width,width); pPoly->addVertexAt(2,pt3,1,width,width); pPoly->setClosed(Adesk::kTrue);
return CDwgDatabaseUtil::PostToModelSpace(pPoly); } |
四、总结
1、AUTOCAD的三种多段线对象:AcDbPolyline、AcDb2dPolyline、AcDb3dPolyline,我们重点实现了AcDbPolyline、AcDb3dPolyline。
2、如何创建多段线,利用多段线对象来创建正多边形、矩形、圆、圆弧(关键点在于顶点坐标的计算和顶点凸度的概念的掌握)。如果对凸度概念不是很清楚,自己可以用代码来验证。