明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7986|回复: 11

[JIG] [求助]如何模仿MLINE用Jig画两条平行线

  [复制链接]
发表于 2009-6-22 17:06 | 显示全部楼层 |阅读模式

如何模仿MLINE用Jig画两条平行线。

对Jig非常感兴趣,但是实在模不到门路。

希望哪位高手能花点时间给个例子:

1. “指定起点”pt1 命令行显示“平行线间距:100”(默认间距为100)

2.   命令行出现 “指定下一点[平行线间距(S)/放弃(U)]<退出>:”

2.1 如果输入S则用getdistance 取得平行线的间距,输完后回到“指定下一点[平行线间距(S)/放弃(U)]<退出>:”

2.2  至于放弃(U)这个功能,参看Line命令。

2.3   如果指定了下一点。即返回为点值。即绘制出两条平行的LINE。

3.  如此循环,类似绘制LINE (Mline)

 楼主| 发表于 2009-6-22 17:23 | 显示全部楼层

类似mline,其对正类型为无(Z).

看KEAN例子,看晕了。

这个应该是动态生成再加上动态输入关键字

如果高手觉得太麻烦。

给个更简单的例子也行:

就是按一楼的那样子,不要生成平行线。

用JIG模仿LINE命令(会不会思路更清晰些)

发表于 2009-6-23 20:59 | 显示全部楼层
本帖最后由 作者 于 2009-6-25 21:41:16 编辑

