哇。。。
这真是太好了...非常感谢这个伟大的链接,一个伟大的工作!
我没有找到这个;我谷歌DCStoWCS而不是DCS2WCS...
这是工作代码
- public void RectangleAtScreen()
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- Point3d LL, UR;
- if (!GetRectangle(out LL, out UR))
- ed.WriteMessage("*Cancel*");
- else
- {
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // Start a transaction
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- Matrix3d ToWCS = ed.UCS2WCS();
- Matrix3d ToDCS = ed.WCS2DCS();
- LL = LL.TransformBy(ToWCS).TransformBy(ToDCS);
- UR = UR.TransformBy(ToWCS).TransformBy(ToDCS);
- ToWCS = ed.DCS2WCS();
- using (Polyline pl = new Polyline())
- {
- pl.SetDatabaseDefaults();
- pl.ColorIndex = 2;
- pl.Closed = true;
- var LL2d = LL.Convert2d();
- var UR2d = UR.Convert2d();
- pl.AddVertexAt(0, LL2d, 0, 0, 0);
- pl.AddVertexAt(1, new Point2d(UR2d.X, LL2d.Y), 0, 0, 0);
- pl.AddVertexAt(2, UR2d, 0, 0, 0);
- pl.AddVertexAt(3, new Point2d(LL2d.X, UR2d.Y), 0, 0, 0);
- pl.TransformBy(ToWCS);
- acBlkTblRec.AppendEntity(pl);
- acTrans.AddNewlyCreatedDBObject(pl, true);
- acTrans.Commit();
- }
- }
- }
- }
- private static bool GetRectangle(out Point3d first, out Point3d second)
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- first = default(Point3d);
- second = default(Point3d);
- try
- {
- PromptPointResult ppt = ed.GetPoint("Pick Lower-Left Corner");
- if (ppt.Status == PromptStatus.OK)
- {
- first = ppt.Value;
- var pco = new PromptCornerOptions("Pick Upper-Right Corner", first);
- pco.UseDashedLine = true;
- ppt = ed.GetCorner(pco);
- if (ppt.Status == PromptStatus.OK)
- {
- second = ppt.Value;
- return true;
- }
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(ex.ToString());
- }
- return false;
- }
问候reltro |