Creating Angle Constraint With 1 part plane and 1 Assembly Plane.

Creating Angle Constraint With 1 part plane and 1 Assembly Plane.

arnold.ogalo
Contributor Contributor
1,012 Views
8 Replies
Message 1 of 9

Creating Angle Constraint With 1 part plane and 1 Assembly Plane.

arnold.ogalo
Contributor
Contributor

Hi Inventor Family, 

I have been Working on a Inventor VBA project that requires me to:

1) Import a part to an assembly 

2) Insert mate the part to another component 

3) Create an angle Constraint between a possible part plane  and an assembly plane.

I have been able to complete the first 2. Please advise on the third step. 

Thank You

0 Likes
Accepted solutions (2)
1,013 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @arnold.ogalo.  What you are asking can be complicated to just provide a solution for, without having your exact CAD model files to test it on.  There are a lot of variables involved, and questions that need to be answered.

However, below is a fairly basic example of creating an angular constraint between a components work plane proxy, and one of the main assembly's work planes.  I don't know which component you want to target, or how you plan on identifying it, so I just specified a generic part type component name, as the one to target.  I also do not know which plane of the component you want to create the constraint to, so I just specified the 'first' work plane found within the component, which will be one of its 'origin' planes.  I did that same for the main assembly, since I did not know which one you wanted, I just specified the first one.  Then, since I did not know what angle you wanted, I just specified 45 degrees.

 

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oConsts As AssemblyConstraints = oADef.Constraints

'specify which component you want to constrain (one way or another)
Dim oOcc As ComponentOccurrence = oOccs.ItemByName("Part1:1")

'specify which plane of the component you want to constrain
Dim oWP1 As WorkPlane = oOcc.Definition.WorkPlanes.Item(1)
'get its 'Proxy' (the copy of it that exists within the 3D space of the assembly)
Dim oWP1Proxy As WorkPlaneProxy = Nothing
'this sets the value of that last variable
oOcc.CreateGeometryProxy(oWP1, oWP1Proxy)
'now specify which Plane of the Assembly to constrain it to
Dim oAsmWP1 As WorkPlane = oADef.WorkPlanes.Item(1)
'now create the constraint between these two planes
'when specifying angle, if you specify a raw number, it will be understood as radians
'or you can specify angle with a String (expression), which contains the number followed by units specifier
Dim oAngleConst As AngleConstraint = oConsts.AddAngleConstraint(oWP1Proxy, oAsmWP1, "45 deg")

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Edit:  Oops.  This example is in iLogic, not VBA.  In VBA you need to set variable's values on another line, using the 'Set' keyword, and you can't use the 'ThisDoc.Document' term (could use ThisApplication.ActiveDocument instead), but the rest should be fairly simple to translate.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Hi @arnold.ogalo.  I realized the last example I posted was in iLogic, not VBA, so I converted the code example to VBA for you below.  Keep in mind, to be able to use it as it is, you would have to have an assembly document active, with a component within it named "Part1:1" (or you can just edit this component name within the code).

Sub CreateAngleAssemblyCosntraint()
    If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
        Call MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
        Exit Sub
    End If
    Dim oADoc As AssemblyDocument
    Set oADoc = ThisApplication.ActiveDocument
    Dim oADef As AssemblyComponentDefinition
    Set oADef = oADoc.ComponentDefinition
    Dim oOccs As ComponentOccurrences
    Set oOccs = oADef.Occurrences
    Dim oConsts As AssemblyConstraints
    Set oConsts = oADef.Constraints
    
    'specify which component you want to constrain (one way or another)
    Dim oOcc As ComponentOccurrence
    Set oOcc = oOccs.ItemByName("Part1:1")
    
    'specify which plane of the component you want to constrain
    Dim oWP1 As WorkPlane
    Set oWP1 = oOcc.Definition.WorkPlanes.Item(1)
    'get its 'Proxy' (the copy of it that exists within the 3D space of the assembly)
    Dim oWP1Proxy As WorkPlaneProxy
    'this sets the value of that last variable
    Call oOcc.CreateGeometryProxy(oWP1, oWP1Proxy)
    'now specify which Plane of the Assembly to constrain it to
    Dim oAsmWP1 As WorkPlane
    Set oAsmWP1 = oADef.WorkPlanes.Item(1)
    'now create the constraint between these two planes
    'when specifying angle, if you specify a raw number, it will be understood as radians
    'or you can specify angle with a String (expression), which contains the number followed by units specifier
    Dim oAngleConst As AngleConstraint
    Set oAngleConst = oConsts.AddAngleConstraint(oWP1Proxy, oAsmWP1, "45 deg")
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 9

arnold.ogalo
Contributor
Contributor

Hi @WCrihfield ,

I really Appreciate the quick response. I was able to use your code to edit mine to work and I am pleased with the results so far. 

I can understand where the difficulty in creating such a constraint can raise from but 1 more I need to adjust for is the possibility  that the choose part plane and the assembly plane are not compatible hence creating a broken mate. Is there a way to make it such that if the constraint is broke VBA can iterate to the next plane and try if it can create the mate. 

I have added my code to the post. 

Public Sub PlaceAndInsertwithiMate()
'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

'Identifies the Workspace to collect parts from
Dim oWorkspaceFolder As String
oWorkspaceFolder = ThisApplication.FileLocations.Workspace & "\"
MsgBox (oWorkspaceFolder)

'SelectEdge to Mate to
'Set a reference to the select set.
Dim oSelectSet As SelectSet
Set oSelectSet = ThisApplication.ActiveDocument.SelectSet

