Deleting virtual components
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So I have a bit of an oddity with virtual components. The same bit of code works just fine in some instances of my program and refuses to work in others.
I have an assembly that may or may not need virtual components. One particular name I'm sure I'll need gets ordered and deleted correctly. There seems to be no issues with it.
Another instance of my program the amount depends on what I have in the assembly and so if I remove parts it's supposed to go back and update the number of parts. The issue with this one is that if goes to 0, it doesn't delete the last virtual part. It always leaves exactly one if there are supposed to be none in the assembly. The oddity with this one is that the same exact function is used in the two different rules and works in one but not the other. I guess it works in one because I know I'll always need at least one of the components so it never actually attempts to have no instances of the virtual components in the assembly.
Function UpdateVirtParts (sVirtPart, Description, iQty)
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
'Iterate through all of the occurrences in the assembly
Dim asmOcc As ComponentOccurrence
For Each asmOcc In oAsmCompDef.Occurrences
'get name of occurence only (sees only everything left of the colon)
Dim oOcc As Object
oOcc = asmOcc.Name.Split(":")(0)
'look at only virtual components
If TypeOf asmOcc.Definition Is VirtualComponentDefinition
'compare name selected from list to the
'existing virtual parts
If oOcc = sVirtPart Then asmOcc.delete
End If
Next
Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix
'create first instance of the virtual part
If iQTY > 0
Dim virtOcc As ComponentOccurrence
virtOcc = occs.AddVirtual(sVirtPart, identity)
iProperties.Value(sVirtPart & ":1", "Project", "Description") = Description
iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = sVirtPart
For index As Integer = 2 To iQTY
occs.AddByComponentDefinition(virtOcc.Definition, identity)
Next
End If
End Function
The final instance is meant to purge the program of all virtual components because this one may have ordered different parts previously. The exact virtual component it needs will depend on the geometry of the assembly, so when I change it may end up needing a different part entirely. This one runs fine if there's no virtual components to delete, but the first time it checks for an actual virtual component and finds one, it errors.
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Function PurgeVirtualComponents() Dim asmDoc As AssemblyDocument asmDoc = ThisApplication.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences in the assembly and purge virtual components Dim asmOcc As ComponentOccurrence For Each asmOcc In oAsmCompDef.Occurrences If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then asmOcc.delete Next End Function '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
This is the error in question: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
Thank you,
Thomas Long