Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add virtual component if .ipt file not found

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
kpk7VYPK
708 Views, 16 Replies

Add virtual component if .ipt file not found

kpk7VYPK
Enthusiast
Enthusiast

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
0 Likes

Add virtual component if .ipt file not found

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
16 REPLIES 16
Message 2 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor

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.

EESignature

0 Likes

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.

EESignature

Message 3 of 17
kpk7VYPK
in reply to: Andrii_Humeniuk

kpk7VYPK
Enthusiast
Enthusiast

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.

0 Likes

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.

Message 4 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor

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.

EESignature

0 Likes

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.

EESignature

Message 5 of 17
kpk7VYPK
in reply to: Andrii_Humeniuk

kpk7VYPK
Enthusiast
Enthusiast

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! 

0 Likes

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! 

Message 6 of 17
kpk7VYPK
in reply to: kpk7VYPK

kpk7VYPK
Enthusiast
Enthusiast

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?

0 Likes

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?

Message 7 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor

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).

Test comp.png 

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.

EESignature

0 Likes

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).

Test comp.png 

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.

EESignature

Message 8 of 17
kpk7VYPK
in reply to: kpk7VYPK

kpk7VYPK
Enthusiast
Enthusiast

It is adding the correct number of virtual parts, but not the ipt files it finds. See screen shot of BOM and excel sheet:

 

kpk7VYPK_0-1691148308957.png

 

kpk7VYPK_2-1691148328961.png

 

 

0 Likes

It is adding the correct number of virtual parts, but not the ipt files it finds. See screen shot of BOM and excel sheet:

 

kpk7VYPK_0-1691148308957.png

 

kpk7VYPK_2-1691148328961.png

 

 

Message 9 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

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.

EESignature

0 Likes

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.

EESignature

Message 10 of 17
kpk7VYPK
in reply to: Andrii_Humeniuk

kpk7VYPK
Enthusiast
Enthusiast

Exactly! Now it works 

kpk7VYPK_0-1691149181262.png

 

0 Likes

Exactly! Now it works 

kpk7VYPK_0-1691149181262.png

 

Message 11 of 17
kpk7VYPK
in reply to: Andrii_Humeniuk

kpk7VYPK
Enthusiast
Enthusiast

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? 🙏

0 Likes

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? 🙏

Message 12 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor

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.

EESignature

0 Likes

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.

EESignature

Message 13 of 17
kpk7VYPK
in reply to: Andrii_Humeniuk

kpk7VYPK
Enthusiast
Enthusiast

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.

0 Likes

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.

Message 14 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor

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.

EESignature

0 Likes

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.

EESignature

Message 15 of 17
kpk7VYPK
in reply to: kpk7VYPK

kpk7VYPK
Enthusiast
Enthusiast

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\

0 Likes

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\

Message 16 of 17
Andrii_Humeniuk
in reply to: kpk7VYPK

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

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.

EESignature

0 Likes

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.

EESignature

Message 17 of 17
kpk7VYPK
in reply to: kpk7VYPK

kpk7VYPK
Enthusiast
Enthusiast

Thats it! Perfect. Thank you.

0 Likes

Thats it! Perfect. Thank you.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report