Supress all components with the same name

Supress all components with the same name

tomislav.peran
Advocate Advocate
307 Views
2 Replies
Message 1 of 3

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
Accepted solutions (1)
308 Views
2 Replies
Replies (2)
Message 2 of 3

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
Message 3 of 3

tomislav.peran
Advocate
Advocate

Hi Zach,

 

Your code works perfectly. This is much much better.

 

Thanks a lot.

 

Tom