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:
'Open assembly document and run this subroutine
Dim sb As New System.Text.StringBuilder
Dim oAssyDoc As Inventor.AssemblyDocument _
= TryCast(oApp.ActiveDocument, AssemblyDocument)
If oAssyDoc IsNot Nothing Then
Dim oAssyDef As AssemblyComponentDefinition _
Dim oConstraints As AssemblyConstraints _
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"
Status = "Suppressed"
'get the constraint type
Dim ConstraintType As String = ""
Select Case oConstraint.Type
ConstraintType = "Angle Constraint"
ConstraintType = "Flush Constraint"
ConstraintType = "Insert Constraint"
ConstraintType = "Mate Constraint"
ConstraintType = "Rotate-Rotate Constraint"
ConstraintType = "Rotate-Translate Constraint"
ConstraintType = "Tangent Constraint"
ConstraintType = "Transitional Constraint"
'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 _
Dim Occurrence2 As ComponentOccurrence _
'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)
If (Occurrence1 Is Nothing) And (Occurrence2 Is Nothing) Then
ElseIf Occurrence1 Is Nothing Then
sb.AppendLine(" Constraint on " & Occurrence2.Name)
sb.AppendLine(" Constraint on " & Occurrence1.Name)
sb.AppendLine("Assembly constrains total number: " _
sb.AppendLine(" No Constraints have been applied")
'print results in Debug window
Catch ex As Exception
"There was a problem getting some constraint information", _
"Error", MessageBoxButtons.OK, _
sb = Nothing