private _Db.Viewport layoutViewportGetter(_Db.Layout lay) { _Db.ObjectIdCollection viewIds = lay.GetViewports(); _Db.Viewport vp = null; foreach (_Db.ObjectId id in viewIds) { _Db.Viewport vp2 = _c.trans.GetObject(id, _Db.OpenMode.ForWrite) as _Db.Viewport; if (vp2 != null && vp2.Number == 2) { vp = vp2; break; } } if (vp == null) { _Db.BlockTableRecord btr = _c.trans.GetObject(lay.BlockTableRecordId, _Db.OpenMode.ForWrite) as _Db.BlockTableRecord; vp = new _Db.Viewport(); btr.AppendEntity(vp); _c.trans.AddNewlyCreatedDBObject(vp, true); vp.On = true; vp.GridOn = false; } return(vp); }
/// <summary> /// Applies an action to the specified viewport from this layout. /// Creates a new viewport if none is found withthat number. /// </summary> /// <param name="tr">The transaction to use to open the viewports.</param> /// <param name="vpNum">The number of the target viewport.</param> /// <param name="f">The action to apply to each of the viewports.</param> public static void ApplyToViewport( this _AcDb.Layout lay, _AcDb.Transaction tr, int vpNum, Action <_AcDb.Viewport> f ) { var vpIds = lay.GetViewports(); _AcDb.Viewport vp = null; foreach (_AcDb.ObjectId vpId in vpIds) { var vp2 = tr.GetObject(vpId, _AcDb.OpenMode.ForWrite) as _AcDb.Viewport; if (vp2 != null && vp2.Number == vpNum) { // We have found our viewport, so call the action vp = vp2; break; } } if (vp == null) { // We have not found our viewport, so create one var btr = (_AcDb.BlockTableRecord)tr.GetObject( lay.BlockTableRecordId, _AcDb.OpenMode.ForWrite ); vp = new _AcDb.Viewport(); // Add it to the database btr.AppendEntity(vp); tr.AddNewlyCreatedDBObject(vp, true); // Turn it - and its grid - on vp.On = true; vp.GridOn = true; } // Finally we call our function on it f(vp); }