|
| 1 | +using Autodesk.AutoCAD.ApplicationServices.Core; |
| 2 | +using Autodesk.AutoCAD.DatabaseServices; |
| 3 | +using Autodesk.AutoCAD.EditorInput; |
| 4 | +using Autodesk.AutoCAD.Geometry; |
| 5 | +using Autodesk.AutoCAD.Runtime; |
| 6 | + |
| 7 | +namespace Test; |
| 8 | + |
| 9 | +public class RectangleFromTwoPoints |
| 10 | +{ |
| 11 | + [CommandMethod("Rect2Pts")] |
| 12 | + public static void CreateRectangleFromTwoCorners() |
| 13 | + { |
| 14 | + var doc = Application.DocumentManager.MdiActiveDocument; |
| 15 | + var db = doc.Database; |
| 16 | + var ed = doc.Editor; |
| 17 | + |
| 18 | + var p1Res = ed.GetPoint("\nPick first corner:"); |
| 19 | + if (p1Res.Status != PromptStatus.OK) return; |
| 20 | + var p2Res = ed.GetCorner(new PromptCornerOptions("\nPick opposite corner:", p1Res.Value)); |
| 21 | + if (p2Res.Status != PromptStatus.OK) return; |
| 22 | + |
| 23 | + var p1 = p1Res.Value; |
| 24 | + var p2 = p2Res.Value; |
| 25 | + |
| 26 | + // Compute other corners (axis-aligned) |
| 27 | + var p3 = new Point3d(p1.X, p2.Y, p1.Z); |
| 28 | + var p4 = new Point3d(p2.X, p1.Y, p1.Z); |
| 29 | + |
| 30 | + using (var tr = db.TransactionManager.StartTransaction()) |
| 31 | + { |
| 32 | + var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); |
| 33 | + var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); |
| 34 | + |
| 35 | + var pl = new Polyline { Closed = true }; |
| 36 | + pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0); |
| 37 | + pl.AddVertexAt(1, new Point2d(p3.X, p3.Y), 0, 0, 0); |
| 38 | + pl.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0); |
| 39 | + pl.AddVertexAt(3, new Point2d(p4.X, p4.Y), 0, 0, 0); |
| 40 | + |
| 41 | + ms.AppendEntity(pl); |
| 42 | + tr.AddNewlyCreatedDBObject(pl, true); |
| 43 | + tr.Commit(); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments