Reply

AddIn - ModelRecomputeEvents

Hello Solid Edge Community,

 

I'm trying to code an addin which renames features in the pathfinder when I add or modify a feature in a part document.

I’m using Solid Edge ST8 and Visual Studio 2010.

 

It seems I need to use the ModelRecomputeEvents interface for this macro. ModelRecomputeEvents is a part of the Model collection of Part document.

 

To detect my events, I used this code:

 

Private pPartEvents As SolidEdgeFramework.ModelRecomputeEvents

Private ListPartEvents As List(Of SolidEdgeFramework.ModelRecomputeEvents) = Nothing

Public Sub AfterActiveDocumentChange(ByVal theDocument As Object)

        On Error Resume Next

 

        'Si on a un document d'ouvert, on verifie son type

        If Not theDocument Is Nothing Then

            'In part document, for each model, I get ModelRecomputeEvents

        If pApplication.ActiveDocumentType = DocumentTypeConstants.igPartDocument Then

                Dim ObjDocument As SolidEdgePart.PartDocument

                ObjDocument = theDocument

'for each model in my part document, I would like to detect ModelRecomputeEvents

                ListPartEvents = New List(Of SolidEdgeFramework.ModelRecomputeEvents)

                For Each modelbody As SolidEdgePart.Model In ObjDocument.Models

                    pPartEvents = modelbody.ModelRecomputeEvents

                    ListPartEvents.Add(pPartEvents)

                Next

                AddOrRemoveEventHandlersPart(True)

                'sinon, on les enleve

                If Not ObjDocument Is Nothing Then

                    Marshal.ReleaseComObject(ObjDocument)

                    ObjDocument = Nothing

                End If

            Else

                If Not ListPartEvents Is Nothing Then

                    AddOrRemoveEventHandlersPart(False)

                End If

            End If

        End If

 

        If Not theDocument Is Nothing Then

            System.Runtime.InteropServices.Marshal.ReleaseComObject(theDocument)

            theDocument = Nothing

        End If

End Sub

Private Sub AddOrRemoveEventHandlersPart(ByVal Add As Boolean)

        If Not ListPartEvents Is Nothing Then

            If Add Then

                For Each modelevent As SolidEdgeFramework.ModelRecomputeEvents In ListPartEvents

                    AddHandler modelevent.AfterFeatureIsAdded, AddressOf AfterFeatureIsAdded

                    AddHandler modelevent.AfterFeatureIsModified, AddressOf AfterFeatureIsModified

                    AddHandler modelevent.AfterRecompute, AddressOf AfterRecompute

                Next

            Else

                For Each modelevent As SolidEdgeFramework.ModelRecomputeEvents In ListPartEvents

                    RemoveHandler modelevent.AfterFeatureIsAdded, AddressOf AfterFeatureIsAdded

                    RemoveHandler modelevent.AfterFeatureIsModified, AddressOf AfterFeatureIsModified

                    RemoveHandler modelevent.AfterRecompute, AddressOf AfterRecompute

 

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(modelevent)

                Next

                ListPartEvents = Nothing

            End If

        End If

    End Sub

 

My problem is that pPartEvents = Nothing in my code: ModelRecomputeEvents seems not to be empty, but it’s impossible to assign those events in pPartEvents or in my list.

 

Where did I make a mistake? How can I detect my events “AfterFeatureIsAdded” and “AfterFeatureIsModified”  in a opened part document ?

 

Thanks,