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