.NET

Reply
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 1 of 12 (276 Views)
Accepted Solution

Creating UCS and making it active.

276 Views, 11 Replies
08-29-2013 12:36 PM

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

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

 

Brilliant!!! Thank you.

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 2 of 12 (250 Views)

Re: Creating UCS and making it active.

08-30-2013 01:15 AM 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
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 3 of 12 (234 Views)

Re: Creating UCS and making it active.

08-30-2013 09:07 AM 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

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 4 of 12 (213 Views)

Re: Creating UCS and making it active.

08-30-2013 03:08 PM 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
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 5 of 12 (210 Views)

Re: Creating UCS and making it active.

08-30-2013 03:12 PM 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

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 6 of 12 (202 Views)

Re: Creating UCS and making it active.

08-30-2013 06:03 PM 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
*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 7 of 12 (193 Views)

Re: Creating UCS and making it active.

08-31-2013 12:29 AM 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
*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 8 of 12 (186 Views)

Re: Creating UCS and making it active.

08-31-2013 04:28 AM 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
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 9 of 12 (177 Views)

Re: Creating UCS and making it active.

08-31-2013 12:12 PM 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

Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 10 of 12 (130 Views)

Re: Creating UCS and making it active.

09-11-2013 01:40 AM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.