其他分享
首页 > 其他分享> > 自定义捕捉对象动态Jig绘制,实现实时更具选择对象变化预览图块

自定义捕捉对象动态Jig绘制,实现实时更具选择对象变化预览图块

作者:互联网

1、自定义靶框对象

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 //CAD开发库
 8 using Autodesk.AutoCAD.EditorInput;
 9 using Autodesk.AutoCAD.ApplicationServices;
10 using Autodesk.AutoCAD.Runtime;
11 using Autodesk.AutoCAD.Windows;
12 using Autodesk.AutoCAD.Geometry;
13 using Autodesk.AutoCAD.DatabaseServices;
14 using Autodesk.AutoCAD.GraphicsInterface;
15 
16 //CAD开发基础库
17 using BaseLibrary.Runtime;
18 using BaseLibrary.ResultData;
19 using BaseLibrary.ExtensionMethod;
20 using BaseLibrary.Filter;
21 
22 namespace TSTJig
23 {
24     public class BaseGlyph : Glyph
25     {
26         public override void SetLocation(Point3d point)
27         {
28 
29         }
30 
31         protected override void SubViewportDraw(ViewportDraw vd)
32         {
33 
34         }
35     }
36 }

2、自定义JIg对象

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 //CAD开发库
  7 using Autodesk.AutoCAD.EditorInput;
  8 using Autodesk.AutoCAD.ApplicationServices;
  9 using Autodesk.AutoCAD.Runtime;
 10 using Autodesk.AutoCAD.Windows;
 11 using Autodesk.AutoCAD.Geometry;
 12 using Autodesk.AutoCAD.DatabaseServices;
 13 
 14 //CAD开发基础库
 15 using BaseLibrary.Runtime;
 16 using BaseLibrary.ResultData;
 17 using BaseLibrary.ExtensionMethod;
 18 using BaseLibrary.Filter;
 19 
 20 namespace TSTJig
 21 {
 22     class SnapEntityJig : EntityJig
 23     {
 24         private Point3d m_Position;
 25         private Vector3d m_Direction;   //块方向
 26         private Curve m_Curve = null;
 27 
 28 
 29         public SnapEntityJig(Entity entity) : base(entity)
 30         {
 31             //((Circle)Entity).Center = m_CenterPt;
 32             //((Circle)Entity).Radius = m_Radius;
 33             
 34 
 35             //创建自定义对象捕捉模式
 36             CustomObjectSnapMode mode = new CustomObjectSnapMode("third", "_third", "三分之一", new BaseGlyph());
 37             //捕捉模式的实体类型为曲线
 38             mode.ApplyToEntityType(RXClass.GetClass(typeof(Curve)), CurveSnap);
 39             //开启自定义对象捕捉模式
 40             CustomObjectSnapMode.Activate("_third");
 41         }
 42 
 43         public void CurveSnap(ObjectSnapContext context, ObjectSnapInfo result)
 44         {
 45             m_Curve = (Curve)context.PickedObject;//当前捕捉到的曲线对象
 46             //Point3dCollection snaps = result.SnapPoints;//获取对象的捕捉点集合
 47             //                                            //Curve3dCollection snapCurves = result.SnapCurves;
 48 
 49             //if (curve.Closed) return;//如果为闭合曲线,则返回
 50             //double startParam = curve.StartParam;//曲线的起点参数
 51             //double endParam = curve.EndParam;//曲线的终点参数
 52 
 53 
 54 
 55 
 56             //if (context.LastPoint != null) {
 57             //    ed.Ex_WriteMessage("LastPoint" + context.LastPoint.ToString());
 58             //    string str = context.LastPoint.ToString();
 59             //}
 60             //if (context.PickPoint != null) {
 61             //    ed.Ex_WriteMessage("PickPoint" + context.PickPoint.ToString());
 62             //    string str = context.LastPoint.ToString();
 63             //}
 64             //ed.Ex_WriteMessage(snaps.Count);
 65 
 66 
 67 
 68             //将与曲线起点的距离为1/3曲线长度的点作为捕捉点
 69             //snaps.Add(curve.GetPointAtParameter(startParam + ((endParam - startParam) / 3)));
 70             //将与曲线起点的距离为2/3曲线长度的点作为捕捉点
 71             //snaps.Add(curve.GetPointAtParameter(startParam + ((endParam - startParam) * 2 / 3)));
 72             //ed.Ex_WriteMessage(context.PickPoint.ToString());
 73         }
 74 
 75         public void StopSnap()
 76         {
 77             //关闭自定义对象捕捉模式
 78             CustomObjectSnapMode.Deactivate("_third");
 79         }
 80 
 81         protected override SamplerStatus Sampler(JigPrompts prompts)
 82         {
 83             JigPromptPointOptions optJig = new JigPromptPointOptions();
 84             // 用AcquirePoint函数得到用户输入的点.
 85             PromptPointResult resJigDis = prompts.AcquirePoint(optJig);
 86             Point3d curPt = resJigDis.Value; //获取满足输入选项的输入值
 87 
 88             if (resJigDis.Status == PromptStatus.Cancel)
 89             {
 90                 return SamplerStatus.Cancel;
 91             }
 92 
 93             if (m_Position != curPt)
 94             {
 95                 if (m_Curve != null) {
 96                     if (m_Curve.Ex_IsCurvePoint(m_Position)) {
 97                         m_Direction = m_Curve.Ex_GetPerpendicularVector(m_Position); //获取垂直向量
 98                     }
 99                 }
100                 // 保存当前点.
101                 m_Position = curPt;
102                 return SamplerStatus.OK;
103             }
104             else
105             {
106                 return SamplerStatus.NoChange;
107             }
108         }
109 
110         protected override bool Update()
111         {
112             ((BlockReference)Entity).Position = m_Position;
113             ((BlockReference)Entity).Rotation = m_Direction.AngleOnPlane(new Plane());
114 
115             return true;
116         }
117     }
118 }

3、测试:创建测试图块TST,执行TSTSnapEntityJig

 1   [CommandMethod("TSTSnapEntityJig")]
 2         public void TSTSnapEntityJig() {
 3             Document doc = Application.DocumentManager.MdiActiveDocument;
 4             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 5             Database db = HostApplicationServices.WorkingDatabase;
 6             DocumentLock Lock = Application.DocumentManager.GetDocument(db).LockDocument(); //非模态模式,修改文件前进行上锁操作
 7                 using (Transaction trans = doc.TransactionManager.StartTransaction())
 8                 {
 9                     BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
10                     BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
11                 string blockName = "TST";
12                 if (bt.Has(blockName))
13                     {
14                         //第一个状态,实现
15                         BlockReference br = new BlockReference(Point3d.Origin, bt[blockName]);
16                     SnapEntityJig blockJig = new SnapEntityJig(br);
17                         PromptResult resJig = ed.Drag(blockJig);
18                         if (resJig.Status == PromptStatus.Cancel)
19                         {
20                             blockJig.StopSnap();
21                             return;
22                         }
23                         if (resJig.Status == PromptStatus.OK)
24                         {
25                             blockJig.StopSnap();
26                         }
27                     }
28                     trans.Commit();
29                 }
30             Lock.Dispose();
31         }

 

标签:Jig,AutoCAD,自定义,Autodesk,选择对象,BaseLibrary,System,context,using
来源: https://www.cnblogs.com/chenshuangjian/p/15958257.html