明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 261|回复: 0

ObjectArx 实现类似C# 的DrawJig,支持多实体拖动

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式
首先实现接口,因为AcEdJig 仅仅支持单实体进行更新显示,如果用多实体那么就需要一个AcDbEntity 去包装,本次我们换个方式,直接在类中加入AcDbEntity接口,实现subWorldDraw显示多个图元



  1. class DrawJig : public AcDbEntity, public AcEdJig {
  2. public:
  3.         virtual DragStatus sampler() = 0;
  4.         virtual bool  update() = 0;
  5.         virtual bool subWorldDraw(AcGiWorldDraw* mode) = 0;
  6. protected:
  7.         AcDbEntity* entity() const final
  8.         {
  9.                 return (AcDbEntity*)this;
  10.         }
  11. };
复制代码
下面用一个简单的例子继承测试下

  1. class TestDrrawJig :public DrawJig {
  2. public:
  3.         TestDrrawJig() {}
  4.         bool startDrag(AcGePoint3d ptCenter);
  5.         DragStatus sampler() override;
  6.         bool update() override;
  7.         bool subWorldDraw(AcGiWorldDraw* mode)override;
  8.         AcGePoint2d Point3dTo2d(AcGePoint3d pnt);
  9.         AcDbVoidPtrArray CreateEntitys();
  10.         AcArray<AcDbEntity*> GetEntities();
  11. private:
  12.         AcGePoint3d m_centerPnt;
  13.         AcGePoint3d m_curPnt;
  14.         AcDbVoidPtrArray m_aryPtr;
  15. };

  16. inline AcEdJig::DragStatus TestDrrawJig::sampler()
  17. {
  18.         setUserInputControls((UserInputControls)(AcEdJig::kAccept3dCoordinates
  19.                 | AcEdJig::kNoNegativeResponseAccepted
  20.                 | AcEdJig::kNullResponseAccepted));
  21.         AcGePoint3d pointTemp;
  22.         DragStatus stat = acquirePoint(m_curPnt);
  23.         if (pointTemp != m_curPnt)
  24.         {
  25.                 pointTemp = m_curPnt;
  26.         }
  27.         else if (stat == AcEdJig::kNormal)
  28.         {
  29.                 return AcEdJig::kNoChange;
  30.         }
  31.         return stat;
  32. }

  33. inline bool TestDrrawJig::update()
  34. {
  35.         for (int i = 0; i < m_aryPtr.length(); i++)
  36.         {
  37.                 AcDbEntity* pEnt = (AcDbEntity*)m_aryPtr.at(i);
  38.                 delete pEnt;
  39.                 pEnt = NULL;
  40.         }
  41.         //创建新实体
  42.         m_aryPtr = CreateEntitys();

  43.         return Adesk::kTrue;
  44. }
  45. inline bool TestDrrawJig::subWorldDraw(AcGiWorldDraw* mode)
  46. {
  47.         for (int i = 0; i < m_aryPtr.length(); i++)
  48.         {
  49.                 AcDbEntity* pEnt = (AcDbEntity*)m_aryPtr[i];
  50.                 mode->geometry().draw(pEnt);
  51.         }

  52.         return Adesk::kTrue;
  53. }


  54. inline AcGePoint2d TestDrrawJig::Point3dTo2d(AcGePoint3d pnt)
  55. {
  56.         return AcGePoint2d(pnt.x, pnt.y);
  57. }

  58. inline AcDbVoidPtrArray TestDrrawJig::CreateEntitys()
  59. {
  60.         AcDbVoidPtrArray aryEnts;
  61.         //创建实体
  62.         double dist = Point3dTo2d(m_curPnt).distanceTo(Point3dTo2d(m_centerPnt));
  63.         AcGeVector2d vec = Point3dTo2d(m_curPnt) - Point3dTo2d(m_centerPnt);
  64.         AcGePoint2d pt1, pt2, pt3, pt4;
  65.         pt1 = Point3dTo2d(m_curPnt);
  66.         pt3 = pt1 - 2 * vec;
  67.         vec.rotateBy(PI * 0.5);
  68.         pt2 = Point3dTo2d(m_centerPnt) + vec;
  69.         pt4 = pt2 - 2 * vec;
  70.         AcDbPolyline* poly = new AcDbPolyline;
  71.         poly->addVertexAt(0, pt1);
  72.         poly->addVertexAt(1, pt2);
  73.         poly->addVertexAt(2, pt3);
  74.         poly->addVertexAt(3, pt4);
  75.         poly->setClosed(Adesk::kTrue);
  76.         poly->setColorIndex(1);
  77.         aryEnts.append(static_cast<void*>(poly));
  78.         AcDbCircle* circle = new AcDbCircle(m_centerPnt, AcGeVector3d::kZAxis, pt1.distanceTo(pt2) * 0.5);
  79.         circle->setColorIndex(3);
  80.         aryEnts.append(static_cast<void*>(circle));
  81.         return aryEnts;
  82. }

  83. inline AcArray<AcDbEntity*> TestDrrawJig::GetEntities()
  84. {
  85.         AcArray <AcDbEntity*> ents;
  86.         for (int i = 0; i < m_aryPtr.length(); i++) {
  87.                 ents.append((AcDbEntity*)(m_aryPtr[i]));
  88.         }
  89.         return ents;
  90. }

  91. inline bool TestDrrawJig::startDrag(AcGePoint3d ptCenter)
  92. {
  93.         m_centerPnt = ptCenter;
  94.         m_curPnt = ptCenter;
  95.         AcString prompt = _T("\n指定角点");
  96.         setDispPrompt(prompt);
  97.         AcEdJig::DragStatus stat = drag();
  98.         if (stat == AcEdJig::kNormal)
  99.         {
  100.                 return true;
  101.         }
  102.         else
  103.         {
  104.                 return false;
  105.         }
  106. }
  1. void tsJig()
  2. {
  3.         DbTrans tr;
  4.         auto btr = tr.CurrentSpace();
  5.         auto res = MapleGetPoint();
  6.         if (!res.Ok) return;
  7.         TestDrrawJig jig;
  8.         if (jig.startDrag(res.Value)) {
  9.                 auto ents = jig.GetEntities();
  10.                 tr.AddEntities(btr, ents);
  11.         }

  12. }
复制代码
这样就可以使用多实体Jig了,与C#的DrawJig 差不多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-15 02:04 , Processed in 0.291709 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表