谢谢,这对我帮助很大
如果你想用另一种方式来做:从模型空间到纸上空间,这里是我提出的一个小测试项目。我希望它能帮助别人
关键位有以下几种方法:
- Matrix3d m = ed.WCS2DCS();
- Matrix3d m2 = ed.DCS2PSDCS();
首先我们需要从WCS到DCS,然后从DCS到PSDC。它们是使用Gilles几何扩展库中提供的方便扩展方法完成的。我会将它们粘贴在这里,以便于您参考-但请务必获取原件及其相关注释:
-
- public static Matrix3d WCS2DCS(this Editor ed)
- {
- return ed.DCS2WCS().Inverse();
- }
- public static Matrix3d DCS2PSDCS(this Editor ed)
- {
- Database db = ed.Document.Database;
- if (db.TileMode)
- throw new AcRx.Exception(AcRx.ErrorStatus.NotInPaperspace);
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Viewport vp =
- (Viewport)tr.GetObject(ed.CurrentViewportObjectId, OpenMode.ForRead);
- if (vp.Number == 1)
- {
- try
- {
- ed.SwitchToModelSpace();
- vp = (Viewport)tr.GetObject(ed.CurrentViewportObjectId, OpenMode.ForRead);
- ed.SwitchToPaperSpace();
- }
- catch
- {
- throw new AcRx.Exception(AcRx.ErrorStatus.CannotChangeActiveViewport);
- }
- }
- return vp.DCS2PSDCS();
- }
- }
说明:打开CAD文件。下载DLL。考虑到视口UCS的定义不同,应在图纸空间中在模型空间中拾取的几何图形上绘制圆
- [CommandMethod("ViewPortTest")]
- public static void ViewPortTest()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord ms = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- Line line = new Line();
- line.StartPoint = new Point3d(3167, 448, 0);
- line.EndPoint = new Point3d(3167, 594, 0);
- ms.AppendEntity(line);
- tr.AddNewlyCreatedDBObject(line, true);
- tr.Commit();
- }
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord ms = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- RXClass rxLine = RXClass.GetClass(typeof(Line));
- Line line = ms.Cast().Where(id => id.ObjectClass == rxLine).Select(id => tr.GetObject(id, OpenMode.ForRead) as Line).First();
- Point3d startPoint = line.StartPoint;
- Point3d endPoint = line.EndPoint;
- //// When working with paper space
- /// PSDCS to DCS
- /// DCS to WCS
- Matrix3d m = ed.WCS2DCS();
- Matrix3d m2 = ed.DCS2PSDCS();
- Point3d newStart = startPoint.TransformBy(m).TransformBy(m2);
- Point3d newEnd = endPoint.TransformBy(m).TransformBy(m2);
- ed.SwitchToPaperSpace();
- BlockTableRecord ps = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- using (Circle circle = new Circle(newStart, Vector3d.ZAxis, 20))
- {
- ps.AppendEntity(circle);
- tr.AddNewlyCreatedDBObject(circle, true);
- }
- using (Circle circle = new Circle(newEnd, Vector3d.ZAxis, 20))
- {
- ps.AppendEntity(circle);
- tr.AddNewlyCreatedDBObject(circle, true);
- }
- tr.Commit();
- }
- }
|