4 Replies Latest reply on Feb 8, 2016 6:33 PM by Artem Taturevych

    AddRelation not working

    Joshua Linsky


      I've tried many different ways to get the AddRelation to actually add a relation, but none of them seem to work. Has anyone been able to actually use this method? I've tried looking online but I can't find very much about this. This is my code:


      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim modelType As Long

      Dim swSkMgr As SldWorks.SketchManager

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swSkRelMgr As SldWorks.SketchRelationManager

      Dim swSkRel As SldWorks.SketchRelation

      Dim swSketch As Sketch

      Dim sketchSegs() As SldWorks.SketchLine

      Type coordinates

          x As Double

          y As Double

          z As Double

      End Type


      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2014\templates\Part.prtdot", 0, 0, 0)

          Set swSkMgr = swModel.SketchManager

          Set swSelMgr = swModel.SelectionManager

          swSkMgr.Insert3DSketch True

          Set swSketch = swSkMgr.ActiveSketch

      'This line prints "True" every time indicating that my sketch IS active

          Debug.Print (swSketch.Is3D)

          Set swSkRelMgr = swSketch.RelationManager

          'swModel.SetAddToDB (True)

          'swModel.SetDisplayWhenAdded (False)

          'Call makeModel

          Call makeModelWithRelations

          'swModel.SetAddToDB (False)

          'swModel.SetDisplayWhenAdded (True)

      End Sub



      Sub makeModelWithRelations()

          Dim boolstatus As Boolean

          Dim startPoint As coordinates

          Dim endPoint As coordinates

          Dim vEntities As Variant

          Dim objArray(2) As Object

          Dim segment(1) As SketchSegment

          Dim segment2(1) As SketchLine

          Dim heading As Double

          Dim relations As Integer

          For currentInstruction = 0 To UBound(GetFile.bendProg.rotation)

              If currentInstruction = 0 Then

                  startPoint = setPoint(0, 0, 0)

                  endPoint = setPoint(0, bendProg.straight(0), 0)

                  createAline startPoint, endPoint

                  Set objArray(0) = sketchSegs(currentInstruction)

                  Set segment(0) = objArray(0)

                  Set segment2(0) = objArray(0)

                  Set swSkRel = swSkRelMgr.AddRelation(segment, swConstraintType_e.swConstraintType_FIXED)

                  Set swSkRel = swSkRelMgr.AddRelation(segment2, swConstraintType_e.swConstraintType_FIXED)

                  Set swSkRel = swSkRelMgr.AddRelation(objArray, swConstraintType_e.swConstraintType_FIXED)

                'If there is a relation it will show up in between the "df" and "SG", but every time this is run all that is printed is "dfSG"

                  Debug.Print ("df" & swSkRelMgr.GetRelations(swAll) & "SG")

                  relations = swSkRelMgr.IGetAllowedRelations(1, segment2(0), 4)

                'This always prints out 17, indicating that I CAN make this line fixed

                  Debug.Print (relations)




              End If


          Next currentInstruction

      End Sub


      Sub createAline(ByRef startPt As coordinates, endPt As coordinates)

          Dim boolstatus As SldWorks.SketchLine

          Dim bool As Boolean

          Dim dummy1 As coordinates

          Dim dummy2 As coordinates

          dummy1 = setPoint(startPt.x, startPt.y, startPt.z)

          dummy2 = setPoint(endPt.x, endPt.y, endPt.z)

          dummy1 = convertToInch(dummy1)

          dummy2 = convertToInch(dummy2)

          Set boolstatus = swSkMgr.CreateLine(dummy1.x, dummy1.y, dummy1.z, dummy2.x, dummy2.y, dummy2.z)

          ReDim Preserve sketchSegs(currentInstruction + 1)

          Set sketchSegs(currentInstruction) = boolstatus

      End Sub


      Function setPoint(ByVal X2 As Double, ByVal Y2 As Double, ByVal z2 As Double) As coordinates

          Dim toSet As coordinates

          toSet.x = X2

          toSet.y = Y2

          toSet.z = z2

          setPoint = toSet

      End Function


      Function convertToInch(toConv As coordinates) As coordinates

          toConv.x = toConv.x * metersToInch

          toConv.y = toConv.y * metersToInch

          toConv.z = toConv.z * metersToInch

          convertToInch = toConv

      End Function


      The documentation for AddRelation is as follows:


      Function AddRelation( _

         ByVal Entities As System.Object, _

         ByVal RelationType As System.Integer _

      ) As SketchRelation


      Visual Basic (Usage) 

      Dim instance As ISketchRelationManager

      Dim Entities As System.Object

      Dim RelationType As System.Integer

      Dim value As SketchRelation


      value = instance.AddRelation(Entities, RelationType)


      I've tried making "Entities" an array of: Objects, SketchLine, and SketchSegment and none of these seem to be what "entities" should be.


      Also, if I just try:

      Set swSkRel = swSkRelMgr.AddRelation(objArray(0), swConstraintType_e.swConstraintType_FIXED), which only references the one line I've made, solidworks just crashes


      Can anyone tell me what I'm doing wrong? I'm about at my wits end on this.