Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic - Push Parameters from parent to children occurrences

3 REPLIES 3
Reply
Message 1 of 4
JusticeSmith
308 Views, 3 Replies

iLogic - Push Parameters from parent to children occurrences

To whom it may concern,

I am attempting to control 3 parameters from my parent assembly, and once set, push them into non-defined part and assembly occurrences. 

To be more specific, I have a parameter named finish. I would like to set a finish type (text parameter) to, for example, "PowderCoat-White". I can set this in my parent, and I can set this in my parts independently. So the data is there.

But how can I change the part's parameter without directly referencing the part's name? (Every build contains different components)

This is my current script:

'--------FINISH--------

Sub Main()
' Parent assembly rule
Dim parentValueFinish As String = Finish

	' Iterate through all components in the assembly
	For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
	        Try
	            Dim childDoc As Document = comp.ReferencedDocumentDescriptor.ReferencedDocument
	            Dim param As Parameter = childDoc.ComponentDefinition.Parameters.Item("Finish")
	            param.Value = parentValueFinish
	        Catch ex As Exception
	            ' Handle any errors (e.g., parameter doesn't exist in the child)
			End Try
	Next
	
End Sub


This actually seems to work for the most part.. 

The issue I'm having is that when I have a child sub-assembly with, lets say 2 instances, both having different model states activated (I use a configurable bracket), only the first instance will actually take the finish parameter.

I figured this was just a write issue with model states, so I modified the part's "pencil setting" to edit all model states... but that doesn't seem to change anything.

-

I guess what I'm asking is,

1) Is there an easier way to execute what I am trying to do here? I suppose I could use Try...Catch on every possible part name I have in my library and directly change the parameters that way. But A) that doesn't solve the model state issue, and B) that turns into a nightmare every time I want to add a new part into my library.

and

2) Could someone be willing to modify my code to actually work?


Labels (1)
3 REPLIES 3
Message 2 of 4

Hi 

 

In my experience, its probably best to post this in the Inventor - programming - ilogic forum.

see below. The people in there are very helpful.

Failing that, I find ChatGPT is very helpful also with these kind of problems too.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/bd-p/120

Message 3 of 4

This is what ChatGTP responded with....

 

 

Your current approach seems reasonable for the task you're trying to accomplish. However, the issue you're facing with model states might be due to the fact that you're not iterating through all occurrences of the sub-assemblies to set the parameter for each instance. Instead, you're just iterating through the top-level occurrences. To address this, you need to recursively iterate through all occurrences including sub-assemblies.

Here's how you can modify your script to achieve this:

 

Sub SetFinishParameter(ByVal parent As ComponentOccurrence, ByVal parentFinish As String)
' Set finish parameter for the given component occurrence and its children recursively
Try
Dim doc As Document = parent.ReferencedDocumentDescriptor.ReferencedDocument
Dim param As Parameter = doc.ComponentDefinition.Parameters.Item("Finish")
param.Value = parentFinish
Catch ex As Exception
' Handle any errors (e.g., parameter doesn't exist in the child)
End Try

' Recursively process child occurrences
For Each child As ComponentOccurrence In parent.SubOccurrences
SetFinishParameter(child, parentFinish)
Next
End Sub

Sub Main()
' Parent assembly rule
Dim parentValueFinish As String = Finish

' Start the recursive process from the top-level occurrences
For Each topLevelOccurrence As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
SetFinishParameter(topLevelOccurrence, parentValueFinish)
Next
End Sub

 

This modified script will traverse through all occurrences of the assembly, including sub-assemblies, and set the finish parameter accordingly.

Make sure that your assembly structure is correctly organized and that all parts and sub-assemblies have the "Finish" parameter defined. This script should set the parameter for all instances of parts and sub-assemblies, regardless of their model states. If there are any errors or exceptions, they will be caught and handled appropriately.

Message 4 of 4

Sweet. Yeah, I've tried using microsoft co-pilot, but it seems to get snagged at times... I will try this tomorrow, thank you for the response.

Leaving the forum open in case anyone else has any other ideas

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

Post to forums  

Autodesk Design & Make Report