欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 用户坐标系(ucs)与系统坐标系(wcs)的转换详解——CAD c#二次开发

用户坐标系(ucs)与系统坐标系(wcs)的转换详解——CAD c#二次开发

2025/2/25 13:58:48 来源:https://blog.csdn.net/yongshiqq/article/details/145721361  浏览:    关键词:用户坐标系(ucs)与系统坐标系(wcs)的转换详解——CAD c#二次开发

如果不进行用户坐标系(UCS)到世界坐标系(WCS)的转换,直接将用户输入的点写入模型空间,生成的直线的坐标将基于用户坐标系(UCS)。这可能会导致以下情况:

  1. UCS与WCS一致时

    • 如果当前用户坐标系(UCS)与世界坐标系(WCS)完全一致(即没有旋转、平移或缩放),那么不转换坐标系也不会影响结果,生成的直线坐标是正确的。

  2. UCS与WCS不一致时

    • 如果用户坐标系(UCS)与世界坐标系(WCS)不一致(例如,UCS被旋转、平移或缩放),那么直接使用用户输入的点生成的直线将基于UCS,而不是WCS。这会导致直线的位置和方向与预期不符。

假设当前UCS被旋转了90度,用户输入的两个点在UCS中分别是 (0, 0) 和 (10, 0)。如果不进行转换,生成的直线在WCS中的实际坐标可能是 (0, 0) 和 (0, 10),而不是用户预期的 (0, 0) 和 (10, 0)

  • 是否需要转换坐标系:取决于当前UCS是否与WCS一致。如果不确定,建议始终进行转换,以确保生成的几何体在WCS中是正确的。

  • 如何判断UCS与WCS是否一致:可以通过检查ed.CurrentUserCoordinateSystem是否为单位矩阵(Matrix3d.Identity)来判断。如果ed.CurrentUserCoordinateSystem == Matrix3d.Identity,则UCS与WCS一致,无需转换。

演示效果:

 

  • 附代码:

  • [assembly: CommandClass(typeof(IfoxDemo.用户坐标系转换))]//只允许此类快捷键命令
    namespace IfoxDemo
    {public class 用户坐标系转换{[CommandMethod("xx")]public void Demo(){Document doc = Autodesk .AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;Editor ed = doc.Editor;if (ed.CurrentUserCoordinateSystem == Matrix3d.Identity){ed.WriteMessage("\n当前UCS与WCS一致,无需转换。");}else{ed.WriteMessage("\n当前UCS与WCS不一致,建议进行转换。");}"zbx".Print();CreateLineUCSToWcs();CreateLineUCS();}public void CreateLineUCSToWcs(){// 获取当前文档和编辑器Document doc = Application.DocumentManager.MdiActiveDocument;Editor ed = doc.Editor;// 提示用户指定第一个点PromptPointResult ppr1 = ed.GetPoint("\n指定第一个点: ");if (ppr1.Status != PromptStatus.OK)return;// 提示用户指定第二个点PromptPointResult ppr2 = ed.GetPoint("\n指定第二个点: ");if (ppr2.Status != PromptStatus.OK)return;// 将UCS坐标转换为WCS坐标Point3d point1 = ppr1.Value.TransformBy(ed.CurrentUserCoordinateSystem);Point3d point2 = ppr2.Value.TransformBy(ed.CurrentUserCoordinateSystem);// 创建直线using (Transaction tr = doc.TransactionManager.StartTransaction()){// 打开块表BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;// 打开块表记录BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;// 创建直线对象Line line = new Line(point1, point2);// 将直线添加到块表记录中btr.AppendEntity(line);tr.AddNewlyCreatedDBObject(line, true);// 提交事务tr.Commit();}}public void CreateLineUCS(){// 获取当前文档和编辑器Document doc = Application.DocumentManager.MdiActiveDocument;Editor ed = doc.Editor;// 提示用户指定第一个点PromptPointResult ppr1 = ed.GetPoint("\n指定第一个点: ");if (ppr1.Status != PromptStatus.OK)return;// 提示用户指定第二个点PromptPointResult ppr2 = ed.GetPoint("\n指定第二个点: ");if (ppr2.Status != PromptStatus.OK)return;// 将UCS坐标转换为WCS坐标Point3d point1 = ppr1.Value;Point3d point2 = ppr2.Value;// 创建直线using (Transaction tr = doc.TransactionManager.StartTransaction()){// 打开块表BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;// 打开块表记录BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;// 创建直线对象Line line = new Line(point1, point2);line.ColorIndex = 1;// 将直线添加到块表记录中btr.AppendEntity(line);tr.AddNewlyCreatedDBObject(line, true);// 提交事务tr.Commit();}}}
    }
    

    插件代写↓↓↓

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词