Search for Sub-Assemblies

Search for Sub-Assemblies

Anonymous
Not applicable
723 Views
8 Replies
Message 1 of 9

Search for Sub-Assemblies

Anonymous
Not applicable

Good Day,

 

Is there a way to be able to search for the top level sub assemblies from my main assembly?

I am not looking for assemblies within assemblies.

Every time one is found it must fire a rule within the found top level assembly.

 

Gosh,  I hope this makes sense.

0 Likes
Accepted solutions (3)
724 Views
8 Replies
Replies (8)
Message 2 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

Something like this maybe? 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oDoc As Document In oAsm.ReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
		Try
			iLogicVb.Automation.RunRule(oDoc, "RuleName")
			'iLogicVB.Automation.RunExternalRule(oDoc, "RuleName")
		Catch
		End Try
	End If
Next

I don't know if it's an external or internal rule you want to run... But it's pretty self explainatory which line does which 🙂

0 Likes
Message 3 of 9

Anonymous
Not applicable

Thank you that worked which leads me to another question.

 

How do I count the number of sub-assemblies with a specific name?

 

 

 

0 Likes
Message 4 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

Something like this?

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oName As String = InputBox("Name: ", "Count occurrences")
Dim i As Integer = 0
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences
	If oOcc.Name.Split(":")(0) = (oName) Then i += 1
Next
MessageBox.Show("Number of occurrences with name: " & oName & " = " & i, "Count occurrences")
Message 5 of 9

Anonymous
Not applicable

You are an absolute beast.  

Thank you,  your solutions worked perfectly!

Message 6 of 9

tkddud711
Advocate
Advocate

Hello.
I have a question.
The code above applies only to sub-assemblies in level 1 of the assembly.
Can you count the quantity of a second or third level assembly?

0 Likes
Message 7 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

@tkddud711 

Try a recursive function like this 🙂

Sub Main
Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oName As String = InputBox("Name: ", "Count occurrences")
MessageBox.Show("Number of occurrences with name: " & oName & " = " & CountOccs(oAsm, oName), "Count occurrences")
End Sub
Function CountOccs(oAssy As AssemblyDocument, oName As String) As Integer
	Dim oCount As Integer = 0
	For Each oOcc As ComponentOccurrence In oAssy.ComponentDefinition.Occurrences
		If TypeOf (oOcc.Definition) Is AssemblyComponentDefinition
			oCount += CountOccs(oOcc.Definition.Document, oName)
		End If
			If oOcc.Name.Split(":")(0) = (oName) Then oCount += 1
	Next
	Return oCount
End Function
0 Likes
Message 8 of 9

JhoelForshav
Mentor
Mentor

@tkddud711 

I see now that you were probably talking about the first rule I posted!

Just change ReferencedDocuments to AllReferencedDocuments to traverse all levels:

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
		Try
			iLogicVb.Automation.RunRule(oDoc, "RuleName")
			'iLogicVB.Automation.RunExternalRule(oDoc, "RuleName")
		Catch
		End Try
	End If
Next

EDIT: Ignore this... You wrote count so I guess my first answer is what you're looking for 🙂

 

0 Likes
Message 9 of 9

tkddud711
Advocate
Advocate

Thank you.
Works fine.

0 Likes