12-05-2019
05:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
12-05-2019
05:36 AM
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