Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

iLogic Code to remove a rule from Event Trigger

Anonymous

iLogic Code to remove a rule from Event Trigger

Anonymous
Not applicable

I found some code which could add a rule for Event Trigger.

 

But if I want to remove one rule from Event Trigger (After Open Document), what is the code?

 

If the rule is external rule, will it work?

 

I mean I manually put one external rule and using Event Trigger (After Open Document) to run it automatically, I want after run this rule, it could be removed by iLogic code in this external rule.

 

Is it possible?

0 Likes
Reply
Accepted solutions (2)
4,386 Views
10 Replies
Replies (10)

Anonymous
Not applicable
Accepted solution

Do not worry, I got it.

 

I would like to put code here just in case someone may want same function.

 

 

Sub RemoveRulesfromEventTrigger()

    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("iLogicEventsRules")
        
    'iterate through the triggers And delete Each
     For iPropCount = 1 To oiLogicPropSet.Count
        oiLogicPropSet.Item(iPropCount).Delete
    Next iPropCount
            
End Sub

 

tomas_wiklund
Contributor
Contributor

Hi! I've tried this code and got it work...

...well almost!

I changed the row where setting PropertySet.

 

oiLogicPropSet = oiLogicDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")

Now I got it to work when each event only contains one rule.

If they contains more then one the code removes the first, the third, the fourth etc..

 

My complete code:

 

Sub Main()

    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 iPropCount = 1 To oiLogicPropSet.Count
        oiLogicPropSet.Item(iPropCount).Delete
    Next iPropCount

End Sub

 

/Tomas Wiklund, Sweden

 

Autodesk Inventor Professional 2017 (64 Bit Edition)

0 Likes

Anonymous
Not applicable

your iterating through a collection but deleting the first.. if trying to delete all, you should iterate something closer to: 

For i As integer = oiLogicPropSet.Count - 1 To 0 Step -1
             oiLogicPropSet.Item(i).Delete
         Next i

 

tomas_wiklund
Contributor
Contributor

Thanks Kristopher!

 

Now i see...

It worked with One minor edit.

 

Like this:

SyntaxEditor Code Snippet

For i As Integer = oiLogicPropSet.Count To 0 Step -1
        oiLogicPropSet.Item(i).Delete
Next i

 

0 Likes

Charlies_3D_T
Advocate
Advocate

@Anonymous

 

Hello,

 

Could you post the ilogic that place an rule in the event trigger directory? 

0 Likes

jchristman
Explorer
Explorer
Accepted solution

If you just want to remove all events from document

Try
   ThisApplication.ActiveDocument.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete()
Catch ex As Exception
' message or ignore End Try

 

donaldleigh
Advocate
Advocate

@jchristman 

 

Thanks for your rule. This is great. But is there a way to ignore some rules (by name) and delete the rest?

 

Cheers

Donald

0 Likes

Jonathan.WVB
Participant
Participant

Hi, could you send the link of the code you found that ADD rules to event trigger?

0 Likes

NSBowser
Advocate
Advocate

I came here trying to solve a similar problem. We have files with triggers linked to rules that no longer exist. The popup errors are annoying and rather than turn off triggers (a valid option) I decided to build a tool to help strip those triggers from the files. Here is the code I made which might help get someone else working as well.

 

This rule will run from a part or assembly, and process all referenced documents within (for an assembly) removing any and all triggers in every document that contains the "SEARCH TEXT" in the original rule path/name

You'll need to modify the "SEARCH TEXT" - near the end of the program - to locate the specific rule to remove. You could modify it to locate more than one rule, or to remove only specific types of triggers, but this should get you a start, if you were looking to do something similar

Class ThisRule
	
	Dim iLogicAuto As Object
	
	Sub Main()
	
		iLogicAuto = iLogicVb.Automation
		Dim doc As Document = ThisApplication.ActiveDocument
		
		Select Case doc.DocumentType
			Case DocumentTypeEnum.kAssemblyDocumentObject
				Dim aDoc As AssemblyDocument = doc
				ProcessAssembly(aDoc)
			Case DocumentTypeEnum.kPartDocumentObject
				RemoveTriggers(doc)
			Case Else
				Exit Sub
		End Select
	
	End Sub
	
	Sub ProcessAssembly(aDoc As AssemblyDocument)
		RemoveTriggers(aDoc)
		For Each subDoc As Document In aDoc.ReferencedDocuments
			Select Case subDoc.DocumentType
				Case DocumentTypeEnum.kAssemblyDocumentObject
					ProcessAssembly(subDoc)
				Case DocumentTypeEnum.kPartDocumentObject
					RemoveTriggers(subDoc)
				Case Else
					Exit Sub
			End Select
		Next
	End Sub
	
	Sub RemoveTriggers(oDoc As Document)
		Dim rules = iLogicAuto.Rules(oDoc)
    	Dim iLogicPropSet As PropertySet
		Try
			iLogicPropSet = oDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
		Catch
			Exit Sub
		End Try
		Logger.Debug(System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName))
		For i = iLogicPropSet.Count() To 1 Step -1
			Dim prop As Inventor.Property = iLogicPropSet.Item(i)
			Logger.Debug(String.Format("   {0} ({1}...{2})", prop.DisplayName, Left(prop.Expression, 20), Right(prop.Expression, 20) ) )
			If (prop.Expression).contains("SEARCH TEXT")
				Logger.Debug("   ** DELETE TRIGGER**")
				iLogicPropSet.Item(i).Delete
			End If
		Next
	End Sub

End Class

 


Best of Luck

---------------------------------------------------------------------------------------------------------------------------------
If you find this reply helpful or insightful, please use the 'Accept as Solution' or 'Kudos' button below.

b.mccarthy
Collaborator
Collaborator

Hello.

 

Thank you for the code. I ran it on an older assy, but the existing rules were not removed. No error message presented.

 

What would I change if I want to remove any triggers, regardless of the name?

 

Using Inv 2023.1

0 Likes