网上太烂不能点回复,刚刚试了一下,还是不可以。我的这个功能是通过jig实现:画一条线,并在和颜色为8的对象的交点上各画一条水平线。一下是全部代码,还望高手再帮忙看一下。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- using DNA;
- namespace ToCAD
- {
- public class DC_tongji_Draw_Jig: DrawJig
- {
- static Point3d startPoint, endPoint;
- static List<Line> lines = new List<Line>();
- static List<Point3d> points = new List<Point3d>();
- protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
- {
- foreach (Line line in lines)
- {
- draw.Geometry.Draw(line);
- }
- return true;
- }
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- JigPromptPointOptions optJig = new JigPromptPointOptions("\n请指定第二点");
- optJig.UseBasePoint = true;
- optJig.BasePoint = startPoint;
- optJig.UserInputControls = UserInputControls.NullResponseAccepted;
- PromptPointResult resJigDis = prompts.AcquirePoint(optJig);
- Point3d tempPt = resJigDis.Value;
- if (resJigDis.Status == PromptStatus.Cancel)
- {
- return SamplerStatus.Cancel;
- }
- if (endPoint != tempPt)
- {
- endPoint = tempPt;
- //画水平线
- lines[0].StartPoint = startPoint;
- lines[0].EndPoint = endPoint;
- points = getinpoint(lines[0]);
- for (int i = 1; i < points.Count; i++)
- lines[i] = new Line(startPoint, startPoint);
- for (int i = 1; i < points.Count; i++)
- {
- Point3d pt1 = new Point3d(points[i - 1].X - 2, points[i - 1].Y, 0);
- Point3d pt2 = new Point3d(points[i - 1].X + 2, points[i - 1].Y, 0);
- lines[i].StartPoint = pt1;
- lines[i].EndPoint = pt2;
- }
- return SamplerStatus.OK;
- }
- else
- {
- return SamplerStatus.NoChange;
- }
- }
- private List<Point3d> getinpoint(Line line)
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Entity BlockEntity=null;
- Point3dCollection pts = new Point3dCollection();
- List<Point3d> retpoint = new List<Point3d>();
- TypedValue filterValue1 = new TypedValue((int)DxfCode.Color, "8");
- TypedValue[] filterValues1 = {filterValue1};
- SelectionFilter filter2 = new SelectionFilter(filterValues1);
- PromptSelectionResult psr1 = ed.SelectAll(filter2);
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- points.Clear();
- foreach (ObjectId BlockId in psr1.Value.GetObjectIds())
- {
- //获取选择集里块里面的对象
- BlockEntity = trans.GetObject(BlockId, OpenMode.ForRead) as Entity;
- if (BlockEntity.GetType() == typeof(Line) || BlockEntity.GetType() == typeof(Polyline) || BlockEntity.GetType() == typeof(Spline))
- {
- line.IntersectWith(BlockEntity, Intersect.OnBothOperands, pts, 0, 0);
- for (int k = 0; k < pts.Count; k++)
- points.Add(pts[k]);
- }
- }
- trans.Commit();
- }
- return retpoint;
- }
- public static bool DrawJig()
- {
- DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument();//文档加锁
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- DC_tongji_Draw_Jig begin = new DC_tongji_Draw_Jig();
- Database db = Application.DocumentManager.MdiActiveDocument.Database;
- PromptPointResult pRes = ed.GetPoint("请选择第一点:");
- startPoint = pRes.Value;
- lines.Clear();
- lines.Add(new Line(startPoint, startPoint));
- points.Clear();
- if (pRes.Status == PromptStatus.OK)
- {
- PromptResult resJig = ed.Drag(begin);
- if (resJig.Status == PromptStatus.OK)
- {
- Tools.AddEntities(lines.ToArray());//Tools.AddEntities为DNA里面的函数(将对象添加到CAD文档中)
- }
- }
- dl.Dispose();
- return true;
- }
- }
- }
|