Question:
How to insert Standard and Custom iParts in assembly using Inventor API?
Solution
To add an iPart member, you need to use Occurrences.AddiPartMember() method.
Public Function AddiPartMember( _
ByVal FactoryFileName As String, _
ByVal Position As Matrix, _
Optional ByVal Row As Variant _
) As ComponentOccurrence
To add a custom member, you need to use Occurrences.AddCustomiPartMember() method.
Public Function AddCustomiPartMember( _
ByVal FactoryFileName As String, _
ByVal Position As Matrix, _
ByVal FullFileName As String, _
Optional ByVal Row As Variant, _
Optional ByVal CustomInput As Variant _
) As ComponentOccurrence
The optional argument Row specifies the row for the member within the factory and can be:
- a Row index (Long)
- an iPartTableRow object (doesn’t work for Custom iParts).
- a String (part identifier, i.e. ''[Height=1.000 in][Length=2.000 in][Radius=0.250 in]'')
Following is a VB.NET code. It uses the sample model files which locate at SDK of Inventor 2014:
…\SDK\DeveloperTools\Samples\Data_Files\
Sub AddiParts()
Dim DataFolder AsString = _
"C:\Users\Public\Documents\Autodesk"& _
"\Inventor 2014\SDK\DeveloperTools"& _
"\Samples\Data_Files"
Dim standardiPartFile AsString _
= DataFolder & "\StandardFactory.ipt"
Dim customiPartFile AsString _
= DataFolder & "\CustomFactory.ipt"
Dim oTG AsTransientGeometry = _
oApp.TransientGeometry
' Create a matrix. A new matrix is initialized
' with an identity matrix.
Dim oMatrix AsMatrix = oTG.CreateMatrix
Dim oAsmDef AsAssemblyComponentDefinition _
= oApp.ActiveDocument.ComponentDefinition
Dim oOcc AsComponentOccurrence
Dim oDoc AsPartDocument
Dim oPartDef AsPartComponentDefinition
'Open the standard iPart file
oDoc = oApp.Documents.Open(standardiPartFile, True)
oPartDef = oDoc.ComponentDefinition
Dim oTblRow AsiPartTableRow _
= oPartDef.iPartFactory.TableRows.Item(2)
'Three ways to insert Standard iPart
' 1. By Row index
oOcc = oAsmDef.Occurrences.AddiPartMember( _
standardiPartFile, oMatrix, 1)
' 2. By iPart Table Row,
oMatrix.SetTranslation(oTG.CreateVector(100, 0, 0))
oOcc = oAsmDef.Occurrences.AddiPartMember( _
standardiPartFile, oMatrix, oTblRow)
' 3. By string identifier
oMatrix.SetTranslation(oTG.CreateVector(200, 0, 0))
oOcc = oAsmDef.Occurrences.AddiPartMember( _
standardiPartFile, oMatrix, _
"StandardFactory [d0=2.2][d1=6.837][d2=2.551]")
'close without saving any changes
oDoc.Close(True)
'Open the custom iPart file
oDoc = oApp.Documents.Open(customiPartFile, True)
oPartDef = oDoc.ComponentDefinition
If oPartDef.iPartFactory.CustomFactory = TrueThen
MsgBox("This is Custom iPart Factory")
EndIf
Dim oTblRowCust AsiPartTableRow _
= oPartDef.iPartFactory.TableRows.Item(2)
'Two ways to insert custom ipart
'Row by index
oMatrix.SetTranslation(oTG.CreateVector(300, 0, 0))
oOcc = oAsmDef.Occurrences.AddCustomiPartMember( _
customiPartFile, oMatrix, _
"C:\temp\gear1.ipt", 1)
' Row by string identifier
oMatrix.SetTranslation(oTG.CreateVector(400, 0, 0))
oOcc = oAsmDef.Occurrences.AddCustomiPartMember( _
customiPartFile, oMatrix, _
"C:\temp\gear3.ipt", _
"CustomFactory [d0=2.3][d1=6.937][d2=2.552]")
'close without saving any changes
oDoc.Close(True)
EndSub