<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: zoomwindow does wrong extend in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/13328099#M17682</link>
    <description>&lt;P&gt;Hello gile,now i have another wrong that it is like this:&lt;/P&gt;&lt;P&gt;in this line i fetch wrong point,so i want know how to fix this bug when background use.&lt;BR /&gt;var p = tr.Database.Extmin; //&amp;nbsp;The background fetch value is {(1E+. 20,1E+20,1E+20)}&lt;/P&gt;&lt;P&gt;Thank you first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var tr = DBTrans.Top;
tr.Database.TileMode = true;
using ViewportTable vpt = (ViewportTable)tr.GetObject(tr.Database.ViewportTableId);
ObjectId aid = vpt["*Active"];
using ViewportTableRecord vpr = (ViewportTableRecord)tr.GetObject(aid, OpenMode.ForWrite);
double sc = vpr.Width / vpr.Height;


Matrix3d w2d = Matrix3d.PlaneToWorld(vpr.ViewDirection);
w2d = Matrix3d.Displacement(vpr.Target - Point3d.Origin) * w2d;
w2d = Matrix3d.Rotation(-vpr.ViewTwist, vpr.ViewDirection, vpr.Target) * w2d;
w2d = w2d.Inverse();
var p = tr.Database.Extmin; // The background fetch value is {(1E+. 20,1E+20,1E+20)}
Extents3d ext = new(tr.Database.Extmin, tr.Database.Extmax);
ext.TransformBy(w2d);


double width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
if (width &amp;gt; (height * sc))
height = width / sc;
Point3d center = ext.MaxPoint.GetMidPointTo(ext.MinPoint);


