Hook up a chain using iMate’s

<?xml encoding=”UTF-8″>By Adam Nagy

When constraining parts together in an assembly, if you organized things well then with a bit of programming you can automate things nicely.

You could use iMate’s to specify how the parts should be contrained to each other – this would be just a one off thing so could be done in the user interface as well. Then you could programmatically insert multiple instances of them (same number of each) and use the iMate’s to join them.

Chains

In this case we have two parts of a chain assembly. Each have the same iMate’s: iMate:1 and iInsert:1 on one end of the part, and iMate:2 and iInsert:2 on the other end of the part. We could pair them like this:

Chain

This code assumes that there are the same number of occurrences of each part in the assembly already and none of the iMate’s have been used:

Function GetiMate(name As String, occ As ComponentOccurrence) _
As iMateDefinition
Dim imate As iMateDefinition
For Each imate In occ.iMateDefinitions
If imate.name = name Then
Set GetiMate = imate
Exit Function
End If
Next
End Function
' This hooks "chain1" parts to "chain2" parts
' In this assembly we only have these two parts
' so checking if the part documents are not the
' same is enough
Sub HookTheChain()
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument
Dim cd As AssemblyComponentDefinition
Set cd = doc.ComponentDefinition
' Here we only have two referenced files
Dim desc1 As DocumentDescriptor
Set desc1 = doc.ReferencedDocumentDescriptors(1)
Dim occs1 As ComponentOccurrencesEnumerator
Set occs1 = cd.Occurrences.AllReferencedOccurrences(desc1)
Dim desc2 As DocumentDescriptor
Set desc2 = doc.ReferencedDocumentDescriptors(2)
Dim occs2 As ComponentOccurrencesEnumerator
Set occs2 = cd.Occurrences.AllReferencedOccurrences(desc2)
Dim i As Integer
For i = 1 To occs1.Count
' Set occ1 iMate:1/iInsert:1 to
' occ2 iMate:1/iInsert:1
Dim imate1 As iMateDefinition
Set imate1 = GetiMate("iMate:1", occs1(i))
Dim imate2 As iMateDefinition
Set imate2 = GetiMate("iMate:1", occs2(i))
Call cd.iMateResults.AddByTwoiMates(imate1, imate2)
Set imate1 = GetiMate("iInsert:1", occs1(i))
Set imate2 = GetiMate("iInsert:1", occs2(i))
Call cd.iMateResults.AddByTwoiMates(imate1, imate2)
' Set occ1 iMate:2/iInsert:2 to
' another occ2 iMate:2/iInsert:2
Dim ip1 As Integer
ip1 = i Mod occs2.Count + 1
Set imate1 = GetiMate("iMate:2", occs1(i))
Set imate2 = GetiMate("iMate:2", occs2(ip1))
Call cd.iMateResults.AddByTwoiMates(imate1, imate2)
Set imate1 = GetiMate("iInsert:2", occs1(i))
Set imate2 = GetiMate("iInsert:2", occs2(ip1))
Call cd.iMateResults.AddByTwoiMates(imate1, imate2)
Next
End Sub

Once you run the code the parts might end up on top of each other, but when you drag them using the mouse then you’ll see that they can be moved and are hooked up as intended:

Chain2

Parts and assembly files are attached in the zip –
Download Chain. You can just open up the assembly and run the above code.  


Comments

4 responses to “Hook up a chain using iMate’s”

  1. Hi,
    Very nice info… Thanks:)
    Regards,
    Mucip:)

  2. Hi,
    I also find a way in Autodesk forums and tryed to increased functioanality little bit more…
    The code inserting chain couples and mating them each others… You can increase the chain quantity.
    Code is below:
    Public Sub iMateResultCreationSample()
    ‘Mucip Alınan yer aşağıda…
    ‘http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/vba-imate-code-for-assy/m-p/2112512/highlight/true#M22856
    ‘http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/Adding-contraints-with-VBA/m-p/760385/highlight/true#M3939
    ‘ 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(“R:\Resimler\Chain_B.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
    Dim oiMateDef1 As iMateDefinition
    ‘Set oiMateDef1 = oOcc1.iMateDefinitions.Item(1)
    Dim oiMateDef2 As iMateDefinition
    Dim oiMateResult As iMateResult
    Dim Sayac As Long
    For Sayac = 2 To 10 ‘ You may increase the chain quantity with this number…
    If ((Sayac Mod 2) = 0) Then
    Set oOcc2 = oAsmCompDef.Occurrences.Add(“R:\Resimler\Chain_P.ipt”, oMatrix)
    Set oiMateDef2 = oOcc2.iMateDefinitions.Item(1)
    Set oiMateDef1 = oOcc1.iMateDefinitions.Item(1)
    Set oiMateResult = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)
    Else
    Set oOcc2 = oAsmCompDef.Occurrences.Add(“R:\Resimler\Chain_B.ipt”, oMatrix)
    Set oiMateDef1 = oOcc1.iMateDefinitions.Item(2)
    Set oiMateDef2 = oOcc2.iMateDefinitions.Item(2)
    Set oiMateResult = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)
    End If
    ‘Set oiMateResult = oAsmCompDef.iMateResults.AddByTwoiMates(oiMateDef1, oiMateDef2)
    Set oOcc1 = oOcc2
    Next
    End Sub

  3. Carthik Avatar
    Carthik

    Dear Sir,
    can you please post model for Autodesk Inventor 2011 Users….
    and also i found the code is not supported in Autodesk Inventor 2011. Request you to kindly help in this regards,
    Carthik

  4. Carthik Avatar
    Carthik

    hi, i solved the problem. Autodesk Inventor 2011 Does not support “LET and SET” command. I removed those terms in your code and found your code working….

Leave a Reply to CarthikCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading