找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1668|回复: 1

[每日一码] Zoom Extents in .Net

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2016-8-18 19:28:07 | 显示全部楼层 |阅读模式

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

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

×
Zoom Extents in .Net

Issue
Do you have any .Net sample code that illustrates how to perform a zoom extents in model space and ** space from .Net?

Solution
The two following C# samples illustrate how to do zoom extents for both model and ** space using pure .Net AutoCAD API:

  1. public void SetViewportToExtents(Database db, ViewportTableRecord viewportTableRec)
  2. {
  3.     //lets update the database extents first
  4.     //true gives the best fit but will take time
  5.     db.UpdateExt(true);

  6.     //get the screen aspect ratio to calculate the height and width
  7.     double scrRatio = (viewportTableRec.Width / viewportTableRec.Height);

  8.     //prepare Matrix for DCS to WCS transformation
  9.     Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(viewportTableRec.ViewDirection);

  10.     //for DCS target point is the origin
  11.     matWCS2DCS = Matrix3d.Displacement(viewportTableRec.Target-Point3d.Origin) * matWCS2DCS;

  12.     //WCS Xaxis is twisted by twist angle
  13.     matWCS2DCS = Matrix3d.Rotation(-viewportTableRec.ViewTwist,
  14.                                     viewportTableRec.ViewDirection,
  15.                                     viewportTableRec.Target)
  16.                                     * matWCS2DCS;

  17.     matWCS2DCS = matWCS2DCS.Inverse();

  18.     //tranform the extents to the DCS defined by the viewdir
  19.     Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
  20.     extents.TransformBy(matWCS2DCS);

  21.     //width of the extents in current view
  22.     double width = (extents.MaxPoint.X - extents.MinPoint.X);

  23.     //height of the extents in current view
  24.     double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

  25.     //get the view center point
  26.     Point2d center = new Point2d((extents.MaxPoint.X  + extents.MinPoint.X)*0.5,
  27.                                  (extents.MaxPoint.Y  + extents.MinPoint.Y)*0.5);

  28.     //check if the width 'fits' in current window
  29.     //if not then get the new height as per the viewports aspect ratio
  30.     if (width > (height * scrRatio))
  31.         height = width / scrRatio;

  32.     viewportTableRec.Height = height;
  33.     viewportTableRec.Width = height * scrRatio;
  34.     viewportTableRec.CenterPoint = center;
  35. }

  36. [CommandMethod("ModelZoomExtents")]
  37. public void ModelZoomExtents()
  38. {
  39.     Document doc = Application.DocumentManager.MdiActiveDocument;
  40.     Database db = doc.Database;
  41.     Editor ed = doc.Editor;

  42.     using (Transaction Tx = db.TransactionManager.StartTransaction())
  43.     {
  44.         ed.UpdateTiledViewportsInDatabase();

  45.         ViewportTableRecord viewportTableRec = Tx.GetObject(ed.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;

  46.         SetViewportToExtents(db, viewportTableRec);

  47.         ed.UpdateTiledViewportsFromDatabase();

  48.         Tx.Commit();
  49.     }
  50. }

  51. [CommandMethod("**ZoomExtents")]
  52. static public void **ZoomExtents()
  53. {
  54.     Document doc = Application.DocumentManager.MdiActiveDocument;
  55.     Database db = doc.Database;
  56.     Editor ed = doc.Editor;

  57.     PromptEntityOptions peo = new PromptEntityOptions("\nSelect a viewport: ");
  58.     peo.SetRejectMessage("\nMust be a viewport...");
  59.     peo.AddAllowedClass(typeof(Viewport), true);

  60.     PromptEntityResult per = ed.GetEntity(peo);

  61.     if (per.Status != PromptStatus.OK) return;

  62.     using (Transaction Tx = db.TransactionManager.StartTransaction())
  63.     {
  64.         Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForWrite) as Viewport;


  65.         db.UpdateExt(true);

  66.         double scrRatio = (vp.Width / vp.Height);

  67.         Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);

  68.         matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget-Point3d.Origin) * matWCS2DCS;

  69.         matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle,
  70.                                         vp.ViewDirection,
  71.                                         vp.ViewTarget)
  72.                                         * matWCS2DCS;

  73.         matWCS2DCS = matWCS2DCS.Inverse();

  74.         Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
  75.         extents.TransformBy(matWCS2DCS);

  76.         double width = (extents.MaxPoint.X - extents.MinPoint.X);

  77.         double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

  78.         Point2d center = new Point2d((extents.MaxPoint.X  + extents.MinPoint.X)*0.5,
  79.                                      (extents.MaxPoint.Y  + extents.MinPoint.Y)*0.5);

  80.         if (width > (height * scrRatio))
  81.             height = width / scrRatio;

  82.         vp.ViewHeight = height;
  83.         vp.ViewCenter = center;

  84.         Tx.Commit();
  85.     }
  86. }


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

已领礼包: 3915个

财富等级: 富可敌国

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-21 21:02 , Processed in 0.411308 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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