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

你可能感兴趣的:(CAD二次开发,c#)