<?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.
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:
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:
Parts and assembly files are attached in the zip –
Download Chain. You can just open up the assembly and run the above code.




Leave a Reply to CarthikCancel reply