Count all unsupressed occurrences in an assembly with iLogic

Count all unsupressed occurrences in an assembly with iLogic

erobinsonMRQE2
Contributor Contributor
1,505 Views
4 Replies
Message 1 of 5

Count all unsupressed occurrences in an assembly with iLogic

erobinsonMRQE2
Contributor
Contributor

Hi All,

 

I have a rule that a looks through each component in an assembly and counts the occurrences of a specified part, to allow for virtual parts to be generated for nut bolts and such.

 

The rule errors out when it encounters a suppressed component, is there a way to check if components are suppressed and skip those components.

 

Any suggestions would be appreciated 

 

 

'iterate through all Of the occurrences in the Assembly
For Each oOcc In oOccs
MessageBox.Show(oOcc.Name)
'Trace.WriteLine(oOcc.Name, "iLogic") 'debug 
	 If Left(oOcc.Name, oCnt) = sComponent Then 
   			iQTY += 1 'count the components
	
   	End If

    '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
Next
0 Likes
Accepted solutions (1)
1,506 Views
4 Replies
Replies (4)
Message 2 of 5

Sergio.D.Suárez
Mentor
Mentor

Hi, without the complete rule and an example model, it is difficult to try to understand what you need to achieve. To start, I would try to place the "on error resume next" instruction below the line
  For Each oOcc In oOccs
to skip the occurrence that returns error. Cheers


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 3 of 5

erobinsonMRQE2
Contributor
Contributor

Hi Sergio,

Here is the Full code. 

I created a dummy assembly as a test file as I can't send out the real one, but it should function in the same way. 

The code is currently looking for any component named "Lid", and if it finds any it creates a virtual part "Bolt". 

 

The problem is that if any components in the assembly are suppressed the code errors out. I don't have a way to eliminate suppressions from the assembly I am trying to implement this code in.

 

So I'm looking for a way to get this to ideally ignore suppressed parts as I wouldn't want a virtual part created if the part was suppressed.    

 

Thanks

 

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

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

'define virtual component to create
Dim sVirtPart As String
sVirtPart = "Bolt" 

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

'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

Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssyDoc.ComponentDefinition
Dim oOccs As ComponentOccurrences
oOccs = oAssyDoc.ComponentDefinition.Occurrences
Dim iQTY As Integer
iQTY = 0

'iterate through all Of the occurrences in the Assembly
For Each oOcc In oOccs
'MessageBox.Show(oOcc.Name) 'check line for debugging
'Trace.WriteLine(oOcc.Name, "iLogic") 'debug 
	 If Left(oOcc.Name, oCnt) = sComponent Then 
   			iQTY += 1 'count the components
	
   	End If

    '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
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 
index = 2
Do While index <= iQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity) 
index += 1
Loop
0 Likes
Message 4 of 5

mcgyvr
Consultant
Consultant
Accepted solution
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


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 5 of 5

erobinsonMRQE2
Contributor
Contributor

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	

 

0 Likes