.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating UCS and making it active.

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
junoj
860 Views, 11 Replies

Creating UCS and making it active.

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

11 REPLIES 11
Message 2 of 12
jeff
in reply to: junoj

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();
                }
           
        }

 

You can also find your answers @ TheSwamp
Message 3 of 12
junoj
in reply to: jeff

Thank you Jeff for your replay.

 

When I run you code I get an error: eBadUCS. I have attached a picture of the error

Message 4 of 12
jeff
in reply to: junoj

Not sure why,

 

What year are you using and version, and can you load up a empty drawing with same visual settings?

You can also find your answers @ TheSwamp
Message 5 of 12
junoj
in reply to: jeff

I am using:

 

  • Visual Studio 2008
  • AutoCAD 2011
  • OS Win 7 32bit

The drawing I am loading into is just a blank CAD drawing

Message 6 of 12
jeff
in reply to: junoj

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.

 

 

You can also find your answers @ TheSwamp
Message 7 of 12
_gile
in reply to: jeff

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);
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 12
_gile
in reply to: _gile

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);
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 12
junoj
in reply to: _gile

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

Message 10 of 12
junoj
in reply to: junoj

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

Message 11 of 12
_gile
in reply to: junoj

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);
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 12 of 12
junoj
in reply to: _gile

Brilliant!!! Thank you.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost