constrain multiple parts

constrain multiple parts

GeorgK
Advisor Advisor
2,623 Views
11 Replies
Message 1 of 12

constrain multiple parts

GeorgK
Advisor
Advisor

Hello together,

 

I have many different parts which I would like to constrain with insert. How could I select a part on a hole and place it to an plate with many holes? At the end the part should be placed on any hole with the same size or manualy by selecting each of the holes.

 

Thank you

 

Georg

 

 

0 Likes
Accepted solutions (1)
2,624 Views
11 Replies
Replies (11)
Message 2 of 12

GeorgK
Advisor
Advisor

place.png

1. first constrain the washer and the screw

2 select the insert point on the washer

3. washer and screw are copied

4. place the screw and washer on the hole (flip it if it does not fit) or on the middle of the slot as long as the user hits escape.

 

I hope it is now clearer what I would like to achieve.

 

Georg

0 Likes
Message 3 of 12

wayne.brill
Collaborator
Collaborator

Hi Georg,

 

I am not finding an existing example that does what you are describing. However I believe what you are asking for could be done with the API. These DevBlog posts have code that creates contsraints.

 

http://adndevblog.typepad.com/manufacturing/2012/11/constrain-to-the-middle-of-an-edge.html

http://adndevblog.typepad.com/manufacturing/2012/05/create-mate-constraint-with-bias-points.html

http://adndevblog.typepad.com/manufacturing/2013/06/constraint-an-assembly-workplane-with-a-componen...

 

Also see the "Building an Assembly" topic in the API help.

 

 

Maybe there is an App on the Inventor App Store that could help. I see several related to constraints.

 

Thanks,

Wayne

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 4 of 12

GeorgK
Advisor
Advisor

Hello Wayne,

 

whats the best way to keep the insert point of the screw and washer in memory?

 

Georg

0 Likes
Message 5 of 12

GeorgK
Advisor
Advisor

The code so far:

 

Dim m_inventorApp As Inventor.Application = Nothing
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

Dim oAssyDoc As AssemblyDocument
oAssyDoc = m_inventorApp.ActiveDocument


            Dim oAssyDef As AssemblyComponentDefinition
            oAssyDef = oAssyDoc.ComponentDefinition


            ' Have the user select a circular edge.
            Dim partEdge As Edge
            partEdge = m_inventorApp.CommandManager.Pick(SelectionFilterEnum.kPartEdgeCircularFilter, "Select a circular edge.")

            If partEdge Is Nothing Then
                Exit Sub
            End If

            Dim Occurrence As ComponentOccurrence
            Occurrence = partEdge.Parent.Parent

            Call m_inventorApp.ActiveDocument.SelectSet.Select(Occurrence)

            Dim asm As AssemblyDocument
            asm = m_inventorApp.ActiveDocument

            Dim tr As TransientObjects
            tr = m_inventorApp.TransientObjects

            Dim coll As ObjectCollection
            coll = tr.CreateObjectCollection()

            Dim ac As AssemblyConstraint
            For Each ac In Occurrence.Constraints
                coll.Add(ac.OccurrenceOne)
                coll.Add(ac.OccurrenceTwo)
            Next


            Dim occ As ComponentOccurrence
            For Each occ In coll
                'text = text + occ.Name + vbCrLf
                asm.SelectSet.Select(occ)
            Next


            ' Copy (Ctrl + C)
            Dim oCopyControlDef As ControlDefinition
            oCopyControlDef = m_inventorApp.CommandManager.ControlDefinitions.Item("AppCopyCmd")
            oCopyControlDef.Execute()

            ' Paste (Ctrl + V)
            Dim oPasteControlDef As ControlDefinition
            oPasteControlDef = m_inventorApp.CommandManager.ControlDefinitions.Item("AppPasteCmd")
            oPasteControlDef.Execute()

            Dim oObject As Object
            oObject = m_inventorApp.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick a Edge")


            ' Create the insert constraint between the parts.
            Dim oInsert As InsertConstraint
            oInsert = oAssyDef.Constraints.AddInsertConstraint(oObject, partEdge, True, 0)
            Call m_inventorApp.ActiveDocument.SelectSet.Select(Occurrence)
0 Likes
Message 6 of 12

wayne.brill
Collaborator
Collaborator

Hi Georg,

 

The API has an add method that will allow you add an occurrence. (not sure why the commands were being used)

 

Can you make the washer and the screw be an assembly that already has the constraints? This way your code would just need to get that assembly (an occurrence) and constrain it to the hole.

 

I created an assembly and used the VBA code below to test it. (VBA is easier to prototype with) Let me know if you want my assembly to test. It may be more helpful if you upload an assembly I can use to work up the code you have questions about.

 

