Hi everyone, I am trying to create a new UCS and then making it active. The new UCS coordinate is
Origin(0,0,0)
X direction(1.8963581, 0.36861467, 0.51763809)
Y direction(-0.190809, 0.98162718, 0.0)
Any help would be appreciated,
Thank you,
-J
Solved! Go to Solution.
Solved by junoj. Go to Solution.
Solved by _gile. Go to Solution.
One way of setting UCS with information provided
You can get active viewport from from
Editor.ActiveViewportId Property
then use the overloaded version of SetUcs which creates a unnamed UCS
AbstractViewTableRecord.SetUcs(Point3d, Vector3d, Vector3d) Method
and then update it
Editor.UpdateTiledViewportsFromDatabase() Method
Example:
[CommandMethod("FooUcs")] public void FooUcs() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction trx = db.TransactionManager.StartTransaction()) { ViewportTableRecord vtr = (ViewportTableRecord)ed.ActiveViewportId.GetObject(OpenMode.ForRead); vtr.SetUcs(Point3d.Origin, new Vector3d(1.8963581, 0.36861467, 0.51763809), new Vector3d(-0.190809, 0.98162718, 0.0)); ed.UpdateTiledViewportsFromDatabase(); trx.Commit(); } }
Thank you Jeff for your replay.
When I run you code I get an error: eBadUCS. I have attached a picture of the error
Using 2013 maybe someone else with 2011 can try, but if you not get an answer would load up drawing because blank drawings can be very different with the 1,000 different variables for AutoCAD.
And to prove my point I just set UCSFOLLOW to 1 and got same error.
Try again with UCSFOLLOW set to 0 to see if it works and then might need to create named or anonymous UcsTableRecord then use overloaded SetUCS version that takes the objectid of UcsTableRecord.
Someone else might have a better way and I might be steering you in wrong direction.
Hi,
It seems to me the vectors passed to the SetUcs() method aren't strictly perpendicular.
By my side, I'd calculate the vectors from the x one for more accuracy and get their normal (unit vector) before using them to create a Coordinate System.
Jeff's method
[CommandMethod("FooUcs")] public void FooUcs() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; Vector3d xaxis = new Vector3d(1.8963581, 0.36861467, 0.51763809); Vector3d yaxis = new Vector3d(-0.190809, 0.98162718, 0.0); xaxis = xaxis.GetNormal(); Vector3d zaxis = xaxis.CrossProduct(yaxis).GetNormal(); yaxis = zaxis.CrossProduct(xaxis).GetNormal(); using (Transaction trx = db.TransactionManager.StartTransaction()) { ViewportTableRecord vtr = (ViewportTableRecord)ed.ActiveViewportId.GetObject(OpenMode.ForRead); vtr.SetUcs(Point3d.Origin, xaxis, yaxis); ed.UpdateTiledViewportsFromDatabase(); trx.Commit(); } }
Another one:
[CommandMethod("BarUcs", CommandFlags.Modal)] public void BarUcs() { Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor; Vector3d xaxis = new Vector3d(1.8963581, 0.36861467, 0.51763809); Vector3d yaxis = new Vector3d(-0.190809, 0.98162718, 0.0); xaxis = xaxis.GetNormal(); Vector3d zaxis = xaxis.CrossProduct(yaxis).GetNormal(); yaxis = zaxis.CrossProduct(xaxis).GetNormal(); ed.CurrentUserCoordinateSystem = Matrix3d.AlignCoordinateSystem( Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, Point3d.Origin, xaxis, yaxis, zaxis); }
The calls to GetNormal() method does not seem mandatory.
[CommandMethod("FooUcs")] public void FooUcs() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; Vector3d xaxis = new Vector3d(1.8963581, 0.36861467, 0.51763809); Vector3d yaxis = new Vector3d(-0.190809, 0.98162718, 0.0); Vector3d zaxis = xaxis.CrossProduct(yaxis); yaxis = zaxis.CrossProduct(xaxis); using (Transaction trx = db.TransactionManager.StartTransaction()) { ViewportTableRecord vtr = (ViewportTableRecord)ed.ActiveViewportId.GetObject(OpenMode.ForRead); vtr.SetUcs(Point3d.Origin, xaxis, yaxis); ed.UpdateTiledViewportsFromDatabase(); trx.Commit(); } }
[CommandMethod("BarUcs", CommandFlags.Modal)] public void BarUcs() { Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor; Vector3d xaxis = new Vector3d(1.8963581, 0.36861467, 0.51763809); Vector3d yaxis = new Vector3d(-0.190809, 0.98162718, 0.0); Vector3d zaxis = xaxis.CrossProduct(yaxis); yaxis = zaxis.CrossProduct(xaxis); ed.CurrentUserCoordinateSystem = Matrix3d.AlignCoordinateSystem( Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, Point3d.Origin, xaxis, yaxis, zaxis); }
Thank you Jeff and Gilles for all the help, I have learned a lot from this post.
The CrossProduct was the answer !!!!!
It has been some time the last time I used vectors . . . I had to consult with KhanAcademy. I am still trying to understand what will ‘ .GetNormal()’ function actually return vs. CrossProduct ?
Thank you,
-J
Hi Gilles,
You “BarUcs” works to change the orientation of the UCS very nicely. But for the life if me I cannot understand how to move the usc.
Do I have to provide the matrix origin or is it possible to change the origin after the fact (after the ucs has been aligned )
Thank you.
-Jay
Hi, you can directly specify the new origin in the AlignCoordinateSystem arguments:
[CommandMethod("BarUcs", CommandFlags.Modal)] public void BarUcs() { Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor; Vector3d xaxis = new Vector3d(1.8963581, 0.36861467, 0.51763809); Vector3d yaxis = new Vector3d(-0.190809, 0.98162718, 0.0); Vector3d zaxis = xaxis.CrossProduct(yaxis); yaxis = zaxis.CrossProduct(xaxis); ed.CurrentUserCoordinateSystem = Matrix3d.AlignCoordinateSystem( Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, theNewOrigin, xaxis, yaxis, zaxis); }