Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
erobinsonMRQE2
in reply to: mcgyvr

I have encountered another issue, the current iteration of the code can't recognize sub assemblies. 


I have found a way to get the sub assembly occurrence names,  but it does not allow me to create virtual parts with those names. 

 

Is there a better way to do this that will allow for virtual parts to be created for the sub assembly components, or is it better to just have the rule run in each assembly?


'define sComponent &  sVirtPart and the rest 
'of this code should work as written

' Defines list of Parts 
'Dim PartNum As Intager
'PartNum = 1

'Defines a list of parts rule looks through to see if they exist
Dim MyArrayList As New ArrayList
MyArrayList.Add("Lid")
MyArrayList.Add("Module")

Dim VirtList As New ArrayList
VirtList.Add("Bolt")
VirtList.Add("Nut")
VirtList.Add("Washer")
VirtList.Add("Gasket")

'Prepares to turn the Parts list into a string
Dim oString2 As String
j = 0

For Each oString2 In VirtList
	VPart = VirtList.Item(j)
'	MessageBox.Show(VPart)
	j += 1

	Dim oString As String
	i = 0
	Dim iQTY As Integer
	iQTY = 0


	'Iterates through Parts list to 
	For Each oString In MyArrayList
		Part = MyArrayList.Item(i)
		'	MessageBox.Show(Part, "Part")
		i += 1



		'define assembly component to count
		Dim sComponent As String
		sComponent = Part & ":"

		'define virtual component to create
		Dim sVirtPart As String
		sVirtPart = VPart

		'---------------------------

		'count chars in sComponent
		Dim oCnt As Integer = 0
		For Each c As Char In sComponent
			oCnt += 1
		Next

		'count chars in sVirtPart
		Dim oCnt2 As Integer = 0
		For Each c As Char In sVirtPart
			oCnt2 += 1
		Next
		'MessageBox.Show(Part & VirtPart & oCnt & oCnt2)

		Dim oAssyDoc As AssemblyDocument
		oAssyDoc = ThisApplication.ActiveDocument
		Dim oAsmCompDef As AssemblyComponentDefinition
		oAsmCompDef = oAssyDoc.ComponentDefinition
		Dim oOccs As ComponentOccurrencesEnumerator 
		oOccs = oAsmCompDef.Occurrences.AllLeafOccurrences 
'		Dim oSubOccs As ComponentOccurrences
'		oSubOccs = oAssyDoc.ComponentDefinition.Occurrences



		For Each oOcc In oOccs
			MessageBox.Show(oOcc.Name) 'check line for debugging
			'Trace.WriteLine(oOcc.Name, "iLogic") 'debug 
			If Not oOcc.Suppressed Then
				If Left(oOcc.Name, oCnt) = sComponent Then
					iQTY += 1 'count the components

				End If
				'		MessageBox.Show("Done0", "Checker")

				'look at only virtual components
				If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
					'find and delete existing virtual parts by name
					If Left(oOcc.Name, oCnt2 + 1) = sVirtPart & ":" Then
						'delete existing virtual parts if name matches
						oOcc.delete
					End If
				End If
				'		MessageBox.Show("Done1", "Checker")
			End If
		Next

		Dim identity As Matrix
		identity = ThisApplication.TransientGeometry.CreateMatrix()

		'create first instance of the virtual part
		Dim virtOcc As ComponentOccurrence
		virtOcc = oOccs.AddVirtual(sVirtPart, identity)
		
		'add next instance starting at instance2 (if applicable)
		Dim index As Integer
		If VPart = "Bolt"
			QTY = iQTY
		Else If VPart = "Nut"
			QTY = iQTY * 2
		Else If VPart = "Washer"
			QTY = iQTY * 3
		Else If VPart = "Gasket"
			QTY = iQTY
		End If 
				index = 2
				Do While index <= QTY
				oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
				index += 1
			Loop
	Next
Next