vpr.Height = height;
vpr.Width = height * sc;
vpr.CenterPoint = center.Point2d();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ok,i fix it ,the follow is anser,also thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; Matrix3d w2d = Matrix3d.PlaneToWorld(vpr.ViewDirection);
 w2d = Matrix3d.Displacement(vpr.Target - Point3d.Origin) * w2d;
 w2d = Matrix3d.Rotation(-vpr.ViewTwist, vpr.ViewDirection, vpr.Target) * w2d;
 w2d = w2d.Inverse();
 var p = tr.Database.Extmin;
 Extents3d ext = new();
 using var ms = (BlockTableRecord)tr.GetObject(tr.Database.CurrentSpaceId, OpenMode.ForRead);
 foreach (ObjectId id in ms)
 {
     using Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
     ext.AddExtents(entity.GeometricExtents);
 }

 //Extents3d ext = new(tr.Database.Extmin, tr.Database.Extmax);
 ext.TransformBy(w2d);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Feb 2025 15:56:43 GMT</pubDate>
    <dc:creator>Medithaibet</dc:creator>
    <dc:date>2025-02-19T15:56:43Z</dc:date>
    <item>
      <title>zoomwindow does wrong extend</title>
      <link>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9969436#M17679</link>
      <description>&lt;P&gt;use this code.It work well for some file.But zoom window is not correct when zoomwin in "test.dwg"(see the attachment). My AutoCAD version is 2019.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void ZoomWindow(this Editor ed, Point3d pt1, Point3d pt2)
{ 
    using (ViewTableRecord view = ed.GetCurrentView())
    { 
         view.Width = pt1.X - pt1.X;
         view.Height = pt2.Y - pt2.Y;
         view.CenterPoint = new Point2d(pt1.X + (view.Width / 2), 
                      pt1.Y + (view.Height / 2));
         ed.SetCurrentView(view);
      }         
}
[CommandMethod("TestZoomWin", CommandFlags.UsePickSet | CommandFlags.Redraw)]
 public static void OutSelAtrs()
 {       
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = HostApplicationServices.WorkingDatabase;
    Editor ed = doc.Editor;
    TypedValue[] values = new TypedValue[]
            {
                new TypedValue((int)DxfCode.Start, "Insert")
             };
    SelectionFilter filter = new SelectionFilter(values);  
    PromptSelectionResult ents;
    if (ed.SelectImplied().Status == PromptStatus.OK &amp;amp;&amp;amp; ed.SelectImplied().Value.Count &amp;gt; 0)
        ents = ed.GetSelection(filter);
    else
    {
       PromptSelectionOptions pso = new PromptSelectionOptions { MessageForAdding = "Please Select a Block" };
       ents = ed.GetSelection(pso, filter);
       if (ents.Status == PromptStatus.Cancel)
       {
         return;
       }
       else if (ents.Status != PromptStatus.OK)
       {
                    return;
        }
    }
    SelectionSet allselect = ents.Value;
   using (Transaction trans = 
 doc.Database.TransactionManager.StartTransaction())
  {
      foreach (ObjectId id in allselect.GetObjectIds())
      {        
         BlockReference brf = (BlockReference)trans.GetObject(id, OpenMode.ForRead);
          if (brf != null)
          {
                        Extents3d ext = brf.GeometryExtentsBestFit();
                        Point3d p1 = ext.MinPoint;// - brf.BlockTransform.Translation;
                        Point3d p2 = ext.MaxPoint;// - brf.BlockTransform.Translation; 
                        ed.ZoomWindow(p1, p2);
                       
           }

     }
       trans.Commit();
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2020 15:11:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9969436#M17679</guid>
      <dc:creator>风过水无痕</dc:creator>
      <dc:date>2020-12-30T15:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: zoomwindow does wrong extend</title>
      <link>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9969560#M17680</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use these extension methods for different zooms. they work whatever the current view and/or UCS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

using System;
using System.Collections.Generic;
using System.Linq;

namespace ZoomExtensionSample
{
    static class Extension
    {
        public static Matrix3d WorldToEye(this AbstractViewTableRecord view)
        {
            Assert.IsNotNull(view, nameof(view));
            return
                Matrix3d.WorldToPlane(view.ViewDirection) *
                Matrix3d.Displacement(view.Target.GetAsVector().Negate()) *
                Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);
        }

        public static void Zoom(this Editor ed, Extents3d ext)
        {
            Assert.IsNotNull(ed, nameof(ed));
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                ext.TransformBy(view.WorldToEye());
                view.Width = ext.MaxPoint.X - ext.MinPoint.X;
                view.Height = ext.MaxPoint.Y - ext.MinPoint.Y;
                view.CenterPoint = new Point2d(
                    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
                    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
                ed.SetCurrentView(view);
            }
        }

        public static void ZoomExtents(this Editor ed)
        {
            Database db = ed.Document.Database;
            db.UpdateExt(false);
            Extents3d ext = (short)Application.GetSystemVariable("cvport") == 1 ?
                new Extents3d(db.Pextmin, db.Pextmax) :
                new Extents3d(db.Extmin, db.Extmax);
            ed.Zoom(ext);
        }

        public static void ZoomWindow(this Editor ed, Point3d p1, Point3d p2)
        {
            var extents = new Extents3d();
            extents.AddPoint(p1);
            extents.AddPoint(p2);
            ed.Zoom(extents);
        }

        public static void ZoomObjects(this Editor ed, IEnumerable&amp;lt;ObjectId&amp;gt; ids)
        {
            Assert.IsNotNull(ed, nameof(ed));
            Assert.IsNotNull(ids, nameof(ids));
            using (var tr = new OpenCloseTransaction())
            {
                Extents3d ext = ids
                    .Select(id =&amp;gt; (Entity)tr.GetObject(id, OpenMode.ForRead))
                    .Select(ent =&amp;gt; ent.GeometricExtents)
                    .Aggregate((e1, e2) =&amp;gt; { e1.AddExtents(e2); return e1; });
                ed.Zoom(ext);
                tr.Commit();
            }
        }

        public static void ZoomScale(this Editor ed, double scale)
        {
            Assert.IsNotNull(ed, nameof(ed));
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                view.Width /= scale;
                view.Height /= scale;
                ed.SetCurrentView(view);
            }
        }

        public static void ZoomCenter(this Editor ed, Point3d center, double scale = 1.0)
        {
            Assert.IsNotNull(ed, nameof(ed));
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                center = center.TransformBy(view.WorldToEye());
                view.Height /= scale;
                view.Width /= scale;
                view.CenterPoint = new Point2d(center.X, center.Y);
                ed.SetCurrentView(view);
            }
        }
    }

    public static class Assert
    {
        public static void IsNotNull&amp;lt;T&amp;gt;(T obj, string paramName) where T : class
        {
            if (obj == null)
                throw new ArgumentNullException(paramName);
        }
    }
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You test command should be:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TestZoomWin", CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public static void OutSelAtrs()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            var selection = ed.GetSelection(filter);
            if (selection.Status == PromptStatus.OK)
            {
                ed.ZoomObjects(selection.Value.GetObjectIds());
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 30 Dec 2020 16:12:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9969560#M17680</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-12-30T16:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: zoomwindow does wrong extend</title>
      <link>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9971819#M17681</link>
      <description>&lt;P&gt;Thank very mucn.Your codes are very cool!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jan 2021 01:26:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/9971819#M17681</guid>
      <dc:creator>风过水无痕</dc:creator>
      <dc:date>2021-01-01T01:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: zoomwindow does wrong extend</title>
      <link>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/13328099#M17682</link>
      <description>&lt;P&gt;Hello gile,now i have another wrong that it is like this:&lt;/P&gt;&lt;P&gt;in this line i fetch wrong point,so i want know how to fix this bug when background use.&lt;BR /&gt;var p = tr.Database.Extmin; //&amp;nbsp;The background fetch value is {(1E+. 20,1E+20,1E+20)}&lt;/P&gt;&lt;P&gt;Thank you first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var tr = DBTrans.Top;
tr.Database.TileMode = true;
using ViewportTable vpt = (ViewportTable)tr.GetObject(tr.Database.ViewportTableId);
ObjectId aid = vpt["*Active"];
using ViewportTableRecord vpr = (ViewportTableRecord)tr.GetObject(aid, OpenMode.ForWrite);
double sc = vpr.Width / vpr.Height;


Matrix3d w2d = Matrix3d.PlaneToWorld(vpr.ViewDirection);
w2d = Matrix3d.Displacement(vpr.Target - Point3d.Origin) * w2d;
w2d = Matrix3d.Rotation(-vpr.ViewTwist, vpr.ViewDirection, vpr.Target) * w2d;
w2d = w2d.Inverse();
var p = tr.Database.Extmin; // The background fetch value is {(1E+. 20,1E+20,1E+20)}
Extents3d ext = new(tr.Database.Extmin, tr.Database.Extmax);
ext.TransformBy(w2d);


double width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
if (width &amp;gt; (height * sc))
height = width / sc;
Point3d center = ext.MaxPoint.GetMidPointTo(ext.MinPoint);


vpr.Height = height;
vpr.Width = height * sc;
vpr.CenterPoint = center.Point2d();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ok,i fix it ,the follow is anser,also thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; Matrix3d w2d = Matrix3d.PlaneToWorld(vpr.ViewDirection);
 w2d = Matrix3d.Displacement(vpr.Target - Point3d.Origin) * w2d;
 w2d = Matrix3d.Rotation(-vpr.ViewTwist, vpr.ViewDirection, vpr.Target) * w2d;
 w2d = w2d.Inverse();
 var p = tr.Database.Extmin;
 Extents3d ext = new();
 using var ms = (BlockTableRecord)tr.GetObject(tr.Database.CurrentSpaceId, OpenMode.ForRead);
 foreach (ObjectId id in ms)
 {
     using Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
     ext.AddExtents(entity.GeometricExtents);
 }

 //Extents3d ext = new(tr.Database.Extmin, tr.Database.Extmax);
 ext.TransformBy(w2d);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 15:56:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/zoomwindow-does-wrong-extend/m-p/13328099#M17682</guid>
      <dc:creator>Medithaibet</dc:creator>
      <dc:date>2025-02-19T15:56:43Z</dc:date>
    </item>
  </channel>
</rss>

