Hi, I have been working on this code for some days now. Been through many forum posts to get me this far and I am nearly there. The last part of the puzzle i am missing is to get the code to ONLY add the virtual components of the files not found in the component.add part of the code. I know i need some kind of if file not found then....and then i also need to change up the part of the code where it is adding the virtual components but i just cant figure it out. I'd be very grateful for any help.
StartRule : ProjectNo = iProperties.Value("Project", "Project") If Trim(ProjectNo) = "" Then 'get value from user and write it to the project iproperty ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo) Else End If Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & CStr(iProperties.Value("Project", "Project")) & " DK250S Config.xlsx" GoExcel.Open(sFile, "Test") Dim oPNcol = "A" Dim oQTYcol = "B" Dim FirstRow = "2" Dim prevComp, newComp As ManagedComponentOccurrence ' Insert Loop For i As Integer = 0 To 999 Dim Total As Integer Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & (FirstRow + i))) Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & (FirstRow + i))) Total = oQTY For X = 1 To Total Try 'Dim newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing) newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing) Catch VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If VirturalComp = vbYes Then 'define assembly Dim asmDoc As AssemblyDocument asmDoc = ThisApplication.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition Dim occs As ComponentOccurrences occs = asmDoc.ComponentDefinition.Occurrences Dim identity As Matrix identity = ThisApplication.TransientGeometry.CreateMatrix Dim MyArrayList As New ArrayList MyArrayList = GoExcel.CellValues(sFile, "Test", "A2", "A200") Dim sVirtPart As String 'get info from the XLS file For MyRow = 2 To 200 'index row 2 through 200 iQTY = GoExcel.CellValue("B" & MyRow) oProp1 = GoExcel.CellValue("A" & MyRow) sVirtPart = oProp1 'defines the virtual part name '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 Then 'compare name selected from list to the 'existing virtual parts If oOcc = sVirtPart Then 'delete existing virtual parts if name matches asmOcc.Delete End If End If Next 'create first instance of the virtual part Dim virtOcc As ComponentOccurrence If iQTY >= 1 Then virtOcc = occs.AddVirtual(sVirtPart, identity) Try iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1 Catch 'catch error when oProp1 = nothing End Try Try iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp1 Catch 'catch error when oProp2 = nothing End Try End If 'add next instance starting at instance2 (if applicable) Dim index As Integer index = 2 Do While index <= iQTY occs.AddByComponentDefinition(virtOcc.Definition, identity) index += 1 Loop Next End If End Try Next Next ThisApplication.ActiveView.Fit
Solved! Go to Solution.
Hi, I have been working on this code for some days now. Been through many forum posts to get me this far and I am nearly there. The last part of the puzzle i am missing is to get the code to ONLY add the virtual components of the files not found in the component.add part of the code. I know i need some kind of if file not found then....and then i also need to change up the part of the code where it is adding the virtual components but i just cant figure it out. I'd be very grateful for any help.
StartRule : ProjectNo = iProperties.Value("Project", "Project") If Trim(ProjectNo) = "" Then 'get value from user and write it to the project iproperty ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo) Else End If Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & CStr(iProperties.Value("Project", "Project")) & " DK250S Config.xlsx" GoExcel.Open(sFile, "Test") Dim oPNcol = "A" Dim oQTYcol = "B" Dim FirstRow = "2" Dim prevComp, newComp As ManagedComponentOccurrence ' Insert Loop For i As Integer = 0 To 999 Dim Total As Integer Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & (FirstRow + i))) Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & (FirstRow + i))) Total = oQTY For X = 1 To Total Try 'Dim newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing) newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, grounded := False, visible := True, appearance := Nothing) Catch VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If VirturalComp = vbYes Then 'define assembly Dim asmDoc As AssemblyDocument asmDoc = ThisApplication.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition Dim occs As ComponentOccurrences occs = asmDoc.ComponentDefinition.Occurrences Dim identity As Matrix identity = ThisApplication.TransientGeometry.CreateMatrix Dim MyArrayList As New ArrayList MyArrayList = GoExcel.CellValues(sFile, "Test", "A2", "A200") Dim sVirtPart As String 'get info from the XLS file For MyRow = 2 To 200 'index row 2 through 200 iQTY = GoExcel.CellValue("B" & MyRow) oProp1 = GoExcel.CellValue("A" & MyRow) sVirtPart = oProp1 'defines the virtual part name '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 Then 'compare name selected from list to the 'existing virtual parts If oOcc = sVirtPart Then 'delete existing virtual parts if name matches asmOcc.Delete End If End If Next 'create first instance of the virtual part Dim virtOcc As ComponentOccurrence If iQTY >= 1 Then virtOcc = occs.AddVirtual(sVirtPart, identity) Try iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1 Catch 'catch error when oProp1 = nothing End Try Try iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp1 Catch 'catch error when oProp2 = nothing End Try End If 'add next instance starting at instance2 (if applicable) Dim index As Integer index = 2 Do While index <= iQTY occs.AddByComponentDefinition(virtOcc.Definition, identity) index += 1 Loop Next End If End Try Next Next ThisApplication.ActiveView.Fit
Solved! Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Hi @kpk7VYPK . I tried to fix all the errors I found, but in order to check if the code works, I need to have your files (excel and inventor files). Check yourself whether these changes gave the expected result.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= iQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Hi @kpk7VYPK . I tried to fix all the errors I found, but in order to check if the code works, I need to have your files (excel and inventor files). Check yourself whether these changes gave the expected result.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= iQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Thank you!
It works ok, in the sense that it adds all the found .ipt files and then the virtual parts. But i also need it to find the quantity in the excel file and add that many into the assembly. The actual parts need to be added in the actual number of times but for the virtual parts it doesnt matter if they are added in many times or if the BOM QTY is updated from the excel sheet.
Thank you!
It works ok, in the sense that it adds all the found .ipt files and then the virtual parts. But i also need it to find the quantity in the excel file and add that many into the assembly. The actual parts need to be added in the actual number of times but for the virtual parts it doesnt matter if they are added in many times or if the BOM QTY is updated from the excel sheet.
I found an error, in my version of the code (line 74) "Do While index <= iQTY" should be replaced with "oQTY". Try again:
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
I found an error, in my version of the code (line 74) "Do While index <= iQTY" should be replaced with "oQTY". Try again:
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
I had to add in the line:
Total = oQTY For X = 1 To Total
above the Try..catch for it to add the ipt files in multiple times. Now it works!! Genius. Was that the right way to add that in? I am very new to ilogic and its still like a foreign language to me. So now i need to try to break down your code to try to figure out what the heck you did! haha
This is the full code now:
StartRule : Dim oPNcol As Char = "A" Dim oQTYcol As Char = "B" Dim FirstRow As Integer = 2 Dim prevComp, newComp As ManagedComponentOccurrence Dim oInvApp As Inventor.Application = ThisApplication 'define assembly Dim asmDoc As AssemblyDocument asmDoc = oInvApp.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = asmDoc.ComponentDefinition ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value If Trim(ProjectNo) = "" Then 'get value from user and write it to the project iproperty ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo) End If Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _ CStr(ProjectNo) & " DK250S Config.xlsx" GoExcel.Open(sFile, "Test") Dim identity As Matrix identity = oInvApp.TransientGeometry.CreateMatrix Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences ' Insert Loop Do Dim Total As Integer Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow)) If String.IsNullOrEmpty(val) Then GoTo ExitLoop Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow)) Integer.TryParse(oQTY, Total) Total = oQTY For X = 1 To Total Try newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _ grounded := False, visible := True, appearance := Nothing) Catch VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _ CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If VirturalComp = vbNo Then Exit Sub 'Iterate through all of the occurrences in the assembly For Each oOcc As ComponentOccurrence In oOccs 'get name of occurence only (sees only everything left of the colon) If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Dim oOccName As String = oOcc.Name.Split(":")(0) 'look at only virtual components 'compare name selected from list to the 'existing virtual parts If oOccName = val Then 'delete existing virtual parts if name matches oOcc.Delete End If End If Next 'create first instance of the virtual part Dim virtOcc As ComponentOccurrence Dim oVirtDoc As Document If oQTY > 0 Then virtOcc = oOccs.AddVirtual(val, identity) oVirtDoc = virtOcc.Definition.Document Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties") Try : oPropSet("Description").Value = oProp1 : Catch : End Try Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try End If 'add next instance starting at instance2 (if applicable) Dim index As Integer = 2 Do While index <= oQTY oOccs.AddByComponentDefinition(virtOcc.Definition, identity) index += 1 Loop End Try next FirstRow += 1 Loop ExitLoop : oInvApp.ActiveView.Fit
Thanks again!
I had to add in the line:
Total = oQTY For X = 1 To Total
above the Try..catch for it to add the ipt files in multiple times. Now it works!! Genius. Was that the right way to add that in? I am very new to ilogic and its still like a foreign language to me. So now i need to try to break down your code to try to figure out what the heck you did! haha
This is the full code now:
StartRule : Dim oPNcol As Char = "A" Dim oQTYcol As Char = "B" Dim FirstRow As Integer = 2 Dim prevComp, newComp As ManagedComponentOccurrence Dim oInvApp As Inventor.Application = ThisApplication 'define assembly Dim asmDoc As AssemblyDocument asmDoc = oInvApp.ActiveDocument 'define assembly Component Definition Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = asmDoc.ComponentDefinition ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value If Trim(ProjectNo) = "" Then 'get value from user and write it to the project iproperty ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo) End If Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _ CStr(ProjectNo) & " DK250S Config.xlsx" GoExcel.Open(sFile, "Test") Dim identity As Matrix identity = oInvApp.TransientGeometry.CreateMatrix Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences ' Insert Loop Do Dim Total As Integer Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow)) If String.IsNullOrEmpty(val) Then GoTo ExitLoop Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow)) Integer.TryParse(oQTY, Total) Total = oQTY For X = 1 To Total Try newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _ grounded := False, visible := True, appearance := Nothing) Catch VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _ CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If VirturalComp = vbNo Then Exit Sub 'Iterate through all of the occurrences in the assembly For Each oOcc As ComponentOccurrence In oOccs 'get name of occurence only (sees only everything left of the colon) If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Dim oOccName As String = oOcc.Name.Split(":")(0) 'look at only virtual components 'compare name selected from list to the 'existing virtual parts If oOccName = val Then 'delete existing virtual parts if name matches oOcc.Delete End If End If Next 'create first instance of the virtual part Dim virtOcc As ComponentOccurrence Dim oVirtDoc As Document If oQTY > 0 Then virtOcc = oOccs.AddVirtual(val, identity) oVirtDoc = virtOcc.Definition.Document Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties") Try : oPropSet("Description").Value = oProp1 : Catch : End Try Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try End If 'add next instance starting at instance2 (if applicable) Dim index As Integer = 2 Do While index <= oQTY oOccs.AddByComponentDefinition(virtOcc.Definition, identity) index += 1 Loop End Try next FirstRow += 1 Loop ExitLoop : oInvApp.ActiveView.Fit
Thanks again!
However, i have to click 'yes' in the catch as many times as the QTY is in the excel sheet. Is there a way to just say yes once?
However, i have to click 'yes' in the catch as many times as the QTY is in the excel sheet. Is there a way to just say yes once?
It is not necessary, my logic should work correctly.
Total = oQTY For X = 1 To Total
Here is a short version of the code, it can be seen that it works adequately, make sure that Excel imports the correct number of components (line 36).
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
It is not necessary, my logic should work correctly.
Total = oQTY For X = 1 To Total
Here is a short version of the code, it can be seen that it works adequately, make sure that Excel imports the correct number of components (line 36).
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
It is adding the correct number of virtual parts, but not the ipt files it finds. See screen shot of BOM and excel sheet:
It is adding the correct number of virtual parts, but not the ipt files it finds. See screen shot of BOM and excel sheet:
If I understand correctly, you need the number of details (.ipt) to match the ecxel. I made changes to lines 39-42.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For i As Integer = 1 To oQTY
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
If I understand correctly, you need the number of details (.ipt) to match the ecxel. I made changes to lines 39-42.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For i As Integer = 1 To oQTY
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Exactly! Now it works
Exactly! Now it works
Hi again @Andrii_Humeniuk the code is working perfectly. However, I would like to add one more option. In some cases the ipt file has a prefix of just "D" and in others "CO_D" so i need to change the line:
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _ grounded := False, visible := True, appearance := Nothing)
I have tried a few times on my own with just a "*" in the code for the prefix, but the code seems to get confused as to which one to add if it finds both options. So i need an if in there somewhere that says:
If you find file with prefix "CO_D" as a first choice, then add that, if not look for "D" and if it doesnt find either of those then it can run through the virtural component add part of the code.
Can you help with that? 🙏
Hi again @Andrii_Humeniuk the code is working perfectly. However, I would like to add one more option. In some cases the ipt file has a prefix of just "D" and in others "CO_D" so i need to change the line:
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _ grounded := False, visible := True, appearance := Nothing)
I have tried a few times on my own with just a "*" in the code for the prefix, but the code seems to get confused as to which one to add if it finds both options. So i need an if in there somewhere that says:
If you find file with prefix "CO_D" as a first choice, then add that, if not look for "D" and if it doesnt find either of those then it can run through the virtural component add part of the code.
Can you help with that? 🙏
Added a check for existing files (line 42, 45), hope this helps.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
Dim sPathAsm As String = IO.Path.GetDirectoryName(asmDoc.FullFileName)
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For i As Integer = 1 To oQTY
If IO.File.Exists(sPathAsm & "\CO_D" & val & ".ipt") Then
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Else If IO.File.Exists(sPathAsm & "\D" & val & ".ipt") Then
newComp = Components.Add("", "D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
End If
Next
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Added a check for existing files (line 42, 45), hope this helps.
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
Dim sPathAsm As String = IO.Path.GetDirectoryName(asmDoc.FullFileName)
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sFile As String = "C:\VaultWorkspace\Designs\PROJ Skiold\Personlige\KPK\" & _
CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For i As Integer = 1 To oQTY
If IO.File.Exists(sPathAsm & "\CO_D" & val & ".ipt") Then
newComp = Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Else If IO.File.Exists(sPathAsm & "\D" & val & ".ipt") Then
newComp = Components.Add("", "D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
End If
Next
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Thanks, nothing happens when i run that code. It just asks for the project number and stops.
ThesPathAsm needs to look in my Vault work space folder, and not the same folder as the .iam file.
Thanks, nothing happens when i run that code. It just asks for the project number and stops.
ThesPathAsm needs to look in my Vault work space folder, and not the same folder as the .iam file.
sPathAsm - this is the path to your assembly. І thought your files were there. You need to specify the path to your files in sPathAsm, and check for the existence of the file using the IO.File.Exists() function.
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
sPathAsm - this is the path to your assembly. І thought your files were there. You need to specify the path to your files in sPathAsm, and check for the existence of the file using the IO.File.Exists() function.
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
But when the file runs without that, it looks for the files in the Vault Workspace folder with the components.add code.
The files are in many subfolders so i need it to look in all subfolders after C:\VaultWorkspace\Designs\PROJ Skiold\
But when the file runs without that, it looks for the files in the Vault Workspace folder with the components.add code.
The files are in many subfolders so i need it to look in all subfolders after C:\VaultWorkspace\Designs\PROJ Skiold\
Added a list of your parts that are under this path - "C:\VaultWorkspace\Designs\PROJ Skiold" (line 24). Next, I search through the list (lines 41-55).
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sPath As String = "C:\VaultWorkspace\Designs\PROJ Skiold"
Dim sFilesName() As String = IO.Directory.GetFiles(sPath, "*.ipt", IO.SearchOption.AllDirectories)
Dim sFile As String = sPath & "\Personlige\KPK\" & CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For iFile As Integer = 0 To sFilesName.Length-1
If sFilesName(iFile).Contains("\CO_D" & val & ".ipt") Then
For i As Integer = 1 To oQTY
Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Exit For
Else If sFilesName(iFile).Contains("\D" & val & ".ipt") Then
For i As Integer = 1 To oQTY
Components.Add("", "D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Exit For
End If
Next iFile
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Added a list of your parts that are under this path - "C:\VaultWorkspace\Designs\PROJ Skiold" (line 24). Next, I search through the list (lines 41-55).
StartRule :
Dim oPNcol As Char = "A"
Dim oQTYcol As Char = "B"
Dim FirstRow As Integer = 2
Dim prevComp, newComp As ManagedComponentOccurrence
Dim oInvApp As Inventor.Application = ThisApplication
'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = oInvApp.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
ProjectNo = asmDoc.PropertySets("Design Tracking Properties")("Project").Value
If Trim(ProjectNo) = "" Then
'get value from user and write it to the project iproperty
ProjectNo = InputBox("Enter the Project Number", "iLogic", ProjectNo)
End If
Dim sPath As String = "C:\VaultWorkspace\Designs\PROJ Skiold"
Dim sFilesName() As String = IO.Directory.GetFiles(sPath, "*.ipt", IO.SearchOption.AllDirectories)
Dim sFile As String = sPath & "\Personlige\KPK\" & CStr(ProjectNo) & " DK250S Config.xlsx"
GoExcel.Open(sFile, "Test")
Dim identity As Matrix
identity = oInvApp.TransientGeometry.CreateMatrix
Dim oOccs As ComponentOccurrences = oAsmCompDef.Occurrences
' Insert Loop
Do
Dim Total As Integer
Dim val As String = CStr(GoExcel.CellValue(sFile, "Test", oPNcol & FirstRow))
If String.IsNullOrEmpty(val) Then GoTo ExitLoop
Dim oQTY As String = CStr(GoExcel.CellValue(sFile, "Test", oQTYcol & FirstRow))
Integer.TryParse(oQTY,Total)
Try
For iFile As Integer = 0 To sFilesName.Length-1
If sFilesName(iFile).Contains("\CO_D" & val & ".ipt") Then
For i As Integer = 1 To oQTY
Components.Add("", "CO_D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Exit For
Else If sFilesName(iFile).Contains("\D" & val & ".ipt") Then
For i As Integer = 1 To oQTY
Components.Add("", "D" & val & ".ipt", position := Nothing, _
grounded := False, visible := True, appearance := Nothing)
Next
Exit For
End If
Next iFile
Catch
VirturalComp = MessageBox.Show("No dummy found for this part, create virtual component? " & _
CStr(val), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If VirturalComp = vbNo Then Exit Sub
'Iterate through all of the occurrences in the assembly
For Each oOcc As ComponentOccurrence In oOccs
'get name of occurence only (sees only everything left of the colon)
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
Dim oOccName As String = oOcc.Name.Split(":")(0)
'look at only virtual components
'compare name selected from list to the
'existing virtual parts
If oOccName = val Then
'delete existing virtual parts if name matches
oOcc.Delete
End If
End If
Next
'create first instance of the virtual part
Dim virtOcc As ComponentOccurrence
Dim oVirtDoc As Document
If oQTY > 0 Then
virtOcc = oOccs.AddVirtual(val, identity)
oVirtDoc = virtOcc.Definition.Document
Dim oPropSet As PropertySet = oVirtDoc.PropertySets("Design Tracking Properties")
Try : oPropSet("Description").Value = oProp1 : Catch : End Try
Try : oPropSet("Part Number").Value = oProp1 : Catch : End Try
End If
'add next instance starting at instance2 (if applicable)
Dim index As Integer = 2
Do While index <= oQTY
oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
index += 1
Loop
End Try
FirstRow += 1
Loop
ExitLoop :
oInvApp.ActiveView.Fit
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Can't find what you're looking for? Ask the community or share your knowledge.