place a list of components with iMates into an assembly using ilogic/VBA

place a list of components with iMates into an assembly using ilogic/VBA

waynehelley
Collaborator Collaborator
3,353 Views
8 Replies
Message 1 of 9

place a list of components with iMates into an assembly using ilogic/VBA

waynehelley
Collaborator
Collaborator

Hi there,

 

I was just wondering how to place a component into an assembly using ilogic?  If this is possible, is there also an option to choose if iMates will be generated?

 

I would ideally like to place a list of components into an assembly, the list with probably exist in excel, then have iMates set to 'Automatically generate iMates on place' so that my assembly assembles automatically.

 

thanks,

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Accepted solutions (1)
3,354 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

The following is a sample in vba directly from the api help:

 

Public Sub iMateDuringOccurrencePlacementSample()
    ' Get the component definition of the currently open assembly.
    ' This will fail if an assembly document is not open.
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Create a new matrix object. It will be initialized to an identity matrix.
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix

    ' Place the first occurrence.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmCompDef.Occurrences.Add("C:\TempiMatePart.ipt", oMatrix)

    ' Place the second occurrence, but use iMates for its placement. This is
    ' equivalent to "Use iMate" check box on the "Place Component" dialog.
    Dim oOccEnumerator As ComponentOccurrencesEnumerator
    Set oOccEnumerator = oAsmCompDef.Occurrences.AddUsingiMates("C:\TempiMatePart.ipt", False)

    ' Since the 'PlaceAllMatching' flag was specified as False, we can be
    ' sure that just one ComponentOccurrence was returned in the enumerator.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oOccEnumerator.Item(1)
End Sub

I'm not very familiar with iLogic but I am sure it is possible in much the same way. 

Message 3 of 9

waynehelley
Collaborator
Collaborator

Thanks Robert.

 

When I move the code over to ilogic, it can't seem to carry out the command to place the components.

 

' oOcc1 = oAsmCompDef.Occurrences.Add("C:\ARTICULATE\09\090\09090GT0000008\09090P00103695.iam", oMatrix)'

 

Does anybody now how to make this line of code work in ilogic?

 

Thanks,

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Message 4 of 9

Anonymous
Not applicable
Accepted solution

I've confirmed that this code is working in iLogic:

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 7937 StartFragment: 314 EndFragment: 7905 StartSelection: 314 EndSelection: 314

Public Sub Main()
    ' Get the component definition of the currently open assembly.
    ' This will fail if an assembly document is not open.
    Dim oAsmCompDef As AssemblyComponentDefinition
    oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    ' Create a new matrix object. It will be initialized to an identity matrix.
    Dim oMatrix As Matrix
    oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0,0,0))

    ' Place the first occurrence.
    Dim oOcc1 As ComponentOccurrence
    oOcc1 = oAsmCompDef.Occurrences.Add("C:\Part1.ipt"?, oMatrix)

    ' Place the second occurrence, but use iMates for its placement. This is
    ' equivalent to "Use iMate" check box on the "Place Component" dialog.
    Dim oOccEnumerator As ComponentOccurrencesEnumerator
    oOccEnumerator = oAsmCompDef.Occurrences.AddUsingiMates("C:\Part2.ipt", False)

    ' Since the 'PlaceAllMatching' flag was specified as False, we can be
    ' sure that just one ComponentOccurrence was returned in the enumerator.
'    oOcc1 = oOccEnumerator.Item(1)
End Sub
Message 5 of 9

Anonymous
Not applicable

Dear Sir,

I am using Inventor 2011. I tried to use your code and found "'Let' and 'Set' assignment statements are no longer supported.". So i removed the code, now its asking for "Expression expected." Can you please solve the bug in the code below. Thanks in Advance - Carthik

 

Public Sub Main()
ins_comp("PinLink ANSI 50.ipt","RollerLink ANSI 50.ipt")
End Sub

Function ins_comp(p1 as String, p2 as String)
' Get the component definition of the currently open assembly.
' This will fail if an assembly document is not open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Create a new matrix object. It will be initialized to an identity matrix.
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0,0,0))

'file path to use
oPath = ThisDoc.Path & "\"
'file to use
oFile = ins_comp_name

' Place the first occurrence.
Dim oOcc1 As ComponentOccurrence
MessageBox.Show("Message"&p1, "Title")

oOcc1 = oAsmCompDef.Occurrences.Add(& vbLf & oPath & p1, oMatrix)

' Place the second occurrence, but use iMates for its placement. This is
' equivalent to "Use iMate" check box on the "Place Component" dialog.
Dim oOccEnumerator As ComponentOccurrencesEnumerator
oOccEnumerator = oAsmCompDef.Occurrences.AddUsingiMates(& vbLf & oPath & p2, False)

' Since the 'PlaceAllMatching' flag was specified as False, we can be
' sure that just one ComponentOccurrence was returned in the enumerator.
'
oOcc1 = oOccEnumerator.Item(1)

End Function
0 Likes
Message 6 of 9

waynehelley
Collaborator
Collaborator

Hi there,

 

The code attached will place a list of components from a spreadsheet into an assembly.

 

You will have to include the file paths within the spreadsheet.  You will have to adjust the file path of the spreadsheet within the code.

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 7 of 9

Anonymous
Not applicable

Dear Sir,

Thank you very much for the reply.

I tried your code and found some error. I have attached the file for your reference.

Request you to kindly open and find the bug( Note: I am using Autodesk Inventor 2011)

Thanks in advance- Carthik

 

Insert-Error.png

0 Likes
Message 8 of 9

Anonymous
Not applicable
sorry...i missed to include the path of my part file in the xl sheet. That is the bug.....Thanks for the input......
0 Likes
Message 9 of 9

cencinaNB2ET
Advocate
Advocate

Hi,

 

What if Im placing all?

iMate Creation During Occurrence Placement

How would I reference those particular Component Occurrences Enumerators as A component Occurrence?

AddUsingiMates(" Part/Assembly...", true)


 

0 Likes