• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Posts: 58
    Registered: ‎09-03-2012
    Accepted Solution

    Zoom to the selected points extents or window.

    172 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

    Please use plain text.
    *Expert Elite*
    Posts: 1,639
    Registered: ‎04-29-2006

    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
    Please use plain text.
    Valued Contributor
    Posts: 58
    Registered: ‎09-03-2012

    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.

    Please use plain text.
    *Expert Elite*
    Posts: 1,639
    Registered: ‎04-29-2006

    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
    Please use plain text.
    Valued Contributor
    Posts: 58
    Registered: ‎09-03-2012

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

     

    Please use plain text.