Ilogic to use part of filename to populate iProperty

Ilogic to use part of filename to populate iProperty

aurel_e
Collaborator Collaborator
212 Views
2 Replies
Message 1 of 3

Ilogic to use part of filename to populate iProperty

aurel_e
Collaborator
Collaborator

Hi all,

 

I need to add to all the components (parts and subassies) of an assembly an iproperty that is an extract of the filename

I tried the code below but is working just for the parts not the subassies.

 

Sub Main
	On Error Resume Next
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oLeafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
	'when you turn this on, you should also turn it back off when done
	ThisApplication.SilentOperation = True

	
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oLeafOccs
		
		
		iProperties.Value(oOcc.Name, "Custom", "PN5") =(Mid(oOcc.Name, 11, 3))
	Next
End Sub
0 Likes
Accepted solutions (1)
213 Views
2 Replies
Replies (2)
Message 2 of 3

FINET_Laurent
Advisor
Advisor
Accepted solution

Hello @aurel_e,

 

The issue you are facing is that the AllLeafOccurrences collection contains each single part of the assembly. 

To perform the desired task, you will need a recursive function that will go through each occurrences, & sub-occurences if present, thus englobing also the assemblies. Here a code with no error handling :

Sub Main
	On Error Resume Next
	Dim doc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim acd As AssemblyComponentDefinition = doc.ComponentDefinition
	
	addProperty(acd.Occurrences)
	
End Sub

Sub addProperty(occs As Inventor.ComponentOccurrences) 'recursive
	For Each occ As Inventor.ComponentOccurrence In occs
		iProperties.Value(occ.Name, "Custom", "PN5") = (Mid(occ.Name, 11, 3))		
		
		If occ.SubOccurrences.Count > 0 Then addProperty(occ.SubOccurrences)
				
	Next
End Sub

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 3 of 3

aurel_e
Collaborator
Collaborator

Perfect. I had to add that 

On Error Resume Next

  on the 2nd part of the code to make it work.

 

Many thanks.