5 Replies Latest reply on Dec 1, 2015 2:25 PM by John Alexander

    Entity from SketchSegment

    John Alexander

      I'm trying to add relations between pairs of sketch segments. I've noticed that the add relation methods require entities as arguments. However, I only have references to sketchSegments and I can't figure out how to get from one to the other.

        • Re: Entity from SketchSegment
          John Alexander

          Sorry for the weak post. I submitted this prematurely and didn't notice (subcosciously pressing ctrl+s is my worst good habit). There doesn't seem to be any way of editing the top post.

           

          I've tried casting the SketchSegment as an Entitiy and get type errors on assignment. It seems weird that AddRelation takes an array of entities as an argument when there isn't an obvious way of retrieving entities from SketchSegments, Vertices, Etc. (all of the things that one would expect to add relations to). Please tell me I'm missing something simple.

           

          I also seem to see language in the API documentation that leads me to believe that SketchSegments are Entities. If that's the case, why the type mismatch error?

           

          I'm using Solidworks 2012 and writing the macro in VBA.

           

           

          Thank you,

           

          John Alexander

            • Re: Entity from SketchSegment
              Rajat Jain

              John,

               

              Instead of converting the SketchSegments into Entities, create an array of sketchSegments and pass it to the function. It works just fine.

               

              Just check this sample code: -

               

              Dim swApp As SldWorks.SldWorks

               

              Dim Part As SldWorks.ModelDoc2

              Dim boolstatus As Boolean

              Dim longstatus As Long, longwarnings As Long

               

              Sub main()

               

              Set swApp = Application.SldWorks

               

              Set Part = swApp.ActiveDoc

              Dim myModelView As Object

              Set myModelView = Part.ActiveView

              myModelView.FrameState = swWindowState_e.swWindowMaximized

              Part.SketchManager.InsertSketch True

              boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", -1.68043096009569E-02, 0.121369081820774, 0.101066255943181, False, 0, Nothing, 0)

              Part.ClearSelection2 True

              Dim skSegment1 As Object

              Dim skSegment2 As Object

               

              Set skSegment1 = Part.SketchManager.CreateLine(-0.060294, -0.006474, 0#, -0.030619, 0.038038, 0#)

              Set skSegment2 = Part.SketchManager.CreateLine(-0.030619, 0.038038, 0#, 0.025224, 0.032103, 0#)

               

              Dim oSketch As SldWorks.Sketch

              Set oSketch = Part.GetActiveSketch2()

              Part.SetPickMode

              Part.ClearSelection2 True

              Dim obj(1) As Object

              Set obj(0) = skSegment1

              Set obj(1) = skSegment2

              Dim v As Variant

              v = obj

              Call oSketch.RelationManager.AddRelation(v, swConstraintType_PERPENDICULAR)

              Part.ClearSelection2 True

              End Sub

               

              Hope it helps!!!

               

              Rajat Jain

                • Re: Entity from SketchSegment
                  Matthew Cempa

                  This may help a little, look up safe arrays in the API documentation:

                   

                  Passing array data to SolidWorks in Visual Basic requires the use of a
                  VARIANT variable to hold the data. Put the data into an array and assign a VARIANT to the array. The VARIANT is
                  passed to an API function enclosed within parentheses.

                   

                  Dim varArray As Variant

                  Dim DataArray(9) As double

                   

                  ‘ Assign values

                  DataArray(0) = 0.1

                  ...

                   

                  ‘ Pass the data to SolidWorks

                  varArray = DataArray

                  Result = Object.Method( (varArray) )  ‘ The extra parentheses are required

                    • Re: Entity from SketchSegment
                      John Alexander

                      Thanks for that, I was not aware of this (requirement?).

                       

                      I did some experimenting and observed the following behavior (using Rajat's example above as reference):

                       

                      1. If I Dim obj(1) as Variant, and use it in the same way as in Rajat's example, it throws a server exception when I call

                       

                      oSketch.RelationManager.AddRelation(v, swConstraintType_PERPENDICULAR)

                       

                      2. If I Dim obj(1) as Object and assign it to a Variant v, I can pass it to

                       

                      oSketch.RelationManager.AddRelation(v, swConstraintType_PERPENDICULAR)

                       

                      without any hickups (this is literally Rajat's example).

                       

                      3. If I Dim obj(1) as Object and pass it to

                       

                      oSketch.RelationManager.AddRelation((obj), swConstraintType_PERPENDICULAR)

                       

                      It also works (omitting the assignment to a Variantv).

                    • Re: Entity from SketchSegment
                      John Alexander

                      Thank you, this is working for me.