06-27-2022
03:06 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
06-27-2022
03:06 AM
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
Solved! Go to Solution.
06-27-2022
05:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
06-27-2022
05:36 AM
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
06-27-2022
07:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
06-27-2022
07:57 AM
Hi Zach,
Your code works perfectly. This is much much better.
Thanks a lot.
Tom