angei83 发表于 2025-12-3 18:45:39

小白问题求助,怎么实现缩放?




代码如下,提示“CS1061:"Editor"未包含"ZoomExtents”的定义,并且找不到可接受第一个"ditor"类型参数的可访问扩展方法"ZoomExtents”(是否缺少using 指令或程序集引用?)”,问AI,也都是这么实现的。求大佬帮我看看,问题出在哪里?


using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace zoom问题
{
    public class ZoomCommands
    {
      
      public void ZoomToExtents()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            ed.ZoomExtents(); // 全屏缩放
      }

      
      public void ZoomToWindow()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptPointResult p1 = ed.GetPoint("\n选择窗口左下角点: ");
            if (p1.Status != PromptStatus.OK) return;
            PromptPointResult p2 = ed.GetPoint("\n选择窗口右上角点: ");
            if (p2.Status != PromptStatus.OK) return;
            ed.ZoomWindow(p1.Value, p2.Value); // 窗口缩放
      }
    }
}


Bao_lai 发表于 2025-12-3 23:16:35

扩展方法
      
      /// <summary>
      /// 窗口缩放视图
      /// </summary>
      /// <param name="ed">命令行对象</param>
      /// <param name="pt1">窗口的角点</param>
      /// <param name="pt2">窗口的角点</param>
      public static void ZoomWindow(this Editor ed, Point3d pt1, Point3d pt2)
      {
            //创建一临时的直线用于获取两点表示的范围
            using (Line line = new Line(pt1, pt2))
            {
                //获取两点表示的范围
                Extents3d extents = new Extents3d(line.GeometricExtents.MinPoint, line.GeometricExtents.MaxPoint);
                //获取范围内的最小值点及最大值点
                Point2d minPt = new Point2d(extents.MinPoint.X, extents.MinPoint.Y);
                Point2d maxPt = new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y);
                //得到当前视图
                ViewTableRecord view = ed.GetCurrentView();
                //设置视图的中心点、高度和宽度
                view.CenterPoint = minPt + (maxPt - minPt) / 2;
                view.Height = maxPt.Y - minPt.Y;
                view.Width = maxPt.X - minPt.X;
                ed.SetCurrentView(view);//更新当前视图
            }
      }

      /// <summary>
      /// 根据图形边界显示视图
      /// </summary>
      /// <param name="ed">命令行对象</param>
      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();
                Point3d pt1 = new Point3d(plane, db.Limmin);
                Point3d pt2 = new Point3d(plane, db.Limmax);
                ed.ZoomWindow(pt1, pt2);
            }
            else
            {
                ed.ZoomWindow(db.Extmin, db.Extmax);
            }
      }

angei83 发表于 2025-12-17 13:12:11

本帖最后由 angei83 于 2025-12-17 13:15 编辑

d1742647821 发表于 2025-12-4 10:25
editor.GetCurrentView,拿到记录,修改后editor.SetCurrentView,注意视图里用的是DCS坐标系,要转换
谢大佬,当时看到你的回复时我没不明白,后来应用的时候还真是有问题。由于 我的图都是经过UCS旋转的,只能在WCS坐标系才可以。
但还是有很多不懂。GetPoint获得点是基于UCS的坐标,可是根据两个获得点画出的线又是在基于WCS坐标,因此我把UCS坐标值改为WCS坐标值。后来又发现视图也发生了旋转,就是UCS相对WCS的旋转角度,于是我引用了这个角度计算方法,本来想利用中心点旋转一下,我搞不明白为什么点不能旋转,非得要用直线旋转,然后在取直线中点。
最后改的代码是这样的,我不知道走了多少弯路。如果大佬有时间请帮我看看,谢谢。试了很多图,但还有一个图缩放不正常,不知道问题出在哪里。
public static void ZoomWindow(this Editor ed, Point3d pt1, Point3d pt2)
      {
            //计算视图中心点时用WCS坐标,计算视图高度和宽度用UCS坐标
            Point3d wcspt1 = Zoom.UcsToWcs(pt1);
            Point3d wcspt2 = Zoom.UcsToWcs(pt2);
            Line line = new Line(wcspt1, wcspt2);
            double angle = Angle();//对视图中心进行旋转
            Matrix3d matrix1 = Matrix3d.Rotation(-angle, Vector3d.ZAxis, new Point3d(0, 0, 0));
            line.TransformBy(matrix1);
            Point3d point3D = line.GetPointAtDist(line.Length / 2);
            Point2d point2D = new Point2d(point3D.X, point3D.Y);
            ViewTableRecord view = ed.GetCurrentView();
            view.CenterPoint = point2D;
            view.Height = Math.Abs(pt1.Y - pt2.Y);
            view.Width = Math.Abs(pt1.X - pt2.X);
            ed.SetCurrentView(view);
      }

