当前位置:   article > 正文

ObjectARX模仿AutoCAD中多段线命令实现颜色,线型,闭合等

arx线型

首先在acrxEntryPoint.cpp中注册一个命令:

  1. IMPLEMENT_ARX_ENTRYPOINT(CARX_CreatePolylineApp)
  2. ACED_ARXCOMMAND_ENTRY_AUTO(CARX_CreatePolylineApp, HH, AddPoly, AddPoly, ACRX_CMD_TRANSPARENT, NULL)

该命令的具体实现如下:

  1. static void HHAddPoly(void)
  2. {
  3. int nColorIndex = 0; // 颜色索引值
  4. ads_real width = 0; // 多段线的线宽
  5. int nIndex = 2; // 当前输入点的次数
  6. ads_point ptStart; // 起点
  7. ads_point ptPrevious;// 前一个参考点
  8. ads_point ptCurrent; //当前拾取的点
  9. AcDbObjectId polyId; //多段线ID
  10. // 提示用户输入起点
  11. if (RTNORM != acedGetPoint(NULL, _T("\n输入第一点:"), ptStart))
  12. {
  13. return ;
  14. }
  15. // 输入第二点
  16. acedInitGet(NULL, _T("W C B F"));
  17. int rc = acedGetPoint(ptStart,_T("\n输入下一点 [宽度(W)/颜色(C)/闭合(B)]<完成(F)>:"), ptCurrent);
  18. while (RTNORM == rc || RTKWORD == rc)
  19. {
  20. if (RTKWORD == rc) // 如果用户输入了关键字
  21. {
  22. ACHAR keyWord[20];// 关键字
  23. //获取输入的关键字
  24. if (RTNORM != acedGetInput(keyWord))
  25. {
  26. return;
  27. }
  28. if (0 == _tcscmp(keyWord, _T("W")))
  29. {
  30. width = CAuxiliary::GetWidth();
  31. AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);
  32. if (NULL == pPoly)
  33. {
  34. return;
  35. }
  36. pPoly->setConstantWidth(width);
  37. pPoly->close();
  38. }
  39. else if (0 == _tcscmp(keyWord, _T("C")))
  40. {
  41. nColorIndex = CAuxiliary::GetColorIndex();
  42. AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);
  43. if (NULL == pPoly)
  44. {
  45. return;
  46. }
  47. pPoly->setColorIndex(nColorIndex);
  48. pPoly->close();
  49. }
  50. else if (0 == _tcscmp(keyWord, _T("B")))
  51. {
  52. if (nIndex < 3)
  53. {
  54. acutPrintf(_T("\n输入点个数不足,不能执行闭合操作"));
  55. return;
  56. }
  57. AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);
  58. if (NULL == pPoly)
  59. {
  60. return;
  61. }
  62. pPoly->setClosed(Adesk::kTrue);
  63. pPoly->close();
  64. return;
  65. }
  66. else if (0 == _tcscmp(keyWord, _T("F")))
  67. {
  68. return;
  69. }
  70. else
  71. {
  72. acutPrintf(_T("\n无效的关键字."));
  73. }
  74. }
  75. else if (RTNORM == rc) // 用户输入了点
  76. {
  77. acutPrintf(_T("\n输入点的坐标是(%.2f, %.2f, %.2f)"), ptCurrent[X], ptCurrent[Y], ptCurrent[Z]);
  78. if (2 == nIndex)
  79. {
  80. // 创建多段线
  81. polyId = CAuxiliary::CreatePolyline(ptStart, ptCurrent, width, nColorIndex);
  82. }
  83. else if (nIndex > 2)
  84. {
  85. //修改多段线
  86. CAuxiliary::AddPolyline(polyId, ptCurrent,nIndex, width, nColorIndex);
  87. }
  88. ++nIndex;
  89. acdbPointSet(ptCurrent, ptPrevious);//acdbPointSet宏: ads_point 变量值的复制
  90. }
  91. // 提示用户输入新的节点
  92. acedInitGet(NULL, _T("W C B F"));
  93. rc = acedGetPoint(ptPrevious, _T("\n输入下一点 [宽度(W)/颜色(C)/闭合(B)]<完成(F)>:"), ptCurrent);
  94. }
  95. }

