明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1047|回复: 7

发个Jig小例子

[复制链接]
发表于 2025-8-22 14:16:16 | 显示全部楼层 |阅读模式
绘制有向矩形,学习Jig的一个小例子,分享一下,高手见了勿喷。









  1. class CLyFunJigDirectedRect :
  2.     public AcEdJig
  3. {
  4. public:
  5.     static void FunJigDirectedRect();//对外接口,注册命令LYDIRRECT
  6. protected:
  7.     CLyFunJigDirectedRect(void);
  8.     virtual ~CLyFunJigDirectedRect(void);
  9.     virtual void FunDoit();//主流程
  10.     virtual bool GetStPt();//拾取起点
  11.     virtual bool JigFirst();//拾取第二点
  12.     virtual bool JigSecond();//拾取第三点
  13. protected:
  14.     //此函数将被drag函数调用以获得用户输入
  15.     virtual AcEdJig::DragStatus sampler();
  16.     //对需要在拖动过程中发生变化的实体进行修改
  17.     virtual Adesk::Boolean update();
  18.     //指定了Jig所操作的对象
  19.     virtual AcDbEntity* entity() const;
  20.     void InitPolyline();//初始化多段线
  21.     double Hdist(const AcGePoint3d &pt1,const AcGePoint3d &pt2);
  22. protected:
  23.     AcDbPolyline* m_pPolyline;//拖动的实体指针
  24.     AcGePoint3d m_StPt;//起点
  25.     AcGePoint3d m_PtJig;//拖动的点
  26.     int m_Index;//操作的是那个点,2,3
  27.     AcGeLine2d m_BaseLine;//方向线
  28. };






  1. void CLyFunJigDirectedRect::FunJigDirectedRect()
  2. {
  3.     CLyFunJigDirectedRect A;
  4.     A.FunDoit ();
  5. }

  6. CLyFunJigDirectedRect::CLyFunJigDirectedRect(void)
  7. {
  8.     m_pPolyline=nullptr;
  9. }

  10. CLyFunJigDirectedRect::~CLyFunJigDirectedRect(void)
  11. {
  12.     if (m_pPolyline)
  13.     {
  14.      delete m_pPolyline;
  15.      m_pPolyline=nullptr;
  16.     }   
  17. }

  18. void CLyFunJigDirectedRect::FunDoit()
  19. {
  20.     if (!this->GetStPt ())
  21.     {
  22.      return;
  23.     }
  24.     this->InitPolyline ();
  25.     if (!this->JigFirst ())
  26.     {
  27.      return;
  28.     }
  29.     this->JigSecond ();
  30. }

  31. bool CLyFunJigDirectedRect::GetStPt()
  32. {
  33.     ads_point ptads;   
  34.     int retcode=acedGetPoint(nullptr,_T("\n请拾取起点:"),ptads);
  35.     if (retcode!=RTNORM)
  36.     {
  37.      return false;
  38.     }   
  39.     acdbUcs2Wcs(ptads,asDblArray(m_StPt),false);
  40.     return true;
  41. }

  42. bool CLyFunJigDirectedRect::JigFirst()
  43. {
  44.     m_Index=2;
  45.     CString prompt=_T("\n指定第二点<退出>:");
  46.     setDispPrompt(prompt);
  47.     AcEdJig::DragStatus stat=drag ();
  48.     // 拖动之后,根据需要自己的处理方式   
  49.     if (stat==kNormal)
  50.     {
  51.      m_BaseLine.set(AcGePoint2d(m_StPt.x,m_StPt.y),AcGePoint2d(m_PtJig.x,m_PtJig.y));
  52.      return true;
  53.     }
  54.     return false;
  55. }

  56. bool CLyFunJigDirectedRect::JigSecond()
  57. {
  58.     m_Index=3;
  59.     CString prompt=_T("\n指定第三点<退出>:");
  60.     setDispPrompt(prompt);
  61.     AcEdJig::DragStatus stat=drag ();
  62.     // 拖动之后,根据需要自己的处理方式   
  63.     if (stat==kNormal)
  64.     {
  65.      this->append ();
  66.      m_pPolyline=nullptr;
  67.      return true;
  68.     }
  69.     return false;
  70. }

  71. AcEdJig::DragStatus CLyFunJigDirectedRect::sampler()
  72. {
  73.     DragStatus stat;
  74.     setUserInputControls((UserInputControls)(AcEdJig::kDontEchoCancelForCtrlC));
  75.     //|不回显取消
  76.     static AcGePoint3d pointTemp;
  77.     stat = acquirePoint(m_PtJig);
  78.     if (m_Index==2)
  79.     {
  80.      if (Hdist(m_StPt,m_PtJig)<1E-12)
  81.      {
  82.       return AcEdJig::kNoChange;
  83.      }
  84.     }   
  85.     if (pointTemp != m_PtJig)
  86.      pointTemp = m_PtJig;
  87.     else if (stat == AcEdJig::kNormal)
  88.      stat = AcEdJig::kNoChange;
  89.     return stat;
  90. }

  91. Adesk::Boolean CLyFunJigDirectedRect::update()
  92. {
  93.     if (m_Index==2)
  94.     {
  95.      m_PtJig.z=m_StPt.z;
  96.      AcGeVector2d Vec(m_PtJig.x-m_StPt.x,m_PtJig.y-m_StPt.y);
  97.      if (Vec.isZeroLength())
  98.      {
  99.       return Adesk::kFalse;
  100.      }
  101.      AcGeVector2d Vec2(Vec);
  102.      Vec2 *=0.5;
  103.      Vec2.rotateBy(atan (1.0)*2);
  104.      AcGePoint2d pt0;
  105.      m_pPolyline->getPointAt(0,pt0);
  106.      m_pPolyline->setPointAt(1,pt0+Vec);
  107.      m_pPolyline->setPointAt(2,pt0+Vec+Vec2);
  108.      m_pPolyline->setPointAt(3,pt0+Vec2);
  109.     }
  110.     else
  111.     {
  112.      m_PtJig.z=m_StPt.z;
  113.      AcGeVector2d VecSt=m_BaseLine.direction();
  114.      VecSt.rotateBy(atan (1.0)*2);
  115.      AcGeLine2d LinePer(AcGePoint2d(m_PtJig.x,m_PtJig.y),VecSt);
  116.      AcGePoint2d ptper;
  117.      LinePer.intersectWith(m_BaseLine,ptper);
  118.      AcGeVector2d Vec2_3(m_PtJig.x-ptper.x,m_PtJig.y-ptper.y);
  119.      AcGePoint2d pt0;
  120.      m_pPolyline->getPointAt(0,pt0);
  121.      m_pPolyline->setPointAt(1,ptper);
  122.      m_pPolyline->setPointAt(2,ptper+Vec2_3);
  123.      m_pPolyline->setPointAt(3,pt0+Vec2_3);
  124.     }
  125.     return Adesk::kTrue;
  126. }

  127. AcDbEntity* CLyFunJigDirectedRect::entity() const
  128. {
  129.     return m_pPolyline;
  130. }

  131. void CLyFunJigDirectedRect::InitPolyline()
  132. {
  133.     if (m_pPolyline)
  134.     {
  135.      delete m_pPolyline;
  136.     }
  137.     m_pPolyline=new AcDbPolyline;
  138.     for (unsigned int i=0;i<4;i++)
  139.     {
  140.      m_pPolyline->addVertexAt(i,AcGePoint2d(m_StPt.x,m_StPt.y));
  141.     }
  142.     m_pPolyline->setElevation(m_StPt.z);
  143.     m_pPolyline->setClosed(Adesk::kTrue);
  144. }

  145. double CLyFunJigDirectedRect::Hdist( const AcGePoint3d &pt1,const AcGePoint3d &pt2 )
  146. {
  147.     return sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
  148. }