angei83 发表于 2025-12-17 13:15:19

Bao_lai 发表于 2025-12-3 23:16
扩展方法

大佬好,我的大部分图都是使用了UCS旋转的,我把你的代码修改了,我不知道走了多少弯路,如果大佬有时间的话请帮我目看看,谢谢。经过修改,试了很多图,但还有一个图缩放不正常,不知道问题出在哪里。
public static void ZoomWindow(this Editor ed, Point3d pt1, Point3d pt2)
      {
            //计算视图中心点时用WCS坐标,计算视图高度和宽度用UCS坐标
            Point3d wcspt1 = Zoom.UcsToWcs(pt1);
            Point3d wcspt2 = Zoom.UcsToWcs(pt2);
            Line line = new Line(wcspt1, wcspt2);
            double angle = Angle();//对视图中心进行旋转
            Matrix3d matrix1 = Matrix3d.Rotation(-angle, Vector3d.ZAxis, new Point3d(0, 0, 0));
            line.TransformBy(matrix1);
            Point3d point3D = line.GetPointAtDist(line.Length / 2);
            Point2d point2D = new Point2d(point3D.X, point3D.Y);
            ViewTableRecord view = ed.GetCurrentView();
            view.CenterPoint = point2D;
            view.Height = Math.Abs(pt1.Y - pt2.Y);
            view.Width = Math.Abs(pt1.X - pt2.X);
            ed.SetCurrentView(view);
      }

Bao_lai 发表于 2025-12-3 21:27:33

接着问AI:lol

angei83 发表于 2025-12-3 22:41:14

Bao_lai 发表于 2025-12-3 21:27
接着问AI

白天问了近一天,晚上还在问。最后得到结果是“Editor 对象确实没有直接提供 ZoomWindow 方法,但可以通过Zoom 方法调用。”问题我在Editor上按F12就是找不到Zoom方法。:Q-:Q-大佬,你教教呗。

luzhmu 发表于 2025-12-4 08:26:40

vb.net版:
Dim doc As Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim acDb As Database = doc.Database
Dim ed As Editor = doc.Editor
acDb.UpdateExt(True) '更新数据库的扩展边界
Dim minPoint As Point3d = acDb.Extmin '最小边界点
Dim maxPoint As Point3d = acDb.Extmax '最大边界点
Dim min2d As New Point2d(minPoint.X, minPoint.Y) '最小边界点坐标
Dim max2d As New Point2d(maxPoint.X, maxPoint.Y) '最大边界点坐标
Dim NewView As ViewTableRecord = New ViewTableRecord() '构造函数来创建一个新的视图记录
NewView.CenterPoint = min2d + ((max2d - min2d) / 2.0) '设置新视图记录的中心点
NewView.Height = max2d.Y - min2d.Y '设置新视图记录的高度和宽度
NewView.Width = max2d.X - min2d.X
ed.SetCurrentView(NewView) '将新创建的视图记录设置为当前视图

d1742647821 发表于 2025-12-4 10:25:59

editor.GetCurrentView,拿到记录,修改后editor.SetCurrentView,注意视图里用的是DCS坐标系,要转换

angei83 发表于 2025-12-4 14:20:02

Bao_lai 发表于 2025-12-3 23:16
扩展方法

成了成了,非常感谢

angei83 发表于 2025-12-4 14:22:10

luzhmu 发表于 2025-12-4 08:26
vb.net版:
Dim doc As Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument ...

谢谢,根据上面的代码解决了
页: [1] 2
查看完整版本: 小白问题求助,怎么实现缩放?