[CommandMethod("test")] public void test() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Transaction trans = db.TransactionManager.StartTransaction(); BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Line l1 = new Line(new Point3d(100, 400, 0), new Point3d(200, 600, 0)); Line l2 = new Line(new Point3d(150, 400, 0), new Point3d(300, 700, 0)); l1.ColorIndex = 2; l2.ColorIndex = 2; Point3d p1, p2; p1 = l1.GetClosestPointTo(new Point3d(500, 500, 0), true); p2 = l2.GetClosestPointTo(p1, true); Line c = new Line(p1,p2); c.ColorIndex = 1; btr.AppendEntity(l1); trans.AddNewlyCreatedDBObject(l1, true); btr.AppendEntity(l2); trans.AddNewlyCreatedDBObject(l2, true); btr.AppendEntity(c); trans.AddNewlyCreatedDBObject(c, true); trans.Commit(); trans.Dispose(); } |
运行后效果如下,红色线段即是: