找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 726|回复: 1

.NET - how to set the zoom in Paperspace Viewports

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-8 23:45:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

问题:
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.

  1.         [CommandMethod("testVP")]
  2.         static public void TestVP()
  3.         {
  4.             Database db = HostApplicationServices.WorkingDatabase;
  5.             Autodesk.AutoCAD.EditorInput.Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  6.             //db.TileMode = false;


  7.             try
  8.             {


  9.                 Transaction tr;
  10.                 using (tr = db.TransactionManager.StartTransaction())
  11.                 {
  12.                     LayoutManager layoutMgr = LayoutManager.Current;
  13.                     Layout layoutObj;
  14.                     DBDictionary layoutDict;


  15.                     ed.WriteMessage("Number of Layouts = " + layoutMgr.LayoutCount.ToString() + "\n");
  16.                     ed.WriteMessage("Current Layout = " + layoutMgr.CurrentLayout + "\n");


  17.                     Point3d x_Min = db.Extmin;
  18.                     Point3d x_Max = db.Extmax;


  19.                     using (layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead))
  20.                     {
  21.                         foreach (DictionaryEntry layoutEntry in layoutDict)
  22.                         {
  23.                             using (layoutObj = (Layout)tr.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForRead))
  24.                             {


  25.                                 // Set the CurrentLayout to the layout if it is not Modelspace
  26.                                 if(layoutObj.LayoutName.ToString()!= "Model")
  27.                                     layoutMgr.CurrentLayout = layoutObj.LayoutName.ToString();

  28.                                 ed.WriteMessage("Layout Name = " + layoutObj.LayoutName + "\n");
  29.                                 BlockTableRecord r = (BlockTableRecord)tr.GetObject(layoutObj.BlockTableRecordId, OpenMode.ForRead);
  30.                                 foreach (ObjectId obj in r)
  31.                                 {
  32.                                     DBObject dbobj = tr.GetObject(obj, OpenMode.ForRead);
  33.                                     Autodesk.AutoCAD.DatabaseServices.Viewport vp = dbobj as Autodesk.AutoCAD.DatabaseServices.Viewport;
  34.                                     if (vp != null)
  35.                                     {
  36.                                         ed.WriteMessage("\nnumber of Viewport = " + vp.Number.ToString());

  37.                                         //get the screen aspect ratio to calculate the height and width
  38.                                         double mScrRatio;
  39.                                         mScrRatio = (vp.Width / vp.Height);//width/height


  40.                                         Point3d mMaxExt = db.Extmax;
  41.                                         Point3d mMinExt = db.Extmin;


  42.                                         Extents3d mExtents;
  43.                                         mExtents.Set(mMinExt, mMaxExt);

  44.                                         //prepare Matrix for DCS to WCS transformation
  45.                                         Matrix3d matWCS2DCS;
  46.                                         matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
  47.                                         matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;
  48.                                         matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS;
  49.                                         matWCS2DCS = matWCS2DCS.Inverse();


  50.                                         //tranform the extents to the DCS defined by the viewdir
  51.                                         mExtents.TransformBy(matWCS2DCS);


  52.                                         //width of the extents in current view
  53.                                         double mWidth;
  54.                                         mWidth = (mExtents.MaxPoint.X - mExtents.MinPoint.X);


  55.                                         //height of the extents in current view
  56.                                         double mHeight;
  57.                                         mHeight = (mExtents.MaxPoint.Y - mExtents.MinPoint.Y);


  58.                                         //get the view center point
  59.                                         Point2d mCentPt = new Point2d(((mExtents.MaxPoint.X + mExtents.MinPoint.X) * 0.5), ((mExtents.MaxPoint.Y + mExtents.MinPoint.Y) * 0.5));


  60.                                         //check if the width 'fits' in current window,if not then get the new height as per the viewports aspect ratio
  61.                                         if (mWidth > (mHeight * mScrRatio)) mHeight = mWidth / mScrRatio;


  62.                                         //set the viewport parameters
  63.                                         if (vp.Number == 2)
  64.                                         {
  65.                                             vp.UpgradeOpen();
  66.                                             vp.ViewHeight = mHeight * 1.01;    //set the view height - adjusted by 1%
  67.                                             vp.ViewCenter = mCentPt; //set the view center
  68.                                             vp.Visible = true;
  69.                                             vp.On = true;
  70.                                             vp.UpdateDisplay();
  71.                                             ed.SwitchToModelSpace();
  72.                                             Application.SetSystemVariable("CVPORT", vp.Number);
  73.                                         }
  74.                                         if (vp.Number == 3)
  75.                                         {
  76.                                             vp.UpgradeOpen();
  77.                                             vp.ViewHeight = mHeight * 1.25;
  78.                                             vp.ViewCenter = mCentPt; //set the view center
  79.                                             vp.Visible = true;
  80.                                             vp.On = true;
  81.                                             vp.UpdateDisplay();
  82.                                             ed.SwitchToModelSpace();
  83.                                             Application.SetSystemVariable("CVPORT", vp.Number);
  84.                                         }
  85.                                     }//if
  86.                                 }//for each
  87.                             }//using layoutObj
  88.                         }//for each dictionaryEntry
  89.                     }//using layout dict
  90.                     tr.Commit();
  91.                 }


  92.             }
  93.             catch (System.Exception ex)
  94.             {
  95.                 ed.WriteMessage(ex.ToString());
  96.             }
  97.         }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 4365个

财富等级: 富可敌国

发表于 2021-1-9 09:20:51 | 显示全部楼层
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-29 04:32 , Processed in 0.235062 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表