|
下面代码演示如何通过Auto CAD的.Net API中的GetObjectSnapPoints函数获取对象上的捕捉点:
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- namespace SnapPoints
- {
- public class Class1
- {
- [CommandMethod("spnt")]
- public void GetSnapPnts()
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptEntityOptions opt = new PromptEntityOptions("\n选择实体:");
- opt.SetRejectMessage("\n不接受的实体类型,请重新选择:");
- opt.AddAllowedClass(typeof(Line), true);
- opt.AddAllowedClass(typeof(Circle), true);
- opt.AddAllowedClass(typeof(Arc), true);
- opt.AddAllowedClass(typeof(Ellipse), true);
- opt.AddAllowedClass(typeof(BlockReference), true);
- PromptEntityResult EntRes = ed.GetEntity(opt);
- if (EntRes.Status != PromptStatus.OK)
- { return; }
- PromptPointOptions PntOpt = new PromptPointOptions("\n选择参考点:");
- PromptPointResult PntRes;
- PntRes = ed.GetPoint(PntOpt);
- if (PntRes.Status == PromptStatus.OK)
- {
- using (DocumentLock docLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
-
- BlockTable bt;
- bt = trans.GetObject(db.BlockTableId,
- OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr;
- btr = trans.GetObject(bt[BlockTableRecord.ModelSpace],
- OpenMode.ForWrite) as BlockTableRecord;
- Entity ent = (Entity)trans.GetObject(EntRes.ObjectId, OpenMode.ForRead);
- Point3d pickPnt = (Point3d)PntRes.Value;
- ed.WriteMessage("\n参考点:" + pickPnt.ToString());
-
- Point3dCollection snapPnts = new Point3dCollection();
- IntegerCollection geomIds = new IntegerCollection(1);
- geomIds.Add(0);
- int[] ModeNums = { 1, 2, 3, 4, 5, 7, 8, 9, 10 };
- foreach (int j in ModeNums)
- {
- try
- {
- ObjectSnapModes objSnapMode = (ObjectSnapModes)Enum.Parse(typeof(ObjectSnapModes), j.ToString());
- snapPnts.Clear();
- ent.GetObjectSnapPoints(objSnapMode, 0, pickPnt, pickPnt, Matrix3d.Identity, snapPnts, geomIds);
- if (snapPnts.Count>0)
- {
- for (int i = 0; i < snapPnts.Count; i++)
- {
- Line Lobj = new Line(pickPnt, snapPnts);
- btr.AppendEntity(Lobj);
- trans.AddNewlyCreatedDBObject(Lobj,true);
- ed.WriteMessage("\n" + objSnapMode.ToString() + ": " + (i + 1).ToString() + "# " + snapPnts.ToString());
- }
-
- }
- else
- {
- ed.WriteMessage("\n" + objSnapMode.ToString() + ": None ");
- }
- }
- catch { }
- }
- trans.Commit();
- }
- }
- }
- }
- }
- }
复制代码 |
|