Author Topic: Changing UCS without Editor  (Read 3680 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Changing UCS without Editor
« on: April 24, 2012, 04:30:10 PM »
I have a database open via Database.ReadDwg() so the document is not open in the editor.  How do I insert objects into modelspace at a different ucs then world ucs?
Revit 2019, AMEP 2019 64bit Win 10

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Changing UCS without Editor
« Reply #1 on: April 24, 2012, 04:47:37 PM »
Not sure that you can directly do that.  I think it's limited to indirect use, adding the object in WCS coordinates then applying a transform based on the difference between the WCS and desired UCS.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Changing UCS without Editor
« Reply #2 on: April 24, 2012, 05:06:51 PM »
gile and kaefer understand all that smart people stuff with views, transformation, ucs, etc..... so hopefully they will drop in and give you a good answer but are you trying to use one from a Named View?
 
Transform object with View.ViewingMatrix? maybe

kaefer

  • Guest
Re: Changing UCS without Editor
« Reply #3 on: April 24, 2012, 06:39:50 PM »
Transform object with View.ViewingMatrix? maybe

Saved UCS? UcsTable perhaps. Current UCS? I know nothing except that it's stored in the Database and is accessible through the properties Ucsorg, Ucsxdir, Ucsydir, possibly.

Code - C#: [Select]
  1.         public static Matrix3d getUcsMatrix(this Database db)
  2.         {
  3.             Point3d toOrigin = db.Ucsorg;
  4.             Vector3d toXAxis = db.Ucsxdir;
  5.             Vector3d toYAxis = db.Ucsydir;
  6.             Vector3d toZAxis = toXAxis.CrossProduct(toYAxis);
  7.  
  8.             return Matrix3d.AlignCoordinateSystem(
  9.                 Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
  10.                 toOrigin, toXAxis, toYAxis, toZAxis);
  11.         }

BlackBox

  • King Gator
  • Posts: 3770
Re: Changing UCS without Editor
« Reply #4 on: April 24, 2012, 08:56:58 PM »
Thanks Kaefer, someone over at CADTutor asked something similar, and I guessed UcsTableRecord, but it's good to know for sure. Thanks for sharing.
"How we think determines what we do, and what we do determines what we get."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Changing UCS without Editor
« Reply #5 on: April 24, 2012, 09:04:44 PM »
I have a database open via Database.ReadDwg() so the document is not open in the editor.  How do I insert objects into modelspace at a different ucs then world ucs?

I don't have time to play right now, but I assume you mean ReadDwgFile() not ReadDwg() or have I missed something.

I haven't tried what you are doing, but have you looked here for translations :
http://www.theswamp.org/index.php?topic=37686.0
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Changing UCS without Editor
« Reply #6 on: April 25, 2012, 07:48:22 AM »
Here's what I'm trying to do if I were doing this manually in the editor.  Switch to southeast iso view, change ucs to view, draw MLeader.  I'm trying to append this MLeader to modelspace in that ucs while not having the drawing open in the editor.

Yes. I meant ReadDwgFile().

UcsTableRecord and ViewTableRecord are all tied to the Editor. So, if you don't have a document in the Editor you can't use that convention.

Thank you kaefer, once I have the Matrix3d object how do I apply it to the object I'm appending?
« Last Edit: April 25, 2012, 07:54:35 AM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

kaefer

  • Guest
Re: Changing UCS without Editor
« Reply #7 on: April 25, 2012, 08:21:46 AM »
UcsTableRecord and ViewTableRecord are all tied to the Editor. So, if you don't have a document in the Editor you can't use that convention.

Are they? See e.g. Database.UcsTableId for an ObjectId, and then proceed as before to compute the transformation matrix for the UcsTableRecord wanted, viz. Origin, XAxis, YAxis.

As for how to apply a transformation: Don't miss Entity.TransformBy(Matrix3d transform).

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Changing UCS without Editor
« Reply #8 on: April 25, 2012, 10:51:38 AM »
Thanks kaefer. Below is what I was able to make work based on your suggestions.

Code: [Select]
[CommandMethod("LeaderTest")]
        public void LeaderTest()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    //Set up plane to control depth of MLeader
                    var pl = new Plane(new Point3d(0, 0, 120), new Vector3d(0, 0, 1));

                    //Setup Mleader object
                    var ml = new MLeader();
                    ml.SetDatabaseDefaults(db);
                    var l = ml.AddLeader();
                    var ln = ml.AddLeaderLine(l);
                    ml.AddFirstVertex(ln, new Point3d(0, 0, 120));
                    ml.AddLastVertex(ln, new Point3d(24, 36, 120));
                    ml.LeaderLineType = LeaderType.StraightLeader;
                    ml.ArrowSize = 0.125;
                    ml.ContentType = ContentType.NoneContent;

                    //Setting plane will elevate MLeader object
                    ml.SetPlane(pl);

                    //Transform matrix to set MLeader to southeast view
                    var toOrigin = db.Ucsorg;
                    var toXAxis = new Vector3d(0.707106781186548, 0.707106781186548, 0);
                    var toYAxis = new Vector3d(-0.408248290463863, 0.408248290463863, 0.816496580927726);
                    var toZAxis = toXAxis.CrossProduct(toYAxis);
                    var trxMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                                                  toOrigin, toXAxis, toYAxis, toZAxis);
                    ml.TransformBy(trxMatrix);

                    //Add MLeader to modelspace
                    ml.PostMLeaderToDb(db);
                    tr.AddNewlyCreatedDBObject(ml, true);
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                ed.WriteMessage(ex.ToString());
            }
        }

Is there a way to get the toXAxis and toYAxis vectors without hard coding them as I've done here?  I was able to get these vectors by just setting the UCS in AutoCAD and checking the System Variables UCSXDIR and UCSYDIR.
Revit 2019, AMEP 2019 64bit Win 10

kaefer

  • Guest
Re: Changing UCS without Editor
« Reply #9 on: April 25, 2012, 04:46:05 PM »
Is there a way to get the toXAxis and toYAxis vectors without hard coding them as I've done here?  I was able to get these vectors by just setting the UCS in AutoCAD and checking the System Variables UCSXDIR and UCSYDIR.

Where do you wish to get the point and vectors from?  If it's not a named UCS, and not the Current UCS, you could try to derive them from the CoordinateSystem3d of the active ViewportTableRecord, if the latter has an associated UCS. There should be a more direct way to do it than this:

Code - F#: [Select]
  1.         use tr = db.TransactionManager.StartTransaction()
  2.    
  3.         let vt = tr.GetObject(db.ViewportTableId, OpenMode.ForRead) :?> ViewportTable
  4.         if vt.Has "*Active" then
  5.             let vtr =
  6.                 tr.GetObject(vt.["*Active"], OpenMode.ForRead) :?> ViewportTableRecord
  7.             let ucs = vtr.Ucs
  8.             let mat =
  9.                 Matrix3d.AlignCoordinateSystem(
  10.                     Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
  11.                     ucs.Origin, ucs.Xaxis, ucs.Yaxis, ucs.Zaxis )
  12.             ...
  13.             ent.TransformBy mat

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Changing UCS without Editor
« Reply #10 on: April 25, 2012, 08:08:52 PM »
Also you don't have to transform it. Insert it at 0,0,0 with the correct normal  then move it to the desired point (Well this is a simple transformation)