Overwrite double click on sketched symbol

Overwrite double click on sketched symbol

wannensn
Enthusiast Enthusiast
522 Views
4 Replies
Message 1 of 5

Overwrite double click on sketched symbol

wannensn
Enthusiast
Enthusiast

Hi,

 

I programmatically place some sketched symbols in a drawing. When the user double-clicks on such a symbol I want to open a custom dialog and not the standard Inventor dialog.

 

How can this be done?

 

Thanks,

Stephan

0 Likes
Accepted solutions (1)
523 Views
4 Replies
Replies (4)
Message 2 of 5

Michael.Navara
Advisor
Advisor
Accepted solution

You can handle OnDoubleClick event from UserInputEvents. This is a small example how to do it in iLogic. Main code is in class SketchedSymbolDoubleClickHandler. Usage of SharedVariable is necessary to keep the handler alive between rule edits. 

 

 

 

    Sub Main()
        Dim doubleClickHandler As SketchedSymbolDoubleClickHandler
        Dim name = "DoubleClickHandler"
        If SharedVariable.Exists(name) Then
            doubleClickHandler = SharedVariable.Value(name)
        Else
            doubleClickHandler = New SketchedSymbolDoubleClickHandler(ThisApplication)
            SharedVariable.Value(name) = doubleClickHandler
        End If

        If doubleClickHandler.IsActive Then
            doubleClickHandler.StopHandling()
        Else
            doubleClickHandler.StartHandling()
        End If

    End Sub



    Class SketchedSymbolDoubleClickHandler
        Implements IDisposable

        Private ReadOnly inventor As Application
        Private userInputEvents As UserInputEvents

        Public Sub New(inventor As Inventor.Application)
            Me.inventor = inventor
        End Sub

        Public Sub StartHandling()
            userInputEvents = inventor.CommandManager.UserInputEvents
            AddHandler userInputEvents.OnDoubleClick, AddressOf UserInputEvents_OnDoubleClick
            IsActive = True
        End Sub

        Public Sub StopHandling()
            If userInputEvents Is Nothing Then Return

            RemoveHandler userInputEvents.OnDoubleClick, AddressOf UserInputEvents_OnDoubleClick
            userInputEvents = Nothing
            IsActive = False
        End Sub

        Public Property IsActive As Boolean

        Private Sub UserInputEvents_OnDoubleClick(selectedentities As ObjectsEnumerator,
                                                  selectiondevice As SelectionDeviceEnum,
                                                  button As MouseButtonEnum,
                                                  shiftkeys As ShiftStateEnum,
                                                  modelposition As Point,
                                                  viewposition As Point2d,
                                                  view As View,
                                                  additionalinfo As NameValueMap,
                                                  ByRef handlingcode As HandlingCodeEnum)

            Dim sketchedSymbol As SketchedSymbol = Nothing
            For Each entity As Object In selectedentities
                If TypeOf entity Is SketchedSymbol Then
                    sketchedSymbol = entity
                    Exit For
                End If
            Next

            If sketchedSymbol Is Nothing Then
                handlingcode = HandlingCodeEnum.kEventNotHandled
                Exit Sub
            End If


            'Implement your logic here
            Dim msgList As New List(Of String)
            msgList.Add("Name: " & sketchedSymbol.Name)
            msgList.Add("Button: " & [Enum].GetName(GetType(MouseButtonEnum), button))
            msgList.Add("ShiftKeys: " & [Enum].GetName(GetType(ShiftStateEnum), shiftkeys))
            msgList.Add(String.Format("Model position: [{0:N3}, {1:N3}, {2:N3}]", modelposition.X, modelposition.Y, modelposition.Z))
            msgList.Add(String.Format("View position: [{0}, {1}]", viewposition.X, viewposition.Y))

            Dim msg = String.Join(vbCrLf, msgList)
            MsgBox(msg, Title:="OnDoubleClick")

            handlingcode = HandlingCodeEnum.kEventHandled

        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            StopHandling()
        End Sub
    End Class

 

 

0 Likes
Message 3 of 5

Ralf_Krieg
Advisor
Advisor

Hello

 

@Michael.Navara 

How do you "start" the handler? OnOpenDocument event? What if a second drawing is opened? As I see the handler is stopped at this moment. Please, can you explain in short why you implement IDisposable?

Would love to see a solution handling such events in iLogic in a proper way. Until now I always switch to an Addin at this point.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 4 of 5

Michael.Navara
Advisor
Advisor

 

For production is much better to use it in addin, but for this forum I need some way, how to test the code without any addin implementation.

 

This example is iLogic script and you can start them in standard way by "Run Rule".

After that the shared variable "DoubleClickHandler" is ensured and its value is instance of class "SketchedSymbolDoubleClickHandler". This is because I need to keep the handler alive between rule edits. (Not necessary when you use it in addin.)

 

After that I switch the state of handler Active/Inactive.

 

I implement interface IDisposable for cleanup reason, because I need to remove the event handler. For cleanup I use my own external rule instead of standard SharedVariable.RemoveAll() method. In standard way the list of shared variables is cleared, but if you have an event handler active, the object can not be removed from memory and the event handler is still active.

This is my rule to cleanup SharedVariables object. It is little bit hackish, but it works for debugging purposes.

Dim bindingFlags As Reflection.BindingFlags = System.Reflection.BindingFlags.NonPublic + System.Reflection.BindingFlags.GetField + System.Reflection.BindingFlags.Instance
Dim variables = SharedVariable.GetType().InvokeMember("m_ruleTempStorage", bindingFlags, Nothing, SharedVariable, Nothing)
Logger.Debug(variables.Count)
For Each keyValue In variables
    Dim v = keyValue.Value
    Logger.Debug(v.GetType().Name)
    If TypeOf v Is IDisposable Then
        v.Dispose
        Logger.Debug(" - Disposed")
    End If
Next
SharedVariable.RemoveAll()

 The similar solution I use for AddIns also, because I can keep many handlers in one collection and I can clean up them in single For Each loop.

 

Message 5 of 5

wannensn
Enthusiast
Enthusiast
Thanks, works perfect for me. I use the code in an addin.
--Stephan
0 Likes