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: 

vba-imate code for assy

3 REPLIES 3
Reply
Message 1 of 4
brittain
1542 Views, 3 Replies

vba-imate code for assy

The sample vba code in inventor that enables the assy using imate names is for 2 parts which works fine(shown below)but I cannot seem to change it to run for multiple parts due to my vba ignorance!

Does anybody have any similar existing code or help that will assemble mutiple specified parts with respective matching imate names that can be run from a macro?


Example

[Visual Basic] This sample demonstrates creating an iMate result using two existin iMate definitions. To use this sample create a new part by extruding a rectangle to create a cube. Create a mate iMate on one of the faces. This sample assumes the iMate is named the default name used in the English version of Inventor, which is iMate:1. If the iMate definition is created with another name you can either edit the name of the iMate definition in the part file, or edit the sample code below to use the different name. Save the part to C:\Temp\iMatePart.ipt. Finally, have an assembly open and run the sample code.


Public Sub iMateResultCreationSample()
' 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:\Temp\iMatePart.ipt", oMatrix)

' Place the second occurrence, but adjust the matrix slightly so they're
' not right on top of each other.
oMatrix.Cell(1, 4) = 10
Dim oOcc2 As ComponentOccurrence
Set oOcc2 = oAsmCompDef.Occurrences.Add("C:\Temp\iMatePart.ipt", oMatrix)

' Look through the iMateDefinitions defined for the first occurrence
' and find the one named "iMate:1". This loop demonstrates using the
' Count and Item properties of the iMateDefinitions object.
Dim i As Long
Dim oiMateDef1 As iMateDefinition
For i = 1 To oOcc1.iMateDefinitions.Count
If oOcc1.iMateDefinitions.Item(i).Name = "iMate:1" Then
Set oiMateDef1 = oOcc1.iMateDefinitions.Item(i)
Exit For
End If
Next

If oiMateDef1 Is Nothing Then
MsgBox "An iMate definition named ""iMate:1"" does not exist in " & oOcc1.Name
Exit Sub
End If

' Look through the iMateDefinitions defined for the second occurrence
' and find the one named "iMate:1". This loop demonstrates using the
' For Each method of iterating through a collection.
Dim oiMateDef2 As iMateDefinition
Dim bFoundDefinition As Boolean
For Each oiMateDef2 In oOcc2.iMateDefinitions
If oiMateDef2.Name = "iMate:1" Then
bFoundDefinition = True
Exit For
End If
Next

If Not bFoundDefinition Then
MsgBox "An iMate definition named ""iMate:1"" does not exist in " & oOcc2.Name
Exit Sub
End If

' Create an iMate result using the two definitions.
Dim oiMateResult As iMateResult
Set oiMateResult = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)
End Sub
3 REPLIES 3
Message 2 of 4
SRayC
in reply to: brittain

Below is some sample pieces of code doing some of what you described. The trick is knowing which parts you want to mate together. In my sample I have one base part and mate all others occurances to it. I put the secondary components in a list box on a form selected by the user then parse throught the list and mate up the components.
Hope this helps.

'<
' << Create a new matrix object. It will be initialized to an identity matrix.
Set oBPMatrix = ThisApplication.TransientGeometry.CreateMatrix
Set BasePartoOcc = oAsmCompDef.Occurrences.Add(BasePartFile, oBPMatrix) '<< Add first part to assembly.
BasePartoOcc.Grounded = True '<< make sure it is grounded
Set oBPMatrix = BasePartoOcc.Transformation '<< get the base point location for the base part


For i = 0 To Form.lstPlaceableComps.ListCount - 1
ModelNm = Form.lstPlaceableComps.List(i)


Set SecondaryPartoOcc = oAsmCompDef.Occurrences.Add(ModelNm, oBPMatrix)
DoiMates BasePartoOcc, SecondaryPartoOcc

n = n + 12

Dim oTransform As Matrix
Set oTransform = SecondaryPartoOcc.Transformation
oTransform.SetTranslation ThisApplication.TransientGeometry.CreateVector(n, 20, 0)
SecondaryPartoOcc.Transformation = oTransform
Next




Public Sub DoiMates(BaseComponent As ComponentOccurrence, SecondComponent As ComponentOccurrence)

Dim oiMateDef1 As iMateDefinition
Dim oiMateDef2 As iMateDefinition
Dim oiMateResults As iMateResult

For Each oiMateDef1 In BaseComponent.iMateDefinitions
If oiMateDef1.Name = "PB2_FaceiMate" Then Exit For
Next
If oiMateDef1 Is Nothing Then
MsgBox "Missing iMate information on Base Component"
Exit Sub
End If
For Each oiMateDef2 In SecondComponent.iMateDefinitions
If oiMateDef2.Name = "PB2_FaceiMate" Then Exit For
Next
If oiMateDef2 Is Nothing Then
MsgBox "Missing iMate information on Next Component", vbCritical
Exit Sub
End If

Set oiMateResults = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)

End Sub

Ray C.
Message 3 of 4
brittain
in reply to: brittain

I appreciate your help.......
I struggle with the vba code itself, but where does this list of parts live and will this code mate all of the matching imates of the components as they assemble?
Message 4 of 4
Carthik_Babu
in reply to: SRayC

Dear Sir,

Can you please upload sample inventor 2011 assembly file for better understanding......I have no idea on VBA programming....Kindly help. 

Regards,

Carthik

Carthik Babu M.S, Asst Manager - Machine Building,
Gabriel India Ltd,Hosur, TN, INDIA
Email:carthik_ms@yahoo.co.in ,
https://grabcad.com/carthik-1/projects
"May all beings be happy" http://www.dhamma.org/

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

Post to forums  

Autodesk Design & Make Report