Get a notification when a flat pattern changes?

By Xiaodong Liang

Issue
We need to get a notification when a flat pattern changes by an internal update of Inventor. For example when we change a sketch geometries in a model with an existing flat pattern, the flat pattern automatically gets updated by Inventor. DocumentEvents::OnChange fires, but how can we know that the flat pattern has been really changed when we only switch from flat pattern to folded model, without any change in the real flat pattern.

Solution
You could check Context if it is switching between folder mode and flatten mode. This code snippet below assumes you have had an event class which implements OnChange.

Dim oDocument As Document 
Dim oDocumentEvents As DocumentEvents
Private Sub startEvents()   
    oDocument = m_invApp.ActiveDocument 
    oDocumentEvents = oDocument.DocumentEvents
 
    AddHandler oDocumentEvents.OnChange,AddressOf oDocumentEvents_OnChange
End Sub
Private Sub printContext(Context As NameValueMap)    
 
For j = 1 To Context.count    
 
     Try
       Dim oEachContext As object  = _
                Context.Item(j)       
      ' has sub context
       If typeof oEachContext is Array Then
         For k = 0 To oEachContext.Length -1
 
             Debug.Print ( "[SubContext Name]" & _
                          oEachContext(k))
        Next
       Else
            Debug.Print( "[Context Name] "  &   _
                        Context.Name(j)  & _
                            "Value:"  &  _
                            Context.Value(Context.Name(j) ) ) 
       End If     
 
 
    Catch ex As Exception
 
    End Try                
Next   
 
End Sub
Private Sub oDocumentEvents_OnChange( _
ByVal ReasonsForChange As Inventor.CommandTypesEnum, _
 ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
 ByVal Context As Inventor.NameValueMap, _ 
 ByRef HandlingCode As Inventor.HandlingCodeEnum)
 
    If TypeOf m_invApp.ActiveEditObject Is FlatPattern Then
 
        Dim isSwitching As Boolean
        isSwitching = False
        If Context.count = 2 Then
            For I = 1 To Context.count
                If Context.Value(Context.Name(I)) =  _
              "Switch Part Representation" Then
                    ' is swtiching
                    isSwitching = True
                    Exit For
                End If
            Next
        End If
        If isSwitching Then
            Debug.Print("isSwitching")
        Else
            Debug.Print("flat pattern is changed")
            Call printContext(Context)
        End If
    End If

 
End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading