明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4070|回复: 9

动态插入箭头

[复制链接]
发表于 2013-4-15 11:17 | 显示全部楼层 |阅读模式
平常因为工作的关系经常要插入一些箭头,根据鼠标的位置变化方向和变化.之前有人写过这个功能,我就按照样例仿写了一下,可能因为水平的关系算法有些复杂,还希望大家轻拍,麻烦各位大侠帮忙把算法优化一下,刚写完没有完全测试可能有点小问题.

  1. (defun c:tt ()
  2.    (vl-load-com)
  3.    (setq acad (vlax-get-acad-object))
  4.    (setq acaddocument (vla-get-activedocument acad))
  5.    (setq mspace (vla-get-modelspace acaddocument))
  6.    (setq h (getreal "\n请输入偏移距离"))
  7. (setq endata (entsel "\n请选择一条线"))
  8.    (setq ename (car endata))
  9.    (setq p0 (cadr endata))
  10.    (setq obj (vlax-ename->vla-object ename))
  11.    (setq l1 (car (vlax-safearray->list (vlax-variant-value (vla-offset obj h)))))           ;偏移曲线1
  12.    (setq l2 (car (vlax-safearray->list (vlax-variant-value (vla-offset obj (* -1 h))))))    ;偏移曲线2
  13.    (setq p1 (vlax-curve-getclosestpointto l1 p0 t))      ;插入点1
  14.    (setq p2 (vlax-curve-getclosestpointto l2 p0 t))      ;插入点2
  15.    (vla-delete l1)    ;删除曲线1
  16.    (vla-delete l2)    ;删除曲线2
  17.    (setq ang1 (angle p2 p1))  ;插入点1 点2角度
  18.    (setq ang2 (- ang1 (/ pi 2)))  ;计算直线角度或者曲线的切线角度
  19.    (setq obj (vla-insertblock mspace (vlax-3d-point p2) "11005" 1 1 1 ang2)) ;插入块名为11005的图块
  20.    (setq loop t)
  21.   (while loop
  22.     (setq code (grread t 8))
  23.     (cond
  24.      ((= (car code) 5)
  25.     (setq ang3 (- (angle p0 (cadr code)) ang2))     ;光标所在点与单选点的角度减去直线的或者曲线切线的角度
  26.     (if (< ang3 0)
  27.         (setq ang3 (+ ang3 (* 2 pi))))
  28.      (cond
  29.         ((and (> ang3 0 ) (< ang3  (/ pi 2)))  
  30.          (vla-put-Rotation obj ang2)  
  31.          (vla-put-insertionpoint obj (vlax-3d-point p1))
  32.          )
  33.         ((and (> ang3 (/ pi 2)) (< ang3 pi))
  34.          (vla-put-Rotation obj (- ang2 pi))
  35.          (vla-put-insertionpoint obj (vlax-3d-point p1))
  36.          )
  37.         ((and (> ang3 pi) (< ang3  (* 3 (/ pi 2))))
  38.          (vla-put-Rotation obj (- ang2 pi))
  39.          (vla-put-insertionpoint obj (vlax-3d-point p2))
  40.          )
  41.         ((and (> ang3 (* 3 (/ pi 2))) (< ang3  (* 2 pi)))
  42.          (vla-put-Rotation obj ang2)
  43.          (vla-put-insertionpoint obj (vlax-3d-point p2))
  44.          )            
  45.        )
  46.     )
  47.    ((= code '(25 37)) (vla-delete obj))
  48.    (T (setq loop nil))
  49.    )
  50.    )
  51.   )


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2020-1-10 13:32 | 显示全部楼层
根据自己使用改了下,在图形中第一次使用时,输入名字,自动创建一个800*400的箭头块,或者选择已有块

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复 支持 1 反对 0

使用道具 举报

发表于 2013-4-15 14:09 | 显示全部楼层
;; 曲线动态插入箭头

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2013-4-15 16:44 | 显示全部楼层
本帖最后由 adolfken 于 2013-4-15 18:39 编辑

在院长和wowan两位大侠的指导下我按照正常切线方向的计算写了代码,非常感谢两位.
  1. (defun c:tt ()
  2.           (vl-load-com)
  3.           (setq acad (vlax-get-acad-object))
  4.           (setq acaddocument (vla-get-activedocument acad))
  5.           (setq mspace (vla-get-modelspace acaddocument))
  6.           (setq h (getreal "\n请输入偏移距离"))
  7.         (setq endata (entsel "\n请选择一条线"))
  8.           (setq p0 (cadr endata))
  9.         (setq l1 (vlax-ename->vla-object (car endata)))
  10.           (setq p1 (vlax-curve-getclosestpointto l1 p0 t))
  11.           (setq ang1 (angle p1 (mapcar '+ p1 (vlax-curve-getfirstderiv l1 (vlax-curve-getparamatpoint l1 p1)))))
  12.         (setq p2 (polar p1 (+ ang1 (/ pi 2)) h))
  13.           (setq p3 (polar p1 (- ang1 (/ pi 2)) h))
  14.           (setq obj (vla-insertblock mspace (vlax-3d-point p2) "11005" 1 1 1 ang1))
  15.           (setq loop t)
  16.         (while loop
  17.                   (setq code (grread t 8))
  18.                   (cond
  19.                           ((= (car code) 5)
  20.                          (setq ang2 (- (angle p1 (cadr code)) ang1))  
  21.                          (if (< ang2 0)
  22.                              (setq ang2 (+ ang2 (* 2 pi))))
  23.                           (cond
  24.                              ((and (> ang2 0 ) (< ang2 (/ pi 2)))               
  25.                               (vla-put-Rotation obj ang1)               
  26.                               (vla-put-insertionpoint obj (vlax-3d-point p2))
  27.                               )
  28.                              ((and (> ang2 (/ pi 2) ) (< ang2 pi))               
  29.                               (vla-put-Rotation obj (- ang1 pi))               
  30.                               (vla-put-insertionpoint obj (vlax-3d-point p2))
  31.                               )
  32.                              ((and (> ang2 pi ) (< ang2  (* 3 (/ pi 2))))               
  33.                               (vla-put-Rotation obj (- ang1 pi))               
  34.                               (vla-put-insertionpoint obj (vlax-3d-point p3))
  35.                               )
  36.                              ((and (> ang2 (* 3 (/ pi 2))) (< ang2  (* pi 2)))               
  37.                               (vla-put-Rotation obj ang1)               
  38.                               (vla-put-insertionpoint obj (vlax-3d-point p3))
  39.                               )
  40.                             )
  41.                          )
  42.                         ((= (car code) 25) (setq loop nil) (vla-delete obj))
  43.                         (T (setq loop nil))
  44.                         )
  45.           )
  46.   )
发表于 2013-4-19 06:35 | 显示全部楼层
这个好是好 工作关系用不上 感谢热心人
发表于 2013-5-10 11:00 | 显示全部楼层
出现错误:
请选择一条线; 错误: Automation 错误。 文件处理器错误
另外最好距离可以点选。
发表于 2013-5-10 11:27 | 显示全部楼层
yaya-54 发表于 2013-5-10 11:00
出现错误:
请选择一条线; 错误: Automation 错误。 文件处理器错误
另外最好距离可以点选。

同样的错误
同样的建议
发表于 2013-5-10 11:35 | 显示全部楼层
看明白了,原来是要先自己画个箭头做个块
发表于 2018-12-16 22:36 | 显示全部楼层
错误: Automation 错误。 文件处理器错误
发表于 2020-1-10 14:38 | 显示全部楼层
本帖最后由 j15tty 于 2020-1-10 14:44 编辑

C:\Users\Administrator\Desktop\GIF动画录制工具20200110141850.gif
  1. public class 坡度标注Jig : DrawJig
  2.     {
  3.         public Autodesk.AutoCAD.DatabaseServices.Polyline m_pl;
  4.         private Point3d m_pt, m_insertPt;
  5.         public static DBText m_text = new DBText();
  6.         private Curve m_c;
  7.         public static double m_bl = 1;
  8.         public static double m_jj = 3;
  9.         public static double m_h = 3;
  10.         private Database m_db;
  11.         public 坡度标注Jig(Autodesk.AutoCAD.DatabaseServices.Polyline pl, DBText text, Curve c, Point3d pt, Database db, double bl, double jj, double h)
  12.         {
  13.             m_pl = pl;
  14.             m_pt = pt;
  15.             m_text = text;
  16.             m_c = c;
  17.             m_db = db;
  18.             m_bl = bl;
  19.             m_jj = jj;
  20.             m_h = h;
  21.         }
  22.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  23.         {
  24.             Point3d p1 = m_c.GetClosestPointTo(m_insertPt, false);
  25.             Vector3d vecCz = m_insertPt - p1;//鼠标点到曲线的垂足点的向量
  26.             Vector3d vec = m_insertPt - m_pt;//得到鼠标与曲线上点取点的向量
  27.             Vector3d vec1 = m_c.GetFirstDerivative(m_pt);//曲线点切向量
  28.             double angFx = vec.GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis);//鼠标点与曲线上点取点向量与X轴的夹角;
  29.             double angQx = vec1.GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis);
  30.             double ang = vec.GetAngleTo(vec1);
  31.             Point3d insertPt, pt1, pt2, pt3;
  32.             bool flag = IsLeftOfCurve(m_c, m_insertPt);
  33.             if (flag == true)
  34.             {
  35.                 insertPt = m_pt.polarPoint(angQx + Math.PI / 2, m_jj);
  36.                 if (ang < Math.PI / 2)
  37.                 {
  38.                     pt1 = insertPt.polarPoint(angQx + Math.PI, m_bl * 5);
  39.                     pt2 = pt1.polarPoint(angQx, m_bl * 10);
  40.                     pt3 = pt2.polarPoint(angQx, m_bl * 3.5);
  41.                     m_text.Position = insertPt.polarPoint(angQx + Math.PI / 2, m_bl * 0.5 + m_text.Height / 2);
  42.                     m_text.Rotation = angQx;
  43.                     m_text.Height = m_h;
  44.                     m_text.HorizontalMode = TextHorizontalMode.TextMid;
  45.                     m_text.VerticalMode = TextVerticalMode.TextBottom;
  46.                     m_text.AlignmentPoint = m_text.Position;
  47.                     m_text.AdjustAlignment(m_db);
  48.                 }
  49.                 else
  50.                 {
  51.                     pt1 = insertPt.polarPoint(angQx, m_bl * 5);
  52.                     pt2 = pt1.polarPoint(angQx + Math.PI, m_bl * 10);
  53.                     pt3 = pt2.polarPoint(angQx + Math.PI, m_bl * 3.5);
  54.                     m_text.Position = insertPt.polarPoint(angQx + Math.PI / 2, m_bl * 0.5 + m_text.Height / 2);
  55.                     m_text.Rotation = angQx;
  56.                     m_text.Height = m_h;
  57.                     m_text.HorizontalMode = TextHorizontalMode.TextMid;
  58.                     m_text.VerticalMode = TextVerticalMode.TextBottom;
  59.                     m_text.AlignmentPoint = m_text.Position;
  60.                     m_text.AdjustAlignment(m_db);
  61.                 }

  62.             }
  63.             else
  64.             {
  65.                 insertPt = m_pt.polarPoint(angQx - Math.PI / 2, m_jj);
  66.                 if (ang < Math.PI / 2)
  67.                 {
  68.                     pt1 = insertPt.polarPoint(angQx + Math.PI, m_bl * 5);
  69.                     pt2 = pt1.polarPoint(angQx, m_bl * 10);
  70.                     pt3 = pt2.polarPoint(angQx, m_bl * 3.5);
  71.                     m_text.Position = insertPt.polarPoint(angQx - Math.PI / 2, m_bl * 0.5 + m_text.Height / 2);
  72.                     m_text.Rotation = angQx + Math.PI;
  73.                     m_text.Height = m_h;
  74.                     m_text.HorizontalMode = TextHorizontalMode.TextMid;
  75.                     m_text.VerticalMode = TextVerticalMode.TextBottom;
  76.                     m_text.AlignmentPoint = m_text.Position;
  77.                     m_text.AdjustAlignment(m_db);
  78.                 }
  79.                 else
  80.                 {
  81.                     pt1 = insertPt.polarPoint(angQx, m_bl * 5);
  82.                     pt2 = pt1.polarPoint(angQx + Math.PI, m_bl * 10);
  83.                     pt3 = pt2.polarPoint(angQx + Math.PI, m_bl * 3.5);
  84.                     m_text.Position = insertPt.polarPoint(angQx - Math.PI / 2, m_bl * 0.5 + m_text.Height / 2);
  85.                     m_text.Rotation = angQx + Math.PI;
  86.                     m_text.Height = m_h;
  87.                     m_text.HorizontalMode = TextHorizontalMode.TextMid;
  88.                     m_text.VerticalMode = TextVerticalMode.TextBottom;
  89.                     m_text.AlignmentPoint = m_text.Position;
  90.                     m_text.AdjustAlignment(m_db);
  91.                 }
  92.             }
  93.             m_pl.Normal = Vector3d.ZAxis;
  94.             m_pl.Elevation = 0.0;
  95.             m_pl.SetPointAt(0, new Point2d(pt1.X, pt1.Y));
  96.             m_pl.SetPointAt(1, new Point2d(pt2.X, pt2.Y));
  97.             m_pl.SetPointAt(2, new Point2d(pt3.X, pt3.Y));
  98.             m_pl.SetStartWidthAt(0, 0);
  99.             m_pl.SetEndWidthAt(0, 0);
  100.             m_pl.SetStartWidthAt(1, m_bl);
  101.             m_pl.SetEndWidthAt(1, 0);
  102.             m_pl.SetStartWidthAt(2, 0);
  103.             m_pl.SetEndWidthAt(2, 0);
  104.             draw.Geometry.Draw(m_pl);
  105.             draw.Geometry.Draw(m_text);
  106.             return true;
  107.         }

  108.         protected override SamplerStatus Sampler(JigPrompts prompts)
  109.         {
  110.             Database db = HostApplicationServices.WorkingDatabase;
  111.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  112.             Matrix3d mat = ed.CurrentUserCoordinateSystem;
  113.             JigPromptPointOptions optInsertPt = new JigPromptPointOptions("\n请选择方向或[比例增(A)/比例减(S)/间距增(D)/间距减(F)/坡度设置(Q)]");
  114.             optInsertPt.UserInputControls = (UserInputControls.Accept3dCoordinates |
  115.                                              UserInputControls.NullResponseAccepted |
  116.                                              UserInputControls.NoNegativeResponseAccepted |
  117.                                              UserInputControls.GovernedByOrthoMode);
  118.             optInsertPt.Keywords.Add("A");
  119.             optInsertPt.Keywords.Add("S");
  120.             optInsertPt.Keywords.Add("D");
  121.             optInsertPt.Keywords.Add("F");
  122.             optInsertPt.Keywords.Add("Q");
  123.             optInsertPt.AppendKeywordsToMessage = false;
  124.             //m_pt = m_pt.TransformBy(mat);
  125.             PromptPointResult resJigInsertPt = prompts.AcquirePoint(optInsertPt);
  126.             if (resJigInsertPt.Status == PromptStatus.Cancel)
  127.                 return SamplerStatus.Cancel;
  128.             if (resJigInsertPt.Status == PromptStatus.Keyword)
  129.             {
  130.                 switch (resJigInsertPt.StringResult)
  131.                 {
  132.                     case "A":
  133.                         {
  134.                             m_bl = m_bl * 2;
  135.                             m_h = m_h * 2;
  136.                             break;
  137.                         }
  138.                     case "S":
  139.                         {
  140.                             m_bl = m_bl * 0.5;
  141.                             m_h = m_h * 0.5;
  142.                             break;
  143.                         }
  144.                     case "D":
  145.                         {
  146.                             m_jj = m_jj * 1.1;
  147.                             break;
  148.                         }
  149.                     case "F":
  150.                         {
  151.                             m_jj = m_jj * 0.9;
  152.                             break;
  153.                         }
  154.                     case "Q":
  155.                         {

  156.                             PromptStringOptions opt = new PromptStringOptions("\n请输入坡度");
  157.                             PromptResult res = ed.GetString(opt);
  158.                             if (res.Status != PromptStatus.OK)
  159.                                 break;
  160.                             else
  161.                             {
  162.                                 try
  163.                                 {
  164.                                     double value = Convert.ToDouble(res.StringResult);
  165.                                     if (value >= 10)
  166.                                     {
  167.                                         if (MyEntity.IsInteger(res.StringResult))
  168.                                         {
  169.                                             m_text.TextString = (value / 10).ToString("0") + "%";
  170.                                         }
  171.                                         else
  172.                                         {
  173.                                             m_text.TextString = (value / 10).ToString("0.####") + "%";
  174.                                         }
  175.                                     }
  176.                                     else
  177.                                         m_text.TextString = res.StringResult + "‰";
  178.                                 }
  179.                                 catch (System.Exception ex)
  180.                                 {
  181.                                     ed.WriteMessage(ex.Message);
  182.                                     m_text.TextString = "3" + "‰";
  183.                                 }
  184.                             }
  185.                             break;
  186.                         }
  187.                 }

  188.             }
  189.             if (m_insertPt != resJigInsertPt.Value)
  190.             {

  191.                 m_insertPt = resJigInsertPt.Value;

  192.                 return SamplerStatus.OK;
  193.             }
  194.             else
  195.             {

  196.                 return SamplerStatus.NoChange;
  197.             }

  198.         }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

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

GMT+8, 2024-4-26 16:07 , Processed in 0.497898 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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