.NET

.NET

Reply
Valued Contributor
newautocad123
Posts: 58
Registered: ‎09-03-2012
Message 1 of 5 (293 Views)
Accepted Solution

Zoom to the selected points extents or window.

293 Views, 4 Replies
12-06-2012 10:38 PM

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

*Expert Elite*
_gile
Posts: 2,082
Registered: ‎04-29-2006
Message 2 of 5 (283 Views)

Re : Zoom to the selected points extents or window.

12-07-2012 12:44 AM 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
Valued Contributor
newautocad123
Posts: 58
Registered: ‎09-03-2012
Message 3 of 5 (277 Views)

Re : Zoom to the selected points extents or window.

12-07-2012 01:28 AM in reply to: _gile

 

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

 

Thanks. It helped me lot.

*Expert Elite*
_gile
Posts: 2,082
Registered: ‎04-29-2006
Message 4 of 5 (270 Views)

Re : Zoom to the selected points extents or window.

12-07-2012 02:02 AM 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
Valued Contributor
newautocad123
Posts: 58
Registered: ‎09-03-2012
Message 5 of 5 (268 Views)

Re : Zoom to the selected points extents or window.

12-07-2012 02:34 AM 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);

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.