这几天有点忙:),天天开会,汗
写了个模拟Line的,写的有点乱:)
改写了下,这样可能要清晰一点
实际上Line应该可以不用Jig,这里只是实现一下Jig的流程
双线的Jig加一些计算,改动一下就可以了吧
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.AutoCAD.Geometry;
  8. [assembly: CommandClass(typeof(TlsCad.MyLineJig))]
  9. namespace TlsCad
  10. {
  11.     class MyLineJig : DrawJig
  12.     {
  13.         List<Point3d> m_Points = new List<Point3d>();
  14.         List<ObjectId> m_Lines = new List<ObjectId>();
  15.         Point3d m_EndPoint;
  16.         LineJigStatus m_Status = LineJigStatus.Add;
  17.         public enum LineJigStatus
  18.         {
  19.             Add,
  20.             Undo,
  21.             Close,
  22.             Finish
  23.         }
  24.         public LineJigStatus JigStatus
  25.         {
  26.             get { return m_Status; }
  27.             set { m_Status = value; }
  28.         }
  29.         public Point3d StartPoint
  30.         {
  31.             get { return m_Points[1]; }
  32.         }
  33.         public Point3d EndPoint
  34.         {
  35.             get { return m_Points[0]; }
  36.         }
  37.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  38.         {
  39.             return true;
  40.         }
  41.         protected override SamplerStatus Sampler(JigPrompts prompts)
  42.         {
  43.             JigPromptPointOptions jigOpts = new JigPromptPointOptions();
  44.             jigOpts.UserInputControls =
  45.                 UserInputControls.Accept3dCoordinates |
  46.                 UserInputControls.NoZeroResponseAccepted |
  47.                 UserInputControls.NoNegativeResponseAccepted |
  48.                 UserInputControls.NullResponseAccepted;
  49.             switch (m_Points.Count)
  50.             {
  51.                 case 0:
  52.                     jigOpts.Message = "\n指定第一点:";
  53.                     break;
  54.                 case 1:
  55.                 case 2:
  56.                     jigOpts.Cursor = CursorType.RubberBand;
  57.                     jigOpts.SetMessageAndKeywords("\n指定下一点或 [放弃(U)]:", "Undo");
  58.                     jigOpts.UseBasePoint = true;
  59.                     jigOpts.BasePoint = m_Points[0];
  60.                     break;
  61.                 default:
  62.                     jigOpts.Cursor = CursorType.RubberBand;
  63.                     jigOpts.SetMessageAndKeywords("\n指定下一点或 [闭合(C)/放弃(U)]:", "Close Undo");
  64.                     jigOpts.UseBasePoint = true;
  65.                     jigOpts.BasePoint = m_Points[0];
  66.                     break;
  67.             }
  68.             PromptPointResult res = prompts.AcquirePoint(jigOpts);
  69.             m_Status = LineJigStatus.Add;
  70.             switch (res.Status)
  71.             {
  72.                 case PromptStatus.Cancel:
  73.                 case PromptStatus.None:
  74.                     m_Status = LineJigStatus.Finish;
  75.                     return SamplerStatus.Cancel;
  76.                 case PromptStatus.Keyword:
  77.                     switch (res.StringResult)
  78.                     {
  79.                         case "Undo":
  80.                             m_Status = LineJigStatus.Undo;
  81.                             break;
  82.                         case "Close":
  83.                             m_EndPoint = m_Points[m_Points.Count - 1];
  84.                             m_Status = LineJigStatus.Close;
  85.                             break;
  86.                     }
  87.                     return SamplerStatus.OK;
  88.                 default:
  89.                     Point3d positionTemp = res.Value;
  90.                     if (positionTemp != m_EndPoint)
  91.                     {
  92.                         m_EndPoint = positionTemp;
  93.                     }
  94.                     else
  95.                     {
  96.                         return SamplerStatus.NoChange;
  97.                     }
  98.                     break;
  99.             }
  100.             return SamplerStatus.OK;
  101.         }
  102.         public int AddPoint()
  103.         {
  104.             m_Points.Insert(0, m_EndPoint);
  105.             return m_Points.Count;
  106.         }
  107.         public void AddLine(Line line)
  108.         {
  109.             m_Lines.Insert(0, line.ObjectId);
  110.         }
  111.         public ObjectId CurrLine
  112.         {
  113.             get { return m_Lines.Count == 0 ? ObjectId.Null : m_Lines[0]; }
  114.         }
  115.         public int RemovePoint()
  116.         {
  117.             m_Points.RemoveAt(0);
  118.             return m_Points.Count;
  119.         }
  120.         public void RemveLine()
  121.         {
  122.             m_Lines.RemoveAt(0);
  123.         }
  124.         [CommandMethod("lj")]
  125.         public static void DoIt()
  126.         {
  127.             MyLineJig ljig = new MyLineJig();
  128.             Document doc = Application.DocumentManager.MdiActiveDocument;
  129.             Database db = doc.Database;
  130.             Editor ed = doc.Editor;
  131.             while (ljig.JigStatus != LineJigStatus.Finish)
  132.             {
  133.                 PromptResult res = ed.Drag(ljig);
  134.                 Line line;
  135.                 switch (ljig.JigStatus)
  136.                 {
  137.                     case LineJigStatus.Add:
  138.                     case LineJigStatus.Close:
  139.                         if (ljig.AddPoint() != 1)
  140.                         {
  141.                             using (Transaction tr = db.TransactionManager.StartTransaction())
  142.                             {
  143.                                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  144.                                 line = new Line(ljig.StartPoint, ljig.EndPoint);
  145.                                 line.SetDatabaseDefaults();
  146.                                 btr.AppendEntity(line);
  147.                                 tr.AddNewlyCreatedDBObject(line, true);
  148.                                 ljig.AddLine(line);
  149.                                 tr.Commit();
  150.                             }
  151.                         }
  152.                         break;
  153.                     case LineJigStatus.Undo:
  154.                         ljig.RemovePoint();
  155.                         if (ljig.CurrLine != ObjectId.Null)
  156.                         {
  157.                             using (Transaction tr = db.TransactionManager.StartTransaction())
  158.                             {
  159.                                 line = (Line)ljig.CurrLine.GetObject(OpenMode.ForWrite);
  160.                                 line.Erase(true);
  161.                                 ljig.RemveLine();
  162.                                 tr.Commit();
  163.                             }
  164.                         }
  165.                         break;
  166.                 }
  167.                 if (ljig.JigStatus == LineJigStatus.Close)
  168.                     ljig.JigStatus = LineJigStatus.Finish;
  169.             }
  170.         }
  171.     }
  172. }

评分

参与人数 1威望 +1 明经币 +2 金钱 +20 贡献 +5 激情 +5 收起 理由
ahlzl + 1 + 2 + 20 + 5 + 5 【精华】 好程序

查看全部评分

 楼主| 发表于 2009-6-24 09:17 | 显示全部楼层

太感谢版主了。。。

因为有这么热心的版主。。。

我相信这个版块一定会越办越好。

我先好好学习一下。

发表于 2009-6-27 22:59 | 显示全部楼层
试着改写成PolyLine的,:)
没有考虑Ucs的
  1.     class PolyLineJig : DrawJig
  2.     {
  3.         ObjectId m_PolyLine = ObjectId.Null;
  4.         Point3d m_FirstPoint;
  5.         Polyline m_CurrPolyLine;
  6.         Point3d m_StartPoint = Point3d.Origin;
  7.         Vector3d m_StartVector = Vector3d.XAxis;
  8.         Point3d m_EndPoint;
  9.         bool m_IsLine = true;
  10.         int m_Count = -1;
  11.         PolyLineJigStatus m_Status = PolyLineJigStatus.Add;
  12.         public enum PolyLineJigStatus
  13.         {
  14.             Add,
  15.             Keyword,
  16.             Undo,
  17.             Finish,
  18.             Close,
  19.         }
  20.         public PolyLineJig()
  21.         {
  22.             m_CurrPolyLine = new Polyline();
  23.             m_CurrPolyLine.AddVertexAt(0, m_StartPoint.Convert2d(new Plane()), 0, 0, 0);
  24.             m_CurrPolyLine.AddVertexAt(0, m_StartPoint.Convert2d(new Plane()), 0, 0, 0);
  25.         }
  26.         public PolyLineJigStatus JigStatus
  27.         {
  28.             get { return m_Status; }
  29.             set { m_Status = value; }
  30.         }
  31.         public Point3d StartPoint
  32.         {
  33.             get { return m_StartPoint; }
  34.         }
  35.         public Point3d EndPoint
  36.         {
  37.             get { return m_EndPoint; }
  38.         }
  39.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  40.         {
  41.             if (m_Count > -1)
  42.             {
  43.                 double bugle = GetBugle();
  44.                 if (bugle != 0)
  45.                 {
  46.                     m_CurrPolyLine.SetBulgeAt(0, GetBugle());
  47.                     m_CurrPolyLine.SetPointAt(1, m_EndPoint.Convert2d(new Plane()));
  48.                     return draw.Geometry.Draw(m_CurrPolyLine);
  49.                 }
  50.             }
  51.             return true;
  52.         }
  53.         private double GetBugle()
  54.         {
  55.             if (m_IsLine)
  56.             {
  57.                 return 0;
  58.             }
  59.             else
  60.             {
  61.                 Plane plane = new Plane();
  62.                 double ang = (m_EndPoint - m_StartPoint).AngleOnPlane(plane);
  63.                 ang -= m_StartVector.AngleOnPlane(plane);
  64.                 return Math.Tan(ang * 0.5);
  65.             }
  66.         }
  67.         public Polyline Update()
  68.         {
  69.             Polyline pl;
  70.             switch (m_Status)
  71.             {
  72.                 case PolyLineJigStatus.Add:
  73.                 case PolyLineJigStatus.Close:
  74.                     if (m_Count > -1)
  75.                     {
  76.                         pl = (Polyline)m_PolyLine.GetObject(OpenMode.ForWrite);
  77.                         pl.SetBulgeAt(m_Count, GetBugle());
  78.                     }
  79.                     else
  80.                     {
  81.                         pl = new Polyline();
  82.                         m_FirstPoint = m_EndPoint;
  83.                     }
  84.                     m_Count++;
  85.                     pl.AddVertexAt(m_Count, m_EndPoint.Convert2d(new Plane()), GetBugle(), 0, 0);
  86.                     m_StartPoint = m_EndPoint;
  87.                     m_CurrPolyLine.SetPointAt(0, m_StartPoint.Convert2d(new Plane()));
  88.                     try
  89.                     {
  90.                         m_StartVector = pl.GetFirstDerivative(pl.EndParam);
  91.                     }
  92.                     catch
  93.                     {
  94.                         m_StartVector = Vector3d.XAxis;
  95.                     }
  96.                     return pl;
  97.                 case PolyLineJigStatus.Undo:
  98.                     pl = (Polyline)m_PolyLine.GetObject(OpenMode.ForWrite);
  99.                     if (m_Count == 0)
  100.                     {
  101.                         pl.Erase(true);
  102.                         m_PolyLine = ObjectId.Null;
  103.                     }
  104.                     else
  105.                     {
  106.                         pl.RemoveVertexAt(m_Count);
  107.                         m_StartPoint = pl.EndPoint;
  108.                         m_CurrPolyLine.SetPointAt(0, m_StartPoint.Convert2d(new Plane()));
  109.                     }
  110.                     m_Count--;
  111.                     if (m_Count > -1)
  112.                     {
  113.                         m_StartPoint = pl.GetPoint3dAt(m_Count);
  114.                     }
  115.                     try
  116.                     {
  117.                         m_StartVector = pl.GetFirstDerivative(pl.EndParam);
  118.                     }
  119.                     catch
  120.                     {
  121.                         m_StartVector = Vector3d.XAxis;
  122.                     }
  123.                     break;
  124.             }
  125.             return null;
  126.         }
  127.         protected override SamplerStatus Sampler(JigPrompts prompts)
  128.         {
  129.             JigPromptPointOptions jigOpts = new JigPromptPointOptions();
  130.             jigOpts.UserInputControls =
  131.                 UserInputControls.Accept3dCoordinates |
  132.                 UserInputControls.NoZeroResponseAccepted |
  133.                 UserInputControls.NoNegativeResponseAccepted |
  134.                 UserInputControls.NullResponseAccepted;
  135.             switch (m_Count)
  136.             {
  137.                 case -1:
  138.                     jigOpts.Message = "\n指定第一点:";
  139.                     break;
  140.                 case 0:
  141.                 case 1:
  142.                     jigOpts.Cursor = CursorType.RubberBand;
  143.                     jigOpts.Message = "\n指定下一个点:";
  144.                     if (m_IsLine)
  145.                     {
  146.                         jigOpts.Keywords.Add("Arc", "Arc", "圆弧(A)");
  147.                     }
  148.                     else
  149.                     {
  150.                         jigOpts.Keywords.Add("Line", "Line", "直线(L)");
  151.                     }
  152.                     jigOpts.Keywords.Add("Undo", "Undo", "放弃(U)");
  153.                     jigOpts.UseBasePoint = true;
  154.                     jigOpts.BasePoint = m_StartPoint;
  155.                     break;
  156.                 default:
  157.                     jigOpts.Cursor = CursorType.RubberBand;
  158.                     jigOpts.Message = "\n指定下一个点:";
  159.                     if (m_IsLine)
  160.                     {
  161.                         jigOpts.Keywords.Add("Arc", "Arc", "圆弧(A)");
  162.                     }
  163.                     else
  164.                     {
  165.                         jigOpts.Keywords.Add("Line", "Line", "直线(L)");
  166.                     }
  167.                     jigOpts.Keywords.Add("Close", "Close", "闭合(C)");
  168.                     jigOpts.Keywords.Add("Undo", "Undo", "放弃(U)");
  169.                     jigOpts.UseBasePoint = true;
  170.                     jigOpts.BasePoint = m_StartPoint;
  171.                     break;
  172.             }
  173.             PromptPointResult res = prompts.AcquirePoint(jigOpts);
  174.             m_Status = PolyLineJigStatus.Add;
  175.             switch (res.Status)
  176.             {
  177.                 case PromptStatus.Cancel:
  178.                 case PromptStatus.None:
  179.                     m_Status = PolyLineJigStatus.Finish;
  180.                     return SamplerStatus.Cancel;
  181.                 case PromptStatus.Keyword:
  182.                     switch (res.StringResult)
  183.                     {
  184.                         case "Arc":
  185.                         case "Line":
  186.                             m_IsLine = !m_IsLine;
  187.                             m_Status = PolyLineJigStatus.Keyword;
  188.                             return SamplerStatus.OK;
  189.                         case "Add":
  190.                             m_Status = PolyLineJigStatus.Add;
  191.                             return SamplerStatus.OK;
  192.                         case "Undo":
  193.                             m_Status = PolyLineJigStatus.Undo;
  194.                             return SamplerStatus.OK;
  195.                         case "Close":
  196.                             m_EndPoint = m_FirstPoint;
  197.                             m_Status = PolyLineJigStatus.Close;
  198.                             return SamplerStatus.Cancel;
  199.                     }
  200.                     break;
  201.                 default:
  202.                     Point3d positionTemp = res.Value;
  203.                     if (positionTemp != m_EndPoint)
  204.                     {
  205.                         m_EndPoint = positionTemp;
  206.                     }
  207.                     else
  208.                     {
  209.                         return SamplerStatus.NoChange;
  210.                     }
  211.                     break;
  212.             }
  213.             return SamplerStatus.OK;
  214.         }
  215.         [CommandMethod("plj")]
  216.         public static void DoIt()
  217.         {
  218.             PolyLineJig ljig = new PolyLineJig();
  219.             Document doc = Application.DocumentManager.MdiActiveDocument;
  220.             Database db = doc.Database;
  221.             Editor ed = doc.Editor;
  222.             while (ljig.JigStatus != PolyLineJigStatus.Finish)
  223.             {
  224.                 ed.Drag(ljig);
  225.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  226.                 {
  227.                     Polyline pl = ljig.Update();
  228.                     if (ljig.m_PolyLine == ObjectId.Null && pl != null)
  229.                     {
  230.                         BlockTableRecord btr = db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
  231.                         ljig.m_PolyLine = btr.AppendEntity(pl);
  232.                         tr.AddNewlyCreatedDBObject(pl, true);
  233.                     }
  234.                     tr.Commit();
  235.                 }
  236.                 if (ljig.JigStatus == PolyLineJigStatus.Close)
  237.                     ljig.JigStatus = PolyLineJigStatus.Finish;
  238.             }
  239.         }
  240.     }

评分

参与人数 2威望 +1 明经币 +3 金钱 +70 贡献 +5 激情 +5 收起 理由
xgr + 1 + 50 jigOpts.Cursor = CursorType.RubberBand找.
ahlzl + 1 + 2 + 20 + 5 + 5 【精华】好程序

查看全部评分

发表于 2009-8-4 09:28 | 显示全部楼层
怎么看不到
发表于 2011-3-21 14:05 | 显示全部楼层
学习了......
发表于 2012-2-22 17:02 | 显示全部楼层
本帖最后由 shirazbj 于 2012-2-22 17:10 编辑

学习了。就是用plj时,每次只是显示一条线,前一步的线要是也能显示出来,就和本身的pl命令一样了。那就太棒了。
lj点击输入下一点后,线怎么不画出来?
发表于 2012-2-26 21:38 | 显示全部楼层
第一次来这里,还不知道程序怎么用,保存的后缀是什么?命令名是什么?还望多多指教
发表于 2012-2-27 18:47 | 显示全部楼层
2.3   如果指定了下一点。即返回为点值。即绘制出两条平行的LINE。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 13:19 , Processed in 1.462799 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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