关于Zoom(W/E)
一直有关于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;
Point3d[] dpnts = new Point3d;
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 = new Point3d(oldpnts, oldpnts, oldpnts);
dpnts = pnts.TransformBy(ed.GetMatrixFromWcsToMDcs());
}
}
}
double xmin, xmax, ymin, ymax;
xmin = xmax = dpnts;
ymin = ymax = dpnts;
for (int i = 1; i < 8; i++)
{
xmin = Math.Min(xmin, dpnts);
xmax = Math.Max(xmax, dpnts);
ymin = Math.Min(ymin, dpnts);
ymax = Math.Max(ymax, dpnts);
}
vtr.Width = xmax - xmin;
vtr.Height = ymax - ymin;
vtr.CenterPoint = (dpnts + (dpnts - dpnts) / 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
}
}
高,实在是高。多向你学习 收藏了,我记得那个手册中的总有问题,看看你的方法是不是能解决~~ 请问一下。为什么我用过了zoom以后,命令栏是空的呢。如下(无图,模拟)
命令: COMMANDLINE
命令: properties
命令: zd
命令: 正在重生成模型。
| | 学习领教了,收益匪浅! 向楼主学习了,非常感谢 Extmin / Extmax 不是实际范围,是 Limits 命令定义的角点 试过了,希望老大能改进一下,这个代码好像不能准确放大到给定坐标范围,并且误差比较大。
页:
[1]