/// <summary> /// FIX ALL OF THESE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! /// Collects info on a viewport; using a TitleBlock's Object ID this /// finds and all viewports in the same Layout and returns their relevant info /// </summary> /// <param name="tblk">TitleBlock which should be used</param> public static List <AutoCADViewport> Xref_ViewPortInfo(ObjectId tblk_layid) { Document doc = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; List <AutoCADViewport> list_vpinfo = new List <AutoCADViewport>(); if (!tblk_layid.IsNull) { using (Transaction tr = db.TransactionManager.StartTransaction()) { Layout curlay = tr.GetObject(tblk_layid, OpenMode.ForRead) as Layout; if (!curlay.ModelType) //if the layout is not a modelspace layout { string layname = curlay.LayoutName; ObjectIdCollection vports = curlay.GetViewports(); int numofvps = 0; foreach (ObjectId v in vports) { numofvps++; AutoCADViewport vpinfo = new AutoCADViewport(v.GetObject(OpenMode.ForRead) as Viewport); list_vpinfo.Add(vpinfo); } } } } return(list_vpinfo); }
public static void Viewport_Create(this Layout layout, AutoCADViewport vpinfo, Document doc) { Database db = doc.Database; Editor ed = doc.Editor; using (DocumentLock doclock = doc.LockDocument()) { using (Transaction tr = db.TransactionManager.StartTransaction()) { // Get the BlockTableRecord so you can add to it BlockTable blt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr_paper = tr.GetObject(blt[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord; //change the current space to Paper ed.SwitchToPaperSpace(); //switch current layer to viewport layer global::Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "x-vport"); doc.LayerManagement("Lock", "x-vport", false); // create a new viewport global::Autodesk.AutoCAD.DatabaseServices.Viewport vp = new global::Autodesk.AutoCAD.DatabaseServices.Viewport(); vp.Width = vpinfo.Width; vp.Height = vpinfo.Height; vp.CenterPoint = vpinfo.CenterPoint; // Add the viewport to the PaperSpaceRecord btr_paper.AppendEntity(vp); tr.AddNewlyCreatedDBObject(vp, true); //add all the corresponding viewport properties vp.ViewCenter = vpinfo.ViewCenter; vp.ViewHeight = vpinfo.ViewHeight; vp.ViewTarget = vpinfo.ViewTarget; vp.FreezeLayersInViewport(vpinfo.FrozenLayers.GetEnumerator()); try { vp.StandardScale = vpinfo.StandardScale; } catch (global::Autodesk.AutoCAD.Runtime.Exception aex) { if (aex.ErrorStatus == ErrorStatus.InvalidInput) { vp.CustomScale = vpinfo.CustomScale; } } //Enables the viewport vp.On = true; global::Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "0"); tr.Commit(); } } }