Has anybody gotten SketchRelationManager.AddRelation() to work? Here is what I have but it always gets a COM error.
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Macro14.DLL
Error occurred: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
This part of the macro expects solidworks to editing a sketch and there are two line selected.
Bill
Dim swModel As ModelDoc2
Dim swSketchMgr As SketchManager
Dim swSketch As Sketch
Dim swRelMgr As SketchRelationManager
Dim swSelMgr As SelectionMgr
swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
' This works but so should the stuff below
'swModel.SketchAddConstraints("sgSYMMETRIC")
' get our sketch manager
swSketchMgr = swModel.SketchManager
If swSketchMgr Is Nothing Then Exit Sub
' get our sketch
swSketch = swSketchMgr.ActiveSketch
'swSketch = swModel.GetActiveSketch2 'This also works to get sketch
If swSketch Is Nothing Then Exit Sub
' get our sketch relation manager
swRelMgr = swSketch.RelationManager
If swRelMgr Is Nothing Then Exit Sub
'Just to double prove that we really have a valid Relation Manager
Debug.Print("Relation Count: " & swRelMgr.GetRelationsCount(0).ToString)
' get our selection managager
swSelMgr = swModel.SelectionManager
If swSelMgr Is Nothing Then Exit Sub
'Just to double prove that we really have a valid Selection Manager
Debug.Print("Selected Objects:" & swSelMgr.GetSelectedObjectCount2(-1).ToString)
Dim vLines(0 To 1) As Object
Dim segment As SketchSegment
vLines(0) = swSelMgr.GetSelectedObject6(1, -1)
If vLines(0) Is Nothing Then Exit Sub
' Just to double prove we really have line segments
segment = vLines(0)
Debug.Print("Line 1: " & segment.GetLength.ToString)
vLines(1) = swSelMgr.GetSelectedObject6(2, -1)
' Just to double prove we really have line segments
segment = vLines(1)
Debug.Print("Line 2: " & segment.GetLength.ToString)
Try
' this seems to always fail
'swRelMgr.AddRelation(vLines, swConstraintType_e.swConstraintType_PARALLEL)
' similar call attempt using the same entities. Does not crash but returns nothing
'Dim relations As Object
'relations = swRelMgr.GetAllowedRelations(vLines)
'If relations IsNot Nothing Then
' Dim x As Integer
' For x = 0 To UBound(relations)
' Debug.Print(relations(x).ToString)
' Next
'End If
Catch e As Exception
Debug.Print("Error occurred: " & e.Message)
End Try
SolidworksApi macros