创建多段线
函数申明:
1 //多段线 2 static AcDbObjectId CreatePolyline(AcGePoint2dArray pts, double width=0); //创建多段线 3 static AcDbObjectId CreatePolyLine(AcGePoint2d ptStart, AcGePoint2d ptEnd, double width); //利用多段线创建直线 4 static AcDbObjectId Create3dPolyline(AcGePoint3dArray pts3d); //多点创建三维多段线 5 static AcDbObjectId CreatePolygon(AcGePoint2d ptCenter, int number ,double radius, double rotateAng, double width); //创建多边形 6 static AcDbObjectId CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2, double width); //创建矩形 7 static AcDbObjectId CreatePolyCircle(AcGePoint2d ptCenter, double radius, double width); //创建圆 8 static AcDbObjectId CreatePolyArc(AcGePoint2d ptCenter, double radius, double startAng, double endAng, double width); //创建圆弧
创建多段线函数:
1 AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2dArray pts, double width/* =0 */) 2 { 3 int numVertices = pts.length(); //顶点个数 4 AcDbPolyline* pPolyline = new AcDbPolyline(numVertices); //多段线 5 for(int i=0;i<numVertices;i++) 6 { 7 pPolyline->addVertexAt(i,pts.at(i),0,width,width); //插入定点,凸度为0 8 } 9 return CCreateEnt::PostToModelSpace(pPolyline); //添加到模型空间 10 }
利用多段线创建直线:
1 AcDbObjectId CCreateEnt::CreatePolyLine(AcGePoint2d ptStart, AcGePoint2d ptEnd, double width) 2 { 3 AcGePoint2dArray pts; 4 pts.append(ptStart); //直线起点 5 pts.append(ptEnd); //终点 6 7 return CCreateEnt::CreatePolyline(pts,width); 8 }
利用多段线创建矩形:
1 AcDbObjectId CCreateEnt::CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2, double width) 2 { 3 double x1=pt1.x,x2= pt2.x; 4 double y1=pt1.y,y2=pt2.y; 5 6 AcGePoint2d pLB(CCalculation::Min(x1,x2),CCalculation::Min(y1,y2)); //左下角点 7 AcGePoint2d pRB(CCalculation::Max(x1,x2),CCalculation::Min(y1,y2)); //右下角点 8 AcGePoint2d pRT(CCalculation::Max(x1,x2),CCalculation::Max(y1,y2)); //右上角点 9 AcGePoint2d pLT(CCalculation::Min(x1,x2),CCalculation::Max(y1,y2)); //左上角点 10 11 AcGePoint2dArray pts; 12 AcDbPolyline* pPolyline= new AcDbPolyline(4); 13 pPolyline->addVertexAt(0,pLB,0,width,width); 14 pPolyline->addVertexAt(1,pRB,0,width,width); 15 pPolyline->addVertexAt(2,pRT,0,width,width); 16 pPolyline->addVertexAt(3,pLT,0,width,width); 17 pPolyline->setClosed(Adesk::kTrue); //使多段线闭合 18 19 return CCreateEnt::PostToModelSpace(pPolyline); 20 21 }
利用多段线创建正多边形:
1 AcDbObjectId CCreateEnt::CreatePolygon(AcGePoint2d ptCenter, int number ,double radius, double rotateAng, double width) //中心点,边数,初始角度,旋转角度,线宽 2 { 3 AcGePoint2dArray pts; 4 double rad= 2*CCalculation::PI()/number; //获取角度 5 6 for (int i=0;i<number;i++) //获取多边形的各个定点 7 { 8 AcGePoint2d pt; 9 pt.x= ptCenter.x+radius*cos(i*rad); 10 pt.y = ptCenter.y+radius*sin(i*rad); 11 pts.append(pt); 12 13 } 14 15 AcDbObjectId polyId=CCreateEnt::CreatePolyline(pts,width); //创建正多边形 16 17 AcDbEntity* pEnt; 18 acdbOpenObject(pEnt,polyId,AcDb::kForWrite); 19 if(pEnt->isKindOf(pEnt->desc()) == Adesk::kTrue) 20 { 21 AcDbPolyline* pPolyline = AcDbPolyline::cast(pEnt); 22 if(NULL != pPolyline) 23 { 24 pPolyline->setClosed(Adesk::kTrue); //闭合多段线 25 pPolyline->close(); 26 } 27 } 28 pEnt->close(); 29 30 CModifyEnt::Rotate(polyId,CCalculation::Pt2dTo3d(ptCenter),rotateAng); //旋转正多边形 31 return polyId; 32 33 }
对象旋转函数如下:
1 //旋转 2 Acad::ErrorStatus CModifyEnt::Rotate(AcDbObjectId entId, AcGePoint3d ptBase, double rotateAng) //对象ID,基点,旋转角度 3 { 4 AcGeMatrix3d xform; //旋转矩阵 5 AcGeVector3d vec(0,0,1); //法向量 6 xform.setToRotation(rotateAng,vec,ptBase); 7 8 return CModifyEnt::Transform(xform,entId); 9 }
变换函数如下:
1 inline static Acad::ErrorStatus Transform(AcGeMatrix3d& xform, AcDbObjectId entId) //变换矩阵,对象ID 2 { 3 AcDbEntity* pEnt=0; 4 Acad::ErrorStatus es; 5 es = acdbOpenObject(pEnt,entId,AcDb::kForWrite); //获取对象 6 if(es == Acad::eOk) 7 { 8 pEnt->transformBy(xform); //变换对象 9 pEnt->close(); 10 } 11 return es; 12 }
变换函数根据传入不同的变换矩阵,对实体进行变换操作(旋转,平移,缩放)
利用多段线创建圆
1 AcDbObjectId CCreateEnt::CreatePolyCircle(AcGePoint2d ptCenter, double radius, double width) //圆心,半径,线宽 2 { 3 AcGePoint2d pt1,pt2,pt3; //构建圆上3点 4 pt1.x=ptCenter.x+radius; 5 pt1.y=pt2.y=pt3.y=ptCenter.y; 6 pt2.x=ptCenter.x-radius; 7 pt3.x=ptCenter.x+radius; 8 9 AcDbPolyline* pPolyline = new AcDbPolyline(3); 10 pPolyline->addVertexAt(0,pt1,1,width,width); 11 pPolyline->addVertexAt(1,pt2,1,width,width); 12 pPolyline->addVertexAt(2,pt3,1,width,width); 13 pPolyline->setClosed(Adesk::kTrue); 14 15 return CCreateEnt::PostToModelSpace(pPolyline); 16 17 }
利用多段线创建圆弧:
1 AcDbObjectId CCreateEnt::CreatePolyArc(AcGePoint2d ptCenter, double radius, double startAng, double endAng, double width) //圆心,起始角度,结束角度,线宽 2 { 3 AcGePoint2d pt1,pt2; 4 pt1.x=ptCenter.x+radius*cos(startAng); 5 pt1.y=ptCenter.y+radius*sin(startAng); 6 pt2.x=ptCenter.x+radius*cos(endAng); 7 pt2.y=ptCenter.y+radius*sin(endAng); 8 9 10 AcDbPolyline* pPolyline = new AcDbPolyline(2); 11 pPolyline->addVertexAt(0,pt1,tan((endAng-startAng)/4),width,width); //凸度:结束角度与起始角度差值的1/4的tan值 12 pPolyline->addVertexAt(1,pt2,0,width,width); 13 14 return CCreateEnt::PostToModelSpace(pPolyline); 15 16 }
平移对象:
1 //移动 2 Acad::ErrorStatus CModifyEnt::Move(AcDbObjectId entId, AcGePoint3d ptBase, AcGePoint3d ptDest) 3 { 4 AcGeMatrix3d xform; 5 AcGeVector3d vec(ptDest.x-ptBase.x,ptDest.y-ptBase.y,ptDest.z-ptBase.z); 6 xform.setToTranslation(vec); //构建平移矩阵 7 8 return CModifyEnt::Transform(xform,entId); //调用变换函数 9 }
缩放对象:
1 //放大 2 Acad::ErrorStatus CModifyEnt::Scale(AcDbObjectId entId, AcGePoint3d ptBase, double scaleFactor) //对象Id,基点,比例因子 3 { 4 AcGeMatrix3d xform; 5 xform.setToScaling(scaleFactor,ptBase); //构建缩放矩阵 6 7 return CModifyEnt::Transform(xform,entId); 8 }
测试函数
1 public: 2 3 // - CADShellCreateEntsUnmanaged._AddPolyline command (do not rename) 4 static void CADShellCreateEntsUnmanaged_AddPolyline(void) 5 { 6 //直线 7 AcGePoint2d ptStart(0,0),ptEnd(100,100); 8 CCreateEnt::CreatePolyLine(ptStart,ptEnd,1); 9 10 //三角形 11 AcGePoint2d pt1(0,0),pt2(100,0),pt3(100,100); 12 AcGePoint2dArray pts; 13 pts.append(pt1); 14 pts.append(pt2); 15 pts.append(pt3); 16 CCreateEnt::CreatePolyline(pts,0); //0不表示线条宽度为0 17 18 //多边形 19 AcDbObjectId oId=CCreateEnt::CreatePolygon(AcGePoint2d::kOrigin,6,30,CCalculation::G2R(20),1); 20 CModifyEnt::Scale(oId,CCalculation::Pt2dTo3d(AcGePoint2d::kOrigin),5); 21 AcGePoint3d ptDest(100,100,0); 22 //CModifyEnt::Move(oId,CCalculation::Pt2dTo3d(AcGePoint2d::kOrigin),ptDest); 23 //CModifyEnt::Rotate(oId,CCalculation::Pt2dTo3d(AcGePoint2d::kOrigin),CCalculation::G2R(20)); 24 25 //矩形 26 AcGePoint2d pt(60,70); 27 CCreateEnt::CreateRectangle(pt,ptEnd,1); 28 29 //圆 30 pt.set(50,50); 31 CCreateEnt::CreatePolyCircle(pt,30,1); 32 33 //弧 34 CCreateEnt::CreatePolyArc(pt,50,CCalculation::G2R(45.),CCalculation::G2R(225.),1); 35 36 }
弧度转角度:
double CCalculation::R2G(double rad) { return rad*180/CCalculation::PI(); }
角度转弧度:
1 double CCalculation::G2R(double angle) 2 { 3 return angle*CCalculation::PI()/180; 4 }
需要重点掌握的函数:
Acad::ErrorStatus addVertexAt (unsigned int index, //顶点序号 const AcGePoint2d& pt, //顶点 double bulge = 0., //凸度 double startWidth = -1., //起始宽度 double endWidth = -1); //结束宽度
多段线添加顶点,添加完毕后,可以调用setClose使多段线闭合
inline Acad::ErrorStatus acdbOpenObject(
AcDbEntity *& pEnt, AcDbObjectId id, AcDb::OpenMode mode)
根据Id打开对象,mode指定访问模式,pEnt返回对象
inline bool AcRxObject::isKindOf(const AcRxClass* pOtherClass) const
判断对象的类型
static AcRxClass* desc();
获取对象的类型,参数传递对象指针,一般与isKindof函数一起使用
AcDbPolyline::cast()
把对象转化给AcDbPolyline指针
pPolyline->setClosed(Adesk::kTrue);
多段线闭合
xform.setToTranslation(vec); //设置平移矩阵
xform.setToScaling(scaleFactor,ptBase); //设置缩放矩阵
xform.setToRotation(rotateAng,vec,ptBase); //设置旋转矩阵
pEnt->transformBy(xform); //实体根据矩阵变换