|
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //https://www.eabim.net EaBIM论坛
- /////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- [assembly: CommandClass(typeof(Sample.PolylineJigSample))]
- namespace Sample
- {
- class PolylineJigSample
- {
- [CommandMethod("PolylineJig")]
- public void PolylineJig()
- {
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
- PromptPointOptions opts = new PromptPointOptions("\n选择起点:");
- PromptPointResult res = ed.GetPoint(opts);
- if (res.Status != PromptStatus.OK)
- return;
- Point3dCollection pts = new Point3dCollection();
- pts.Add(res.Value);
- PolylineJig jig = new PolylineJig(pts);
- int pcount = pts.Count;
- ed.Drag(jig);
- while (jig.Status == 1)
- {
- pts = new Point3dCollection();
- for (int i = 0; i <= pcount; i++)
- {
- pts.Add(((Polyline)jig.GetEntity()).GetPoint3dAt(i));
- }
- jig = new PolylineJig(pts);
- pcount += 1;
- ed.Drag(jig);
- }
- if (jig.Status == 0)
- return;
- ToModelSpace(jig.GetEntity());
- }
- /// <summary>
- /// 添加对象到模型空间
- /// </summary>
- /// <param name="ent">要添加的对象</param>
- /// <returns></returns>
- public static ObjectId ToModelSpace(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entId;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return entId;
- }
- }
- public class PolylineJig : EntityJig
- {
- Point3d endPoint, startPoint;
- int status = 1;
- int pcount = 0;
- public PolylineJig(Point3dCollection p3ds)
- : base(new Autodesk.AutoCAD.DatabaseServices.Polyline())
- {
- for (int i = 0; i < p3ds.Count; i++)
- {
- ((Autodesk.AutoCAD.DatabaseServices.Polyline)Entity).AddVertexAt(i, new Point2d(p3ds.X, p3ds.Y), 0, 0, 0);
- }
- ((Autodesk.AutoCAD.DatabaseServices.Polyline)Entity).AddVertexAt(p3ds.Count, new Point2d(p3ds[p3ds.Count - 1].X, p3ds[p3ds.Count - 1].Y), 0, 0, 0);
- pcount = p3ds.Count;
- endPoint = p3ds[p3ds.Count - 1];
- startPoint = endPoint;
- }
- protected override bool Update()
- {
- try
- {
- ((Autodesk.AutoCAD.DatabaseServices.Polyline)Entity).SetPointAt(pcount, new Point2d(endPoint.X, endPoint.Y));
- }
- catch (System.Exception ex)
- {
- return false;
- }
- return true;
- }
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- try
- {
- JigPromptPointOptions jigopts = new JigPromptPointOptions();
- jigopts.UserInputControls = UserInputControls.NullResponseAccepted;
- jigopts.BasePoint = startPoint;
- jigopts.UseBasePoint = true;
- jigopts.Message = "\n选择下一个顶点:";
- PromptPointResult dres = prompts.AcquirePoint(jigopts);
- if (dres.Value != endPoint)
- {
- endPoint = dres.Value;
- }
- else
- {
- return SamplerStatus.NoChange;
- }
- if (dres.Status == PromptStatus.Cancel || dres.Status == PromptStatus.Error)
- {
- status = 0;
- return SamplerStatus.Cancel;
- }
- else if (dres.Status == PromptStatus.None)
- {
- status = 2;
- return SamplerStatus.Cancel;
- }
- else
- {
- return SamplerStatus.OK;
- }
- }
- catch (System.Exception ex)
- {
- return SamplerStatus.OK;
- }
- }
- public int Status { get { return status; } }
- public Entity GetEntity()
- {
- return Entity;
- }
- }
- }
复制代码 |
|