<?xml encoding=”UTF-8″>By Balaji Ramamoorthy
Recently, a developer wanted to modify the sample code from here to create split model space viewports and set different orthographic view in each of them. Here is a sample code to do that. The below code creates four split modelspace viewports. When creating the new ViewportTableRecords that represent the newly created split viewports, its view parameters are set from the active ViewportTableRecord. This ensures that the ViewportTableRecord is correctly setup for us to set the orthographic view. Finally, each modelspace viewport is zoomed to extents. Here is a screenshot of the resulting viewport arrangement.
<span>// Create 4 split modelspace viewports,</span><span> </span>
<span>// set different orthographic view direction in each </span><span> </span>
<span>// and zoom to extents.</span><span> </span>
[CommandMethod(<span>"SplitMVP"</span><span> )]</span>
<span>public</span><span> <span>static</span><span> <span>void</span><span> SplitAndSetViewModelViewports()</span></span></span>
<span>{</span>
Document doc
= Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
db.UpdateExt(<span>true</span><span> );</span>
Extents3d dbExtent
= <span>new</span><span> Extents3d(db.Extmin, db.Extmax);</span>
<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 vtr1 = tr.GetObject(
doc.Editor.ActiveViewportId,
OpenMode.ForWrite) <span>as</span><span> ViewportTableRecord;</span>
Point2d ll = vtr1.LowerLeftCorner;
Point2d ur = vtr1.UpperRightCorner;
vtr1.LowerLeftCorner = ll;
vtr1.UpperRightCorner = <span>new</span><span> Point2d(</span>
ll.X + (ur.X - ll.X) * 0.5,
ll.Y + (ur.Y - ll.Y) * 0.5);
vtr1.SetViewDirection(OrthographicView.LeftView);
ZoomExtents(vtr1, dbExtent);
ViewportTableRecord vtr2 =
CreateVTR(vt, vtr1,
<span>new</span><span> Point2d(ll.X + (ur.X - ll.X) * 0.5, ll.Y), </span>
<span>new</span><span> Point2d(ur.X, ll.Y + (ur.Y - ll.Y) * 0.5), </span>
dbExtent, OrthographicView.RightView);
vt.Add(vtr2);
tr.AddNewlyCreatedDBObject(vtr2, <span>true</span><span> );</span>
ViewportTableRecord vtr3 =
CreateVTR(vt, vtr1,
vtr1.UpperRightCorner, ur,
dbExtent, OrthographicView.BottomView);
vt.Add(vtr3);
tr.AddNewlyCreatedDBObject(vtr3, <span>true</span><span> );</span>
ViewportTableRecord vtr4 =
CreateVTR(vt, vtr1,
<span>new</span><span> Point2d(ll.X, ll.Y + (ur.Y - ll.Y) * 0.5), </span>
<span>new</span><span> Point2d(ll.X + (ur.X - ll.X) * 0.5, ur.Y), </span>
dbExtent, OrthographicView.TopView);
vt.Add(vtr4);
tr.AddNewlyCreatedDBObject(vtr4, <span>true</span><span> );</span>
<span>// Update the display with new tiled viewports</span><span> </span>
doc.Editor.UpdateTiledViewportsFromDatabase();
<span>// Commit the changes </span><span> </span>
tr.Commit();
<span>}</span>
<span>}</span>
<span>// Create a model space viewport and uses the </span><span> </span>
<span>// view parameters from a reference viewport</span><span> </span>
<span>// before setting the orthographic view and zooming to extents</span><span> </span>
<span>public</span><span> <span>static</span><span> ViewportTableRecord CreateVTR(</span></span>
ViewportTable vt, ViewportTableRecord refVTR,
Point2d ll, Point2d ur, Extents3d dbExtent,
OrthographicView ov)
<span>{</span>
ViewportTableRecord newVTR = <span>new</span><span> ViewportTableRecord();</span>
newVTR.LowerLeftCorner = ll;
newVTR.UpperRightCorner = ur;
newVTR.Name = <span>"*Active"</span><span> ;</span>
newVTR.ViewDirection = refVTR.ViewDirection;
newVTR.ViewTwist = refVTR.ViewTwist;
newVTR.Target = refVTR.Target;
newVTR.BackClipEnabled = refVTR.BackClipEnabled;
newVTR.BackClipDistance = refVTR.BackClipDistance;
newVTR.FrontClipEnabled = refVTR.FrontClipEnabled;
newVTR.FrontClipDistance = refVTR.FrontClipDistance;
newVTR.Elevation = refVTR.Elevation;
newVTR.SetViewDirection(ov);
ZoomExtents(newVTR, dbExtent);
<span>return</span><span> newVTR;</span>
<span>}</span>
<span>public</span><span> <span>static</span><span> <span>void</span><span> ZoomExtents</span></span></span>
(ViewportTableRecord vtr, Extents3d dbExtent)
<span>{</span>
<span>//get the screen aspect ratio to </span><span> </span>
<span>// calculate the height and width</span><span> </span>
<span>double</span><span> scrRatio = (vtr.Width / vtr.Height);</span>
<span>//prepare Matrix for DCS to WCS transformation</span><span> </span>
Matrix3d matWCS2DCS
= Matrix3d.PlaneToWorld(vtr.ViewDirection);
<span>//for DCS target point is the origin</span><span> </span>
matWCS2DCS = Matrix3d.Displacement
(vtr.Target - Point3d.Origin) * matWCS2DCS;
<span>//WCS Xaxis is twisted by twist angle</span><span> </span>
matWCS2DCS = Matrix3d.Rotation(-vtr.ViewTwist,
vtr.ViewDirection,
vtr.Target
) * matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();
<span>//tranform the extents to the DCS </span><span> </span>
<span>// defined by the viewdir</span><span> </span>
dbExtent.TransformBy(matWCS2DCS);
<span>//width of the extents in current view</span><span> </span>
<span>double</span><span> width </span>
= (dbExtent.MaxPoint.X - dbExtent.MinPoint.X);
<span>//height of the extents in current view</span><span> </span>
<span>double</span><span> height </span>
= (dbExtent.MaxPoint.Y - dbExtent.MinPoint.Y);
<span>//get the view center point</span><span> </span>
Point2d center = <span>new</span><span> Point2d(</span>
(dbExtent.MaxPoint.X + dbExtent.MinPoint.X) * 0.5,
(dbExtent.MaxPoint.Y + dbExtent.MinPoint.Y) * 0.5);
<span>//check if the width' in current window</span><span> </span>
<span>//if not then get the new height as per the </span><span> </span>
<span>// viewports aspect ratio</span><span> </span>
<span>if</span><span> (width > (height * scrRatio))</span>
height = width / scrRatio;
vtr.Height = height;
vtr.Width = height * scrRatio;
vtr.CenterPoint = center;
<span>}</span>


Leave a Reply