Traversing subassemblies within parent assembly

Traversing subassemblies within parent assembly

J-Camper
Advisor Advisor
974 Views
6 Replies
Message 1 of 7

Traversing subassemblies within parent assembly

J-Camper
Advisor
Advisor

I have some code that will:

  • look at all occurrences in an assembly
  • for each, get the reference file name, and trim down to desired string
  • load this string into the iproperty part number

It is supposed to look at the occurrence and, if it is an assembly, run through the previous steps for the suboccurrences.  I can't seem to get the rule to change the iproperties in my suboccurrences. Below is my code:

 

 

Sub Main
'Gets the Active Document
oDoc = ThisDoc.Document

'Saves this document
oDoc.Save

'Checks if the active document is an assembly
If oDoc.DocumentType = kAssemblyDocumentObject
	'Gets the assembly occurrences
	Dim oOccs As ComponentOccurrences
	oOccs = oDoc.ComponentDefinition.Occurrences
	'Call the subprocedure to traverse the assembly
	Call TraverseAssembly(oOccs)
End If

End Sub

'***************
Public Sub TraverseAssembly(oOccs As ComponentOccurrences)
 'Integer to traverse the assembly occurrences
 Dim i As Integer
 
  Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
 	If oOcc.Suppressed = False
   
		'Occurrence Name (Display Name)
		oOccName = oOcc.Name
		
		'Builing Part Number
		
		'get full file path
		Dim pnName As String
		pnName = oOcc.ReferencedFileDescriptor.FullFileName
		'Dump file path
		Dim dumpName As String
		dumpName = pnName.Remove(0, pnName.LastIndexOf("\") + 1)
		Dim dumpLength As Long
		dumpLength = Len(dumpName)
		'trim file file extension [.ipt or .iam]
		Dim trimName As String
		trimName = Left(dumpName, dumpLength - 4)
		Dim trimLength As Long
		trimLength = Len(trimName)
		'set value to feed
		Dim feedName As String
		feedName = Right(trimName, trimLength - 10)
		
			Try
				'iProperties.Value(oOccName, "Project", "Project") = iProperties.Value("Project", "Project")
				iProperties.Value(oOccName, "Project", "Part Number") = feedName		
			Catch
				'It won't be able to change Read-Only parts (e.g. Content Center)
				'MessageBox.Show("This is a content center file: " & oOcc.Name, "Title")
			End Try

		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	End If
Next
End Sub

 

 

0 Likes
Accepted solutions (1)
975 Views
6 Replies
Replies (6)
Message 2 of 7

JaneFan
Autodesk
Autodesk

Hey @J-Camper , 

 

Did you meet some error when running the rule? Are the sub occurrences read only? 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 3 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support

@J-Camper ,

 

To traverse sub occurrences, need to check whether occurrence contains sub occurrences or not. Try suggestions provided in below help documentation link.

 

http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-71992135-2633-4DE3-ACA7-60738CD204E7

 

Actually, code in above link is in VBA code. Just remove "Set" from the code and works as iLogic code.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 7

J-Camper
Advisor
Advisor

There is no error code, but the part numbers are not being filled in for the parts within a sub-assembly.  The sub-occurrences are not read only but one of the sub-occurrences is usually the same name as the parent sub-assembly, not sure if that is throwing it off.

0 Likes
Message 5 of 7

DRoam
Mentor
Mentor
Accepted solution

A couple thoughts...

 

  1. Do you actually need to traverse the assembly structure? Could you just iterate through all documents referenced by your top assembly instead?
  2. It's possible your Try/Catch is hiding some errors other than simple read-only file errors. Rather than using Try/Catch, instead you could place this line after "If oOcc.Suppressed = False": "If oOcc.Definition.Document.IsModifiable = True". This will skip any files that are read-only. Then you can remove the Try/Catch, and see if there are any errors causing your code not to work.
0 Likes
Message 6 of 7

J-Camper
Advisor
Advisor

Thank you for your thoughts DRoam.

 

1) This is my first iteration of filling all Part numbers, so if looking at all reference files is a better way, could you point me in the right direction?  The code current takes about a minute to run in an assembly with 180 files, but it also isn't doing exactly what I intend

 

2) I made the change you suggested but I'm still not getting any errors and my sub-occurrences are still not getting changed.

 

I Think my problem might be in:

For Each oOcc In oOccs

 When I ask the rule to call the sub function for oOcc.SubOccurrences it seems like I'm still only looking at occurrences within the initial level of occurrences.  Any thoughts on rewording the "In" part of the For each statement?

0 Likes
Message 7 of 7

J-Camper
Advisor
Advisor

I think it was getting confused by generic occurrence names we use for our drawer box assemblies so the internal sizing rules don't have to be modified when changing the reference files.  I was getting the wrong part number in one occurrence and the other occurrences, with the same generic name, were not changing.

 

I have since re-written the rule to sift through all referenced documents and it works exactly as I want it.  I am accepting your answer because you recommended the better route for this task.

0 Likes