Author Topic: SystemVariableChanged event  (Read 3995 times)

0 Members and 1 Guest are viewing this topic.

ekohl

  • Guest
SystemVariableChanged event
« on: April 05, 2011, 02:53:27 AM »
Hi everyone,

Following event is found in autocad help, works fine, until you change layout.
Then it is not triggered anymore.
has anyone this problem,
or you know how I can solve this?
Please let me know!

I'm using Autocad 2011.

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Public Class Useri1Event
    <CommandMethod("AddAppEvent")> Public Sub AddAppEvent()
        AddHandler Application.SystemVariableChanged, AddressOf appSysVarChanged
    End Sub
    <CommandMethod("RemoveAppEvent")> Public Sub RemoveAppEvent()
        RemoveHandler Application.SystemVariableChanged, AddressOf appSysVarChanged
    End Sub
    Public Sub appSysVarChanged(ByVal senderObj As Object, ByVal sysVarChEvtArgs As Autodesk.AutoCAD.ApplicationServices.SystemVariabl​eChangedEventArgs)
        Dim oVal As Object = Application.GetSystemVariable(sysVarChEvtArgs.Name​)
        '' Display a message box with the system variable name and the new value
        Application.ShowAlertDialog(sysVarChEvtArgs.Name & " was changed." & vbLf & "New value: " & oVal.ToString())

        'End If
    End Sub
End Class

kaefer

  • Guest
Re: SystemVariableChanged event
« Reply #1 on: April 05, 2011, 06:48:47 AM »
Following event is found in autocad help, works fine, until you change layout.
Then it is not triggered anymore.

You mean this here?

I find it pretty clever of AutoCAD that it's able to detect a modal dialog blocking its progress and to deregister the offending event handler. Try a modeless dialog instead.

Lots of fun...

ekohl

  • Guest
Re: SystemVariableChanged event
« Reply #2 on: April 05, 2011, 06:57:19 AM »
Thanks for your reply!

This code I have already tried without dialog box,
 even then the event is deregisterd.
 Also you can not re-register the event.

kaefer

  • Guest
Re: SystemVariableChanged event
« Reply #3 on: April 05, 2011, 07:21:54 AM »
This code I have already tried without dialog box,
 even then the event is deregisterd.
 Also you can not re-register the event.

Errm, right. Read disabled for deregistered. That ain't clever, actually, but still better than an application crash.

I've tried a statusbar pane as display mechanism, here's the F# code. Doesn't work satisfactorily, since you'll only see the last sysvar change effected by a command.

Code: [Select]
type acApp = Autodesk.AutoCAD.ApplicationServices.Application

let lazyPane =
    lazy(
        let tmp =
            new Pane(
                Enabled = true,
                Visible = true,
                Style = PaneStyles.Normal )
        acApp.StatusBar.Panes.Add tmp |> ignore
        acApp.StatusBar.Update()
        tmp )
let svceh =
    new SystemVariableChangedEventHandler(
        fun _ e ->
            let oVal = Application.GetSystemVariable(e.Name)
            lazyPane.Value.Text <- e.Name
            lazyPane.Value.ToolTipText <-
                e.Name + " was changed." + "\nNew value: " + string oVal
            acApp.StatusBar.Update() )
           
[<CommandMethod "AddAppEvent">]
let AddAppEvent() =
    acApp.SystemVariableChanged.AddHandler svceh

[<CommandMethod "RemoveAppEvent">]
let RemoveAppEvent() =
    acApp.SystemVariableChanged.RemoveHandler svceh
    acApp.StatusBar.Panes.Remove lazyPane.Value
    acApp.StatusBar.Update()

finnishflash

  • Guest
Re: SystemVariableChanged event
« Reply #4 on: April 07, 2011, 11:27:24 AM »
I'd say it's a crash, or the handler crashes due to unhandled exception(s). Enclose the sysvar retrieval in a try/catch block. I noticed you'll get exceptions only when switching to an empty Layout.

       public void appSysVarChanged(object senderObj, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs sysVarChEvtArgs)
        {
            try
            {
                object oVal = Application.GetSystemVariable(sysVarChEvtArgs.Name);

                // Display a message box with the system variable name and the new value
                Application.ShowAlertDialog(sysVarChEvtArgs.Name + " was changed." + "\nNew value: " + oVal.ToString());
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nappSysVarChanged: " + sysVarChEvtArgs.Name + " - " + ex.Message + "\n");
            }
        }


Output:
Command:   <Switching to: Layout1>
Regenerating layout.
appSysVarChanged: PLIMMIN - eInvalidInput
appSysVarChanged: PLIMMAX - eInvalidInput
appSysVarChanged: PLIMMIN - eInvalidInput
appSysVarChanged: PLIMMAX - eInvalidInput
appSysVarChanged: PLIMMIN - eInvalidInput
appSysVarChanged: PLIMMAX - eInvalidInput
Regenerating layout.


Jeff H

  • Needs a day job
  • Posts: 6150
Re: SystemVariableChanged event
« Reply #5 on: April 07, 2011, 04:45:07 PM »
Quote
This callback always occurs when a system variable is changed directly through the SETVAR command or by entering the variable name at the command line. For other built-in AutoCAD commands that provoke system variable changes, this notification is not guaranteed.

If I understand your question correctly some events will not notify the event handler and in the past I had to find a event that would be caught then check against a variable that held the previous setting or something similar to varify that the change has happened.

ekohl

  • Guest
Re: SystemVariableChanged event
« Reply #6 on: April 08, 2011, 07:35:48 AM »
Thanks for all your response,
By applying the code to try and catch the program works perfectly!

Here is the code I used.


    Private Sub appSysVarChanged(ByVal senderObj As Object, ByVal sysVarChEvtArgs As Autodesk.AutoCAD.ApplicationServices.SystemVariableChangingEventArgs)

        Try
            Dim oVal As Object = Application.GetSystemVariable(sysVarChEvtArgs.Name)
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("appSysVarChanged: " + sysVarChEvtArgs.Name + vbCr)
        Catch
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("appSysVarChanged: " + sysVarChEvtArgs.Name + "-" + "er is een fout aangetroffen!" + vbCr)
        End Try

    End Sub