zoomwindow does wrong extend

zoomwindow does wrong extend

风过水无痕
Enthusiast Enthusiast
923 Views
3 Replies
Message 1 of 4

zoomwindow does wrong extend

风过水无痕
Enthusiast
Enthusiast

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.

 

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 && ed.SelectImplied().Value.Count > 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();
  }
}

 

 

0 Likes
Accepted solutions (1)
924 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use these extension methods for different zooms. they work whatever the current view and/or UCS.

 

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<ObjectId> ids)
        {
            Assert.IsNotNull(ed, nameof(ed));
            Assert.IsNotNull(ids, nameof(ids));
            using (var tr = new OpenCloseTransaction())
            {
                Extents3d ext = ids
                    .Select(id => (Entity)tr.GetObject(id, OpenMode.ForRead))
                    .Select(ent => ent.GeometricExtents)
                    .Aggregate((e1, e2) => { 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<T>(T obj, string paramName) where T : class
        {
            if (obj == null)
                throw new ArgumentNullException(paramName);
        }
    }
}

 

You test command should be:

        [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());
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

风过水无痕
Enthusiast
Enthusiast

Thank very mucn.Your codes are very cool!

0 Likes
Message 4 of 4

Medithaibet
Contributor
Contributor

Hello gile,now i have another wrong that it is like this:

in this line i fetch wrong point,so i want know how to fix this bug when background use.
var p = tr.Database.Extmin; // The background fetch value is {(1E+. 20,1E+20,1E+20)}

Thank you first.

 

 

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 > (height * sc))
height = width / sc;
Point3d center = ext.MaxPoint.GetMidPointTo(ext.MinPoint);


vpr.Height = height;
vpr.Width = height * sc;
vpr.CenterPoint = center.Point2d();

 

 

ok,i fix it ,the follow is anser,also thank you 

 

 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);

 

0 Likes