SketchRelationManager.AddRelation fails

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
Answers 2
KR
KR 2014-12-09 Best Answer

Bill,

That error message can mean one of two things:

1. You have lost connection to SolidWorks (the COM server), perhaps because it crashed or you did something to kill the pointer. In this case, the fault lies with your code.

2. There is a bug within the SolidWorks API that you can do nothing about, given the current way you're using the API call. Maybe there is another way to use the API call that doesn't run into the code. Anyway, in this case, the fault lies with the SolidWorks API.

For your case I believe that #2 is true because if you simply change this line:

Dim vLines(0 to 1) As Object

To this:

Dim vLines(0 to 1) As SketchLine

The exception is not thrown, at least on my computer. I tried the first approach in VBA and the relation was added, so perhaps this is .NET related. I could speculate about why this is happening, but if you wanted a definitive answer you would need to talk to API support.

Keith

SolidWorks API Video Tutorials

JB
JB 2019-04-12

Keith/Bill

I know it is an old thread but I had the exact same problem trying to create a coincident relation between two points and I came across this post in my search for a solution. So I thought I would just add my small contribution in case others sit in the same situation and finds this post.

I can confirm that the AddRelation call is very sensitive to having the array of entities declared as the specific correct type rather than just as Object. In my case AddRelation worked when I changed the entity array from type Object to type SketchPoint.

Regards,

Jørn