Trigger Rule When Placing Component in Assembly

Trigger Rule When Placing Component in Assembly

bhudson4XNR5
Participant Participant
213 Views
2 Replies
Message 1 of 3

Trigger Rule When Placing Component in Assembly

bhudson4XNR5
Participant
Participant

Hi All,

 

I have a rule in an assembly template that I want to run any time a new component is placed in that assembly. I'm looking for a way to trigger that rule, but it doesn't look like there is anything under the event triggers that would apply to this.  Any thoughts on a way to accomplish this?

 

Thanks!

0 Likes
214 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

I think your best option is to create an addin and watch for the event.

Inventor.Application.ApplicationEvents.OnDocumentChange

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

bhudson4XNR5
Participant
Participant

I've been working to solve this and thought I had it but have run into an issue. 

 

The goal of all this is that when this specific assembly document type is opened, it waits for a new occurrence, and runs the rule "Material AutoFill".  "Material AutoFill" will take the last part occurrence and place that part's name into a custom iProperty of the current assembly file.

 

I created an internal rule inside the specific template called "event trigger" using handlers.  This rule triggers on a new document or open document, so anytime a new or existing assembly of this type is created/opened the handler runs.  

bhudson4XNR5_0-1665601637429.png

The "event trigger" code is below:

Sub Main
	Dim oAsmEvents As AssemblyEvents = ThisApplication.AssemblyEvents
	AddHandler oAsmEvents.OnNewOccurrence, AddressOf oAsmEvents_OnNewOccurrence

	Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
	AddHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
End Sub

Public Sub oAsmEvents_OnNewOccurrence(oAsmDoc As AssemblyDocument, oOcc As ComponentOccurrence, oTiming As Inventor.EventTimingEnum, oContext As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
	If oTiming = kAfter Then
		iLogicVb.RunRule("Material AutoFill")
	End If
End Sub

Public Sub ApplicationEvents_OnCloseDocument(oDoc As Inventor._Document, oFullName As String, oTiming As Inventor.EventTimingEnum, oContext As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
	If oDoc Is ThisDoc.Document Then
		Dim oAsmEvents As AssemblyEvents = ThisApplication.AssemblyEvents
		Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
		RemoveHandler oAsmEvents.OnNewOccurrence, AddressOf oAsmEvents_OnNewOccurrence
		RemoveHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
	End If
End Sub

The autofill rule is a separate internal rule that looks through all occurrences, and takes the last one and places it into the custom iProperty field. 

 

The "Material AutoFill" code:

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    Dim oName As String
    oName = Left(oOccurrence.Name, 9)
    iProperties.Value("Custom", "Material2") = oName
Next

This has worked exactly as I wanted until I started working in higher level assemblies.  If I open a higher level assembly that contains one of the assemblies with these rules, the handler will run and any new occurrences in the higher level assembly will trigger the rules.  The higher level assembly in these cases does not contain either of the rules above. 

 

I only want the rules to trigger when that specific document is worked on, and only when that specific document sees a new occurrence.  

 

Any thoughts on how to address this?

0 Likes