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


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

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

        [CommandMethod("ZOOMWIN")]
        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); // 窗口缩放
        }
    }
}




网友答: 接着问AI

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

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

网友答: 扩展方法
  1.         
  2.         /// <summary>
  3.         /// 窗口缩放视图
  4.         /// </summary>
  5.         /// <param name="ed">命令行对象</param>
  6.         /// <param name="pt1">窗口的角点</param>
  7.         /// <param name="pt2">窗口的角点</param>
  8.         public static void ZoomWindow(this Editor ed, Point3d pt1, Point3d pt2)
  9.         {
  10.             //创建一临时的直线用于获取两点表示的范围
  11.             using (Line line = new Line(pt1, pt2))
  12.             {
  13.                 //获取两点表示的范围
  14.                 Extents3d extents = new Extents3d(line.GeometricExtents.MinPoint, line.GeometricExtents.MaxPoint);
  15.                 //获取范围内的最小值点及最大值点
  16.                 Point2d minPt = new Point2d(extents.MinPoint.X, extents.MinPoint.Y);
  17.                 Point2d maxPt = new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y);
  18.                 //得到当前视图
  19.                 ViewTableRecord view = ed.GetCurrentView();
  20.                 //设置视图的中心点、高度和宽度
  21.                 view.CenterPoint = minPt + (maxPt - minPt) / 2;
  22.                 view.Height = maxPt.Y - minPt.Y;
  23.                 view.Width = maxPt.X - minPt.X;
  24.                 ed.SetCurrentView(view);//更新当前视图
  25.             }
  26.         }

  27.         /// <summary>
  28.         /// 根据图形边界显示视图
  29.         /// </summary>
  30.         /// <param name="ed">命令行对象</param>
  31.         public static void ZoomExtents(this Editor ed)
  32.         {
  33.             Database db = ed.Document.Database;
  34.             db.UpdateExt(true);//更新当前模型空间的范围
  35.             //根据当前图形的界限范围对视图进行缩放
  36.             if (db.Extmax.X < db.Extmin.X)
  37.             {
  38.                 Plane plane = new Plane();
  39.                 Point3d pt1 = new Point3d(plane, db.Limmin);
  40.                 Point3d pt2 = new Point3d(plane, db.Limmax);
  41.                 ed.ZoomWindow(pt1, pt2);
  42.             }
  43.             else
  44.             {
  45.                 ed.ZoomWindow(db.Extmin, db.Extmax);
  46.             }
  47.         }



网友答: 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) '将新创建的视图记录设置为当前视图

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

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

成了成了,非常感谢

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

谢谢,根据上面的代码解决了
  • 上一篇:如何在ribbon按钮的右侧制作一个下拉箭头
  • 下一篇:没有了