当前位置:   article > 正文

ObjectARX_多段线(多边形,矩形,圆,圆弧)

arx 圆弧转换成多段线

运行效果:

命令的主要执行

  1. void ZffCHAP2AddPolyline()
  2. {
  3. // 创建仅包含一段直线的多段线
  4. AcGePoint2d ptStart(0, 0), ptEnd(100, 100);
  5. CCreateEnt::CreatePolyline(ptStart, ptEnd, 1);
  6. //创建二维多义线
  7. //设置顶点坐标
  8. AcGePoint3dArray ptArr;
  9. ptArr.setLogicalLength(4);
  10. for(int i = 0; i < 4; i++)
  11. {
  12. ptArr[i].set((double)(i/2),(double)(i%2),0.0);
  13. }
  14. CCreateEnt::Create2dPolyLine(ptArr);
  15. // 创建三维多段线
  16. AcGePoint3d pt1(0, 0, 0), pt2(100, 0, 0), pt3(100, 100, 0);
  17. AcGePoint3dArray points;
  18. points.append(pt1);
  19. points.append(pt2);
  20. points.append(pt3);
  21. CCreateEnt::Create3dPolyline(points);
  22. // 创建正多边形
  23. CCreateEnt::CreatePolygon(AcGePoint2d::kOrigin, 6, 30, 0, 1);
  24. // 创建矩形
  25. AcGePoint2d pt(60, 70);
  26. CCreateEnt::CreateRectangle(pt, ptEnd, 1);
  27. // 创建圆
  28. pt.set(50, 50);
  29. CCreateEnt::CreatePolyCircle(pt, 30, 1);
  30. // 创建圆弧
  31. CCreateEnt::CreatePolyArc(pt, 50, CCalculation::GtoR(45),
  32. CCalculation::GtoR(225), 1);
  33. }

封装的一些函数

  1. // 弧度转化为角度
  2. double CCalculation::RtoG(double angle)
  3. {
  4. return angle * 180 / CCalculation::PI();
  5. }
  6. // 角度转化为弧度
  7. double CCalculation::GtoR(double angle)
  8. {
  9. return angle * CCalculation::PI() / 180;
  10. }
  11. double CCalculation::Max(double a, double b)
  12. {
  13. if (a > b)
  14. {
  15. return a;
  16. }
  17. else
  18. {
  19. return b;
  20. }
  21. }
  22. double CCalculation::Min(double a, double b)
  23. {
  24. if (a < b)
  25. {
  26. return a;
  27. }
  28. else
  29. {
  30. return b;
  31. }
  32. }
  33. //旋转
  34. Acad::ErrorStatus CModifyEnt::Rotate(AcDbObjectId entId,
  35. AcGePoint2d ptBase, double
  36. rotation)
  37. {
  38. AcGeMatrix3d xform;
  39. AcGeVector3d vec(0, 0, 1);
  40. xform.setToRotation(rotation, vec, CCalculation::Pt2dTo3d(ptBase));
  41. AcDbEntity *pEnt;
  42. Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
  43. pEnt->transformBy(xform);
  44. pEnt->close();
  45. return es;
  46. }
  47. //移动
  48. Acad::ErrorStatus CModifyEnt::Move(AcDbObjectId entId, AcGePoint3d ptBase,
  49. AcGePoint3d ptDest)
  50. {
  51. // 设置变换矩阵的参数
  52. AcGeMatrix3d xform;
  53. AcGeVector3d vec(ptDest.x - ptBase.x, ptDest.y - ptBase.y,ptDest.z - ptBase.z);
  54. xform.setToTranslation(vec);
  55. AcDbEntity *pEnt;
  56. Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
  57. pEnt->transformBy(xform);
  58. pEnt->close();
  59. return es;
  60. }
  61. //缩放
  62. Acad::ErrorStatus CModifyEnt::Scale(AcDbObjectId entId,
  63. AcGePoint3d ptBase, double scaleFactor)
  64. {
  65. // 设置变换矩阵的参数
  66. AcGeMatrix3d xform;
  67. xform.setToScaling(scaleFactor, ptBase);
  68. AcDbEntity *pEnt;
  69. Acad::ErrorStatus es = acdbOpenObject(pEnt, entId, AcDb::kForWrite,false);
  70. pEnt->transformBy(xform);
  71. pEnt->close();
  72. return es;
  73. }