其中调用了一些函数,新建一个类CAuxiliary,具体实现如下:

  1. ***********************Auxiliary.h********************
  2. #pragma once
  3. class CAuxiliary
  4. {
  5. public:
  6. CAuxiliary(void);
  7. ~CAuxiliary(void);
  8. //图形数据库
  9. static bool PostToModelSpace(AcDbEntity* pEnt, AcDbObjectId &entId);
  10. //通过ID获取对象
  11. static AcDbPolyline * GetPolyLine(AcDbObjectId polyId);
  12. //创建仅包含一条直线的多段线
  13. static AcDbObjectId CreatePolyline(ads_point ptStart, ads_point ptEnd, double width, int nColorIndex);
  14. //添加一条直线到多段线
  15. static void AddPolyline(AcDbObjectId polyId, ads_point ptCurrent,int nIndex, double width, int nColorIndex);
  16. //获取用户输入的线宽
  17. static ads_real GetWidth();
  18. //提示用户输入颜色索引值
  19. static int GetColorIndex();
  20. };

 

  1. ***********************Auxiliary.cpp********************
  2. #include "StdAfx.h"
  3. #include "Auxiliary.h"
  4. #include <atlstr.h>
  5. #include <tchar.h>
  6. CAuxiliary::CAuxiliary(void)
  7. {
  8. }
  9. CAuxiliary::~CAuxiliary(void)
  10. {
  11. }
  12. bool CAuxiliary::PostToModelSpace(AcDbEntity* pEnt, AcDbObjectId &entId)
  13. {
  14. if (NULL == pEnt)
  15. {
  16. return false;
  17. }
  18. // 获得指向块表的指针
  19. AcDbBlockTable *pBlockTable = NULL;
  20. //workingDatabase()能够获得一个指向当前活动的图形数据库的指针,
  21. acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
  22. if (NULL == pBlockTable)
  23. {
  24. return false;
  25. }
  26. // 获得指向特定的块表记录(模型空间)的指针
  27. AcDbBlockTableRecord *pBlockTableRecord = NULL;
  28. pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
  29. if (NULL == pBlockTableRecord)
  30. {
  31. return false;
  32. }
  33. // 将AcDbLine类的对象添加到块表记录中
  34. pBlockTableRecord->appendAcDbEntity(entId, pEnt);
  35. // 关闭图形数据库的各种对象
  36. pBlockTable->close();
  37. pBlockTableRecord->close();
  38. pEnt->close();
  39. return true;
  40. }
  41. AcDbPolyline * CAuxiliary::GetPolyLine(AcDbObjectId polyId)
  42. {
  43. AcDbPolyline * pPoly(NULL);
  44. if (Acad::eOk != acdbOpenObject(pPoly, polyId, AcDb::kForWrite))
  45. {
  46. return NULL;
  47. }
  48. return pPoly;
  49. }
  50. AcDbObjectId CAuxiliary::CreatePolyline(ads_point ptStart, ads_point ptCurrent, double width,int nColorIndex)
  51. {
  52. AcDbPolyline *pPoly = new AcDbPolyline;
  53. AcGePoint2d ptInsert = asPnt2d(ptStart);
  54. AcGePoint2d ptInsert1 = asPnt2d(ptCurrent);
  55. pPoly->addVertexAt(0, ptInsert, nColorIndex, width, width);
  56. pPoly->addVertexAt(1, ptInsert1, nColorIndex, width, width);
  57. AcDbObjectId polyId;
  58. if (!CAuxiliary::PostToModelSpace(pPoly, polyId))
  59. {
  60. acutPrintf(_T("\n加入图形数据库失败"));
  61. return NULL;
  62. }
  63. return polyId;
  64. }
  65. void CAuxiliary::AddPolyline(AcDbObjectId polyId, ads_point ptCurrent,int nIndex, double width, int nColorIndex)
  66. {
  67. AcDbPolyline *pPoly = GetPolyLine(polyId);
  68. if (NULL == pPoly)
  69. {
  70. return;
  71. }
  72. AcGePoint2d ptInsert = asPnt2d(ptCurrent);
  73. pPoly->addVertexAt(nIndex - 1, ptInsert, nColorIndex, width, width);
  74. pPoly->close();
  75. }
  76. ads_real CAuxiliary::GetWidth()
  77. {
  78. ads_real width = 0;
  79. if (RTNORM == acedGetReal(_T("\n输入线宽:"), &width))
  80. {
  81. return width;
  82. }
  83. else
  84. {
  85. return 0;
  86. }
  87. }
  88. int CAuxiliary::GetColorIndex()
  89. {
  90. int nColorIndex = 0;
  91. if (RTNORM != acedGetInt(_T("\n输入颜色索引值(0~256):"), &nColorIndex))
  92. {
  93. return 0;
  94. }
  95. // 处理颜色索引值无效的情况
  96. while (nColorIndex < 0 || nColorIndex > 256)
  97. {
  98. acedPrompt(_T("\n输入了无效的颜色索引."));
  99. if (RTNORM != acedGetInt(_T("\n输入颜色索引值(0~256):"), &nColorIndex))
  100. {
  101. return 0;
  102. }
  103. }
  104. return nColorIndex;
  105. }

 

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

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

闽ICP备14008679号