当前位置: 首页 > article >正文

用户坐标系(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();
                }
                
            }
        }
    }
    

    插件代写↓↓↓


http://www.kler.cn/a/553081.html

相关文章:

  • 【AI工程实践】阅文集团:NLP在网络文学领域的应用
  • Java Spring boot 篇:常用注解
  • 数智驱动:医学编程与建模技术在智慧医院AI建设中的创新与变革
  • floodfill算法系列一>衣橱整理
  • 4.7 模型训练基类Trainer:Hugging Face工业级训练引擎深度剖析
  • 安装mmdet3d报错【fatal error: spconv/maxpool.h: No such file or directory】
  • `fi` 是 Bash 脚本中用来结束 `if` 条件语句块的关键字
  • firefox升级后如何恢复收藏夹和密码的问题
  • SPO(Self-Supervised Prompt Optimization)自我监督Prompt提示优化的全景指南
  • 机器人路径规划 | 基于极光PLO优化算法的机器人三维路径规划Matlab代码
  • 【嵌入式Linux应用开发基础】特殊进程
  • 机试刷题_矩阵的最小路径和【python】
  • 7.【线性代数】——求解Ax=0,主列和自由列
  • Spring Cloud环境搭建
  • 【数据结构】队列(Queue)
  • 前端需要学习 Docker 吗?
  • 【Elasticsearch】近实时搜索与刷新机制
  • Dockerfile制作镜像示例 X86版本
  • Unity长按按钮多次升级
  • java数据结构_优先级队列(堆)_6.2