'Allows the User to select the edge to mate with
Dim oSelEdge As Edge
Set oSelEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Circular Edge") ' .Pick allows the user to pick a feature/Part

'Place the first occurrence.
Dim oOcc1 As ComponentOccurrence
Set oOcc1 = oAsmCompDef.Occurrences.Add(oWorkspaceFolder & "Part1.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 = "iInsert:1" Then 'The iMate in the part should be called iInsert:1
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

'Create an iMate result using the two definitions.
Dim oiMateResult1 As iMateResult
Set oiMateResult1 = oAsmCompDef.iMateResults.AddByiMateAndEntity(oiMateDef1, oSelEdge)

'Set up part Planes for the Angle Constraints
Dim oPartPlane1 As WorkPlane
Set oPartPlane1 = oOcc1.Definition.WorkPlanes.Item(1)

'Get its 'Proxy' (the copy of it that exists within the 3D space of the assembly)
Dim oWP1Proxy As WorkPlaneProxy
Call oOcc1.CreateGeometryProxy(oPartPlane1, oWP1Proxy)

'Set Up Assembly Planes for the Angle Constraint
Dim oAsmPlane1 As WorkPlane
Set oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(1)


'Create an angle mate between part plane and the origin plane
Dim oAngleResult As AngleConstraint
Set oAngleResult = oAsmCompDef.Constraints.AddAngleConstraint(oWP1Proxy, oAsmPlane1, "45 deg")
End Sub

0 Likes
Message 5 of 9

arnold.ogalo
Contributor
Contributor

Consider only the last 4 blocks of code for the Angle Constraint. 

As per my Last question I was trying to iterate through the items in:

 

Set oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(1)

 

If the angle constraint is broken

and possibly if they are all broken a message can show. "Can not Create Angleconstraint"

I hope this is not a wild request. 

Thanks Again. 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Hi @arnold.ogalo.  This is an odd request.  Most folks know exactly how they want their component to be oriented and what exact faces/planes they want to use to achieve that constraint when constraining them.  Angular type constraints are also more complicated than Mate or Flush, because it can be difficult to convey the intended direction of the angle, and inside vs outside version of angle.  It is in the details of which entity is first in the set, and the natural directions of both entities involved.

I created a loop of sorts within the last part of your code for you to try out, with some error handling involved.

Public Sub PlaceAndInsertwithiMate()
    '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
    
    'Identifies the Workspace to collect parts from
    Dim oWorkspaceFolder As String
    oWorkspaceFolder = ThisApplication.FileLocations.Workspace & "\"
    MsgBox (oWorkspaceFolder)
    
    'SelectEdge to Mate to
    'Set a reference to the select set.
    Dim oSelectSet As SelectSet
    Set oSelectSet = ThisApplication.ActiveDocument.SelectSet
    
    'Allows the User to select the edge to mate with
    Dim oSelEdge As Edge
    Set oSelEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Circular Edge") ' .Pick allows the user to pick a feature/Part
    
    'Place the first occurrence.
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmCompDef.Occurrences.Add(oWorkspaceFolder & "Part1.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 = "iInsert:1" Then 'The iMate in the part should be called iInsert:1
    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
    
    'Create an iMate result using the two definitions.
    Dim oiMateResult1 As iMateResult
    Set oiMateResult1 = oAsmCompDef.iMateResults.AddByiMateAndEntity(oiMateDef1, oSelEdge)
    
    'Set up part Planes for the Angle Constraints
    Dim oPartPlane1 As WorkPlane
    Set oPartPlane1 = oOcc1.Definition.WorkPlanes.Item(1)
    
    'Get its 'Proxy' (the copy of it that exists within the 3D space of the assembly)
    Dim oWP1Proxy As WorkPlaneProxy
    Call oOcc1.CreateGeometryProxy(oPartPlane1, oWP1Proxy)
    
    'define variables before loop
    Dim oAsmPlane As WorkPlane
    Dim oAngleResult As AngleConstraint
    Dim i As Integer
    For i = 1 To 3
        'Set Up Assembly Planes for the Angle Constraint
        Set oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(i)
        On Error Resume Next 'pause error interuptions/reporting
        'Create an angle mate between part plane and the origin plane
        Set oAngleResult = oAsmCompDef.Constraints.AddAngleConstraint(oWP1Proxy, oAsmPlane, "45 deg")
        If Err = 0 Then
            Exit For
        Else
            On Error GoTo 0 'reset error handling to default, and loop again
        End If
    Next i
    If oAngleResult Is Nothing Then
        Call MsgBox("Angle constraint could not be created.", , "")
    End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

By the way, here is the online help page for that AddAngleConstraint method.

(I have resorted to posting 2 versions of most of my links, because I never know which one will work for the other person(s).  Autodesk needs to fix these URL's so there is just 1, instead of 2 versions of them.)

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=AssemblyConstraints_AddAngleConstraint 

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-BA55D69F-9E8D-4FFA-B21A-302B4DD97B40 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

arnold.ogalo
Contributor
Contributor

 

The motivation for such a request was:

I am dealing with 100s of pipe fittings and sadly they are not designed with the same orientations . I am creating this code to import that part, create an insert constraint and create an angle constraint with a viable plane. 

Thanks @WCrihfield this was Helpful.

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

OK.  I get it.  We have tons of old model files too that have been created by several different designers/engineers over the years, and the same type of part, maybe just slight size differences, can be totally different orientations, and even created out in space, away from the origin planes.  This can be very challenging to work with, especially when trying to coordinate automation solutions that may have to include them.  Glad I was able to help you out along the way.  Have a good one. 😉

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes