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

Supress all components with the same name

tomislav.peran
Advocate

Supress all components with the same name

tomislav.peran
Advocate
Advocate

Hello,

 

I would like to suppress all components in my assembly which contain the name "Cube: " in itself.

I tried making a code by using other codes as an example but, even though the code does what I want, it is not a good one.

 

I have no idea how to stop the code iteration backwards so I just inserted Try & Catch as a quick fix.

Otherwise, I will get an error "Cube:0" can not be found.

 

If somebody knows how to do this properly please let me know,

Tom

 

Dim oDoc As AssemblyDocument
oDoc = ThisDoc.Document
Dim oCubeOccPrefix = "Cube:"

i = 0
'count existing cubes, if any
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		i = i + 1
	End If
Next

i = 1
'renumber
For Each oOcc In oDoc.ComponentDefinition.Occurrences
	If oOcc.name.contains(oCubeOccPrefix) Then
		oOcc.Name = oCubeOccPrefix & i 'set  name
		i = i + 1
	End If
Next

Try	
	i = i - 1
	
	For Each oOcc In oDoc.ComponentDefinition.Occurrences
		
Component.IsActive(oCubeOccPrefix & i) = False

Next

	For Each oOcc In oDoc.ComponentDefinition.Occurrences
		
	i = i - 1 
		
Component.IsActive(oCubeOccPrefix & i) = False

Next
 
Catch
End Try	

 

0 Likes
Reply
Accepted solutions (1)
257 Views
2 Replies
Replies (2)

Zach.Stauffer
Advocate
Advocate
Accepted solution

For something like this I think you can do everything in a single loop instead of multiple loops and trying to keep track of everything. Try this.

Dim assemblyDoc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument

Dim cubeOccurrencePrefix = "Cube:"

Dim numberOfOccurrences As Integer = 0

For Each occurrence As Inventor.ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
	If occurrence.Name.Contains(cubeOccurrencePrefix) Then 
		numberOfOccurrences += 1
		occurrence.Name = cubeOccurrencePrefix & numberOfOccurrences
		Component.IsActive(occurrence.Name) = False
	End If
Next

tomislav.peran
Advocate
Advocate

Hi Zach,

 

Your code works perfectly. This is much much better.

 

Thanks a lot.

 

Tom