.NET

Reply
Member
daydream17
Posts: 4
Registered: ‎07-16-2014
Message 1 of 5 (240 Views)
Accepted Solution

Problem with execute 'SendStringToExecute'

240 Views, 4 Replies
07-30-2014 07:56 PM

Hello all

 

I'm trying to zoom the drawing before doing next functions in a same UI event.

But 'SendStringToExecute' method execute after finishing the event function.

 

Is there any way to execute "Zoom" right after like below example?

 

public void Zoom()

{

     Doc.SendStringToExecute("_.zoom _all ", true, false, true);

     // "zoom" drawing and than execute next funcfion.. 

     next function(int);

}

 

Hi,

 

I forgot to mention the RunCommand() method does not work in Application context execution. This was checked in an older wrapper (always by Tony Tanzillo).

 

Anyway, as I said upper, I think it's a better way to avoid calling commands.

 

This will work either in document context execution (modal form) or in application context (modeless form):

 

...
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
...

private void button1_Click(object sender, System.EventArgs e) { Document doc = AcAp.DocumentManager.MdiActiveDocument; using (doc.LockDocument()) { Database db = doc.Database; Extents3d ext = (short)AcAp.GetSystemVariable("cvport") == 1 ? new Extents3d(db.Pextmin, db.Pextmax) : new Extents3d(db.Extmin, db.Extmax); Editor ed = doc.Editor; using (ViewTableRecord view = ed.GetCurrentView()) { Matrix3d worldToEye = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) * Matrix3d.Displacement(view.Target - Point3d.Origin) * Matrix3d.PlaneToWorld(view.ViewDirection) .Inverse(); 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); } }

 

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 2 of 5 (224 Views)

Re : Problem with execute 'SendStringToExecute'

07-30-2014 10:28 PM in reply to: daydream17

Hi,

 

This is a well known issue with SendStringToExecute() (and SendCommand()) method wich runs asynchronously.

 

If you're targeting AutoCAD 2015, use the new Editor.Command() method. for prior versions, you can use Tony Tanzillo's (diningPhilosopher) wrapper for the undocumented RunCommand() method.

 

But in my opinion, the best way is to avoid calling commands and hard code a Zoom(Extents) method.

You'll find an example here or can get some inspiration from the following extension methods.

 

    public static class ExtensionMethods
    {
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            if (ed == null)
                throw new ArgumentNullException("ed");

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

        public static void ZoomExtents(this Editor ed)
        {
            if (ed == null)
                throw new ArgumentNullException("ed");

            Database db = ed.Document.Database;
            Extents3d ext = (short)Application.GetSystemVariable("cvport") == 1 ?
                new Extents3d(db.Pextmin, db.Pextmax) :
                new Extents3d(db.Extmin, db.Extmax);
            ed.Zoom(ext);
        }

        public static void ZoomWindow(this Editor ed, Point3d p1, Point3d p2)
        {
            using (Line line = new Line(p1, p2))
            {
                ed.Zoom(line.GeometricExtents);
            }
        }
        
        public static Matrix3d EyeToWorld(this ViewTableRecord view)
        {
            if (view == null)
                throw new ArgumentNullException("view");

            return
                Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                Matrix3d.Displacement(view.Target - Point3d.Origin) *
                Matrix3d.PlaneToWorld(view.ViewDirection);
        }

        public static Matrix3d WorldToEye(this ViewTableRecord view)
        {
            return view.EyeToWorld().Inverse();
        }
    }

 

Gilles Chanteau
Member
daydream17
Posts: 4
Registered: ‎07-16-2014
Message 3 of 5 (168 Views)

Re : Problem with execute 'SendStringToExecute'

08-02-2014 01:04 AM in reply to: _gile

Thanks _hile

 

I checked Tranzillo's one and 'Command' mathod works well in the [CommandMethod]

but when I use ModelessDialog that method doesn't work..

 

It doesn't make sense to me

is there any reason why it's different with both [CommandMethod] and [Dialog] type?

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 4 of 5 (158 Views)

Re : Problem with execute 'SendStringToExecute'

08-02-2014 05:42 AM in reply to: daydream17

Hi,

 

I forgot to mention the RunCommand() method does not work in Application context execution. This was checked in an older wrapper (always by Tony Tanzillo).

 

Anyway, as I said upper, I think it's a better way to avoid calling commands.

 

This will work either in document context execution (modal form) or in application context (modeless form):

 

...
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
...

private void button1_Click(object sender, System.EventArgs e) { Document doc = AcAp.DocumentManager.MdiActiveDocument; using (doc.LockDocument()) { Database db = doc.Database; Extents3d ext = (short)AcAp.GetSystemVariable("cvport") == 1 ? new Extents3d(db.Pextmin, db.Pextmax) : new Extents3d(db.Extmin, db.Extmax); Editor ed = doc.Editor; using (ViewTableRecord view = ed.GetCurrentView()) { Matrix3d worldToEye = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) * Matrix3d.Displacement(view.Target - Point3d.Origin) * Matrix3d.PlaneToWorld(view.ViewDirection) .Inverse(); 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); } }

 

Gilles Chanteau
Member
daydream17
Posts: 4
Registered: ‎07-16-2014
Message 5 of 5 (111 Views)

Re : Problem with execute 'SendStringToExecute'

08-04-2014 04:55 PM in reply to: _gile

Thanks Gilles

 

Yes, I also think hard coding 'Zoom' method is better than just calling commands

Above sample code was good for my job and I could have checked 'Lock Document'

 

Thanks!!

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.