多段线:

  1. AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2d ptStart,
  2. AcGePoint2d ptEnd, double width)
  3. {
  4. AcGePoint2dArray points;
  5. points.append(ptStart);
  6. points.append(ptEnd);
  7. return CCreateEnt::CreatePolyline(points, width);
  8. }
  9. AcDbObjectId CCreateEnt::CreatePolyline(AcGePoint2dArray points, double width)
  10. {
  11. int numVertices = points.length();
  12. AcDbPolyline *pPoly = new AcDbPolyline(numVertices);
  13. for (int i = 0; i < numVertices; i++)
  14. {
  15. pPoly->addVertexAt(i, points.at(i), 0, width, width);
  16. }
  17. AcDbObjectId polyId;
  18. polyId = CCreateEnt::PostToModelSpace(pPoly);
  19. return polyId;
  20. }
  21. //得到多段线顶点的坐标
  22. void GetPolyLineSelectLineIndex(AcDbPolyline *Poly,AcGePoint3d SelectPt,int Index)
  23. {
  24. AcGePoint2d tempPt(SelectPt.x,SelectPt.y);
  25. double Distance=0.0;
  26. AcDbVoidPtrArray entitySet;
  27. Poly->explode(entitySet); //将曲线分解成单独的分量。
  28. for(int i=0;i<entitySet.length(); ++i)
  29. {
  30. AcDbEntity *pEnt = AcDbPolyline::cast((AcRxObject*)entitySet[i]);
  31. if(pEnt->isA()==AcDbLine::desc())
  32. {
  33. AcDbLine *pLine=AcDbLine::cast(pEnt);
  34. AcGePoint2d StPt,EndPt;
  35. StPt.x=pLine->startPoint().x;
  36. StPt.y=pLine->startPoint().y;
  37. EndPt.x=pLine->endPoint().x;
  38. EndPt.y=pLine->endPoint().y;
  39. pLine->close();
  40. AcGeLine2d GeLine(StPt,EndPt);
  41. double pama=0;
  42. if((GeLine.isOn(tempPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
  43. {
  44. Index=i;
  45. break;
  46. }
  47. else
  48. {
  49. if(GeLine.distanceTo(tempPt)
  50. {
  51. AcGeLine2d tempLine;
  52. GeLine.getPerpLine(tempPt,tempLine);
  53. AcGePoint2d InsertPt;
  54. GeLine.intersectWith(tempLine,InsertPt);
  55. double pama=0.0;
  56. if((GeLine.isOn(InsertPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
  57. {
  58. Distance=GeLine.distanceTo(tempPt);
  59. Index=i;
  60. }
  61. }
  62. }
  63. }
  64. pEnt->close();
  65. }

二维多义线:

  1. AcDbObjectId CCreateEnt::Creat2dPolyLine(AcGePoint3dArray ptArr)
  2. {
  3. AcDb2dPolyLine * pPoly2d = new AcDb2dPolyLine(AcDb::k2dSimplePoly,ptArr,0.0,Adesk::kTrue);
  4. return CCreateEnt::PostToModelSpace(pPoly2d );
  5. }

三维多段线:

  1. AcDbObjectId CCreateEnt::Create3dPolyline(AcGePoint3dArray points)
  2. {
  3. AcDb3dPolyline *pPoly3d = new
  4. AcDb3dPolyline(AcDb::k3dSimplePoly, points);
  5. return CCreateEnt::PostToModelSpace(pPoly3d);
  6. }

正多边形:

  1. AcDbObjectId CCreateEnt::CreatePolygon(AcGePoint2d ptCenter, int number,
  2. double radius, double rotation, double width)
  3. {
  4. AcGePoint2dArray points;
  5. double angle = 2 * CCalculation::PI() / (double)number;
  6. for (int i = 0; i < number; i++)
  7. {
  8. AcGePoint2d pt;
  9. pt.x = ptCenter.x + radius * cos(i * angle);
  10. pt.y = ptCenter.y + radius * sin(i * angle);
  11. points.append(pt);
  12. }
  13. AcDbObjectId polyId = CCreateEnt::CreatePolyline(points, width);
  14. // 将其闭合
  15. AcDbEntity *pEnt;
  16. acdbOpenAcDbEntity(pEnt, polyId, AcDb::kForWrite);
  17. AcDbPolyline *pPoly = AcDbPolyline::cast(pEnt);
  18. if (pPoly != NULL)
  19. {
  20. pPoly->setClosed(Adesk::kTrue);
  21. }
  22. pEnt->close();
  23. CModifyEnt::Rotate(polyId, ptCenter, rotation);
  24. return polyId;
  25. }

矩形:

  1. AcDbObjectId CCreateEnt::CreateRectangle(AcGePoint2d pt1, AcGePoint2d pt2,
  2. double width)
  3. {
  4. // 提取两个角点的坐标值
  5. double x1 = pt1.x, x2 = pt2.x;
  6. double y1 = pt1.y, y2 = pt2.y;
  7. // 计算矩形的角点
  8. AcGePoint2d ptLeftBottom(CCalculation::Min(x1, x2),
  9. CCalculation::Min(y1, y2));
  10. AcGePoint2d ptRightBottom(CCalculation::Max(x1, x2),
  11. CCalculation::Min(y1, y2));
  12. AcGePoint2d ptRightTop(CCalculation::Max(x1, x2),
  13. CCalculation::Max(y1, y2));
  14. AcGePoint2d ptLeftTop(CCalculation::Min(x1, x2),
  15. CCalculation::Max(y1, y2));
  16. // 创建对应的多段线
  17. AcDbPolyline *pPoly = new AcDbPolyline(4);
  18. pPoly->addVertexAt(0, ptLeftBottom, 0, width, width);
  19. pPoly->addVertexAt(1, ptRightBottom, 0, width, width);
  20. pPoly->addVertexAt(2, ptRightTop, 0, width, width);
  21. pPoly->addVertexAt(3, ptLeftTop, 0, width, width);
  22. pPoly->setClosed(Adesk::kTrue);
  23. // 将多段线添加到模型空间
  24. AcDbObjectId polyId;
  25. polyId = CCreateEnt::PostToModelSpace(pPoly);
  26. return polyId;
  27. }

圆:

  1. AcDbObjectId CCreateEnt::CreatePolyCircle(AcGePoint2d ptCenter,
  2. double radius, double width)
  3. {
  4. // 计算顶点的位置
  5. AcGePoint2d pt1, pt2, pt3;
  6. pt1.x = ptCenter.x + radius;
  7. pt1.y = ptCenter.y;
  8. pt2.x = ptCenter.x - radius;
  9. pt2.y = ptCenter.y;
  10. pt3.x = ptCenter.x + radius;
  11. pt3.y = ptCenter.y;
  12. // 创建多段线
  13. AcDbPolyline *pPoly = new AcDbPolyline(3);
  14. pPoly->addVertexAt(0, pt1, 1, width, width);
  15. pPoly->addVertexAt(1, pt2, 1, width, width);
  16. pPoly->addVertexAt(2, pt3, 1, width, width);
  17. pPoly->setClosed(Adesk::kTrue);
  18. // 将多段线添加到模型空间
  19. AcDbObjectId polyId;
  20. polyId = CCreateEnt::PostToModelSpace(pPoly);
  21. return polyId;
  22. }

圆弧:

  1. AcDbObjectId CCreateEnt::CreatePolyArc(AcGePoint2d ptCenter, double radius,
  2. double angleStart, double angleEnd, double width)
  3. {
  4. // 计算顶点的位置
  5. AcGePoint2d pt1, pt2;
  6. pt1.x = ptCenter.x + radius * cos(angleStart);
  7. pt1.y = ptCenter.y + radius * sin(angleStart);
  8. pt2.x = ptCenter.x + radius * cos(angleEnd);
  9. pt2.y = ptCenter.y + radius * sin(angleEnd);
  10. // 创建多段线
  11. AcDbPolyline *pPoly = new AcDbPolyline(3);
  12. pPoly->addVertexAt(0, pt1, tan((angleEnd - angleStart) / 4), width,width);
  13. pPoly->addVertexAt(1, pt2, 0, width, width);
  14. // 将多段线添加到模型空间
  15. AcDbObjectId polyId;
  16. polyId = CCreateEnt::PostToModelSpace(pPoly);
  17. return polyId;
  18. }

CCreateEnt::PostToModelSpace:添加到图形数据库的模型空间参考另外一篇博客:https://my.oschina.net/u/2930533/blog/760855

转载于:https://my.oschina.net/u/2930533/blog/1584456

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/114360
推荐阅读
  

闽ICP备14008679号