Public Sub ConstraintsTest()
    Dim m_inventorApp As Inventor.Application
    Set m_inventorApp = ThisApplication
    
    Dim oAssyDoc As AssemblyDocument
    Set oAssyDoc = m_inventorApp.ActiveDocument

    Dim oAssyDef As AssemblyComponentDefinition
    Set oAssyDef = oAssyDoc.ComponentDefinition
    
     ' Have the user select a circular edge.
     Dim boltEdge As Edge
     Set boltEdge = m_inventorApp.CommandManager.Pick _
      (SelectionFilterEnum.kPartEdgeCircularFilter, "Select a circular edge on the bolt")
    
     If boltEdge Is Nothing Then
         Exit Sub
     End If
    
    ' in my assembly this will be the washer occurrence
    Dim Occurrence As ComponentOccurrence
    Set Occurrence = boltEdge.Parent.Parent
    
    'in my assembly this will be the bolt (screw and washer already constrained)
    Dim boltOccurrence As ComponentOccurrence
    Set boltOccurrence = Occurrence.parentOccurrence
    
    ' get the name of the document for the bolt occurrence, need it for Occurrences.Add
    Dim boltOccDocName As String
    boltOccDocName = boltOccurrence.Definition.Document.FullFileName
    
    ' create a matrix, need it for Occurrences.Add, transform the matrix to the
    ' location of the existing occurrence
    Dim oMat As Matrix
    Set oMat = ThisApplication.TransientGeometry.CreateMatrix
    Call oMat.TransformBy(boltOccurrence.Transformation)
    
    ' Add another occurrence
    Dim oNewBoltOcc As ComponentOccurrence
    Set oNewBoltOcc = oAssyDoc.ComponentDefinition.Occurrences.Add(boltOccDocName, oMat)
 
     Dim blockEdge As Edge
     Set blockEdge = m_inventorApp.CommandManager.Pick _
            (SelectionFilterEnum.kPartEdgeFilter, "Pick a hole Edge on the block")

     ' Create the insert constraint between the parts.
     Dim oInsert As InsertConstraint
     Set oInsert = oAssyDef.Constraints.AddInsertConstraint(boltEdge, blockEdge, True, 0)
    ' Call m_inventorApp.ActiveDocument.SelectSet.Select(boltOccurrence)
     Call m_inventorApp.ActiveDocument.SelectSet.Select(oNewBoltOcc)
            
End Sub

Thanks,

Wayne

 

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 12

GeorgK
Advisor
Advisor

Hello Wayne,

 

the problem is to get the center of the slot and to insert the screw and washer, which are already constrained, multiple times not only once.

 

Thanks

 

Georg

0 Likes
Message 8 of 12

GeorgK
Advisor
Advisor
bumping for answer day 🙂
0 Likes
Message 9 of 12

wayne.brill
Collaborator
Collaborator

Hello Georg,

 

Are you able to make the constraint at the center of the slot using the Inventor User Interface?

 

Please provide an assembly I can use to research this. If you want to keep it private you can upload it to the case that was created for this post.

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 10 of 12

GeorgK
Advisor
Advisor
Hello Wayne,

I send you the files. They are attached to the case.

Thanks

Georg
0 Likes
Message 11 of 12

wayne.brill
Collaborator
Collaborator

Hi Georg,

 

Thanks for providing the example. (by direct email)

Would it work for you if the slot had a work axis at the center? (It seems to me that there needs to be something there to constrain to) If so the VBA code below demonstrates what could be a solution. 

You could add attributes to the faces and the work axis to make it easier for the user. (Reduce what they would need to select. Your code could find the faces and edges using an attribute)

 

Public Sub ConstraintsTest2()
    Dim m_inventorApp As Inventor.Application
    Set m_inventorApp = ThisApplication
    
    Dim oAssyDoc As AssemblyDocument
    Set oAssyDoc = m_inventorApp.ActiveDocument

    Dim oAssyDef As AssemblyComponentDefinition
    Set oAssyDef = oAssyDoc.ComponentDefinition
    
    Dim boltEdge As Edge
    Set boltEdge = m_inventorApp.CommandManager.Pick _
      (SelectionFilterEnum.kPartEdgeCircularFilter, "Select a circular edge on the bolt")
    
     If boltEdge Is Nothing Then
         Exit Sub
     End If
     
     Dim boltFace As Face
     Set boltFace = m_inventorApp.CommandManager.Pick _
      (SelectionFilterEnum.kPartFaceFilter, "Select a face on the bolt")
    
    
    Dim slotCenterAxis As WorkAxis
    Set slotCenterAxis = m_inventorApp.CommandManager.Pick _
            (SelectionFilterEnum.kAllLinearEntities, "Pick a Work Axis at center of the slot")
    
    Dim blockFace As Face
    Set blockFace = m_inventorApp.CommandManager.Pick _
            (SelectionFilterEnum.kAllPlanarEntities, "Pick top face on the block")

       
     Dim oMateConstraint As MateConstraint
     Set oMateConstraint = oAssyDef.Constraints.AddMateConstraint(slotCenterAxis, boltEdge, 0, kInferredLine, kInferredPoint)
     
     Dim oMateConstraint2 As MateConstraint
     Set oMateConstraint2 = oAssyDef.Constraints.AddMateConstraint(blockFace, boltFace, 0)
   
