public void autoca() { var doc = Application.DocumentManager.MdiActiveDocument; var db = HostApplicationServices.WorkingDatabase; var ed = doc.Editor; try { var per1 = ed.GetEntity("选择测试设备:\n"); if (per1.Status != PromptStatus.OK) { return; } using (var tr = doc.TransactionManager.StartTransaction()) { var bt = (BlockTable)(tr.GetObject(db.BlockTableId, OpenMode.ForRead)); var btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); var ent1 = tr.GetObject(per1.ObjectId, OpenMode.ForRead) as Entity; if (ent1 == null) { return; } if (ent1 is Polyline) { var pp = ent1 as Polyline; ed.WriteMessage(pp.Closed); ed.WriteMessage("\n"); } else if (ent1 is BlockReference) { var rr = ent1.Clone() as BlockReference; if (rr != null) { // 记录当前角度 var rolat = rr.Rotation; // 把角度设置为0 rr.Rotation = 0; // 得到角度为0的实体范围 var ext = rr.GeometricExtents; // 生成一个测试矩形,颜色为黄色 var pl = new Polyline(); pl.CreateRectangle(new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), new Point2d(ext.MinPoint.X, ext.MinPoint.Y)); pl.ColorIndex = 2; // 旋转实体范围 Matrix3d mt = Matrix3d.Rotation(rolat, Vector3d.ZAxis, GeTools.MidPoint(ext.MaxPoint, ext.MinPoint)); pl.TransformBy(mt); // 加入到模型空间 btr.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); } } tr.Commit(); } } catch (Exception ex) { ed.WriteMessageWithReturn(ex); } }
public static void TileContour() { AcadLib.CommandStart.Start((doc) => { Database db = doc.Database; Editor ed = doc.Editor; // Jig отрисовки полилинии по задаваемым точкам. TileLineJig jigTest = new TileLineJig(); PromptResult jigRes; bool status = true; do { jigRes = ed.Drag(jigTest); if (jigRes.Status == PromptStatus.OK) { // Добавление указанной точки jigTest.AddNewPromptPoint(); } else if (jigRes.Status == PromptStatus.Cancel || jigRes.Status == PromptStatus.Error) { return; } else if (jigRes.Status == PromptStatus.Other) { status = false; } } while (status); // Добавление полилинии в чертеж. using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline(); pl.SetDatabaseDefaults(); for (int i = 0; i < jigTest.AllVertex.Count; i++) { Point3d pt3d = jigTest.AllVertex[i]; Point2d pt2d = new Point2d(pt3d.X, pt3d.Y); pl.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid); } pl.TransformBy(jigTest.UCS); btr.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); tr.Commit(); } }); }
protected override SamplerStatus Sampler(JigPrompts prompts) { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; Matrix3d mt = ed.CurrentUserCoordinateSystem; JigPromptPointOptions optJigPoint = new JigPromptPointOptions("\n请指定矩形的另一角点") { Cursor = CursorType.Crosshair, UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted, BasePoint = m_Pt1.TransformBy(mt), UseBasePoint = true }; PromptPointResult resJigPoint = prompts.AcquirePoint(optJigPoint); Point3d tempPt = resJigPoint.Value; if (resJigPoint.Status == PromptStatus.Cancel) { return(SamplerStatus.Cancel); } if (m_Pt2 != tempPt) { m_Pt2 = tempPt; Point3d ucsPt2 = m_Pt2.TransformBy(mt.Inverse()); m_Polyline.Normal = Vector3d.ZAxis; m_Polyline.Elevation = 0.0; m_Polyline.SetPointAt(0, new Point2d(m_Pt1.X, m_Pt1.Y)); m_Polyline.SetPointAt(1, new Point2d(ucsPt2.X, m_Pt1.Y)); m_Polyline.SetPointAt(2, new Point2d(ucsPt2.X, ucsPt2.Y)); m_Polyline.SetPointAt(3, new Point2d(m_Pt1.X, ucsPt2.Y)); m_Polyline.TransformBy(mt); Point3d cenPt = GeTools.MidPoint(m_Pt1.TransformBy(mt), m_Pt2); Point3d majorPt = new Point3d(ucsPt2.X, m_Pt1.Y, 0); Vector3d vecX = GeTools.MidPoint(majorPt, ucsPt2).TransformBy(mt) - cenPt; try { if (Math.Abs(ucsPt2.X - m_Pt1.X) < 0.0000001 | Math.Abs(ucsPt2.Y - m_Pt1.Y) < 0.000001) { m_Ellipse = new Ellipse(Point3d.Origin, Vector3d.ZAxis, new Vector3d(0.00000001, 0, 0), 1.0, 0.0, 0.0); } else { double radiusRatio = Math.Abs((ucsPt2.X - m_Pt1.X) / (ucsPt2.Y - m_Pt1.Y)); if (radiusRatio < 1.0) { majorPt = new Point3d(m_Pt1.X, ucsPt2.Y, 0.0); vecX = GeTools.MidPoint(majorPt, ucsPt2).TransformBy(mt) - cenPt; } else { radiusRatio = 1.0 / radiusRatio; } m_Ellipse.Set(cenPt, mt.CoordinateSystem3d.Zaxis, vecX, radiusRatio, 0, 2 * Math.PI); } } catch { } return(SamplerStatus.OK); } else { return(SamplerStatus.NoChange); } }