Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Jakob_Hamburg
2035 Views, 17 Replies

Ilogic create “empty parts“ and fill their iProperties from excel

Dear all,

 

I would like to make an iLogic rule that creates “empty parts“ within an assembly and fill their iProperties from an excel sheet.

I do not want to create “virtual parts” but “empty parts (.ipt)“.

(I need the .ipt files later in Vault to follow an established process that brings articles (information of electric and hydraulic parts) into an ERP system. That’s why I do not want to use virtual parts)

 

My main problem is that I am still just a beginner in iLogic/Vba/VB, so I do not know how to fit my snippets together.

 

I found a rule that creates virtual parts in an assembly based on an excel sheet. It works fine.

(adjusting this rule to my own excel sheet requirements is doable for me).

 

 

'[ Browse for the Excel file
oMsg = "Select a Project Information Excel File"

'update the status bar 
ThisApplication.StatusBarText = oMsg

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg.DialogTitle = oMsg
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True	

Try	
	oFileDlg.ShowOpen()
Catch
	'catch error when no file is selected
	Return 'exit rule
End Try

If Err.Number <> 0 Then	
	MessageBox.Show("A problem occured when getting the Excel file.", "iLogic",MessageBoxButtons.OK,MessageBoxIcon.Error)	
	Return 'exit if file not selected
	
ElseIf oFileDlg.FileName <> "" Then
	myXLS  = oFileDlg.FileName
	
	'update the status bar with the Excel file name
	ThisApplication.StatusBarText = "...reading info from " & myXLS
End If
']

Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues(myXLS, "Sheet1", "A2", "A200")

'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 sVirtPart As String

'get info from the XLS file
For MyRow = 2 To 200 'index row 2 through 200	
	
    iQTY = GoExcel.CellValue("A" & MyRow)
    oProp1 = GoExcel.CellValue("B" & MyRow)	
    oProp2 = GoExcel.CellValue("C" & MyRow)
    oProp3 = GoExcel.CellValue("D" & MyRow)
    oProp4 = GoExcel.CellValue("E" & MyRow)	
	sVirtPart = oProp1 'defines the virtual part name
	
	'update the status bar with the name
	ThisApplication.StatusBarText = sVirtPart
	
	'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") = oProp2
		Catch 'catch error when oProp2 = nothing
		End Try

		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Vendor") = oProp3
		Catch 'catch error when oProp4 = nothing
		End Try
		
		Try
		iProperties.Value(sVirtPart & ":1", "Summary", "Comments") = oProp4
		Catch 'catch error when oProp5 = 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

'update the status bar 
ThisApplication.StatusBarText = "Virtual components added!"

 

Jakob_Hamburg_0-1604659907102.png

 

 

This rule comes from here:

https://forums.autodesk.com/t5/inventor-customization/ilogic-add-standard-virtual-parts-from-an-exce...

(from post #12 in that thread)

 

But as I said I cannot use virtual parts but I need real (empty) parts.

 

So I found another rule that creates one empty part (.ipt) in an existing assembly.

So I wonder if it is possible to replace parts in the first rule by the second rule in order to create real parts but not virtual parts.

And at which point a save command for new created parts (.ipt) should be added in the merged rule.

 

Does anybody have an advice for me?

 

This is the second rule which creates one empty part (.ipt) in an existing assembly:

 

Dim oAssDoc As AssemblyDocument
    oAssDoc = ThisApplication.ActiveDocument
    
     Dim oAssDef As AssemblyComponentDefinition
     oAssDef = oAssDoc.ComponentDefinition
    
    Dim oNewPart As PartDocument
     oNewPart = ThisApplication.Documents.Add(kPartDocumentObject, , False)
    
    Dim oNewPartDef As PartComponentDefinition
     oNewPartDef = oNewPart.ComponentDefinition
    
    'do what you need to create in this part
    'e.g. create a block
    
    
    
    'place without any transformation
    'can adjust the matrix with your requirement
    Dim oMartrix As Matrix
    oMatrix = ThisApplication.TransientGeometry.CreateMatrix()
    
    'add the new part to the assembly
    Dim oNewOcc As ComponentOccurrence
    oNewOcc = oAssDef.Occurrences.AddByComponentDefinition(oNewPartDef, oMatrix)
    
     
    
 

 

That rule comes from here:

https://forums.autodesk.com/t5/inventor-customization/creation-of-part-using-ilogic/td-p/6334536

 

Does anybody have an advice for me?