static public void selectMsFromPs() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; // pick a PS Viewport PromptEntityOptions opts = new PromptEntityOptions("Pick PS Viewport"); opts.SetRejectMessage("Must select PS Viewport objects only"); opts.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Viewport), false); PromptEntityResult res = ed.GetEntity(opts); if (res.Status == PromptStatus.OK) { int vpNumber = 0; // extract the viewport points Point3dCollection psVpPnts = new Point3dCollection(); using (Autodesk.AutoCAD.DatabaseServices.Viewport psVp = res.ObjectId.Open(OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Viewport) { // get the vp number vpNumber = psVp.Number; // now extract the viewport geometry psVp.GetGripPoints(psVpPnts, new IntegerCollection(), new IntegerCollection()); } // let's assume a rectangular vport for now, make the cross-direction grips square Point3d tmp = psVpPnts[2]; psVpPnts[2] = psVpPnts[1]; psVpPnts[1] = tmp; // Transform the PS points to MS points ResultBuffer rbFrom = new ResultBuffer(new TypedValue(5003, 3)); ResultBuffer rbTo = new ResultBuffer(new TypedValue(5003, 2)); double[] retPoint = new double[] { 0, 0, 0 }; // loop the ps points Point3dCollection msVpPnts = new Point3dCollection(); foreach (Point3d pnt in psVpPnts) { // translate from from the DCS of Paper Space (PSDCS) RTSHORT=3 and // the DCS of the current model space viewport RTSHORT=2 acedTrans(pnt.ToArray(), rbFrom.UnmanagedObject, rbTo.UnmanagedObject, 0, retPoint); // add the resulting point to the ms pnt array msVpPnts.Add(new Point3d(retPoint)); ed.WriteMessage("\n" + new Point3d(retPoint).ToString()); } // now switch to MS ed.SwitchToModelSpace(); // set the CVPort Application.SetSystemVariable("CVPORT", vpNumber); // once switched, we can use the normal selection mode to select PromptSelectionResult selectionresult = ed.SelectCrossingPolygon(msVpPnts); // now switch back to PS ed.SwitchToPaperSpace(); } }
/// <summary> /// Sets the size of the viewport according to the provided extents. /// </summary> /// <param name="ext">The extents of the viewport on the page.</param> /// <param name="fac">Optional factor to provide padding.</param> public static void ResizeViewport(this Autodesk.AutoCAD.DatabaseServices.Viewport vp, Extents2d ext, double fac, int num, int i) { vp.CenterPoint = new Point3d(ext.MaxPoint.X * fac / 2 + 1.2, ext.MaxPoint.Y * fac / 3 / 2 * num + 1, 0); vp.Width = ext.MaxPoint.X * fac + 2; //- ext.MinPoint.X)- ext.MinPoint.Y) vp.Height = ext.MaxPoint.Y * fac / 3; double zuobiao = 175 * (2 * i + 1); // vp.ViewTarget = new Point3d(zuobiao, 10,0); vp.ViewCenter = new Point2d(zuobiao, 5); // vp.StandardScale = StandardScaleType.Scale1To10; vp.ViewHeight = 70; //网格 vp.GridOn = false; }
/// <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 Layout lay, Transaction tr, int vpNum, Action <Viewport> f) { var vpIds = lay.GetViewports(); Autodesk.AutoCAD.DatabaseServices.Viewport vp = null; foreach (ObjectId vpId in vpIds) { var vp2 = tr.GetObject(vpId, OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.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 = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite); vp = new Autodesk.AutoCAD.DatabaseServices.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); }
/// <summary> /// Sets the view in a viewport to contain the specified model extents. /// </summary> /// <param name="ext">The extents of the content to fit the viewport.</param> /// <param name="fac">Optional factor to provide padding.</param> public static void FitContentToViewport(this Autodesk.AutoCAD.DatabaseServices.Viewport vp, Extents3d ext, double fac = 1.0) { // Let's zoom to just larger than the extents /* vp.ViewCenter = * * (ext.MinPoint + ((ext.MaxPoint - ext.MinPoint) * 0.5)).Strip();*/ // Get the dimensions of our view from the database extents var hgt = ext.MaxPoint.Y - ext.MinPoint.Y; var wid = ext.MaxPoint.X - ext.MinPoint.X; // We'll compare with the aspect ratio of the viewport itself // (which is derived from the page size) var aspect = vp.Width / vp.Height; // If our content is wider than the aspect ratio, make sure we // set the proposed height to be larger to accommodate the // content if (wid / hgt > aspect) { hgt = wid / aspect; } // Set the height so we're exactly at the extents vp.ViewHeight = hgt; // Set a custom scale to zoom out slightly (could also // vp.ViewHeight *= 1.1, for instance) vp.CustomScale *= fac; }
public void ApplyPaperOrientationTransform(Autodesk.AutoCAD.DatabaseServices.Viewport viewport) { createInstance(); BaseDBObject.ApplyPaperOrientationTransform(viewport); tr.Dispose(); }
public void CreateLayout() { using (AcadDb.Transaction tr = AcadFuncs.GetActiveDb().TransactionManager.StartTransaction()) { AcadDb.LayoutManager layout_man = AcadDb.LayoutManager.Current; AcadDb.ObjectId layout_id = layout_man.CreateLayout("My_Layout"); layout_man.SetCurrentLayoutId(layout_id); AcadDb.Layout layout = tr.GetObject(layout_id, AcadDb.OpenMode.ForRead) as AcadDb.Layout; if (null == layout) { return; } AcadDb.BlockTableRecord blk_tbl_rcd = tr.GetObject(layout.BlockTableRecordId, AcadDb.OpenMode.ForRead) as AcadDb.BlockTableRecord; if (null == blk_tbl_rcd) { return; } AcadDb.ObjectIdCollection vp_ids = layout.GetViewports(); AcadDb.Viewport vp = null; foreach (AcadDb.ObjectId vp_id in vp_ids) { AcadDb.Viewport vp2 = tr.GetObject(vp_id, AcadDb.OpenMode.ForWrite) as AcadDb.Viewport; if (null != vp2 && 2 == vp2.Number) { vp = vp2; break; } } if (null == vp) { vp = new AcadDb.Viewport(); blk_tbl_rcd.UpgradeOpen(); blk_tbl_rcd.AppendEntity(vp); tr.AddNewlyCreatedDBObject(vp, true); vp.On = true; vp.GridOn = true; } vp.ViewCenter = new AcadGeo.Point2d(0.0, 0.0); double scale = 0; { AcadEd.PromptDoubleOptions prmpt_pnt = new AcadEd.PromptDoubleOptions("Nhập scale:"); AcadEd.PromptDoubleResult prmpt_ret = AcadFuncs.GetEditor().GetDouble(prmpt_pnt); if (AcadEd.PromptStatus.Cancel == prmpt_ret.Status) { tr.Abort(); tr.Dispose(); return; } scale = prmpt_ret.Value; } vp.CustomScale = scale; vp.Locked = true; tr.Commit(); } }
public static void Viewport_Create(this Layout layout, Project_Setup.Viewport 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 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "x-vport"); doc.LayerManagement("Lock", "x-vport", false); // create a new viewport Autodesk.AutoCAD.DatabaseServices.Viewport vp = new 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.FrznLayers.GetEnumerator()); try { vp.StandardScale = vpinfo.StandardScale; } catch (Autodesk.AutoCAD.Runtime.Exception aex) { if (aex.ErrorStatus == ErrorStatus.InvalidInput) { vp.CustomScale = vpinfo.CustomScale; } } //Enables the viewport vp.On = true; Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "0"); tr.Commit(); } } }