Autocad event Handler goes into an infinite loop

Autocad event Handler goes into an infinite loop

inversiones_edgar_itriago
Enthusiast Enthusiast
920 Views
3 Replies
Message 1 of 4

Autocad event Handler goes into an infinite loop

inversiones_edgar_itriago
Enthusiast
Enthusiast

Hello, I'm writing a C # program that retrieves the CENTERVIEW of any existing floating viewport when it is activated by user (Double Click). I have added an Event Handler to currentViewportChanged, but the event handler goes into an infinite loop when any Viewport is activated, see code in the attached file. Thanks!

 

 

 

 

 

0 Likes
Accepted solutions (1)
921 Views
3 Replies
Replies (3)
Message 2 of 4

5thSth
Advocate
Advocate

depending on how "clean" your code you want to be, 

 

I suggest either:

1) use messagefilter to look for mouse kliks, so your event to count only after mouse klik has been accepted.

 

2) use a timer and allow only one eventhandler to trigger per second

 

 

0 Likes
Message 3 of 4

_gile
Consultant
Consultant

Hi

 

This works for me:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.ComponentModel;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace ViewportToWorldCoordinates
{
    public class Commands
    {
        [CommandMethod("EV")]
        public void UIBindingsNotificationExample()
        {
            INotifyPropertyChanged source = Application.UIBindings.CurrentViewport as INotifyPropertyChanged;
            source.PropertyChanged += currentViewportChanged;
        }

        void currentViewportChanged(object sender, PropertyChangedEventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            // Check to see if the Model layout is active
            if (db.TileMode)
            {
                // Model layout
            }
            else
            {
                var cvport = (short)AcAp.GetSystemVariable("CVPORT");
                if (cvport == 1)
                {
                    // paper space
                }
                else
                {
                    using (var tr = db.TransactionManager.StartTransaction())
                    {
                        var vp = (Viewport)tr.GetObject(ed.ActiveViewportId, OpenMode.ForRead);
                        Matrix3d EyeToWorld =
                            Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) *
                            Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) *
                            Matrix3d.WorldToPlane(vp.ViewDirection);
                        var ptCenter = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0).TransformBy(EyeToWorld);
                        ed.WriteMessage("\n*** Current Viewport= {0} ViewVenter=  {1},{2}\n", cvport, ptCenter.X, ptCenter.Y);
                        tr.Commit();
                    }
                }
            }
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

inversiones_edgar_itriago
Enthusiast
Enthusiast
Accepted solution

Ok, I check your code, the event ceased the infinite loop, but now executes 3 and 4 times consecutives, but at least improved je je je.

0 Likes