Author Topic: Viewport ScaletoFit problem  (Read 2090 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Viewport ScaletoFit problem
« on: May 09, 2012, 02:44:29 PM »
I'm "wblocking" some objects out to another database.  In the new database I want to create a paperspace viewport showing a southeast isometric view zoomed into the "wblocked" objects when the user opens the dwg later.

Here is the block of code where I create the new database and insert the new viewport.

extents is an Extents3d with each entitys GeometricExtents added to it in a loop earlier in the program.
Code: [Select]
                    var xdb = new Database(true, false);
                    xdb.ReadDwgFile(path, FileOpenMode.OpenForReadAndWriteNoShare, true, null);

                    using (Transaction xtr = xdb.TransactionManager.StartTransaction())
                    {
                            var ps = xdb.PaperSpace(OpenMode.ForWrite);
                           
                            var vp = new Viewport();
                            vp.SetDatabaseDefaults(xdb);
                            vp.CenterPoint = new Point3d(19.75, 14.75, 0);
                            vp.Width = 38.125;
                            vp.Height = 27.375;
                            ps.AppendEntity(vp);
                            xtr.AddNewlyCreatedDBObject(vp, true);

                            vp.On = true;;
                            vp.ViewDirection = new Vector3d(1, -1, 1);
                            vp.ViewCenter = Point2d.Origin;
                            vp.ViewTarget = extents.MinPoint + ((extents.MaxPoint - extents.MinPoint) / 2);
                            vp.StandardScale = StandardScaleType.ScaleToFit;
                            vp.UpdateDisplay();

                            xtr.Commit();
                    }



Everything shows up in the new viewport when I open the drawing except its not scaled to fit.  Its about half the scale it needs to be to take up the entire viewport.  If I select the viewport and select Scale to Fit  it looks exactly as I want it.  Can someone tell me what I'm missing here.
Revit 2019, AMEP 2019 64bit Win 10

TheMaster

  • Guest
Re: Viewport ScaletoFit problem
« Reply #1 on: May 11, 2012, 02:05:03 PM »
I'm "wblocking" some objects out to another database.  In the new database I want to create a paperspace viewport showing a southeast isometric view zoomed into the "wblocked" objects when the user opens the dwg later.

Here is the block of code where I create the new database and insert the new viewport.

extents is an Extents3d with each entitys GeometricExtents added to it in a loop earlier in the program.
Code: [Select]
                    var xdb = new Database(true, false);
                    xdb.ReadDwgFile(path, FileOpenMode.OpenForReadAndWriteNoShare, true, null);

                    using (Transaction xtr = xdb.TransactionManager.StartTransaction())
                    {
                            var ps = xdb.PaperSpace(OpenMode.ForWrite);
                           
                            var vp = new Viewport();
                            vp.SetDatabaseDefaults(xdb);
                            vp.CenterPoint = new Point3d(19.75, 14.75, 0);
                            vp.Width = 38.125;
                            vp.Height = 27.375;
                            ps.AppendEntity(vp);
                            xtr.AddNewlyCreatedDBObject(vp, true);

                            vp.On = true;;
                            vp.ViewDirection = new Vector3d(1, -1, 1);
                            vp.ViewCenter = Point2d.Origin;
                            vp.ViewTarget = extents.MinPoint + ((extents.MaxPoint - extents.MinPoint) / 2);
                            vp.StandardScale = StandardScaleType.ScaleToFit;
                            vp.UpdateDisplay();

                            xtr.Commit();
                    }



Everything shows up in the new viewport when I open the drawing except its not scaled to fit.  Its about half the scale it needs to be to take up the entire viewport.  If I select the viewport and select Scale to Fit  it looks exactly as I want it.  Can someone tell me what I'm missing here.

Scaling the view to fit objects in any orientation isn't simple to do, and I'm pretty sure the way AutoCAD does that is by calling each object's viewportDraw() method, passing in a 'dummy' AcGiViewportGeometry object that records calls to draw primitives (lines, curves, etc.),  and simply computes the min/max extents based on the coordinates passed into those methods.

Unfortunately, there is no way to do that in managed code. In native code, it requires you to implement your own AcGiViewportGeometry object to capture the calls to its methods and compute the extents in a given view orientation, and you must then call the worldDraw() or viewportDraw() methods of each entity involved, and pass it your custom AcGiViewportGeometry instance.

There may be some undocumented native APIs that can help with that, but I've never looked into it.

What you could do, is stick some data in the database that you could check for when the drawing is first opened, and use that to set the view to what you need at the point when the drawing is first opened in the editor, and then erase the information.

« Last Edit: May 11, 2012, 02:11:14 PM by TheMaster »