Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic, set Event triggers from rule

6 REPLIES 6
Reply
Message 1 of 7
Tigerpirro
2921 Views, 6 Replies

iLogic, set Event triggers from rule

I want my rule to set up Event triggers for itself from inside the rule. Is there a good way to do this?

6 REPLIES 6
Message 2 of 7

Hi Tigerpirro,

 

You can copy the event triggers that are set in another file (a template for instance).

 

http://adndevblog.typepad.com/manufacturing/2015/01/add-external-ilogic-rule-to-event-trigger.html

 

my modified version of  the example at that link is below. I set up external rules with event triggers in the templates, and then I have this "Copy Event Triggers rule" called from various other external rules as needed. Keep in mind, I repurposed the rule I found at the adndevblog, so there is likely some extra stuff in there that is not needed, etc. I never did go back through it and clean it up.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Imports System.Collections.Generic
'code taken and modified from:
'http://adndevblog.typepad.com/manufacturing/2015/01/add-external-ilogic-rule-to-event-trigger.html

Sub Main
Trace.WriteLine("Event Driven Rules Copy", "iLogic") 'debug 

	Dim oTemplateDoc As Document
	Dim oTemplate As String 
	
	'check file type 
	If ThisApplication.ActiveEditDocument.DocumentType = kDrawingDocumentObject Then
	'If ThisDoc.Document.DocumentType = kDrawingDocumentObject Then
		oTemplate = "\\server\departments\CAD\Inventor\Templates\Standard.idw"   'file to copy events from
	ElseIf ThisApplication.ActiveEditDocument.DocumentType = kPartDocumentObject Then
	'ElseIf ThisDoc.Document.DocumentType = kPartDocumentObject Then
		oTemplate = "\\server\departments\CAD\Inventor\Templates\Standard.ipt"   'file to copy events from
	Else
		oTemplate = "\\server\departments\CAD\Inventor\Templates\Standard.iam"   'file to copy events from
	End If
	
	'file to copy events to
	oInput =  ThisApplication.ActiveEditDocument.FullFileName 
	If oInput = "" Then 'catch a new file with no path
		Return
	End If	
	
	'open the template
	oTemplateDoc = ThisApplication.Documents.Open(oTemplate, False)
	CopyEventsToDocuments(oTemplateDoc)
	oTemplateDoc.Close 
End Sub

Private Const m_ourGuid As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Private savedEnabled As Boolean
Dim oInput As String

Public Sub CopyEventsToDocuments(ByVal sourceDoc As Document)
	Dim sourcePropSet As PropertySet = GetEventRulesPropertySet(sourceDoc)
	Dim inputs() As String = { oInput} 
	Dim destinationFileNames As List(Of String) = New List(Of String)(inputs)
	If (destinationFileNames Is Nothing) Then Return
	savedEnabled = iLogicVb.Automation.RulesOnEventsEnabled
	iLogicVb.Automation.RulesOnEventsEnabled = False
	Try	
		CopyEventsToAllDocuments(sourcePropSet, sourceDoc, destinationFileNames)
	Finally		
		iLogicVb.Automation.RulesOnEventsEnabled = savedEnabled
	End Try

End Sub

Function GetEventRulesPropertySet(ByVal sourceDoc As Document) As PropertySet
	Try
		Return sourceDoc.PropertySets(m_ourGuid)
	Catch ex As Exception
		Return Nothing
	End Try
End Function

Function VerifyRulesPropertySet(ByVal propSet As PropertySet) As Boolean
	If (propSet Is Nothing) Then Return True
	' See if any rules are internal instead of external.  We can't copy those.
	For Each prop As Inventor.Property In propSet
		Dim strValue As String = prop.Value.ToString
		If (Not strValue.StartsWith("file://")) Then Return False
	Next
	Return True
End Function


Sub CopyEventsToAllDocuments(ByVal sourcePropSet As PropertySet, ByVal sourceDoc As Document, ByVal destinationFileNames As List(Of String))
	For Each fileName As String In destinationFileNames
		If (String.Equals(sourceDoc.FullFileName, fileName)) Then Continue For
		CopyEventsToDocument(sourcePropSet, fileName)
	Next
End Sub