End Sub

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 12 of 12

wayne.brill
Collaborator
Collaborator
Accepted solution

Hi,

 

Here is the current version of the code that is working with the latest assembly provided. (posting here in case someone else may be interested)

 

Public Sub ConstraintsTest4_WB_Updated()
 
 Dim m_inventorApp As Inventor.Application
 Set m_inventorApp = ThisApplication

 Dim oAssyDoc As AssemblyDocument
 Set oAssyDoc = m_inventorApp.ActiveDocument
 
 Dim oAssyDef As AssemblyComponentDefinition
 Set oAssyDef = oAssyDoc.ComponentDefinition
 
 ' Have the user select a circular edge on bottom of washer.
 Dim selectedWasherEdge As Edge
 Set selectedWasherEdge = m_inventorApp.CommandManager.Pick _
 (SelectionFilterEnum.kPartEdgeCircularFilter, "Select an edge on face of washer to constrain to block")

 If selectedWasherEdge Is Nothing Then
    Exit Sub
 End If
  
 ' The washer occurrence
 Dim washerOccurrence As ComponentOccurrence
 Set washerOccurrence = selectedWasherEdge.Parent.Parent
 
 ' get the name of the document for the washer occurrence, need it for Occurrences.Add
 Dim washerOccDocName As String
 washerOccDocName = washerOccurrence.Definition.Document.FullFileName

 Dim oConstraint As AssemblyConstraint
 Set oConstraint = washerOccurrence.Constraints(2)

 Dim boltOccurrence As ComponentOccurrence
 Set boltOccurrence = oConstraint.AffectedOccurrenceTwo

 Dim boltOccDocName As String
 boltOccDocName = boltOccurrence.Definition.Document.FullFileName

 'Get the geometry of the washer
 Dim SelectedWasherEdgeGeometry As Inventor.Circle
 Set SelectedWasherEdgeGeometry = selectedWasherEdge.Geometry

 'define insertionpoint for washer
 Dim oPointSelectedWasherForRay As Inventor.Point
 Set oPointSelectedWasherForRay = SelectedWasherEdgeGeometry.Center

 ' This puts the point for the ray closer to the selectedWasherEdge
 oPointSelectedWasherForRay.y = oPointSelectedWasherForRay.y - 0.3

 Dim oPointSelectedWasherOnEdge As Inventor.Point
 Set oPointSelectedWasherOnEdge = selectedWasherEdge.PointOnEdge

 Dim oSelectedWasherUnitVec As UnitVector
 Set oSelectedWasherUnitVec = ThisApplication.TransientGeometry.CreateUnitVector(oPointSelectedWasherOnEdge.x, oPointSelectedWasherOnEdge.y)

 Dim washerSelectedEdgeRadius As Double
 washerSelectedEdgeRadius = SelectedWasherEdgeGeometry.Radius
 
 
 'Get the geomtry of the washer from the occurence constraint
 Dim WasherEdgeGeometry As Inventor.Circle
 Set WasherEdgeGeometry = oConstraint.EntityOne.Geometry

 'define insertionpoint for washer
 Dim oPointWasherForRay As Inventor.Point
 Set oPointWasherForRay = WasherEdgeGeometry.Center

 ' This puts the point for the ray closer to the selectedWasherEdge
 oPointWasherForRay.y = oPointWasherForRay.y - 0.3

 Dim oPointWasherOnEdge As Inventor.Point
 Set oPointWasherOnEdge = oConstraint.EntityOne.PointOnEdge

 Dim oWasherUnitVec As UnitVector
 Set oWasherUnitVec = ThisApplication.TransientGeometry.CreateUnitVector(oPointWasherOnEdge.x, oPointWasherOnEdge.y)

 Dim washerEdgeRadius As Double
 washerEdgeRadius = WasherEdgeGeometry.Radius
 
 'Get the geometry of the bolt
 Dim boltEdgeGeometry As Inventor.Circle
 Set boltEdgeGeometry = oConstraint.EntityTwo.Geometry

 'define insertionpoint for bolt
 Dim oPointBoltForRay As Inventor.Point
 Set oPointBoltForRay = boltEdgeGeometry.Center

 ' This puts the point for the ray closer to the bolt edge
 oPointBoltForRay.y = oPointBoltForRay.y + 0.3

 Dim oPointBoltOnEdge As Inventor.Point
 Set oPointBoltOnEdge = oConstraint.EntityTwo.PointOnEdge

 Dim oBoltUnitVec As UnitVector
 Set oBoltUnitVec = ThisApplication.TransientGeometry.CreateUnitVector(oPointBoltOnEdge.x, oPointBoltOnEdge.y)

 Dim boltEdgeRadius As Double
 boltEdgeRadius = boltEdgeGeometry.Radius

 Dim blockEdge As Edge
 Set blockEdge = m_inventorApp.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick a hole Edge on the block")

 ' create a matrix for the washer, need it for Occurrences.Add, transform the matrix to the
 ' location of the existing washer occurrence
 Dim oWasherMat As Matrix
 Set oWasherMat = ThisApplication.TransientGeometry.CreateMatrix
 Call oWasherMat.TransformBy(washerOccurrence.Transformation)

