'Place virtual parts selected from Excel sheet (original concept by C.Waguespack)
'CAD Studio - www.cadstudio.cz  www.cadforum.cz
'--------------------------------------------------------------------------------
'EN: Variables; CZ: Promnn:
'either: absolute filename, relative to the assembly or Workspace, "3rd Party:Name.xls", "3rd Party:Embedding#"
Dim XLSname As String = "S:\Library\VirtualParts.xlsx"
'EN: Data start at row; CZ: Data zanaj na dku:
Dim FirstDataRow As Integer = 2

'check environment
doc = ThisDoc.ModelDocument
If doc.DocumentType <> kAssemblyDocumentObject Then
 MessageBox.Show("Must run in an assembly document!", "iLogic")
 Return ' exit
End If

Dim ListOfVParts As New ArrayList
Try
 ListOfVParts = GoExcel.CellValues(XLSname, "Sheet1", "A2", "")
Catch
 ListOfVParts = GoExcel.CellValues(XLSname, "List1", "A2", "") ' localized sheet name, e.g. "Tabelle1"
End Try

Dim sVirtPart As String
sVirtPart = InputListBox("Select a virtual part to add", ListOfVParts, ListOfVParts.Item(0), "iLogic", "Standard Virtual Parts")
If sVirtPart = "" Then Return ' exit

'prompt for quantity
Qty = InputBox("Enter the total number of" _
& vbLf & """" & sVirtPart & """" _
& vbLf & "parts to place in the assembly" _
& vbLf &  vbLf & "(entering 0 will delete all existing instances)", "iLogic", "1")
If Qty = "" Then Return 'exit

'GoExcel.FindRow ?
'get iProperties from the XLS file rows
For XlsRow = FirstDataRow To ListOfVParts.Count+FirstDataRow-1
     If sVirtPart = (GoExcel.CellValue("A" & XlsRow)) Then  'match: get the iProperty from the XLS file columns
            oProp1 = GoExcel.CellValue("A" & XlsRow) 'column A
            oProp2 = GoExcel.CellValue("B" & XlsRow) 'column B
            oProp3 = GoExcel.CellValue("C" & XlsRow) 'column C
            oProp4 = GoExcel.CellValue("D" & XlsRow) 'column D
            oProp5 = GoExcel.CellValue("E" & XlsRow) 'column E
            oProp6 = GoExcel.CellValue("F" & XlsRow) 'column F  etc. etc.
            Exit For
     End If
Next

Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
Dim AsmCompDef As AssemblyComponentDefinition
AsmCompDef = asmDoc.ComponentDefinition
Dim oOcc As Object

'All occurrences in the assembly
Dim asmOcc As ComponentOccurrence
For Each asmOcc In AsmCompDef.Occurrences
   'get name of occurrence (left of the colon)
   oOcc = asmOcc.Name.Split(":")(0)
   If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then    'only virtual components
     If oOcc = sVirtPart Then asmOcc.delete  'delete existing virtual part
   End If
Next
 
Dim oCocs As ComponentOccurrences
oCocs = asmDoc.ComponentDefinition.Occurrences
Dim identity As Matrix = ThisApplication.TransientGeometry.CreateMatrix

'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
If Qty > 0 Then
   virtOcc = oCocs.AddVirtual(sVirtPart, identity)
   Try
     iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1
   Catch
   End Try
   Try
     iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp2
   Catch
   End Try
   Try
     iProperties.Value(sVirtPart & ":1", "Project", "Stock Number") = oProp3
   Catch
   End Try
   Try
     iProperties.Value(sVirtPart & ":1", "Project", "Vendor") = oProp4
   Catch
   End Try
   Try
     iProperties.Value(sVirtPart & ":1", "Summary", "Comments") = oProp5
   Catch
   End Try
   Try
     iProperties.Value(sVirtPart & ":1", "Custom", "Param1") = oProp6  ' etc. etc.
   Catch
   End Try
Else
   Return
End If

' add more instances
Dim idx As Integer
idx = 2
While idx <= Qty
 oCocs.AddByComponentDefinition(virtOcc.Definition, identity)
 idx += 1
End While
