Do zoom extents in C# .NET (AutoCAD)

Do zoom extents in C# .NET (AutoCAD)

Anonymous
Not applicable
10,685 Views
7 Replies
Message 1 of 8

Do zoom extents in C# .NET (AutoCAD)

Anonymous
Not applicable

Hi!!

I am developing a plugin for AutoCAD and I want to Know how to do Zoom Extents in C# .NET 

 

 

0 Likes
Accepted solutions (1)
10,686 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

 

The shorter and simpler way is to call the COM AcadApplication.ZoomExtents() method. This can be done without referencing COM libraries by using the dynamic type

 

dynamic acadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
acadApp.ZoomExtents();


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

_gile
Consultant
Consultant
Accepted solution

You can also use the following extension method:

 

    public static class EditorExtension
    {
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            if (ed == null)
                throw new ArgumentNullException("ed");
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                Matrix3d worldToEye = Matrix3d.WorldToPlane(view.ViewDirection) *
                    Matrix3d.Displacement(Point3d.Origin - view.Target) *
                    Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);
                ext.TransformBy(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);
            }
        }

        public static void ZoomExtents(this Editor ed)
        {
            Database db = ed.Document.Database;
            db.UpdateExt(false);
            Extents3d ext = (short)Application.GetSystemVariable("cvport") == 1 ?
                new Extents3d(db.Pextmin, db.Pextmax) :
                new Extents3d(db.Extmin, db.Extmax);
            ed.Zoom(ext);
        }
    }

 

 Using

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.ZoomExtents();


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 8

Anonymous
Not applicable

Hi Gile,

 

I was trying to use your code to zoom extents and I'm getting an eNotApplicable error in the line

 

using (ViewTableRecord view = ed.GetCurrentView())

 

Basically the codes that call this methos is 

DocumentCollection docCollection = Application.DocumentManager;

doc = docCollection.Open(dwg.ToString(), false, null);
Application.DocumentManager.MdiActiveDocument = doc;

using (doc.LockDocument())
{
LayoutManager.Current.CurrentLayout = "Model";

doc.Editor.ZoomExtents(); ----->Here I get the exception

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

Hi,

There's nothing wrong with the code you show. The error is due to something else.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

Anonymous
Not applicable
Thanks Gilles, looks like it was a session flag problem.
0 Likes
Message 7 of 8

1603567506
Observer
Observer

If I write it this way, he will shrink the window and move the coordinates to a very large position. Why is that?

0 Likes
Message 8 of 8

nshupeFMPE3
Advocate
Advocate

@_gile wrote:

Hi,

 

The shorter and simpler way is to call the COM AcadApplication.ZoomExtents() method. This can be done without referencing COM libraries by using the dynamic type

 

dynamic acadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
acadApp.ZoomExtents();

May I ask a question about this? You mention not needing to reference COM libraries. I recently went through a lot of learning around COM and the C++ api for AutoCAD in order to get this code from Kean to work for adding properties to the Properties window in ACAD. But in using dynamic like you show above, are you accessing the underlying objects, not just those that are exposed to .NET? 

0 Likes