马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
问题:
I need to control the zoom value in viewports on a layout. How can this be done using the .NET API????
解答:
Attached is a C# project with two custom commands. "TestVP" command iterates through all of the layouts and changes the zoom by setting the ViewHeight and ViewCenter of the viewports. The CVPORT variable is used to set the current viewport. This example uses the extents of the database so run the code in a drawing that has at least one entity. Please note that only viewports with the number 2 and 3 are handled in this example. "NewLayout" command in the example project creates a new layout and adds a viewport.
- [CommandMethod("testVP")]
- static public void TestVP()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Autodesk.AutoCAD.EditorInput.Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- //db.TileMode = false;
- try
- {
- Transaction tr;
- using (tr = db.TransactionManager.StartTransaction())
- {
- LayoutManager layoutMgr = LayoutManager.Current;
- Layout layoutObj;
- DBDictionary layoutDict;
- ed.WriteMessage("Number of Layouts = " + layoutMgr.LayoutCount.ToString() + "\n");
- ed.WriteMessage("Current Layout = " + layoutMgr.CurrentLayout + "\n");
- Point3d x_Min = db.Extmin;
- Point3d x_Max = db.Extmax;
- using (layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead))
- {
- foreach (DictionaryEntry layoutEntry in layoutDict)
- {
- using (layoutObj = (Layout)tr.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForRead))
- {
- // Set the CurrentLayout to the layout if it is not Modelspace
- if(layoutObj.LayoutName.ToString()!= "Model")
- layoutMgr.CurrentLayout = layoutObj.LayoutName.ToString();
- ed.WriteMessage("Layout Name = " + layoutObj.LayoutName + "\n");
- BlockTableRecord r = (BlockTableRecord)tr.GetObject(layoutObj.BlockTableRecordId, OpenMode.ForRead);
- foreach (ObjectId obj in r)
- {
- DBObject dbobj = tr.GetObject(obj, OpenMode.ForRead);
- Autodesk.AutoCAD.DatabaseServices.Viewport vp = dbobj as Autodesk.AutoCAD.DatabaseServices.Viewport;
- if (vp != null)
- {
- ed.WriteMessage("\nnumber of Viewport = " + vp.Number.ToString());
- //get the screen aspect ratio to calculate the height and width
- double mScrRatio;
- mScrRatio = (vp.Width / vp.Height);//width/height
- Point3d mMaxExt = db.Extmax;
- Point3d mMinExt = db.Extmin;
- Extents3d mExtents;
- mExtents.Set(mMinExt, mMaxExt);
- //prepare Matrix for DCS to WCS transformation
- Matrix3d matWCS2DCS;
- matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
- matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;
- matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS;
- matWCS2DCS = matWCS2DCS.Inverse();
- //tranform the extents to the DCS defined by the viewdir
- mExtents.TransformBy(matWCS2DCS);
- //width of the extents in current view
- double mWidth;
- mWidth = (mExtents.MaxPoint.X - mExtents.MinPoint.X);
- //height of the extents in current view
- double mHeight;
- mHeight = (mExtents.MaxPoint.Y - mExtents.MinPoint.Y);
- //get the view center point
- Point2d mCentPt = new Point2d(((mExtents.MaxPoint.X + mExtents.MinPoint.X) * 0.5), ((mExtents.MaxPoint.Y + mExtents.MinPoint.Y) * 0.5));
- //check if the width 'fits' in current window,if not then get the new height as per the viewports aspect ratio
- if (mWidth > (mHeight * mScrRatio)) mHeight = mWidth / mScrRatio;
- //set the viewport parameters
- if (vp.Number == 2)
- {
- vp.UpgradeOpen();
- vp.ViewHeight = mHeight * 1.01; //set the view height - adjusted by 1%
- vp.ViewCenter = mCentPt; //set the view center
- vp.Visible = true;
- vp.On = true;
- vp.UpdateDisplay();
- ed.SwitchToModelSpace();
- Application.SetSystemVariable("CVPORT", vp.Number);
- }
- if (vp.Number == 3)
- {
- vp.UpgradeOpen();
- vp.ViewHeight = mHeight * 1.25;
- vp.ViewCenter = mCentPt; //set the view center
- vp.Visible = true;
- vp.On = true;
- vp.UpdateDisplay();
- ed.SwitchToModelSpace();
- Application.SetSystemVariable("CVPORT", vp.Number);
- }
- }//if
- }//for each
- }//using layoutObj
- }//for each dictionaryEntry
- }//using layout dict
- tr.Commit();
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(ex.ToString());
- }
- }
-
|