评分

参与人数 2明经币 +2 金钱 +100 收起 理由
edata + 1 + 50 很给力!
gzxl + 1 + 50 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2025-8-25 00:08:02 来自手机 | 显示全部楼层
兄弟,我建议少参与C#的话题,哈哈,看了帖子才知道前几年已经有不同的意见
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2025-8-22 21:40:41 | 显示全部楼层
tranque 发表于 2025-8-22 18:41
现在发帖都得先叠甲了

没办法,人家有的lisp总会来鄙视ARX。咱只是简单的分享个源码,没有其他意思,避免有人来喷,不值得。
回复 支持 1 反对 0

使用道具 举报

发表于 2025-8-22 14:24:47 | 显示全部楼层
放心吧,会 arx 没人喷,发现有 bug 的只会给些技术建议。
回复 支持 反对

使用道具 举报

发表于 2025-8-22 14:35:22 来自手机 | 显示全部楼层
新手可能不知道如何入手
回复 支持 反对

使用道具 举报

发表于 2025-8-22 17:27:49 | 显示全部楼层
本帖最后由 你有种再说一遍 于 2025-8-22 18:24 编辑

JIG这个东西有个难点,异步刷新和同步刷新,
同步刷新封装之后写起来简单,图元可以动态加入容器.
异步则需要暴露事件使用,并且是固定数组长度,毕竟它任何时候都会高频获取数组成员.
https://www.cnblogs.com/JJBox/p/15650770.html
回复 支持 反对

使用道具 举报

发表于 2025-8-22 18:41:24 | 显示全部楼层
现在发帖都得先叠甲了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-8-25 00:13:43 | 显示全部楼层
gzxl 发表于 2025-8-25 00:08
兄弟,我建议少参与C#的话题,哈哈,看了帖子才知道前几年已经有不同的意见

谢谢哥们儿提醒
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-26 22:36 , Processed in 0.359341 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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