' create a matrix for the bolt, need it for Occurrences.Add, transform the matrix to the
 ' location of the existing bolt occurrence
 Dim oBoltMat As Matrix
 Set oBoltMat = ThisApplication.TransientGeometry.CreateMatrix
 Call oBoltMat.TransformBy(boltOccurrence.Transformation)


 ' Insert constraint for the first bolt
 Dim oInsert As InsertConstraint
 Set oInsert = oAssyDef.Constraints.AddInsertConstraint(selectedWasherEdge, blockEdge, True, 0)
 
 Dim oVbMsgBoxResult As VbMsgBoxResult
 oVbMsgBoxResult = MsgBox("add more bolts?", vbYesNo)
 If oVbMsgBoxResult = vbNo Then
 Exit Sub
 End If

 Dim i As Integer
 i = 0

 Do While i <> 1
 
  ' hole for the next constraint
 Set blockEdge = m_inventorApp.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Pick a hole Edge on the block")

 ' Add another washer occurrence
 Dim oNewWasherOcc As ComponentOccurrence
 Set oNewWasherOcc = oAssyDoc.ComponentDefinition.Occurrences.Add(washerOccDocName, oWasherMat)
 
 Dim objsWasherEnumerator As ObjectsEnumerator
 Dim pntsEnumerator As ObjectsEnumerator

 ' Using the top level assembly to find the edge
 ' this works because the new occurrences are placed
 ' in exactly the same place as the one that existed
 ' when the edge of the washer was first selected
 'Find the edge in the washer to constrain to the bolt
 Call oAssyDef.FindUsingRay(oPointWasherForRay, oWasherUnitVec, washerEdgeRadius + 1, objsWasherEnumerator, pntsEnumerator, True)

 Dim objInWasher As Object
 Set objInWasher = objsWasherEnumerator(1)

 Dim washerEdge As Edge
 Set washerEdge = objInWasher.Edges(1)
 
 ' Find the edge of the washer that was selected
 Dim objsSelectedWasherEnumerator As ObjectsEnumerator
 Call oAssyDef.FindUsingRay(oPointSelectedWasherForRay, oSelectedWasherUnitVec, washerSelectedEdgeRadius + 1, objsSelectedWasherEnumerator, pntsEnumerator, True)

 Dim objInSelectedWasher As Object
 Set objInSelectedWasher = objsSelectedWasherEnumerator(1)

 Dim washerSelectedEdge As Edge
 Set washerSelectedEdge = objInSelectedWasher.Edges(1)
 
  ' Add another bolt occurrence
 Dim oNewBoltOcc As ComponentOccurrence
 Set oNewBoltOcc = oAssyDoc.ComponentDefinition.Occurrences.Add(boltOccDocName, oBoltMat)
 
 Dim objsBoltEnumerator As ObjectsEnumerator
 'Find the edge in the bolt
 Call oAssyDef.FindUsingRay(oPointBoltForRay, oBoltUnitVec, boltEdgeRadius + 0.5, objsBoltEnumerator, pntsEnumerator, True)
 
 Dim objInBolt As Object
 Set objInBolt = objsBoltEnumerator(1)

 Dim boltEdge As Edge
 Set boltEdge = objInBolt.Edges(1)
 
 Set oInsert = oAssyDef.Constraints.AddInsertConstraint(washerEdge, boltEdge, True, 0)
 
 ' Constraint between the selected washer edge and the edge of the hole
 Set oInsert = oAssyDef.Constraints.AddInsertConstraint(blockEdge, washerSelectedEdge, True, 0)

 oVbMsgBoxResult = MsgBox("add more bolts?", vbYesNo)
 If oVbMsgBoxResult = vbNo Then
 i = 1
 End If

 Loop

End Sub

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network