Zoom Extents on new Database

Zoom Extents on new Database

Anonymous
Not applicable
6,090 Views
16 Replies
Message 1 of 17

Zoom Extents on new Database

Anonymous
Not applicable
I have created a new database:
Database newDb = new Database(true, true)
Inserted a block into modelspace
and now I want to zoom extent before saving.

Can this be done working on a new database in memory?
all the examples I have seen on this board use Interop on a open document.

Does anyone have a point in the right direction
0 Likes
6,091 Views
16 Replies
Replies (16)
Message 2 of 17

Anonymous
Not applicable
Others have posted lots of good working methodologies ... search on zoomextents

Maybe these lines will give you a start ....

AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
db.TileMode = false;//*PAPER_SPACE
db.TileMode = true;//*MODEL_SPACE
app.ZoomExtents();
------------------
LayoutManager layoutMgr = LayoutManager.Current;
layoutMgr.CurrentLayout = "Model";
AcadApplication pApp = (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
pApp.ZoomExtents();
db.UpdateExt(true);
0 Likes
Message 3 of 17

Anonymous
Not applicable
Thanks for the reply

It seems Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication
ONLY apply to the document currently displayed in autocad.

Maybe I am missing a step but let me try to explain the problem again.
I have a drawing (drawing1) open.
I run my command that creates a NEW database in memory.
I insert a block in the NEW database (in memory) and I want to
Zoom extents and then save the database (drawing2).
When I use the examples in this forum including yours it zoom extents alright but in drawing1, not the new database.

I am still learning so I could be overlooking something
Any help is appreciated.
0 Likes
Message 4 of 17

Anonymous
Not applicable
I'm not sure, but I don't think that ActiveX commands can be applied to the in-memory drawing.
Again, I might be mistaken.
0 Likes
Message 5 of 17

Anonymous
Not applicable
I don't think you can zoom a *database*.
A *document* is required.
0 Likes
Message 6 of 17

Ed__Jobe
Mentor
Mentor
Correct, it has to be the current dwg in the editor to do display changes.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 17

Anonymous
Not applicable
Who told you that?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5726557@discussion.autodesk.com...
Correct, it has to be the current dwg in the editor to do display changes.
0 Likes
Message 8 of 17

Anonymous
Not applicable
Thanks everyone

Maybe I can come at it from a different angle then.
When I complete the operations to the database save it and then open it as a drawing in AutoCAD, the view has been set for me, not where I would like (the extents of the block inserted)

How does one control where the initial view is located when creating a database?

Or is that view default and the need to specify has been overlooked?
0 Likes
Message 9 of 17

Anonymous
Not applicable
Tony,
Is there a way to "zoom" a database?
I made my comment based on my attempts at doing just that. I browsed the object model but saw no zoom functionality associated with the databaseservices. I only found zoom used with the application object and that only effected the active document.
Am I missing something? I know that there are plenty of undocumented or otherwise hidden pieces to the programming puzzle.

By the way, your CommandLine.cs worked fine for zooming but again that would only work on the currently active drawing.
0 Likes
Message 10 of 17

Anonymous
Not applicable
SO maybe zoom extents is not what I want.

Is there a way to specify the initial view on a new database.
The view is set by autocad when you create a new db can we change that default view I guess is the question?

Anyone know how?
Is it possible
0 Likes
Message 11 of 17

Anonymous
Not applicable
Yes Mark, there is a way to "zoom a database"

In the case of a database that's not open in the
AutoCAD editor, "zoom" means to set the view
parameters to whatever is required to achieve
the desired view (e.g., the center, magnification,
direction, etc.).

If you think about it, AutoCAD must read something
in the drawing file to determine the initial view
when it opens the file (which for a drawing that was
previously open in the editor, is the view that was
current when the file was saved).

See this thread for the gory details:

http://discussion.autodesk.com/adskcsp/thread.jspa?messageID=5541432

For zooming to the extents, you could call the
UpdateExts() method of the Database, and then
read the Extmin and Extmax properties to get
the box that has to fit in the view. If the view
is not plan, then it becomes a bit more difficult.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5727528@discussion.autodesk.com...

Tony,
Is there a way to "zoom" a database?
I made my comment based on my attempts at doing just that. I browsed the object model but saw no zoom functionality associated with the databaseservices. I only found zoom used with the application object and that only effected the active document.
Am I missing something? I know that there are plenty of undocumented or otherwise hidden pieces to the programming puzzle.

By the way, your CommandLine.cs worked fine for zooming but again that would only work on the currently active drawing. Message was edited by: Discussion Admin
0 Likes
Message 12 of 17

Anonymous
Not applicable
See my reply to Mark.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5727623@discussion.autodesk.com...
SO maybe zoom extents is not what I want.

Is there a way to specify the initial view on a new database.
The view is set by autocad when you create a new db can we change that default view I guess is the question?

Anyone know how?
Is it possible
0 Likes
Message 13 of 17

Anonymous
Not applicable
Anyboby has a sample of code in paperspace ? This function works well in modelspace :

Sub ZoomExtents(ByVal myDB As Autodesk.AutoCAD.DatabaseServices.Database)
Dim tm As Autodesk.AutoCAD.DatabaseServices.Transaction = myDB.TransactionManager.StartTransaction
Try
If myDB.TileMode = True Then 'Model
Dim vpt As ViewportTable = tm.GetObject(myDB.ViewportTableId, OpenMode.ForRead)
Dim vptr As ViewportTableRecord = tm.GetObject(vpt.Item("*Active"), OpenMode.ForWrite)
Dim pt3Max As Point3d = myDB.Extmax
Dim pt3Min As Point3d = myDB.Extmin
Dim pt2Max As Autodesk.AutoCAD.Geometry.Point2d = New Autodesk.AutoCAD.Geometry.Point2d(pt3Max.X, pt3Max.Y)
Dim pt2Min As Autodesk.AutoCAD.Geometry.Point2d = New Autodesk.AutoCAD.Geometry.Point2d(pt3Min.X, pt3Min.Y)
vptr.CenterPoint = (pt2Min + (pt2Max - pt2Min) / 2.0)
vptr.Height = (pt2Max.Y - pt2Min.Y)
vptr.Width = (pt2Max.X - pt2Min.X)
myDB.UpdateExt(True)
Else 'Papier



End If
Catch ex As Exception
MsgBox("Erreur de zoom, " & ex.Message)
Finally
tm.Commit()
tm.Dispose()
End Try
End Sub
0 Likes
Message 14 of 17

Anonymous
Not applicable

Thanks cblais for the code. I needed to use it, but in order for this to work a myDB.UpdateExt(True) must be executed before retrieving the Extmax and Extmin values just as Tony Z stated on 9/20/07.

 

aks

0 Likes
Message 15 of 17

Ajilal.Vijayan
Advisor
Advisor

I am trying change the code mentioned in here to use for paperspace.

I tried by changing the code as highlighted below and its not working.

Can anyone help me to show what I am doing wrong or any another way to zoom extent the paperspace of a document which is not active in the editor ?

 

        /// <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 = false;
            Point2d scrSize = (Point2d)AcAp.GetSystemVariable("screensize");
            double ratio = scrSize.X / scrSize.Y;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (Line line = new Line(db.Pextmin, db.Pextmax))
            {
                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();
            }
        }

 

