<?xml encoding=”UTF-8″>By Balaji Ramamoorthy
Here is a previous blog post on a similar topic for setting an orthographic view. The limitation with the code snippet posted in that blog post is that it modifies the active ViewportTableRecord and after the view change, the name of current UCS does not get displayed under the View Cube. When changing the view using AutoCAD’s View Cube and switching to Top View, it only changes the view without affecting the UCS. Also, the X and Y axes are aligned horizontally and vertically. To get a similar behavior using the API, here is a code snippet that sets the viewing direction along +Z axis and aligns the X and Y axes just as the View Cube does.
[DllImport(<span>"accore.dll"</span><span> , </span>
CallingConvention = CallingConvention.Cdecl,
EntryPoint = <span>"acedTrans"</span><span> )]</span>
<span>private</span><span> <span>static</span><span> <span>extern</span><span> <span>int</span><span> acedTrans(</span></span></span></span>
<span>double</span><span> [] point, </span>
IntPtr fromRb,
IntPtr toRb,
<span>int</span><span> disp, </span>
<span>double</span><span> [] result);</span>
[CommandMethod(<span>"SetViewDir"</span><span> )]</span>
<span>public</span><span> <span>static</span><span> <span>void</span><span> SetViewDirMethod()</span></span></span>
<span>{</span>
Document doc
= Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
<span>using</span><span> (Transaction tr </span>
= db.TransactionManager.StartTransaction())
<span>{</span>
ViewportTable vt = tr.GetObject(
db.ViewportTableId,
OpenMode.ForWrite) <span>as</span><span> ViewportTable;</span>
ViewportTableRecord activeVTR =
tr.GetObject(ed.ActiveViewportId,
OpenMode.ForRead) <span>as</span><span> ViewportTableRecord;</span>
<span>using</span><span> (ViewTableRecord vtr = <span>new</span><span> ViewTableRecord())</span></span>
<span>{</span>
vtr.Target = activeVTR.Target;
vtr.ViewDirection = activeVTR.ViewDirection;
vtr.Height = activeVTR.Height;
vtr.CenterPoint = activeVTR.CenterPoint;
vtr.ViewDirection = ucs.CoordinateSystem3d.Zaxis;
ed.SetCurrentView(vtr);
<span>}</span>
tr.Commit();
<span>}</span>
ucs = ed.CurrentUserCoordinateSystem;
<span>double</span><span> [] resVec = <span>new</span><span> <span>double</span><span> [] <span>{</span> 0, 0, 0 <span>}</span>;</span></span></span>
ResultBuffer rbFrom
= <span>new</span><span> ResultBuffer(<span>new</span><span> TypedValue(5003, 0));</span></span>
ResultBuffer rbTo
= <span>new</span><span> ResultBuffer(<span>new</span><span> TypedValue(5003, 2));</span></span>
Vector3d yAxisUCS = ucs.CoordinateSystem3d.Yaxis;
<span>int</span><span> res = acedTrans(</span>
yAxisUCS.ToArray(),
rbFrom.UnmanagedObject,
rbTo.UnmanagedObject,
1,
resVec
);
Vector3d yAxisDCS
= <span>new</span><span> Vector3d(resVec[0], resVec[1], resVec[2]);</span>
<span>double</span><span> twistAngle </span>
= (Math.PI * 0.5)
- Math.Atan2(yAxisDCS.Y, yAxisDCS.X);
<span>using</span><span> (ViewTableRecord vtr </span>
= doc.Editor.GetCurrentView())
<span>{</span>
vtr.ViewTwist = twistAngle;
ed.SetCurrentView(vtr);
<span>}</span>
<span>}</span>
Here is a screenshot of the View cube before and after the View change while retaining the UCS :



Leave a Reply