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);
//...
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Mr Spider,
"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."
When we pick a point, is the result in WCS or UCS? And if we transform by the UCS does that mean we are going from the WCS to UCS or the other way around: the UCS to WCS?
rgds
Ben
Posted by: BKSpurgeon | 12/13/2016 at 06:07 PM
Ben, it's really a good question. The Editor.GetPoint() method always returns a point in UCS. The Editor.CurrentUserCoordinateSystem has the transform matrix from the current UCS to WCS. If the picked point has to participate into some geometry creation or calculation, it has to be transformed from its UCS to WCS since all AutoCAD geometry information is stored in WCS.
Posted by: Spiderinnet1 | 12/13/2016 at 07:07 PM
ok very good Mr Spider.
What if I create a point and add it to the model space: new Point3d(0,0,0) ? Will this mean that the point will go to the WCS coordinate system or the UCS? According to your above answer I think it will go to the WCS system? rgds, Ben
Posted by: BKSpurgeon | 12/13/2016 at 08:07 PM
Ben, you are right. Any points you create will be expressed in WCS. If you'd like to draw them onto the current UCS, the inversion of the Editor.CurrentUserCoordinateSystem can be used to transform them.
Posted by: Spiderinnet1 | 12/13/2016 at 08:19 PM