Delete All event triggers

Delete All event triggers

Anonymous
Not applicable
1,955 Views
6 Replies
Message 1 of 7

Delete All event triggers

Anonymous
Not applicable

After a year of reasearching iParts and iAssemblies i want to contribute to the community with some code i use.

If in a Ipart/Iassembly Factory you assign an event trigger to a rule, the event triggers are also  assigned to its members but the rules aren't, so you get annoying messeges.

 

So to delete ALL  triggers in the assembly(Parts,Assemblies,Iparts and Iasseblies ) i made this rule:


SyntaxEditor Code Snippet

Sub Main
Auto = iLogicVb.Automation
Dim iLogicAuto As Object
iLogicAuto = Auto 
Call deletecurrent()
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
Dim oCompDef As Inventor.ComponentDefinition = oDoc.ComponentDefinition
Dim occ As ComponentOccurrence

For Each occ In oCompDef.Occurrences
isum=isum+1
Next

For i=1 To isum
occ = oDoc.ComponentDefinition.Occurrences(i)
If occ.Name="" Then Goto 1:
occDocument= occ.Definition.Document
asm = ThisApplication.Documents.Open(occ.Definition.Document.fullfilename,False)
Call RemoveRulesfromEventTrigger(occ)
asm.Close(True)

If occDocument.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then 
    If occ.Definition.IsiPartMember Then
    factoryDoc = occ.Definition.iPartMember.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
    asm = ThisApplication.Documents.Open(factoryDoc,False)
    
    Call RemoveRulesfromEventTriggerIpart(occ)
    
    asm.Close(True)
    End If
End If


If occDocument.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
    If occ.Definition.IsiAssemblyMember Then
    factoryDoc = occ.Definition.iAssemblyMember.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
    asm = ThisApplication.Documents.Open(factoryDoc,False)
    
    Call RemoveRulesfromEventTriggerIAssembly(occ)
    
    asm.Close(True)
    End If
Call ProcessFactoryFile(occ)
End If
1:
Next

End Sub



Sub ProcessFactoryFile ( occ As ComponentOccurrence)

oDoc = occ.Definition.Document
Dim oCompDef As Inventor.ComponentDefinition = oDoc.ComponentDefinition
Dim Subocc As ComponentOccurrence

isum=0


For Each Subocc In oCompDef.Occurrences
isum=isum+1
Next

For i=1 To isum

subocc = oDoc.ComponentDefinition.Occurrences(i)
suboccDocument= subocc.Definition.Document

If subocc.Name="" Then Goto 1:

asm = ThisApplication.Documents.Open(subocc.Definition.Document.fullfilename,False)
Call RemoveRulesfromEventTrigger(subocc)
asm.Close(True)
If suboccDocument.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then 
    If subocc.Definition.IsiPartMember Then
    factoryDoc = subocc.Definition.iPartMember.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
    asm = ThisApplication.Documents.Open(factoryDoc,False)
    
    Call RemoveRulesfromEventTriggerIpart(subocc)
    
    asm.Close(True)
    End If
End If

If suboccDocument.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
If subocc.Definition.IsiAssemblyMember Then
    factoryDoc = subocc.Definition.iAssemblyMember.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
    asm = ThisApplication.Documents.Open(factoryDoc,False)
    
    Call RemoveRulesfromEventTriggerIAssembly(subocc)
    
    asm.Close(True)
    End If
Call ProcessFactoryFile(subocc)
End If
1:
Next
End Sub

Sub RemoveRulesfromEventTrigger(occ As ComponentOccurrence)

    'On Error Resume Next    
  

    Auto = iLogicVb.Automation
    Dim iLogicAuto As Object
    iLogicAuto = Auto 
    Dim oiLogicDoc As Document 
    

    'capture current document
    oiLogicDoc = occ.Definition.Document
    'load up ilogic automation
    Dim oiLogicAuto As Object = iLogicVb.Automation
    'get container for all of the ilogic rules
    Dim oRules As Object = oiLogicAuto.rules(oiLogicDoc)
    'variable for the events rules
    Dim oiLogicPropSet As PropertySet
    'set up a counter
    Dim iPropCount As Integer
    

   Try
    'set our property set since we know it exists now
    oiLogicPropSet = oiLogicDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
    'oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules")
        
    Dim strDeletedTriggers As String
    'iterate through the triggers And delete Each
    
    If Not oiLogicPropSet.Count=0 Then
   For i As Integer = oiLogicPropSet.Count To 0 Step -1
        oiLogicPropSet.Item(i).Delete
    Next i
    End If
    Catch
    
    End Try


End Sub