Sub CopyEventsToDocument(ByVal sourcePropSet As PropertySet, ByVal fileName As String)
	'Dim doc As Document = FindOpenDocument(fileName)

	Dim openedHere As Boolean = False

	If (doc Is Nothing) Then
		doc = ThisApplication.Documents.Open(fileName, True)
		openedHere = True
	End If
		
	CopyEventsPropertySetToDocument(sourcePropSet, doc)
	iLogicVb.Automation.RulesOnEventsEnabled = savedEnabled

End Sub

Sub CopyEventsPropertySetToDocument(ByVal sourcePropSet As PropertySet, ByVal doc As Document)
	' If an event-driven rules property set already exists, delete it.
	Dim oldPropSet As PropertySet = GetEventRulesPropertySet(doc)
	If (oldPropSet IsNot Nothing) Then
		oldPropSet.Delete()
	End If
	If (sourcePropSet Is Nothing OrElse sourcePropSet.Count = 0) Then Return
	Dim destPropSet As PropertySet = doc.PropertySets.Add("_iLogicEventsRules", m_ourGuid)
	For Each prop As Inventor.Property In sourcePropSet
		destPropSet.Add(prop.Value, prop.Name, prop.PropId)
	Next
	EnsureDocumentInterest(doc)
End Sub

Const iLogicId As String = "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}"

Sub EnsureDocumentInterest(ByVal doc As Document)
	If (ThisApplication.SoftwareVersion.Major < 15) Then Return ' Inventor 2010 or before
	If (DocumentHasiLogicInterest(doc)) Then Return
	Dim dataVersion As Integer = 20090512
	Dim newInterest As DocumentInterest = doc.DocumentInterests.Add(iLogicId, "iLogic", DocumentInterestTypeEnum.kInterested, dataVersion, Nothing)
End Sub

Function DocumentHasiLogicInterest(ByVal doc As Document) As Boolean
	Dim interest As DocumentInterest = FindDocumentInterest(doc, iLogicId)
	If (interest IsNot Nothing) Then
		If (interest.InterestType = DocumentInterestTypeEnum.kInterested) Then
			Return True
		End If
		interest.Delete()
	End If
	Return False
End Function

Function FindDocumentInterest(ByVal doc As Document, ByVal clientId As String) As DocumentInterest
	For Each interest As DocumentInterest In doc.DocumentInterests
		If (String.Equals(interest.ClientId, clientId, StringComparison.OrdinalIgnoreCase)) Then
			Return interest
		End If
	Next
	Return Nothing
End Function
Message 3 of 7
Anonymous
in reply to: Curtis_Waguespack

 


@Curtis_Waguespack wrote:

Hi Tigerpirro,

 

You can copy the event triggers that are set in another file (a templa...


Hi,

 

Sorry to reply to an old post, but I can't get this to work properly. I get it to copy my 2 event triggers to the new file, but only one of them runs initially when it should. The other doesn't run. When I open the event triggers window, both rules are there. First (which runs as it should) runs on before save, and creates a flat pattern if it doesn't exist. The second runs (or should run) on iproperty change, and updates the material and thickness of the sheet metal part according to two custom iproperties.

 

BUT, if I open the event triggers window, and then press OK (without changing anything), then it works perfectly. If I just press cancel, it still doesn't run.

 

Any ideas what might cause this?

 

Message 4 of 7
MechMachineMan
in reply to: Anonymous

I didnt look at the code very carefully, but i would guess that it may not be increment the same type of triggers.

 

ie; if you have two triggers for before save, one needs to be 200 and the next needs to be 201. any additional ones would have to be 202, 203, 204, and so on.

 

Good luck.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 7
Anonymous
in reply to: MechMachineMan

Where do I see the increment of the trigger? And change them?



I do only have one trigger for each of the two events.




Message 6 of 7
Anonymous
in reply to: Curtis_Waguespack

When I place this rule in an event it doesn't work. I doesn't set the events properly, they are not named (the attached images shows it). When the event occurs I get the error message:

 

 

Event driven iLogic rule


RunRule: Found no rule with name ""

 

 

(it only translated from the german warning so it might be different in detail)

 

Why does it not set the new events properly when this rule is event driven? It works if I fire it manually...

Tags (1)
Message 7 of 7
Anonymous
in reply to: Anonymous

I have to add: I added this rule in an event in my template, so when I run it once manually, it will be in the current document. There it works once and then the error I descriped occurs...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report