CAD开发 UCS转WCS(用户坐标转为世界坐标)

在CAD添加实体到模型空间中都是用世界坐标的点添加的。
UCS坐标是用户GetPoint()这种交互集获取得到用户坐标。
关于UCS转WCS如下:

AutoCAD .NET: Transform Picked Point from Current UCS to WCS
We are addressing a very simple task regarding AutoCAD .NET programming in this article. How to transform a picked point from the current (active) UCS to the WCS?

It sounds too obvious to metion.  That was also what we thought about it, and it explains why the topic was not covered explicitly before. However, judging from some code on web, it apparently is not the case. So, let’s take a few seconds here to have a bit review about it.

//...
 
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
 
PromptPointResult ppr = ed.GetPoint("\nSpecify base point: ");
if (ppr.Status != PromptStatus.OK)
    return;
 
Point3d baseUcs = ppr.Value;
CoordinateSystem3d cs = ucs.CoordinateSystem3d;
 
// Transform from UCS to WCS
Matrix3d mat =
    Matrix3d.AlignCoordinateSystem(
    Point3d.Origin,
    Vector3d.XAxis,
    Vector3d.YAxis,
    Vector3d.ZAxis,
    cs.Origin,
    cs.Xaxis,
    cs.Yaxis,
    cs.Zaxis
    );
 
Point3d baseWcs = baseUcs.TransformBy(mat);
Point3d curPt = baseWcs;
 
//...

Checking the code there more times, got more confused about why the simple task had to be done that way, which is redundant, inefficient, and error prone. In fact, as demonstrated hundreds of times before in our posts, a single line of code can transform the picked point, ppr.Value, from the current UCS to the WCS so easily and reliably.

//...
 
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
 
PromptPointResult ppr = ed.GetPoint("\nSpecify base point: ");
if (ppr.Status != PromptStatus.OK)
    return;
 
Point3d curPt = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
 
//...

WCS转UCS只需

Point3d UCSPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());

参考链接:https://spiderinnet1.typepad.com/blog/2013/05/autocad-net-transform-picked-point-from-current-ucs-to-wcs.html

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值