运行效果:
命令的主要执行
- void ZffCHAP2AddPolyline()
- {
- // 创建仅包含一段直线的多段线
- AcGePoint2d ptStart(0, 0), ptEnd(100, 100);
- CCreateEnt::CreatePolyline(ptStart, ptEnd, 1);
-
- //创建二维多义线
- //设置顶点坐标
- AcGePoint3dArray ptArr;
- ptArr.setLogicalLength(4);
- for(int i = 0; i < 4; i++)
- {
- ptArr[i].set((double)(i/2),(double)(i%2),0.0);
- }
- CCreateEnt::Create2dPolyLine(ptArr);
-
- // 创建三维多段线
- AcGePoint3d pt1(0, 0, 0), pt2(100, 0, 0), pt3(100, 100, 0);
- AcGePoint3dArray points;
- points.append(pt1);
- points.append(pt2);
- points.append(pt3);
- CCreateEnt::Create3dPolyline(points);
-
- // 创建正多边形
- CCreateEnt::CreatePolygon(AcGePoint2d::kOrigin, 6, 30, 0, 1);
-
- // 创建矩形
- AcGePoint2d pt(60, 70);
- CCreateEnt::CreateRectangle(pt, ptEnd, 1);
-
- // 创建圆
- pt.set(50, 50);
- CCreateEnt::CreatePolyCircle(pt, 30, 1);
-
- // 创建圆弧
- CCreateEnt::CreatePolyArc(pt, 50, CCalculation::GtoR(45),
- CCalculation::GtoR(225), 1);
- }
封装的一些函数
- // 弧度转化为角度
- double CCalculation::RtoG(double angle)
- {
- return angle * 180 / CCalculation::PI();
- }
- // 角度转化为弧度
- double CCalculation::GtoR(double angle)
- {
- return angle * CCalculation::PI() / 180;
- }
- double CCalculation::Max(double a, double b)
- {
- if (a > b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- double CCalculation::Min(double a, double b)
- {
- if (a < b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- //旋转
- Acad::ErrorStatus CModifyEnt::Rotate(AcDbObjectId entId,
- AcGePoint2d ptBase, double
- rotation)
- {
- AcGeMatrix3d xform;
- AcGeVector3d vec(0, 0, 1);
- xform.setToRotation(rotation, vec, CCalculation::Pt2dTo3d(ptBase));
- AcDbEntity *pEnt;
- Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
- pEnt->transformBy(xform);
- pEnt->close();
- return es;
- }
- //移动
- Acad::ErrorStatus CModifyEnt::Move(AcDbObjectId entId, AcGePoint3d ptBase,
- AcGePoint3d ptDest)
- {
- // 设置变换矩阵的参数
- AcGeMatrix3d xform;
- AcGeVector3d vec(ptDest.x - ptBase.x, ptDest.y - ptBase.y,ptDest.z - ptBase.z);
- xform.setToTranslation(vec);
- AcDbEntity *pEnt;
- Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
- pEnt->transformBy(xform);
- pEnt->close();
- return es;
- }
- //缩放
- Acad::ErrorStatus CModifyEnt::Scale(AcDbObjectId entId,
- AcGePoint3d ptBase, double scaleFactor)
- {
- // 设置变换矩阵的参数
- AcGeMatrix3d xform;
- xform.setToScaling(scaleFactor, ptBase);
- AcDbEntity *pEnt;
- Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
- pEnt->transformBy(xform);
- pEnt->close();
- return es;
- }
多段线:
- AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2d ptStart,
- AcGePoint2d ptEnd, double width)
- {
- AcGePoint2dArray points;
- points.append(ptStart);
- points.append(ptEnd);
- return CCreateEnt::CreatePolyline(points, width);
- }
-
- AcDbObjectId CCreateEnt::CreatePolyline(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);
- }
- AcDbObjectId polyId;
- polyId = CCreateEnt::PostToModelSpace(pPoly);
- return polyId;
- }
-
- //得到多段线顶点的坐标
- void GetPolyLineSelectLineIndex(AcDbPolyline *Poly,AcGePoint3d SelectPt,int Index)
- {
- AcGePoint2d tempPt(SelectPt.x,SelectPt.y);
- double Distance=0.0;
- AcDbVoidPtrArray entitySet;
- Poly->explode(entitySet); //将曲线分解成单独的分量。
- for(int i=0;i<entitySet.length(); ++i)
- {
- AcDbEntity *pEnt = AcDbPolyline::cast((AcRxObject*)entitySet[i]);
- if(pEnt->isA()==AcDbLine::desc())
- {
- AcDbLine *pLine=AcDbLine::cast(pEnt);
- AcGePoint2d StPt,EndPt;
- StPt.x=pLine->startPoint().x;
- StPt.y=pLine->startPoint().y;
- EndPt.x=pLine->endPoint().x;
- EndPt.y=pLine->endPoint().y;
- pLine->close();
- AcGeLine2d GeLine(StPt,EndPt);
- double pama=0;
- if((GeLine.isOn(tempPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
- {
- Index=i;
- break;
- }
- else
- {
- if(GeLine.distanceTo(tempPt)
- {
- AcGeLine2d tempLine;
- GeLine.getPerpLine(tempPt,tempLine);
- AcGePoint2d InsertPt;
- GeLine.intersectWith(tempLine,InsertPt);
- double pama=0.0;
- if((GeLine.isOn(InsertPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
- {
- Distance=GeLine.distanceTo(tempPt);
- Index=i;
- }
- }
- }
- }
- pEnt->close();
- }
二维多义线:
- AcDbObjectId CCreateEnt::Creat2dPolyLine(AcGePoint3dArray ptArr)
- {
- AcDb2dPolyLine * pPoly2d = new AcDb2dPolyLine(AcDb::k2dSimplePoly,ptArr,0.0,Adesk::kTrue);
- return CCreateEnt::PostToModelSpace(pPoly2d );
- }
三维多段线:
- AcDbObjectId CCreateEnt::Create3dPolyline(AcGePoint3dArray points)
- {
- AcDb3dPolyline *pPoly3d = new
- AcDb3dPolyline(AcDb::k3dSimplePoly, points);
- return CCreateEnt::PostToModelSpace(pPoly3d);
- }
正多边形:
- AcDbObjectId CCreateEnt::CreatePolygon(AcGePoint2d ptCenter, int number,
- double radius, double rotation, double width)
- {
- AcGePoint2dArray points;
- double angle = 2 * CCalculation::PI() / (double)number;
- 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 = CCreateEnt::CreatePolyline(points, width);
-
- // 将其闭合
- AcDbEntity *pEnt;
- acdbOpenAcDbEntity(pEnt, polyId, AcDb::kForWrite);
- AcDbPolyline *pPoly = AcDbPolyline::cast(pEnt);
- if (pPoly != NULL)
- {
- pPoly->setClosed(Adesk::kTrue);
- }
-
- pEnt->close();
- CModifyEnt::Rotate(polyId, ptCenter, rotation);
- return polyId;
- }
矩形:
- AcDbObjectId CCreateEnt::CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2,
- double width)
- {
- // 提取两个角点的坐标值
- double x1 = pt1.x, x2 = pt2.x;
- double y1 = pt1.y, y2 = pt2.y;
-
- // 计算矩形的角点
- AcGePoint2d ptLeftBottom(CCalculation::Min(x1, x2),
- CCalculation::Min(y1, y2));
- AcGePoint2d ptRightBottom(CCalculation::Max(x1, x2),
- CCalculation::Min(y1, y2));
- AcGePoint2d ptRightTop(CCalculation::Max(x1, x2),
- CCalculation::Max(y1, y2));
- AcGePoint2d ptLeftTop(CCalculation::Min(x1, x2),
- CCalculation::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(Adesk::kTrue);
-
- // 将多段线添加到模型空间
- AcDbObjectId polyId;
- polyId = CCreateEnt::PostToModelSpace(pPoly);
- return polyId;
- }
圆:
- AcDbObjectId CCreateEnt::CreatePolyCircle(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);
-
- // 将多段线添加到模型空间
- AcDbObjectId polyId;
- polyId = CCreateEnt::PostToModelSpace(pPoly);
- return polyId;
- }
圆弧:
- AcDbObjectId CCreateEnt::CreatePolyArc(AcGePoint2d ptCenter, double radius,
- double angleStart, double angleEnd, double width)
- {
- // 计算顶点的位置
- AcGePoint2d pt1, pt2;
- pt1.x = ptCenter.x + radius * cos(angleStart);
- pt1.y = ptCenter.y + radius * sin(angleStart);
- pt2.x = ptCenter.x + radius * cos(angleEnd);
- pt2.y = ptCenter.y + radius * sin(angleEnd);
-
- // 创建多段线
- AcDbPolyline *pPoly = new AcDbPolyline(3);
- pPoly->addVertexAt(0, pt1, tan((angleEnd - angleStart) / 4), width,width);
- pPoly->addVertexAt(1, pt2, 0, width, width);
-
- // 将多段线添加到模型空间
- AcDbObjectId polyId;
- polyId = CCreateEnt::PostToModelSpace(pPoly);
- return polyId;
- }
CCreateEnt::PostToModelSpace:添加到图形数据库的模型空间参考另外一篇博客:https://my.oschina.net/u/2930533/blog/760855