0 Likes
Message 16 of 17

ActivistInvestor
Mentor
Mentor

I don't know if you can easily adapt the code you posted or not, because it is dependent on a number of things that require the document and the paperspace layout to be active (for example, the SCREENSIZE system variable, the *Active ViewportTableRecord, etc.). If the document is not active in the editor 

 

I'm not suggesting it isn't possible to do, but given how the code is written, a great deal of it is dependent on the active document and layout.

 

Ajilal.Vijayan wrote:

I am trying change the code mentioned in here to use for paperspace.

I tried by changing the code as highlighted below and its not working.

Can anyone help me to show what I am doing wrong or any another way to zoom extent the paperspace of a document which is not active in the editor ?

 

        /// <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 = false;
            Point2d scrSize = (Point2d)AcAp.GetSystemVariable("screensize");
            double ratio = scrSize.X / scrSize.Y;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (Line line = new Line(db.Pextmin, db.Pextmax))
            {
                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();
            }
        }

 


 

0 Likes
Message 17 of 17

Ajilal.Vijayan
Advisor
Advisor

Thanks for the reply @ActivistInvestor

In modelsapce this code works with the the documents which are not active in the editor.

So I tried with _db.GetViewports(true) and used the first objectid of that collection to get the Viewport.

This seems to be working.

 

ObjectIdCollection vpcol = _db.GetViewports(true);
                    if (vpcol.Count > 0)//Check whether the layout is initialiazed
                    {
                        Viewport vport = (Viewport)_tr.GetObject(vpcol[0], OpenMode.ForWrite);
                        vport.ViewHeight = (width / height) < ratio ? height : width / ratio;
                        vport.ViewCenter = pmin + (pmax - pmin) / 2.0;
                    }

 

0 Likes