Author Topic: Zoom in a new database  (Read 1604 times)

0 Members and 1 Guest are viewing this topic.

Offline rom1

  • Visual Basic
  • *
  • Posts: 21
  • Karma: +3/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Zoom in a new database
« on: August 30, 2012, 07:10:44 AM »
Hi,

A little piece of code in C#.Net in order to define the default view/zoom in a drawing which is not active in the editor. For example, it's useful for a newly created database than you create and save without activating it in the editor.

I share this code because i've spent some hours to find it. Most of the examples we found always concern an open and active document and use the editor.

The 2 main tricks are to use the viewporttablerecord whose name is "*Active" and the method database.UpdateExt() once the viewporttablerecord has been modified.

(Code adapted from this dicussion: http://forums.autodesk.com/t5/NET/Zoom-Extents-on-new-Database/td-p/2070825/highlight/true/page/2 )

Code: [Select]
try
{
    if (sideDb.TileMode == true)// Then 'Model
    {
        ViewportTable vpt = sideTr.GetObject(sideDb.ViewportTableId, OpenMode.ForRead) as ViewportTable;
        ViewportTableRecord vptr = sideTr.GetObject(vpt["*Active"], OpenMode.ForWrite) as ViewportTableRecord;
        Point2d pmin = Point2d.Origin;
        Point2d pmax = new Point2d(3000, 1500);
        vptr.CenterPoint = (pmin + (pmax - pmin) / 2);
        vptr.Height = (pmax.Y - pmin.Y);
        vptr.Width = (pmax.X - pmin.X);
        sideDb.UpdateExt(true);
    }
}
catch { }
« Last Edit: August 30, 2012, 07:15:03 AM by rom1 »

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: Zoom in a new database
« Reply #1 on: August 31, 2012, 06:22:30 PM »
Hi rom1,

Nice trick.
I tried to go a little further and get this work whatever the current view (i.e. an isometric or orbited view).
The main trick is to transform points from World System Coordinates (WCS) to the view Display Coordinate System (DCS) as shown for ZoomObjects or in GeomtryExtensions Editor or Viewport extension methods.
On the other hand, it seems that Database.UpdateExt() is not mandatory. From the tests I did all work fine without calling it.

So, here're some extension methods (yes, I like this feature) used to set a zoom extents in a side Database model space.

Code: [Select]
        /// <summary>
        /// Returns the transformation matrix from the ViewportTableRecord DCS to WCS.
        /// </summary>
        /// <param name="view">The ViewportTableRecord instance this method applies to.</param>
        /// <returns>The DCS to WCS transformation matrix.</returns>
        public static Matrix3d EyeToWorld(this ViewportTableRecord view)
        {
            return
                Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                Matrix3d.Displacement(view.Target - Point3d.Origin) *
                Matrix3d.PlaneToWorld(view.ViewDirection);
        }

        /// <summary>
        /// Returns the transformation matrix from the ViewportTableRecord WCS to DCS.
        /// </summary>
        /// <param name="view">The ViewportTableRecord instance this method applies to.</param>
        /// <returns>The WCS to DCS transformation matrix.</returns>
        public static Matrix3d WorldToEye(this ViewportTableRecord view)
        {
            return view.EyeToWorld().Inverse();
        }

        /// <summary>
        /// Sets a zoom extents of the Database model space
        /// </summary>
        /// <param name="db">The Database instance this method applies to.</param>
        public static void ZoomExtents(this Database db)
        {
            db.TileMode = true;
            Point2d scrSize = (Point2d)AcAp.GetSystemVariable("screensize");
            double ratio = scrSize.X / scrSize.Y;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (Line line = new Line(db.Extmin, db.Extmax))
            {
                ViewportTable vpt =
                    (ViewportTable)tr.GetObject(db.ViewportTableId, OpenMode.ForRead);
                ViewportTableRecord vptr =
                    (ViewportTableRecord)tr.GetObject(vpt["*Active"], OpenMode.ForWrite);
                Extents3d ext = line.GeometricExtents;
                ext.TransformBy(vptr.WorldToEye());
                Point2d pmin = new Point2d(ext.MinPoint.X, ext.MinPoint.Y);
                Point2d pmax = new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y);
                double height = pmax.Y - pmin.Y;
                double width = pmax.X - pmin.X;
                if (width / height < ratio)
                {
                    vptr.Height = height;
                    vptr.Width = height * ratio;
                }
                else
                {
                    vptr.Width = width;
                    vptr.Height = width / ratio;
                }
                vptr.CenterPoint = pmin + (pmax - pmin) / 2.0;
                tr.Commit();
            }
        }

A test command example:
Code: [Select]
        [CommandMethod("zedb")]
        public void ZoomExtentsDb()
        {
            using (Database db = new Database())
            {
                db.ReadDwgFile(@"F:\Dessin1.dwg", System.IO.FileShare.ReadWrite, false, "");
                db.ZoomExtents();
                db.SaveAs(@"F:\Dessin1.dwg", DwgVersion.Current);
            }
        }
« Last Edit: September 02, 2012, 07:06:26 AM by (gile) »