Creating Viewport with UCS Follow

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

Here is a sample code to create a viewport with UCS Follow turned on. This should ensure that the viewport displays the plan based on the UCS whenever it changes.

 [CommandMethod("CreateVP",
     CommandFlags.NoTileMode)]
 <span>public</span><span>  void CreateVPMethod()</span>
 <span>{</span>
     Document doc 
     = Application.DocumentManager.MdiActiveDocument;
 
     Database db = doc.Database;
     Editor ed = doc.Editor;
 
     ObjectId layoutId = LayoutManager.Current.GetLayoutId
                 (LayoutManager.Current.CurrentLayout);
     
     <span>using</span><span>  (Transaction Tx </span>
         = db.TransactionManager.StartTransaction())
     <span>{</span>
         Layout LayoutDest 
         = Tx.GetObject(layoutId, OpenMode.ForRead) 
         <span>as</span><span>  Layout;</span>
 
         BlockTableRecord btrDest 
         = Tx.GetObject(LayoutDest.BlockTableRecordId, 
         OpenMode.ForWrite) <span>as</span><span>  BlockTableRecord;</span>
 
         ViewportTable vt 
         = Tx.GetObject(db.ViewportTableId, OpenMode.ForRead)
         <span>as</span><span>  ViewportTable;</span>
 
         ViewportTableRecord vtr 
         = Tx.GetObject(vt["*Active"], OpenMode.ForRead) 
         <span>as</span><span>  ViewportTableRecord;</span>
 
         UcsTable ucsTbl 
         = Tx.GetObject(db.UcsTableId, OpenMode.ForRead) 
         <span>as</span><span>  UcsTable;</span>
 
         <span>if</span><span>  (vtr != null)</span>
         <span>{</span>
             Autodesk.AutoCAD.DatabaseServices.Viewport vpNew 
             = <span>new</span><span>  Autodesk.AutoCAD.DatabaseServices.Viewport();</span>
 
             vpNew.SetDatabaseDefaults();
             vpNew.Width = 6.0;
             vpNew.Height = 5.0;
             vpNew.CenterPoint = <span>new</span><span>  Point3d(3.25, 3, 0);</span>
             <span>if</span><span>  (ucsTbl.Has("myucs"))</span>
             <span>{</span>
                 ObjectId ucsId = ucsTbl["myucs"];
                 vpNew.SetUcs(ucsId);
             <span>}</span>
             vpNew.StandardScale 
             = StandardScaleType.Scale1To1;
             vpNew.ViewCenter = vtr.CenterPoint;
             vpNew.ViewHeight = vtr.Height;
             vpNew.ViewDirection = vtr.ViewDirection;
             vpNew.ViewTarget = vtr.Target;
             vpNew.TwistAngle = vtr.ViewTwist;
 
             vpNew.UcsPerViewport = <span>true</span><span> ;</span>
             vpNew.UcsFollowModeOn = <span>true</span><span> ;</span>
             vpNew.GridOn = <span>true</span><span> ;</span>
             vpNew.GridFollow = <span>true</span><span> ;</span>
             btrDest.AppendEntity(vpNew);
 
             Tx.AddNewlyCreatedDBObject(vpNew, <span>true</span><span> );</span>
             vpNew.On = <span>true</span><span> ;</span>
         <span>}</span>
 
         Tx.Commit();
     <span>}</span>
 <span>}</span>
 


Comments

One response to “Creating Viewport with UCS Follow”

  1. Alexey Avatar
    Alexey

    Ive tested this code in Autocad 2012 and 2014 - it works improperly.
    Right after command execution there is a viewport in paper space, but the view in it is WCS. Then I "double-click" inside the viewport and outside it - after this manipulation the view in the viewport became "myucs". Interesting that switching between model space and paper space doesn
    t help in this case.
    Is it a bug in Object ARX?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading