Hi all,
Can anyone help me?
Im looking for VBA codes to crate mate constraint between two parts?
I have crated two parts and called up those two parts in assembly document as occurances
but i'm unable to constrain it.
Please help
Regards
KP
Solved! Go to Solution.
Solved by jdkriek. Go to Solution.
If you click on the help within Inventor you will see an option that says "Additional Resources", when you highlight that option on the menu a flyout will give you the option for "Programming Help". Then select "Sample Code" from the side menu and look for "Assembly Constraints and iMates". This will give you several examples on how to do what you want.
If you need more help, let me me know.
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