乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 131|回复: 7

关于Zoom(W/E)

[复制链接]

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2010-9-13 20:41:00 | 显示全部楼层 |阅读模式
一直有关于Zoom的问题,虽然在手册中有相应的代码,但并不能解决实际问题
下面是段在模型空间中Zoom的函数
要求VS版本2008
调用方法
  1. using TlsCad.ExtendMethods;
然后复制代码
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk..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.         ///
  22.         /// 获取UCS到WCS的矩阵
  23.         ///
  24.         ///
  25.         ///
  26.         public static Matrix3d GetMatrixFromUcsToWcs(this Editor editor)
  27.         {
  28.             return editor.CurrentUserCoordinateSystem;
  29.         }
  30.         ///
  31.         /// 获取WCS到UCS的矩阵
  32.         ///
  33.         ///
  34.         ///
  35.         public static Matrix3d GetMatrixFromWcsToUcs(this Editor editor)
  36.         {
  37.             return editor.CurrentUserCoordinateSystem.Inverse();
  38.         }
  39.         ///
  40.         /// 获取MDCS(模型空间)到WCS的矩阵
  41.         ///
  42.         ///
  43.         ///
  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.         ///
  55.         /// 获取WCS到MDCS(模型空间)的矩阵
  56.         ///
  57.         ///
  58.         ///
  59.         public static Matrix3d GetMatrixFromWcsToMDcs(this Editor editor)
  60.         {
  61.             return editor.GetMatrixFromMDcsToWcs().Inverse();
  62.         }
  63.         ///
  64.         /// 获取MDCS(模型空间)到PDCS(图纸空间)的矩阵
  65.         ///
  66.         ///
  67.         ///
  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.         ///
  100.         /// 获取PDCS(图纸空间)到MDCS(模型空间)的矩阵
  101.         ///
  102.         ///
  103.         ///
  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.         #endregion
  167.         #region Zoom
  168.         public static void ZoomWindow(this Editor ed, Point3d minPoint, Point3d maxPoint)
  169.         {
  170.             ViewTableRecord cvtr = ed.GetCurrentView();
  171.             ViewTableRecord vtr = new ViewTableRecord();
  172.             vtr.CopyFrom(cvtr);
  173.             Point3d[] oldpnts = new Point3d[] { minPoint, maxPoint };
  174.             Point3d[] pnts = new Point3d[8];
  175.             Point3d[] dpnts = new Point3d[8];
  176.             for (int i = 0; i < 2; i++)
  177.             {
  178.                 for (int j = 0; j < 2; j++)
  179.                 {
  180.                     for (int k = 0; k < 2; k++)
  181.                     {
  182.                         int n = i * 4 + j * 2 + k;
  183.                         pnts[n] = new Point3d(oldpnts[i][0], oldpnts[j][1], oldpnts[k][2]);
  184.                         dpnts[n] = pnts[n].TransformBy(ed.GetMatrixFromWcsToMDcs());
  185.                     }
  186.                 }
  187.             }
  188.             double xmin, xmax, ymin, ymax;
  189.             xmin = xmax = dpnts[0][0];
  190.             ymin = ymax = dpnts[0][1];
  191.             for (int i = 1; i < 8; i++)
  192.             {
  193.                 xmin = Math.Min(xmin, dpnts[i][0]);
  194.                 xmax = Math.Max(xmax, dpnts[i][0]);
  195.                 ymin = Math.Min(ymin, dpnts[i][1]);
  196.                 ymax = Math.Max(ymax, dpnts[i][1]);
  197.             }
  198.             vtr.Width = xmax - xmin;
  199.             vtr.Height = ymax - ymin;
  200.             vtr.CenterPoint = (dpnts[0] + (dpnts[7] - dpnts[0]) / 2).Convert2d(new Plane());
  201.             ed.SetCurrentView(vtr);
  202.             ed.Regen();
  203.         }
  204.         public static void ZoomWindow(this Editor ed, Extents3d ext)
  205.         {
  206.             ZoomWindow(ed, ext.MinPoint, ext.MaxPoint);
  207.         }
  208.         public static void ZoomExtents(this Editor ed)
  209.         {
  210.             Database db = ed.Document.Database;
  211.             db.UpdateExt(true);
  212.             if (db.Extmax.X < db.Extmin.X)
  213.             {
  214.                 Plane plane = new Plane();
  215.                 ZoomWindow(
  216.                     ed,
  217.                     new Point3d(plane, db.Limmin),
  218.                     new Point3d(plane, db.Limmax));
  219.             }
  220.             else
  221.             {
  222.                 ZoomWindow(ed, db.Extmin, db.Extmax);
  223.             }
  224.         }
  225.         public static void ZoomObject(this Editor ed, Entity entity)
  226.         {
  227.             ZoomWindow(ed, entity.GeometricExtents);
  228.         }
  229.         #endregion
  230.     }
  231. }
回复

使用道具 举报

3

主题

28

帖子

3

银币

初来乍到

Rank: 1

铜币
40
发表于 2011-9-6 15:14:00 | 显示全部楼层
高,实在是高。多向你学习
回复

使用道具 举报

19

主题

154

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
230
发表于 2011-9-7 11:00:00 | 显示全部楼层
收藏了,我记得那个手册中的总有问题,看看你的方法是不是能解决~~
回复

使用道具 举报

6

主题

14

帖子

1

银币

初来乍到

Rank: 1

铜币
38
发表于 2012-6-5 11:44:00 | 显示全部楼层
请问一下。为什么我用过了zoom以后,命令栏是空的呢。如下(无图,模拟)
命令: COMMANDLINE
命令: properties
命令: zd
命令: 正在重生成模型。
|                                                              
回复

使用道具 举报

4

主题

9

帖子

5

银币

初来乍到

Rank: 1

铜币
25
发表于 2013-5-3 11:26:00 | 显示全部楼层
|学习领教了,收益匪浅!
回复

使用道具 举报

2

主题

7

帖子

1

银币

初来乍到

Rank: 1

铜币
15
发表于 2015-3-23 13:37:00 | 显示全部楼层
向楼主学习了,非常感谢
回复

使用道具 举报

0

主题

275

帖子

8

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
275
发表于 2015-6-16 07:06:00 | 显示全部楼层
Extmin / Extmax 不是实际范围,是 Limits 命令定义的角点
回复

使用道具 举报

8

主题

15

帖子

3

银币

初来乍到

Rank: 1

铜币
47
发表于 2015-6-20 18:06:00 | 显示全部楼层
试过了,希望老大能改进一下,这个代码好像不能准确放大到给定坐标范围,并且误差比较大。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 13:34 , Processed in 0.406128 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表