明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7538|回复: 7

[运行时] 关于Zoom(W/E)

[复制链接]
发表于 2010-9-13 20:41 | 显示全部楼层 |阅读模式
一直有关于Zoom的问题,虽然在手册中有相应的代码,但并不能解决实际问题
下面是段在模型空间中Zoom的函数
要求VS版本2008
调用方法
  1. using TlsCad.ExtendMethods;
然后
  1.             Document doc = Application.DocumentManager.MdiActiveDocument;
  2.             Editor ed = doc.Editor;
  3.             Database db = doc.Database;
  4.             ed.ZoomExtents();
复制代码

  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.EditorInput;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.Runtime;
  8. using TlsCad.Collections;
  9. namespace TlsCad.ExtendMethods
  10. {
  11.     public enum CoordinateSystemCode
  12.     {
  13.         Wcs = 0,
  14.         Ucs,
  15.         MDcs,
  16.         PDcs
  17.     }
  18.     public static class EditorEx
  19.     {
  20.         #region Matrix
  21.         /// <summary>
  22.         /// 获取UCS到WCS的矩阵
  23.         /// </summary>
  24.         /// <param name="editor"></param>
  25.         /// <returns></returns>
  26.         public static Matrix3d GetMatrixFromUcsToWcs(this Editor editor)
  27.         {
  28.             return editor.CurrentUserCoordinateSystem;
  29.         }
  30.         /// <summary>
  31.         /// 获取WCS到UCS的矩阵
  32.         /// </summary>
  33.         /// <param name="editor"></param>
  34.         /// <returns></returns>
  35.         public static Matrix3d GetMatrixFromWcsToUcs(this Editor editor)
  36.         {
  37.             return editor.CurrentUserCoordinateSystem.Inverse();
  38.         }
  39.         /// <summary>
  40.         /// 获取MDCS(模型空间)到WCS的矩阵
  41.         /// </summary>
  42.         /// <param name="editor"></param>
  43.         /// <returns></returns>
  44.         public static Matrix3d GetMatrixFromMDcsToWcs(this Editor editor)
  45.         {
  46.             Matrix3d mat;
  47.             using (ViewTableRecord vtr = editor.GetCurrentView())
  48.             {
  49.                 mat = Matrix3d.PlaneToWorld(vtr.ViewDirection);
  50.                 mat = Matrix3d.Displacement(vtr.Target - Point3d.Origin) * mat;
  51.                 return Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target) * mat;
  52.             }
  53.         }
  54.         /// <summary>
  55.         /// 获取WCS到MDCS(模型空间)的矩阵
  56.         /// </summary>
  57.         /// <param name="editor"></param>
  58.         /// <returns></returns>
  59.         public static Matrix3d GetMatrixFromWcsToMDcs(this Editor editor)
  60.         {
  61.             return editor.GetMatrixFromMDcsToWcs().Inverse();
  62.         }
  63.         /// <summary>
  64.         /// 获取MDCS(模型空间)到PDCS(图纸空间)的矩阵
  65.         /// </summary>
  66.         /// <param name="editor"></param>
  67.         /// <returns></returns>
  68.         public static Matrix3d GetMatrixFromMDcsToPDcs(this Editor editor)
  69.         {
  70.             if ((short)Application.GetSystemVariable("TILEMODE") == 1)
  71.                 throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput, "Espace papier uniquement");
  72.             Database db = editor.Document.Database;
  73.             Matrix3d mat;
  74.             using (Transaction tr = db.TransactionManager.StartTransaction())
  75.             {
  76.                 Viewport vp =
  77.                     (Viewport)tr.GetObject(editor.CurrentViewportObjectId, OpenMode.ForRead);
  78.                 if (vp.Number == 1)
  79.                 {
  80.                     try
  81.                     {
  82.                         editor.SwitchToModelSpace();
  83.                         vp = (Viewport)tr.GetObject(editor.CurrentViewportObjectId, OpenMode.ForRead);
  84.                         editor.SwitchToPaperSpace();
  85.                     }
  86.                     catch
  87.                     {
  88.                         throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput, "Aucun fenêtre active");
  89.                     }
  90.                 }
  91.                 Point3d vCtr = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
  92.                 mat = Matrix3d.Displacement(vCtr.GetAsVector().Negate());
  93.                 mat = Matrix3d.Displacement(vp.CenterPoint.GetAsVector()) * mat;
  94.                 mat = Matrix3d.Scaling(vp.CustomScale, vp.CenterPoint) * mat;
  95.                 tr.Commit();
  96.             }
  97.             return mat;
  98.         }
  99.         /// <summary>
  100.         /// 获取PDCS(图纸空间)到MDCS(模型空间)的矩阵
  101.         /// </summary>
  102.         /// <param name="editor"></param>
  103.         /// <returns></returns>
  104.         public static Matrix3d GetMatrixFromPDcsToMDcs(this Editor editor)
  105.         {
  106.             return editor.GetMatrixFromMDcsToPDcs().Inverse();
  107.         }
  108.         public static Matrix3d GetMatrix(this Editor editor, CoordinateSystemCode from, CoordinateSystemCode to)
  109.         {
  110.             switch (from)
  111.             {
  112.                 case CoordinateSystemCode.Wcs:
  113.                     switch (to)
  114.                     {
  115.                         case CoordinateSystemCode.Ucs:
  116.                             return editor.GetMatrixFromWcsToUcs();
  117.                         case CoordinateSystemCode.MDcs:
  118.                             return editor.GetMatrixFromMDcsToWcs();
  119.                         case CoordinateSystemCode.PDcs:
  120.                             throw new Autodesk.AutoCAD.Runtime.Exception(
  121.                                 ErrorStatus.InvalidInput,
  122.                                 "To be used only with DCS");
  123.                     }
  124.                     break;
  125.                 case CoordinateSystemCode.Ucs:
  126.                     switch (to)
  127.                     {
  128.                         case CoordinateSystemCode.Wcs:
  129.                             return editor.GetMatrixFromUcsToWcs();
  130.                         case CoordinateSystemCode.MDcs:
  131.                             return editor.GetMatrixFromUcsToWcs() * editor.GetMatrixFromWcsToMDcs();
  132.                         case CoordinateSystemCode.PDcs:
  133.                             throw new Autodesk.AutoCAD.Runtime.Exception(
  134.                                 ErrorStatus.InvalidInput,
  135.                                 "To be used only with DCS");
  136.                     }
  137.                     break;
  138.                 case CoordinateSystemCode.MDcs:
  139.                     switch (to)
  140.                     {
  141.                         case CoordinateSystemCode.Wcs:
  142.                             return editor.GetMatrixFromMDcsToWcs();
  143.                         case CoordinateSystemCode.Ucs:
  144.                             return editor.GetMatrixFromMDcsToWcs() * editor.GetMatrixFromWcsToUcs();
  145.                         case CoordinateSystemCode.PDcs:
  146.                             return editor.GetMatrixFromMDcsToPDcs();
  147.                     }
  148.                     break;
  149.                 case CoordinateSystemCode.PDcs:
  150.                     switch (to)
  151.                     {
  152.                         case CoordinateSystemCode.Wcs:
  153.                             throw new Autodesk.AutoCAD.Runtime.Exception(
  154.                                 ErrorStatus.InvalidInput,
  155.                                 "To be used only with DCS");
  156.                         case CoordinateSystemCode.Ucs:
  157.                             throw new Autodesk.AutoCAD.Runtime.Exception(
  158.                                 ErrorStatus.InvalidInput,
  159.                                 "To be used only with DCS");
  160.                         case CoordinateSystemCode.MDcs:
  161.                             return editor.GetMatrixFromPDcsToMDcs();
  162.                     }
  163.                     break;
  164.             }
  165.             return Matrix3d.Identity;
  166.         }
  167.         #endregion
  168.         #region Zoom
  169.         public static void ZoomWindow(this Editor ed, Point3d minPoint, Point3d maxPoint)
  170.         {
  171.             ViewTableRecord cvtr = ed.GetCurrentView();
  172.             ViewTableRecord vtr = new ViewTableRecord();
  173.             vtr.CopyFrom(cvtr);
  174.             Point3d[] oldpnts = new Point3d[] { minPoint, maxPoint };
  175.             Point3d[] pnts = new Point3d[8];
  176.             Point3d[] dpnts = new Point3d[8];
  177.             for (int i = 0; i < 2; i++)
  178.             {
  179.                 for (int j = 0; j < 2; j++)
  180.                 {
  181.                     for (int k = 0; k < 2; k++)
  182.                     {
  183.                         int n = i * 4 + j * 2 + k;
  184.                         pnts[n] = new Point3d(oldpnts[i][0], oldpnts[j][1], oldpnts[k][2]);
  185.                         dpnts[n] = pnts[n].TransformBy(ed.GetMatrixFromWcsToMDcs());
  186.                     }
  187.                 }
  188.             }
  189.             double xmin, xmax, ymin, ymax;
  190.             xmin = xmax = dpnts[0][0];
  191.             ymin = ymax = dpnts[0][1];
  192.             for (int i = 1; i < 8; i++)
  193.             {
  194.                 xmin = Math.Min(xmin, dpnts[i][0]);
  195.                 xmax = Math.Max(xmax, dpnts[i][0]);
  196.                 ymin = Math.Min(ymin, dpnts[i][1]);
  197.                 ymax = Math.Max(ymax, dpnts[i][1]);
  198.             }
  199.             vtr.Width = xmax - xmin;
  200.             vtr.Height = ymax - ymin;
  201.             vtr.CenterPoint = (dpnts[0] + (dpnts[7] - dpnts[0]) / 2).Convert2d(new Plane());
  202.             ed.SetCurrentView(vtr);
  203.             ed.Regen();
  204.         }
  205.         public static void ZoomWindow(this Editor ed, Extents3d ext)
  206.         {
  207.             ZoomWindow(ed, ext.MinPoint, ext.MaxPoint);
  208.         }
  209.         public static void ZoomExtents(this Editor ed)
  210.         {
  211.             Database db = ed.Document.Database;
  212.             db.UpdateExt(true);
  213.             if (db.Extmax.X < db.Extmin.X)
  214.             {
  215.                 Plane plane = new Plane();
  216.                 ZoomWindow(
  217.                     ed,
  218.                     new Point3d(plane, db.Limmin),
  219.                     new Point3d(plane, db.Limmax));
  220.             }
  221.             else
  222.             {
  223.                 ZoomWindow(ed, db.Extmin, db.Extmax);
  224.             }
  225.         }
  226.         public static void ZoomObject(this Editor ed, Entity entity)
  227.         {
  228.             ZoomWindow(ed, entity.GeometricExtents);
  229.         }
  230.         #endregion
  231.     }
  232. }
发表于 2011-9-6 15:14 | 显示全部楼层
高,实在是高。多向你学习
发表于 2011-9-7 11:00 | 显示全部楼层
收藏了,我记得那个手册中的总有问题,看看你的方法是不是能解决~~
发表于 2012-6-5 11:44 | 显示全部楼层
请问一下。为什么我用过了zoom以后,命令栏是空的呢。如下(无图,模拟)
命令: COMMANDLINE
命令: properties
命令: zd
命令: 正在重生成模型。
|                                                              |
发表于 2013-5-3 11:26 | 显示全部楼层
学习领教了,收益匪浅!
发表于 2015-3-23 13:37 | 显示全部楼层
向楼主学习了,非常感谢
发表于 2015-6-16 07:06 | 显示全部楼层
Extmin / Extmax 不是实际范围,是 Limits 命令定义的角点
发表于 2015-6-20 18:06 | 显示全部楼层
试过了,希望老大能改进一下,这个代码好像不能准确放大到给定坐标范围,并且误差比较大。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-20 19:32 , Processed in 0.379912 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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