- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
一直有关于Zoom的问题,虽然在手册中有相应的代码,但并不能解决实际问题
下面是段在模型空间中Zoom的函数
要求VS版本2008
调用方法
先- using TlsCad.ExtendMethods;
然后-
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- ed.ZoomExtents();
复制代码
-
- using System;
- using System.Collections.Generic;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using TlsCad.Collections;
- namespace TlsCad.ExtendMethods
- {
- public enum CoordinateSystemCode
- {
- Wcs = 0,
- Ucs,
- MDcs,
- PDcs
- }
- public static class EditorEx
- {
- #region Matrix
- /// <summary>
- /// 获取UCS到WCS的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromUcsToWcs(this Editor editor)
- {
- return editor.CurrentUserCoordinateSystem;
- }
- /// <summary>
- /// 获取WCS到UCS的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromWcsToUcs(this Editor editor)
- {
- return editor.CurrentUserCoordinateSystem.Inverse();
- }
- /// <summary>
- /// 获取MDCS(模型空间)到WCS的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromMDcsToWcs(this Editor editor)
- {
- Matrix3d mat;
- using (ViewTableRecord vtr = editor.GetCurrentView())
- {
- mat = Matrix3d.PlaneToWorld(vtr.ViewDirection);
- mat = Matrix3d.Displacement(vtr.Target - Point3d.Origin) * mat;
- return Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target) * mat;
- }
- }
- /// <summary>
- /// 获取WCS到MDCS(模型空间)的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromWcsToMDcs(this Editor editor)
- {
- return editor.GetMatrixFromMDcsToWcs().Inverse();
- }
- /// <summary>
- /// 获取MDCS(模型空间)到PDCS(图纸空间)的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromMDcsToPDcs(this Editor editor)
- {
- if ((short)Application.GetSystemVariable("TILEMODE") == 1)
- throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput, "Espace papier uniquement");
- Database db = editor.Document.Database;
- Matrix3d mat;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Viewport vp =
- (Viewport)tr.GetObject(editor.CurrentViewportObjectId, OpenMode.ForRead);
- if (vp.Number == 1)
- {
- try
- {
- editor.SwitchToModelSpace();
- vp = (Viewport)tr.GetObject(editor.CurrentViewportObjectId, OpenMode.ForRead);
- editor.SwitchToPaperSpace();
- }
- catch
- {
- throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput, "Aucun fenêtre active");
- }
- }
- Point3d vCtr = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
- mat = Matrix3d.Displacement(vCtr.GetAsVector().Negate());
- mat = Matrix3d.Displacement(vp.CenterPoint.GetAsVector()) * mat;
- mat = Matrix3d.Scaling(vp.CustomScale, vp.CenterPoint) * mat;
- tr.Commit();
- }
- return mat;
- }
- /// <summary>
- /// 获取PDCS(图纸空间)到MDCS(模型空间)的矩阵
- /// </summary>
- /// <param name="editor"></param>
- /// <returns></returns>
- public static Matrix3d GetMatrixFromPDcsToMDcs(this Editor editor)
- {
- return editor.GetMatrixFromMDcsToPDcs().Inverse();
- }
- public static Matrix3d GetMatrix(this Editor editor, CoordinateSystemCode from, CoordinateSystemCode to)
- {
- switch (from)
- {
- case CoordinateSystemCode.Wcs:
- switch (to)
- {
- case CoordinateSystemCode.Ucs:
- return editor.GetMatrixFromWcsToUcs();
- case CoordinateSystemCode.MDcs:
- return editor.GetMatrixFromMDcsToWcs();
- case CoordinateSystemCode.PDcs:
- throw new Autodesk.AutoCAD.Runtime.Exception(
- ErrorStatus.InvalidInput,
- "To be used only with DCS");
- }
- break;
- case CoordinateSystemCode.Ucs:
- switch (to)
- {
- case CoordinateSystemCode.Wcs:
- return editor.GetMatrixFromUcsToWcs();
- case CoordinateSystemCode.MDcs:
- return editor.GetMatrixFromUcsToWcs() * editor.GetMatrixFromWcsToMDcs();
- case CoordinateSystemCode.PDcs:
- throw new Autodesk.AutoCAD.Runtime.Exception(
- ErrorStatus.InvalidInput,
- "To be used only with DCS");
- }
- break;
- case CoordinateSystemCode.MDcs:
- switch (to)
- {
- case CoordinateSystemCode.Wcs:
- return editor.GetMatrixFromMDcsToWcs();
- case CoordinateSystemCode.Ucs:
- return editor.GetMatrixFromMDcsToWcs() * editor.GetMatrixFromWcsToUcs();
- case CoordinateSystemCode.PDcs:
- return editor.GetMatrixFromMDcsToPDcs();
- }
- break;
- case CoordinateSystemCode.PDcs:
- switch (to)
- {
- case CoordinateSystemCode.Wcs:
- throw new Autodesk.AutoCAD.Runtime.Exception(
- ErrorStatus.InvalidInput,
- "To be used only with DCS");
- case CoordinateSystemCode.Ucs:
- throw new Autodesk.AutoCAD.Runtime.Exception(
- ErrorStatus.InvalidInput,
- "To be used only with DCS");
- case CoordinateSystemCode.MDcs:
- return editor.GetMatrixFromPDcsToMDcs();
- }
- break;
- }
- return Matrix3d.Identity;
- }
- #endregion
- #region Zoom
- public static void ZoomWindow(this Editor ed, Point3d minPoint, Point3d maxPoint)
- {
- ViewTableRecord cvtr = ed.GetCurrentView();
- ViewTableRecord vtr = new ViewTableRecord();
- vtr.CopyFrom(cvtr);
- Point3d[] oldpnts = new Point3d[] { minPoint, maxPoint };
- Point3d[] pnts = new Point3d[8];
- Point3d[] dpnts = new Point3d[8];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- for (int k = 0; k < 2; k++)
- {
- int n = i * 4 + j * 2 + k;
- pnts[n] = new Point3d(oldpnts[i][0], oldpnts[j][1], oldpnts[k][2]);
- dpnts[n] = pnts[n].TransformBy(ed.GetMatrixFromWcsToMDcs());
- }
- }
- }
- double xmin, xmax, ymin, ymax;
- xmin = xmax = dpnts[0][0];
- ymin = ymax = dpnts[0][1];
- for (int i = 1; i < 8; i++)
- {
- xmin = Math.Min(xmin, dpnts[i][0]);
- xmax = Math.Max(xmax, dpnts[i][0]);
- ymin = Math.Min(ymin, dpnts[i][1]);
- ymax = Math.Max(ymax, dpnts[i][1]);
- }
- vtr.Width = xmax - xmin;
- vtr.Height = ymax - ymin;
- vtr.CenterPoint = (dpnts[0] + (dpnts[7] - dpnts[0]) / 2).Convert2d(new Plane());
- ed.SetCurrentView(vtr);
- ed.Regen();
- }
- public static void ZoomWindow(this Editor ed, Extents3d ext)
- {
- ZoomWindow(ed, ext.MinPoint, ext.MaxPoint);
- }
- public static void ZoomExtents(this Editor ed)
- {
- Database db = ed.Document.Database;
- db.UpdateExt(true);
- if (db.Extmax.X < db.Extmin.X)
- {
- Plane plane = new Plane();
- ZoomWindow(
- ed,
- new Point3d(plane, db.Limmin),
- new Point3d(plane, db.Limmax));
- }
- else
- {
- ZoomWindow(ed, db.Extmin, db.Extmax);
- }
- }
- public static void ZoomObject(this Editor ed, Entity entity)
- {
- ZoomWindow(ed, entity.GeometricExtents);
- }
- #endregion
- }
- }
|
|