Unable to suppress occurrence in subassembly

Unable to suppress occurrence in subassembly

daniel.puchta
Enthusiast Enthusiast
984 Views
7 Replies
Message 1 of 8

Unable to suppress occurrence in subassembly

daniel.puchta
Enthusiast
Enthusiast

Hello everyone

I have this VBA code that check for each occurrence if it is suppressed. If yes it will unsuppress. Then it recursively calls itself if the occurrence is assembly.

Sub Main()
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = oAsmDoc.ComponentDefinition
    Dim oOccurrences As ComponentOccurrences
    Set oOccurrences = oAsmCompDef.Occurrences
    Zapnuti_occurrence oOccurrences, oAsmDoc
End Sub
Sub Zapnuti_occurrence(oOccurrences As ComponentOccurrences, oDoc As Inventor.AssemblyDocument)
    Dim oSuppress_Occurrence As Boolean
    Dim oModelState As ModelState
    Dim oModelState_created As Boolean
    
    oModelState_created = False
    For Each oOccurrence In oOccurrences 'tady to změnit na list
        oSuppress_Occurrence = False
        If oOccurrence.Suppressed = True Then
            If oModelState_created = False Then
                Set oModelState = ThisApplication.ActiveDocument.ComponentDefinition.ModelStates.Add("Part2Suppressed2")
            End If
            oOccurrence.Unsuppress
            oSuppress_Occurrence = True
        End If
        'do stuff...
        'If oSuppress_Occurrence = True Then
        '    oSuppress_Occurrence = False
        '    oOccurrence.Suppress
        'End If
    
        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
            If InStr(oOccurrence.Definition.Document.DisplayName, new_element) = 0 Then
                oDoc.Update
            End If
            Set oDoc = ThisApplication.Documents.Open(oOccurrence.Definition.Document.FullFileName)
                            
            
            Zapnuti_occurrence oOccurrence.SubOccurrences, oDoc
            
            oDoc.Save2
            oDoc.Close
        End If
    Next
End Sub

 When trying to unsuppress occurrence of subassembly, it throws this error:

Unsuppress error.png

 It does not matter if before trying to suppress occurrence It creates new model state or not. Please do you know how to solve this problem?

0 Likes
Accepted solutions (1)
985 Views
7 Replies
Replies (7)
Message 2 of 8

CattabianiI
Collaborator
Collaborator

It's the desired behavior and pointed out in the LOD to MS migration document (https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/important-for-developers-working-with-l...

Suppress a ComponentOccurrenceProxy is not allowed.

To achieve what you want you should work in the context of oOccurrence parent document. 

Or consider to make the occurrence invisible instead if it's just what you need.
0 Likes
Message 3 of 8

Stakin
Collaborator
Collaborator

 

Sub Main()
    Dim oAsmDoc As Inventor.AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
'    Dim oAsmCompDef As AssemblyComponentDefinition
'    Set oAsmCompDef = oAsmDoc.ComponentDefinition
'    Dim oOccurrences As ComponentOccurrences
'    Set oOccurrences = oAsmCompDef.Occurrences
    Zapnuti_occurrence(oAsmDoc)
End Sub
Sub Zapnuti_occurrence(oDoc As Inventor.AssemblyDocument)
    Dim oAsmCompDef As AssemblyComponentDefinition
    oAsmCompDef = oDoc.ComponentDefinition
    Dim oOccurrences As ComponentOccurrences
	Dim oOccurrence As ComponentOccurrence
    oOccurrences = oAsmCompDef.Occurrences
'    Dim oSuppress_Occurrence As Boolean
'    Dim oModelState As ModelState
'    Dim oModelState_created As Boolean
    
'    oModelState_created = False
    For Each oOccurrence In oOccurrences 'tady to změnit na list
'        oSuppress_Occurrence = False
        If oOccurrence.Suppressed = True Then
'            If oModelState_created = False Then
'                Set oModelState = ThisApplication.ActiveDocument.ComponentDefinition.ModelStates.Add("Part2Suppressed2")
'            End If
            oOccurrence.Unsuppress
'            oSuppress_Occurrence = True
        End If
        'do stuff...
        'If oSuppress_Occurrence = True Then
        '    oSuppress_Occurrence = False
        '    oOccurrence.Suppress
        'End If
    
        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
			    Dim oSubAsmCompDoc As AssemblyDocument
    				oSubAsmCompDoc = oOccurrence.Definition.Document
'            If InStr(oOccurrence.Definition.Document.DisplayName, new_element) = 0 Then
'                oDoc.Update
'            End If
'            oDoc = ThisApplication.Documents.Open(oOccurrence.Definition.Document.FullFileName)
                            
            
            Call Zapnuti_occurrence(oSubAsmCompDoc) 
            
'            oDoc.Save2
'            oDoc.Close
        End If
    Next
End Sub

Which inventor version are you use?

In the 2021,there is no modelstate .

 

 

Try this

0 Likes
Message 4 of 8

daniel.puchta
Enthusiast
Enthusiast

Thank you for your advice, could you be more specific what do you meant? Maybe post example of code?

I have modified the code that it is unsuppressing suboccurrences but this does not work (it throws error).
Heres the code:

Sub Main()
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
'    Dim oAsmCompDef As AssemblyComponentDefinition
'    Set oAsmCompDef = oAsmDoc.ComponentDefinition
'    Dim oOccurrences As ComponentOccurrences
'    Set oOccurrences = oAsmCompDef.Occurrences
    Zapnuti_occurrence oAsmDoc