Sub RemoveRulesfromEventTriggerIpart(occ As ComponentOccurrence)

    'On Error Resume Next    
  
    factorydoc=occ.Definition.iPartMember.Parentfactory
    Auto = iLogicVb.Automation
    Dim iLogicAuto As Object
    iLogicAuto = Auto 
    Dim oiLogicDoc As Document 
    

    'capture current document
    oiLogicDoc =occ.Definition.iPartMember.ReferencedDocumentDescriptor.ReferencedDocument
    'load up ilogic automation
    Dim oiLogicAuto As Object = iLogicVb.Automation
    'get container for all of the ilogic rules
    Dim oRules As Object = oiLogicAuto.rules(oiLogicDoc)
    'variable for the events rules
    Dim oiLogicPropSet As PropertySet
    'set up a counter
    Dim iPropCount As Integer
    

   Try
    'set our property set since we know it exists now
    oiLogicPropSet = oiLogicDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
    'oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules")
        
    Dim strDeletedTriggers As String
    'iterate through the triggers And delete Each
    
    If Not oiLogicPropSet.Count=0 Then
   For i As Integer = oiLogicPropSet.Count To 0 Step -1
        oiLogicPropSet.Item(i).Delete
    Next i
    End If
    Catch
    
    End Try


End Sub

Sub RemoveRulesfromEventTriggerIAssembly(occ As ComponentOccurrence)

    'On Error Resume Next    
  
    factorydoc=occ.Definition.iAssemblyMember.Parentfactory
    Auto = iLogicVb.Automation
    Dim iLogicAuto As Object
    iLogicAuto = Auto 
    Dim oiLogicDoc As Document 
    

    'capture current document
    oiLogicDoc =occ.Definition.iAssemblyMember.ReferencedDocumentDescriptor.ReferencedDocument
    'load up ilogic automation
    Dim oiLogicAuto As Object = iLogicVb.Automation
    'get container for all of the ilogic rules
    Dim oRules As Object = oiLogicAuto.rules(oiLogicDoc)
    'variable for the events rules
    Dim oiLogicPropSet As PropertySet
    'set up a counter
    Dim iPropCount As Integer
    

   Try
    'set our property set since we know it exists now
    oiLogicPropSet = oiLogicDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
    'oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules")
        
    Dim strDeletedTriggers As String
    'iterate through the triggers And delete Each
    
    If Not oiLogicPropSet.Count=0 Then
   For i As Integer = oiLogicPropSet.Count To 0 Step -1
        oiLogicPropSet.Item(i).Delete
    Next i
    End If
    Catch
    
    End Try


End Sub

Sub deletecurrent()

 On Error Resume Next    
    
    Auto = iLogicVb.Automation
    Dim iLogicAuto As Object
    iLogicAuto = Auto 
    Dim oiLogicDoc As Document 
    
    'capture current document
    oiLogicDoc = ThisApplication.ActiveDocument
    'load up ilogic automation
    Dim oiLogicAuto As Object = iLogicVb.Automation
    'get container for all of the ilogic rules
    Dim oRules As Object = oiLogicAuto.rules(oiLogicDoc)
    'variable for the events rules
    Dim oiLogicPropSet As PropertySet
    'set up a counter
    Dim iPropCount As Integer
    
    'set our property set since we know it exists now
    oiLogicPropSet = oiLogicDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
    'oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules")
        
    Dim strDeletedTriggers As String
    'iterate through the triggers And delete Each
    For i As Integer = oiLogicPropSet.Count To 0 Step -1
        oiLogicPropSet.Item(i).Delete
    Next i
End Sub

 

Accepted solutions (1)
1,956 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Accepted solution

Any upgrades to the code are welcomed

Message 3 of 7

Mirtchii
Advocate
Advocate

Hi Vulic,

 

Really appreciate your code. 

We have been deleting event trigger of a lot of files manually and now, thank to this code, just a simple click solve all the tedious job. 😁

 

0 Likes
Message 4 of 7

_dscholtes_
Advocate
Advocate
For Each Subocc In oCompDef.Occurrences
isum=isum+1
Next

For i=1 To isum
subocc = oDoc.ComponentDefinition.Occurrences(i)

Improvements:

  1. You don't need to count the occurrences using 'isum'. oCompDef.Occurrences.Count should give you the amount.
  2. You don't need the for-next loop and set 'subocc' using 'i' as an index. You can use the for-each (Subocc) loop for that.
  3. Remove the label (1:) and use an if-then construction instead. If you need the opposite of 'If occ.Name="" Then' you could use 'If Not occ.Name="" Then' or 'If occ.Name<>"" Then'.
  4. You use 'asm' as reference for both parts and assembly files. That could lead to confusion. Why not use a more generic oFile or oTargetDoc or something instead?
Message 5 of 7

Anonymous
Not applicable

This code was made almost three years ago. This was when I was still learning iLogic and VBA programing. Today i would do a lot of things differently.....

Just a note: 'If Not occ.Name="" was used to find an aditional occ which exists in weldment assembly.

0 Likes
Message 6 of 7

_dscholtes_
Advocate
Advocate
Just a note: 'If Not occ.Name="" was used to find an aditional occ which exists in weldment assembly.

I recently discovered this one (we didn't use weldment assemblies before). I had the same check in my code, until I found a model in which it was named "_WeldBead:1" or something. It would pass the above check, but my macro would fail later on. So I changed the check to below code, to see if the occurrence is based on a document.

If oSourceOcc.ReferencedDocumentDescriptor Is Nothing Then

 

Message 7 of 7

Anonymous
Not applicable

Today i use this line:

 

If occ.Definition.Type =ObjectTypeEnum.kWeldsComponentDefinitionObject Then