List occurrences in specific SubAssembly

List occurrences in specific SubAssembly

viktor.borgen
Participant Participant
572 Views
7 Replies
Message 1 of 8

List occurrences in specific SubAssembly

viktor.borgen
Participant
Participant

Hi,

 

I'm working on an iLogic rule to replace components from an Assembly with components found in Vault.

So far I've gotten it to search for either top-level components or LeafOccurrences, and replace those it finds with the Vault versions.

viktorborgen_0-1656060909344.png

My screenshot shows the model tree after I've run the rule, this is how far I've managed to get.

As shown in the screenshot, all the top-level parts and Subassemblies have been replaced. The Subassembly named "352135:1" is one which was not found in Vault. A copy has been made and given a Vault number, and the original part has been replaced with the copy.

 

I now need to figure out how to replace the parts of "352135:1" by iLogic. I have tried to replace all LeafOccurrences, but some other subassemblies contain their own subassemblies, and i want to replace those before their leaves.

 

All the parts of the Subassembly "352135:1" either exist in Vault and can be downloaded by search, or can be copied locally and used.

 

I've been at this for a few days now, and everything i try results in errors or just won't compile. Am I complicating this more than I need? If so, what can i do to get these files replaced?

 

As stated, I've been able to make copies and search the Vault, so the only thing i need is to access the occurrences in this specific subassembly by code, everything else I think I can handle.

 

Appreciate any help, thanks!

0 Likes
Accepted solutions (1)
573 Views
7 Replies
Replies (7)
Message 2 of 8

dalton98
Collaborator
Collaborator
Accepted solution

You can use something like this. When you run into an assembly (with sub occurrences) it calls to another sub. 

 

Sub Main
Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument
Dim oOcc As ComponentOccurrence
For Each oOcc In oAss.ComponentDefinition.Occurrences
	If oOcc.Suppressed = False
		If oOcc.SubOccurrences.Count > 0
			Call SubOccs(oOcc)
		End If
	End If
Next
End Sub

Sub SubOccs(oOcc As ComponentOccurrence)
	For Each oOcc In oOcc.SubOccurrences
	If oOcc.Suppressed = False
		If oOcc.SubOccurrences.Count > 0
			Call SubOccs(oOcc)
		End If
	End If
Next	
End Sub

 

0 Likes
Message 3 of 8

viktor.borgen
Participant
Participant

Thanks a bunch! This seems to get me going again!

 

I'm trying to get each oOcc's full file name, previously I've used something like

Dim oDoc as Document = oOcc.Definition.Document
oDoc.FullFileName

But when I use this in the context of "SubOcc", I get the error:

(Exception from HRESULT: 0x80004005 (E_FAIL))

I've looked into the error but can't find a clear answer as to what to do to fix this.

 

Do you know how i can get access to each oOcc's FullFileName and information such as that?

0 Likes
Message 4 of 8

dalton98
Collaborator
Collaborator

That should work. Are you just using 'oDoc.FullFileName' or are you assigning a variable to it? ' oFileName = oDoc.FullFileName'. I would need more info on which parts are giving you that error.

0 Likes
Message 5 of 8

viktor.borgen
Participant
Participant

The code I've written thus far goes through all top-level components (both assy and parts) and get the value of their custom iProperties I use to search for parts in Vault.

I don't really know if I need it, but at the same time the iProps are gathered, the code gathers their oDoc.FullFileName.

 

The loop I've used for this is set up like this:

 

For Each oCompOcc In oCompOccs
	
	oDoc = oCompOcc.Definition.Document
	Try
		iLogicTypeArray.Add(iProperties.Value(oCompOcc.Name, "Custom", "iLogic_Type"))
		iLogicParameterArray.Add(iProperties.Value(oCompOcc.Name, "Custom", "iLogic_Parameter"))
		OccurrenceNameArray.Add(oCompOcc.Name)
		OccurrenceFileArray.Add(oDoc.FullFileName)
		FullFileNameArray.Add(oDoc.FullFileName)
	Catch
	End Try
	
Next

This has worked well untill now, and I think it has something to do with the code in your example needing to run through the loop more than once. I'm not too sure what might be wrong, but it seems it has something to do with the call back to your SubOccs:

If oOcc.Suppressed = False
	If oOcc.SubOccurrences.Count > 0
		Call SubOccs(oOcc)
	End If
End If

When oOcc is sent back to SubOccs, I think it brings with it the previously declared

oDoc = oOcc.Definition.Document

But at this point this needs to be (at least i think) something like

oDoc = oOcc.SubOccurrences.DefinitionDocument

 

I'm not sure this makes too much sense, I'm already getting confused by this.

Hopefully you understand my issue and are able to help.

If you need my full code I can see if I can clean it up a bit and send it over. 

0 Likes
Message 6 of 8

viktor.borgen
Participant
Participant

OK @dalton98. I found something i feel might be of note.

 

When i first tried adding the

oDoc = oOcc.Definition.Document

to the code you sent, I got the error i mentioned earlier. This was when this declaration happened inside the for-loop in SubOccs. I just now tried declaring it on the outside of that loop, and that seems to work, not giving any errors.

I'm happy to use it like that, but do you have any idea what is happening? Would love to understand this error better, as I'm sure it might arise later.

 

The error does not show when declaring the oDoc inside the for-loop if the loop is only run once. The moment it runs again the whole rule stops with the cryptic error...

0 Likes
Message 7 of 8

dalton98
Collaborator
Collaborator

I think the problem is you are not calling the arrays to the 'SubOcc' sub. Imagine each sub as its own code, and if you do not pass information between them then it will not recognize information for the other sub. You would set it up like this:

Call SubOccs(oOcc, iLogicTypeArray, iLogicParameterArray, etc....)

Sub SubOccs(oOcc as ComponentOccurrence, iLogicTypeArray as ArrayList, iLogicParameterArray as ArrayList)
0 Likes
Message 8 of 8

viktor.borgen
Participant
Participant

Yeah I'm sure it has something to do with when information is accessed and what information is sent.

I got it to work tho, it seems i had forgotten to declare/update the oDoc variable before sending it back to the recursive sub. Everything seems to work as it should now, all issues I have now are issues I know I've created and believe I can resolve.

 

I'm marking your first response as a solution, as it was with that i resolved it! Thanks a bunch!

0 Likes