.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Zoom to the selected points extents or window.

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
newautocad123
1109 Views, 4 Replies

Zoom to the selected points extents or window.

In a dwg file there some features and and select some of them and want to zoom to that selected features extents only.

or how can I get the extents for that collection so that I can zoom to it.

Please suggest

4 REPLIES 4
Message 2 of 5
_gile
in reply to: newautocad123

Hi,

 

Assuming ids is the selected entities objectId array and ed the current Editor

 

Here's a 'classical' way.

Using : ZoomObjects(ids)

        private void ZoomObjects(ObjectId[] ids)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                Matrix3d WCS2DCS =
                    (Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                    Matrix3d.Displacement(view.Target - Point3d.Origin) *
                    Matrix3d.PlaneToWorld(view.ViewDirection))
                    .Inverse();
                Entity ent = (Entity)tr.GetObject(ids[0], OpenMode.ForRead);
                Extents3d ext = ent.GeometricExtents;
                for (int i = 1; i < ids.Length; i++)
                {
                    ent = (Entity)tr.GetObject(ids[i], OpenMode.ForRead);
                    ext.AddExtents(ent.GeometricExtents);
                }
                ext.TransformBy(WCS2DCS);
                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);
                tr.Commit();
            }
        }

 

 

 Here's a more generic one using extension methods for the Editor type (others zoom methods can be added to this class, calling or not the Editor.Zoom(Extents3d ext) one).

Using: ed.ZoomObjects(ids) // ids may be any type implementing IEnumerabe<ObjectId>

    static class ZoomExtensions
    {
        // Returns the transformation matrix from the ViewTableRecord DCS to WCS
        public static Matrix3d EyeToWorld(this ViewTableRecord view)
        {
            return
                Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                Matrix3d.Displacement(view.Target - Point3d.Origin) *
                Matrix3d.PlaneToWorld(view.ViewDirection);
        }

        // Returns the transformation matrix from WCS to the ViewTableRecord DCS
        public static Matrix3d WorldToEye(this ViewTableRecord view)
        {
            return view.EyeToWorld().Inverse();
        }

        // Process a zoom according to the extents3d in the current viewport
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            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);
            }
        }

        // Process a zoom objects in the current viewport
        public static void ZoomObjects(this Editor ed, IEnumerable<ObjectId> ids)
        {
            using (Transaction tr = ed.Document.TransactionManager.StartTransaction())
            {
                Extents3d ext = ids
                    .Select(id => ((Entity)tr.GetObject(id, OpenMode.ForRead)).GeometricExtents)
                    .Aggregate((e1, e2) => { e1.AddExtents(e2); return e1; });
                ed.Zoom(ext);
                tr.Commit();
            }
        }
    }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5
newautocad123
in reply to: _gile

 

I tried to add object to the db and it did not work.

 

Thanks. It helped me lot.

Message 4 of 5
_gile
in reply to: newautocad123

Sorry I do not understand what you mean: "I tried to add object to the db and it did not work."



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5
newautocad123
in reply to: _gile

Your solution works well for me. What I had tried before posting this in the forum was the one below, which did not work. I was in the wrong track.

 

Thanks.

 

Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
            using (tr)
            {
                foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId oId in oidList)
                {
                    DBObject obj = tr.GetObject(oId, OpenMode.ForRead);
                    db.AddDBObject(obj);
                }
                tr.Commit();
            }
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            ViewTableRecord view = new ViewTableRecord();
            Point3d center = db.Extmin + ((db.Extmax - db.Extmin) / 2);

            view.CenterPoint = new Autodesk.AutoCAD.Geometry.Point2d(center.X, center.Y);
            view.Height = db.Extmax.Y - db.Extmin.Y;
            view.Width = db.Extmax.X - db.Extmin.X;
            editor.SetCurrentView(view);

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost