Issue
How to access the AssemblyConstraints object in Inventor?
Assembly constraints determine how components in the assembly fit together. As you apply constraints, degrees of freedom are removed, restricting the ways components can move. Assembly constraints that exist between different components in an assembly can be accessed using the Inventor API. The AssemblyConstraints collection object can be obtained using the AssemblyComponentDefinition object which is in turn accessed using the AssemblyDocument object. Using this collection object one could access the different AssemblyConstraint objects. Each of the AssemblyConstraint objects supports methods that can be used to access the constraint name, status and type of constraint. Also, methods are provided to access the ComponentOccurrence objects between the particular constraint exists. The following code snippet is an example of how the methods could be used.
VB .NET code:
Sub GetConstraints()
'Open assembly document and run this subroutine
Dim sb As New System.Text.StringBuilder
Try
Dim oAssyDoc As Inventor.AssemblyDocument _
= TryCast(oApp.ActiveDocument, AssemblyDocument)
If oAssyDoc IsNot Nothing Then
Dim oAssyDef As AssemblyComponentDefinition _
= oAssyDoc.ComponentDefinition
Dim oConstraints As AssemblyConstraints _
= oAssyDef.Constraints
For Each oConstraint As AssemblyConstraint In oConstraints
' get the status of constraint
' (suppressed/not suppressed)
Dim Status As String
If oConstraint.Suppressed = False Then
Status = "Not Suppressed"
Else
Status = "Suppressed"
End If
'get the constraint type
Dim ConstraintType As String = ""
Select Case oConstraint.Type
Case ObjectTypeEnum.kAngleConstraintObject
ConstraintType = "Angle Constraint"
Case ObjectTypeEnum.kFlushConstraintObject
ConstraintType = "Flush Constraint"
Case ObjectTypeEnum.kInsertConstraintObject
ConstraintType = "Insert Constraint"
Case ObjectTypeEnum.kMateConstraintObject
ConstraintType = "Mate Constraint"
Case ObjectTypeEnum.kRotateRotateConstraintObject
ConstraintType = "Rotate-Rotate Constraint"
Case ObjectTypeEnum.kRotateTranslateConstraintObject
ConstraintType = "Rotate-Translate Constraint"
Case ObjectTypeEnum.kTangentConstraintObject
ConstraintType = "Tangent Constraint"
Case ObjectTypeEnum.kTransitionalConstraintObject
ConstraintType = "Transitional Constraint"
End Select
'add constraint name,status and type to list
sb.AppendLine("Constraint Name: " & _
oConstraint.Name & _
", Status: " & Status & _
", Type: " & ConstraintType)
'obtain the occurrences between which the constraint exists
Dim Occurrence1 As ComponentOccurrence _
= oConstraint.OccurrenceOne
Dim Occurrence2 As ComponentOccurrence _
= oConstraint.OccurrenceTwo
'add name of occurrences between which constraints exist to the list
If (Occurrence1 IsNot Nothing) And _
(Occurrence2 IsNot Nothing) Then
sb.AppendLine(" Constraint between " _
& Occurrence1.Name & " and " & Occurrence2.Name)
Else
If (Occurrence1 Is Nothing) And (Occurrence2 Is Nothing) Then
'do nothing
ElseIf Occurrence1 Is Nothing Then
sb.AppendLine(" Constraint on " & Occurrence2.Name)
Else
sb.AppendLine(" Constraint on " & Occurrence1.Name)
End If
End If
Next
sb.AppendLine()
sb.AppendLine("Assembly constrains total number: " _
& oConstraints.Count.ToString)
Else
sb.AppendLine(" No Constraints have been applied")
End If
'print results in Debug window
Debug.Print(sb.ToString)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show( _
"There was a problem getting some constraint information", _
"Error", MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Error)
End Try
sb = Nothing
End Sub