1 Reply Latest reply on Dec 9, 2014 3:56 PM by Keith Rice

    SketchRelationManager.AddRelation fails

    Bill Florac

      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.




              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



              ' 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)



                  ' 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

        • Re: SketchRelationManager.AddRelation fails
          Keith Rice



          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.



          SolidWorks API Video Tutorials