End Sub
Sub Zapnuti_occurrence(oDoc As Inventor.AssemblyDocument)
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = oDoc.ComponentDefinition
    Dim oOccurrences As ComponentOccurrences
    Dim oSubOccurrence As ComponentOccurrence
    Dim oOccurrence As ComponentOccurrence
    Set oOccurrences = oAsmCompDef.Occurrences
    
'    Dim oSuppress_Occurrence As Boolean
'    Dim oModelState As ModelState
'    Dim oModelState_created As Boolean
    
'    oModelState_created = False
    For Each oOccurrence In oOccurrences 'tady to změnit na list
'        oSuppress_Occurrence = False
        If oOccurrence.Suppressed = True Then
'            If oModelState_created = False Then
'                Set oModelState = ThisApplication.ActiveDocument.ComponentDefinition.ModelStates.Add("Part2Suppressed2")
'            End If
            oOccurrence.Unsuppress
'            oSuppress_Occurrence = True
        End If
        'do stuff...
        'If oSuppress_Occurrence = True Then
        '    oSuppress_Occurrence = False
        '    oOccurrence.Suppress
        'End If
    
        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
                Dim oSubAsmCompDoc As AssemblyDocument
                Set oSubAsmCompDoc = oOccurrence.Definition.Document
'            If InStr(oOccurrence.Definition.Document.DisplayName, new_element) = 0 Then
'                oDoc.Update
'            End If
'            oDoc = ThisApplication.Documents.Open(oOccurrence.Definition.Document.FullFileName)
             For Each oSubOccurrence In oOccurrence.SubOccurrences
             
                 If oSubOccurrence.Suppressed = True Then
        '            If oModelState_created = False Then
        '                Set oModelState = ThisApplication.ActiveDocument.ComponentDefinition.ModelStates.Add("Part2Suppressed2")
        '            End If
                    oSubOccurrence.Unsuppress
        '            oSuppress_Occurrence = True
                End If
                
             Next
            
            Call Zapnuti_occurrence(oSubAsmCompDoc)
            
'            oDoc.Save2
'            oDoc.Close
        End If
    Next
End Sub
0 Likes
Message 5 of 8

daniel.puchta
Enthusiast
Enthusiast

Sorry I work with Inventor 2022, so there are model states and the behaviour of occurrences and suboccurrences is different..

0 Likes
Message 6 of 8

j.haggenjos
Advocate
Advocate
Accepted solution

What if you change this:

Zapnuti_occurrence oOccurrence.SubOccurrences, oDoc

 

To this:

Zapnuti_occurrence oDoc.ComponentDefinition.Occurrences, oDoc
Message 7 of 8

daniel.puchta
Enthusiast
Enthusiast

Thank you, it works!
Here is the final code:

Sub Main()
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = oAsmDoc.ComponentDefinition
    Dim oOccurrences As ComponentOccurrences
    Set oOccurrences = oAsmCompDef.Occurrences
    Zapnuti_occurrence oOccurrences, oAsmDoc
End Sub
Sub Zapnuti_occurrence(oOccurrences As ComponentOccurrences, oDoc As Inventor.AssemblyDocument)
    Dim oSuppress_Occurrence As Boolean
    Dim oModelState As ModelState
    Dim oModelState_created As Boolean
    
    oModelState_created = False
    For Each oOccurrence In oOccurrences 'tady to změnit na list
        oSuppress_Occurrence = False
        If oOccurrence.Suppressed = True Then
            oOccurrence.Unsuppress
            oSuppress_Occurrence = True
        End If
        'do stuff...
        'If oSuppress_Occurrence = True Then
        '    oSuppress_Occurrence = False
        '    oOccurrence.Suppress
        'End If
    
        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
            If InStr(oOccurrence.Definition.Document.DisplayName, new_element) = 0 Then
                oDoc.Update
            End If
            Set oDoc = ThisApplication.Documents.Open(oOccurrence.Definition.Document.FullFileName)
                            
            
            Zapnuti_occurrence oDoc.ComponentDefinition.Occurrences, oDoc
            
            oDoc.Save2
            oDoc.Close
        End If
    Next
End Sub
0 Likes
Message 8 of 8

Stakin
Collaborator
Collaborator
Sub Main()
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
    Zapnuti_occurrence  oAsmDoc
End Sub
Sub Zapnuti_occurrence( oDoc As Inventor.AssemblyDocument)
    Dim oSuppress_Occurrence As Boolean
    Dim oOccurrences As ComponentOccurrences
        oOccurrences =oDoc.ComponentDefinition.Occurrences
    Dim oModelState As ModelState
    Dim oModelState_created As Boolean
    
    oModelState_created = False
    For Each oOccurrence In oOccurrences 'tady to změnit na list
        oSuppress_Occurrence = False
        If oOccurrence.Suppressed = True Then
            oOccurrence.Unsuppress
            oSuppress_Occurrence = True
        End If

        If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then
            If InStr(oOccurrence.Definition.Document.DisplayName, new_element) = 0 Then
                oDoc.Update
            End If
            Rem In the Opened Asm File,All the oOccurrence were opened in memeroy,needn't use the document open method again; try this.
            Zapnuti_occurrence oOccurrence.Definition.Document
        End If
    Next
oDoc.Save
End Sub

In the Opened Asm File,All the oOccurrence were opened in memeroy,needn't use the document open method again; try this.