public ViewportTableRecord getCurrentViewPort(Document document) { ViewportTableRecord viewport_tablerecord = new ViewportTableRecord(); using (Transaction tr = document.Database.TransactionManager.StartTransaction()) { ViewportTable viewport_table = tr.GetObject(document.Database.ViewportTableId, OpenMode.ForRead) as ViewportTable; viewport_tablerecord = tr.GetObject(viewport_table["*Active"], OpenMode.ForRead) as ViewportTableRecord; tr.Commit(); return(viewport_tablerecord); } }
/// <summary> /// 获取当前活动视口 /// </summary> /// <param name="db">数据库对象</param> /// <returns>返回当前活动视口的Id</returns> public static ObjectId CurrentViewportTableRecordId(this Database db) { ObjectId vtrId = ObjectId.Null; ViewportTable vt = (ViewportTable)db.ViewportTableId.GetObject(OpenMode.ForRead); foreach (ObjectId id in vt) { if (!id.IsErased) { vtrId = id; break; } } return(vtrId); }
public static string ZoomFilesAndSave(string fileName) { string newFileName = ""; using (Database db = new Database(false, false)) { db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndReadShare, true, null); Database prevDb = HostApplicationServices.WorkingDatabase; HostApplicationServices.WorkingDatabase = db; db.UpdateExt(true); using (ViewportTable vTab = db.ViewportTableId.GetObject(OpenMode.ForRead) as ViewportTable) { ObjectId acVptId = vTab["*Active"]; using (ViewportTableRecord vpTabRec = acVptId.GetObject(OpenMode.ForWrite) as ViewportTableRecord) { double scrRatio = (vpTabRec.Width / vpTabRec.Height); Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vpTabRec.ViewDirection); matWCS2DCS = Matrix3d.Displacement(vpTabRec.Target - Point3d.Origin) * matWCS2DCS; matWCS2DCS = Matrix3d.Rotation(-vpTabRec.ViewTwist, vpTabRec.ViewDirection, vpTabRec.Target) * matWCS2DCS; matWCS2DCS = matWCS2DCS.Inverse(); Extents3d extents = new Extents3d(db.Extmin, db.Extmax); extents.TransformBy(matWCS2DCS); double width = (extents.MaxPoint.X - extents.MinPoint.X); double height = (extents.MaxPoint.Y - extents.MinPoint.Y); Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5, (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5); if (width > (height * scrRatio)) { height = width / scrRatio; } vpTabRec.Height = height; vpTabRec.Width = height * scrRatio; vpTabRec.CenterPoint = center; } } HostApplicationServices.WorkingDatabase = prevDb; newFileName = fileName.Substring(0, fileName.Length - 4) + "z.dwg"; db.SaveAs(newFileName, DwgVersion.Current); } return(newFileName); }
public void CreateViewPost() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { ViewportTable vpt = (ViewportTable)trans.GetObject(db.ViewportTableId, OpenMode.ForWrite); string vName = "abc"; ViewportTableRecord vptr1 = new ViewportTableRecord(); vptr1.LowerLeftCorner = new Point2d(0, 0); vptr1.UpperRightCorner = new Point2d(0.5, 0.5); vptr1.Name = vName; ViewportTableRecord vptr2 = new ViewportTableRecord(); vptr2.LowerLeftCorner = new Point2d(0.5, 0); vptr2.UpperRightCorner = new Point2d(1, 0.5); vptr2.Name = vName; ViewportTableRecord vptr3 = new ViewportTableRecord(); vptr3.LowerLeftCorner = new Point2d(0, 0.5); vptr3.UpperRightCorner = new Point2d(0.5, 1); vptr3.Name = vName; ViewportTableRecord vptr4 = new ViewportTableRecord(); vptr4.LowerLeftCorner = new Point2d(0.5, 0.5); vptr4.UpperRightCorner = new Point2d(1, 1); vptr4.Name = vName; vpt.Add(vptr1); vpt.Add(vptr2); vpt.Add(vptr3); vpt.Add(vptr4); trans.AddNewlyCreatedDBObject(vptr1, true); trans.AddNewlyCreatedDBObject(vptr2, true); trans.AddNewlyCreatedDBObject(vptr3, true); trans.AddNewlyCreatedDBObject(vptr4, true); Document doc = Application.DocumentManager.MdiActiveDocument; if (Application.GetSystemVariable("TILEMODE") == (object)1) { doc.SendStringToExecute("-VPORTS 4 ", false, false, false); } else { doc.SendStringToExecute("-VPORTS 4 ", false, false, false); } trans.Commit(); } }
getViewportTable(Transaction tr, Database db) { ObjectId id = ObjectId.Null; try { using (tr) { ViewportTable vpT = (ViewportTable)tr.GetObject(db.ViewportTableId, OpenMode.ForRead); id = vpT.ObjectId; } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " VPort.cs: line: 31"); } return(id); }
public static bool GetActiveViewPortInfo(ref double height, ref double width, ref Point3d target, ref Vector3d viewDir, ref double viewTwist, bool getViewCenter) { // get the editor object Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.UpdateTiledViewportsInDatabase(); Database db = HostApplicationServices.WorkingDatabase; using (Transaction t = db.TransactionManager.StartTransaction()) { ViewportTable vt = (ViewportTable)t.GetObject(db.ViewportTableId, OpenMode.ForRead); ViewportTableRecord btr = (ViewportTableRecord)t.GetObject(vt["*Active"], OpenMode.ForRead); height = btr.Height; width = btr.Width; target = btr.Target; viewDir = btr.ViewDirection; viewTwist = btr.ViewTwist; t.Commit(); } return(true); }
public override void Run(string filename, Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { try { Point2d pmin = new Point2d(db.Extmin.X, db.Extmin.Y); Point2d pmax = new Point2d(db.Extmax.X, db.Extmax.Y); ViewportTable vpt = (ViewportTable)tr.GetObject(db.ViewportTableId, OpenMode.ForRead); ViewportTableRecord vptr = (ViewportTableRecord)tr.GetObject(vpt["*Active"], OpenMode.ForWrite); ZoomToExtentsofViewport(vptr); db.UpdateExt(true); } catch (System.Exception ex) { OnError(ex); } tr.Commit(); } }
private void zoomDb(Autodesk.AutoCAD.DatabaseServices.Database db, Extents3d ext) { using (var switcher = new AcadLib.WorkingDatabaseSwitcher(db)) { //db.UpdateExt(true); using (ViewportTable vTab = db.ViewportTableId.GetObject(OpenMode.ForRead) as ViewportTable) { ObjectId acVptId = vTab["*Active"]; using (ViewportTableRecord vpTabRec = acVptId.GetObject(OpenMode.ForWrite) as ViewportTableRecord) { double scrRatio = (vpTabRec.Width / vpTabRec.Height); //Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vpTabRec.ViewDirection); //matWCS2DCS = Matrix3d.Displacement(vpTabRec.Target - Point3d.Origin) * matWCS2DCS; //matWCS2DCS = Matrix3d.Rotation(-vpTabRec.ViewTwist, // vpTabRec.ViewDirection, // vpTabRec.Target) // * matWCS2DCS; //matWCS2DCS = matWCS2DCS.Inverse(); //Extents3d extents = new Extents3d(db.Extmin, db.Extmax); //extents.TransformBy(matWCS2DCS); //double width = (extents.MaxPoint.X - extents.MinPoint.X); //double height = (extents.MaxPoint.Y - extents.MinPoint.Y); //Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5, // (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5); double width = (ext.MaxPoint.X - ext.MinPoint.X); double height = (ext.MaxPoint.Y - ext.MinPoint.Y); if (width > (height * scrRatio)) { height = width / scrRatio; } vpTabRec.Height = ext.MaxPoint.Y - ext.MinPoint.Y; vpTabRec.Width = height * scrRatio; vpTabRec.CenterPoint = ext.Center().Convert2d(); } } } }
// Create a new tiled viewport configuration with two windows public void Create_Mode_ViewPort() { // get the current database Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction acTrans = db.TransactionManager.StartTransaction()) { // open the viewport table for read ViewportTable vtab = acTrans.GetObject(db.ViewportTableId, OpenMode.ForRead) as ViewportTable; // check to see if the named view 'Test Viewport' exists if (vtab.Has("Test_ViewPort") == false) { // open the view table for write vtab.UpgradeOpen(); // add the new viewport to the viewport table and the transaction ViewportTableRecord vptableRecordLwr = new ViewportTableRecord(); vtab.Add(vptableRecordLwr); acTrans.AddNewlyCreatedDBObject(vptableRecordLwr, true); // name the new viewport 'Test_ViewPort' and assign it to be the lower half of the drawing window vptableRecordLwr.Name = "Test_ViewPort"; vptableRecordLwr.LowerLeftCorner = new Point2d(0, 0); vptableRecordLwr.UpperRightCorner = new Point2d(1, 0.5); // Add the new viewport to the viewport table and the transaction ViewportTableRecord vptableRecordUpr = new ViewportTableRecord(); vtab.Add(vptableRecordUpr); acTrans.AddNewlyCreatedDBObject(vptableRecordUpr, true); // name the new viewport 'Test_ViewPort' and assign it to be the upper half of the drawing window vptableRecordUpr.Name = "Test_ViewPort"; vptableRecordUpr.LowerLeftCorner = new Point2d(0, .5); vptableRecordUpr.UpperRightCorner = new Point2d(1, 1); // to assign the new viewport as the active viewpport, // the viewport named active nedd to be removed and recreated based on "Test_ViewPort" // step through each object in the symbol table foreach (ObjectId acObjectId in vtab) { // open the object for read ViewportTableRecord acVportTblRec = acTrans.GetObject(acObjectId, OpenMode.ForRead) as ViewportTableRecord; // see if it is one of the active viewports, and if so erase it if (acVportTblRec.Name == "*Active") { acVportTblRec.UpgradeOpen(); acVportTblRec.Erase(); } } // clone the new viewport as the active viewports foreach (ObjectId acObjId in vtab) { // open the object for read ViewportTableRecord acVportTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead) as ViewportTableRecord; // see if it is one of the active viewports, and if so erase it if (acVportTblRec.Name == "*Test_ViewPort") { ViewTableRecord acVportTblRecClone = acVportTblRec.Clone() as ViewTableRecord; // add the new viewport to the viewport table and the transaction vtab.Add(acVportTblRecClone); acVportTblRecClone.Name = "*Active"; acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, true); } } // update display with the new tiled viewports arrangment Editor ed = doc.Editor; ed.UpdateTiledViewportsFromDatabase(); // commit changes acTrans.Commit(); } } }
public static void LoadProcedures() { _ListProcedures.Clear(); #region Purge if (_DWGCollection.Purge) { _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG) { // Create the list of objects to "purge" ObjectIdCollection idsToPurge = ArCaUtils.GetObjIdNonGrafical(db); // Call the Purge function to filter the list db.Purge(idsToPurge); if (idsToPurge.Count != 0) { // Erase each of the objects we've been // allowed to foreach (ObjectId id in idsToPurge) { DBObject obj = tr.GetObject(id, OpenMode.ForWrite); obj.Erase(); } //Was Changed objDWG.IsChanged = true; } })); } #endregion #region Lock View Ports if (_DWGCollection.LockViewPorts) { _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG) { //Block Table BlockTable btBlock = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in btBlock) { BlockTableRecord ltr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord; foreach (ObjectId acObjId in ltr) { if (acObjId.ObjectClass.DxfName == "VIEWPORT") { Viewport vp = tr.GetObject(acObjId, OpenMode.ForWrite) as Viewport; vp.Locked = true; //Was Changed objDWG.IsChanged = true; } } } })); } if (_DWGCollection.ZoomExtents) { _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG) { //Model ViewportTable vpt = tr.GetObject(db.ViewportTableId, OpenMode.ForWrite) as ViewportTable; foreach (ObjectId btrId in vpt) { ViewportTableRecord vptrec = tr.GetObject(btrId, OpenMode.ForWrite) as ViewportTableRecord; SetViewportToExtents(db, vptrec); } //Paper Spaces BlockTable btBlock = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (ObjectId btrId in btBlock) { BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord; if (!btr.Name.StartsWith("*Paper_Space")) { continue; } //Retrieve paper space viewport (viewport with the lowest handle) Viewport paperVp = null; long lowestHandle = -1; foreach (ObjectId acObjId in btr) { if (acObjId.ObjectClass.Name == "AcDbViewport") { Viewport vp = tr.GetObject(acObjId, OpenMode.ForRead) as Viewport; if (lowestHandle < 0) { lowestHandle = vp.Handle.Value; paperVp = vp; } else if (vp.Handle.Value < lowestHandle) { paperVp = vp; lowestHandle = vp.Handle.Value; } } } if (paperVp == null) { return; } paperVp.UpgradeOpen(); double scrRatio = (paperVp.Width / paperVp.Height); Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(paperVp.ViewDirection); matWCS2DCS = Matrix3d.Displacement(paperVp.ViewTarget - Point3d.Origin) * matWCS2DCS; matWCS2DCS = Matrix3d.Rotation(-paperVp.TwistAngle, paperVp.ViewDirection, paperVp.ViewTarget) * matWCS2DCS; matWCS2DCS = matWCS2DCS.Inverse(); Extents3d extents = GetExtents(db, btr); extents.TransformBy(matWCS2DCS); double width = (extents.MaxPoint.X - extents.MinPoint.X); double height = (extents.MaxPoint.Y - extents.MinPoint.Y); Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5, (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5); if (width > (height * scrRatio)) { height = width / scrRatio; } paperVp.ViewHeight = height; paperVp.ViewCenter = center; //Was Changed objDWG.IsChanged = true; } })); } #endregion }
public static void QuickView() { List <Point2d> viewsLower = new List <Point2d>() { new Point2d(0, 0.5), new Point2d(0, 0), new Point2d(0.5, 0) }; List <Point2d> viewsUpper = new List <Point2d>() { new Point2d(0.5, 1), new Point2d(0.5, 0.5), new Point2d(1, 0.5) }; List <OrthographicView> viewsDirection = new List <OrthographicView>() { OrthographicView.TopView, OrthographicView.FrontView, OrthographicView.RightView, }; Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Focus(); LayoutManager layoutManager = LayoutManager.Current; if (layoutManager.LayoutExists("QuickView")) { layoutManager.CurrentLayout = "Model"; FirstViewPort = (short)Application.GetSystemVariable("CVPORT"); Application.SetSystemVariable("CVPORT", FirstViewPort); ed.Command("-vports", "toggle"); return; } using (DocumentLock lockDoc = doc.LockDocument()) { using (Transaction trans = db.TransactionManager.StartTransaction()) { ViewportTable vportTable = trans.GetObject(db.ViewportTableId, OpenMode.ForWrite) as ViewportTable; ViewportTableRecord vportTableRec = trans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; BlockTable bTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord bTableRec = trans.GetObject(bTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord; FirstViewPort = System.Convert.ToInt16(vportTableRec.Number); vportTableRec.LowerLeftCorner = new Point2d(0.5, 0.5); vportTableRec.UpperRightCorner = new Point2d(1, 1); vportTableRec.ViewDirection = new Vector3d(Math.Sqrt(1.0 / 3.0), -Math.Sqrt(1.0 / 3.0), Math.Sqrt(1.0 / 3.0)); DBDictionary dict = trans.GetObject(db.VisualStyleDictionaryId, OpenMode.ForRead) as DBDictionary; vportTableRec.VisualStyleId = dict.GetAt("2DWireframe"); for (int i = 0; i < 3; i++) { using (ViewportTableRecord vportTableRecNew = new ViewportTableRecord()) { vportTable.Add(vportTableRecNew); trans.AddNewlyCreatedDBObject(vportTableRecNew, true); vportTableRecNew.SetViewDirection(viewsDirection[i]); vportTableRecNew.Name = "*Active"; vportTableRecNew.LowerLeftCorner = viewsLower[i]; vportTableRecNew.UpperRightCorner = viewsUpper[i]; } } ed.UpdateTiledViewportsFromDatabase(); foreach (ObjectId vp in vportTable) { ViewportTableRecord vpTR = trans.GetObject(vp, OpenMode.ForRead) as ViewportTableRecord; if (vpTR.Name == "*Active") { Application.SetSystemVariable("CVPORT", vpTR.Number); ed.Command("_.zoom", "_extents"); ed.Command("_.zoom", "_scale", "0.7x"); } } ObjectId layoutID = layoutManager.CreateLayout("QuickView"); Layout layout = trans.GetObject(layoutID, OpenMode.ForRead) as Layout; if (layout.TabSelected == false) { layoutManager.CurrentLayout = layout.LayoutName; } BlockTableRecord blckTableRec = trans.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord; int count = 0; foreach (ObjectId id in blckTableRec) { Viewport vp = trans.GetObject(id, OpenMode.ForRead) as Viewport; if (vp != null && count != 0) { vp.UpgradeOpen(); vp.Height = layout.PlotPaperSize.X * 0.4; vp.Width = layout.PlotPaperSize.Y * 0.4; vp.CenterPoint = new Point3d(layout.PlotPaperSize.Y * 0.75, layout.PlotPaperSize.X * 0.75, 0); vp.SetViewDirection(OrthographicView.NonOrthoView); vp.ViewDirection = new Vector3d(Math.Sqrt(1.0 / 3.0), -Math.Sqrt(1.0 / 3.0), Math.Sqrt(1.0 / 3.0)); vp.GridOn = false; ed.SwitchToModelSpace(); ed.Command("_.zoom", "_scale", "0.7x"); ed.SwitchToPaperSpace(); } count++; } List <Point3d> centerPoints = new List <Point3d>() { new Point3d(layout.PlotPaperSize.Y * 0.25, layout.PlotPaperSize.X * 0.75, 0), new Point3d(layout.PlotPaperSize.Y * 0.25, layout.PlotPaperSize.X * 0.25, 0), new Point3d(layout.PlotPaperSize.Y * 0.75, layout.PlotPaperSize.X * 0.25, 0) }; for (int i = 0; i < 3; i++) { using (Viewport vp = new Viewport()) { ed.SwitchToPaperSpace(); blckTableRec.AppendEntity(vp); trans.AddNewlyCreatedDBObject(vp, true); vp.CenterPoint = centerPoints[i]; vp.Height = layout.PlotPaperSize.X * 0.4; vp.Width = layout.PlotPaperSize.Y * 0.4; vp.SetViewDirection(viewsDirection[i]); vp.On = true; ed.SwitchToModelSpace(); ed.Command("_.zoom", "_extents"); ed.Command("_.zoom", "_scale", "0.7x"); } } ed.SwitchToPaperSpace(); trans.Commit(); } } menu.QuickViewButton.Text = "Toggle viewport"; }