2 Replies Latest reply on Feb 18, 2015 4:53 PM by Richard Cook

    Please show me how to select the "known" point in the first sketch so I can add the relation. VBA

    Richard Cook

      Please see this VBA macro...

      I can't figure out how to select the first point by using the ID and sketch name....

      This should be simple...

      Please someone help.

       

      Thanks... Richard

      ==========================================

      == Copy and past below.  Run this macro in a part.  Nothing selected

      ===========================================

      Option Explicit

      Dim swApp As Object

      Dim swModel As SldWorks.ModelDoc2

      Dim swModelDocExt As SldWorks.ModelDocExtension

      Dim swSketchMgr As SldWorks.SketchManager

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swSelData As SldWorks.SelectData

      Dim skLine As SldWorks.SketchLine

      Dim skPoint As Object

      Dim vSketchPtID  As Variant

      'make a storage structure

      Public Type PointStore

           id0 As Long

           id1 As Long

           skName As String

           X As Double

           Y As Double

           Z As Double

      End Type

      Dim mySavedPoint As PointStore

      Dim boolstatus As Boolean

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swSketchMgr = swModel.SketchManager

       

      ' make a new blank Sketch

      swSketchMgr.Insert3DSketch True

       

      'make a point in the sketch and add fix relation

      Set skPoint = swModel.SketchManager.CreatePoint(2.3, 3.3, 1.25)

      swModel.SketchAddConstraints "sgFIXED"

       

      vSketchPtID = skPoint.GetID

       

      mySavedPoint.id0 = vSketchPtID(0)

      mySavedPoint.id1 = vSketchPtID(1)

      mySavedPoint.skName = swSketchMgr.ActiveSketch.Name

      mySavedPoint.X = skPoint.X

      mySavedPoint.Y = skPoint.Y

      mySavedPoint.Z = skPoint.Z

       

      'close this sketch

      swSketchMgr.Insert3DSketch True

      swModel.ClearSelection2 True

       

      'create new 3DSketch

      swSketchMgr.Insert3DSketch True

       

      Set swSelMgr = swModel.SelectionManager

      Set swSelData = swSelMgr.CreateSelectData

       

      ' Draw a line ..

        Set skLine = swSketchMgr.CreateLine(mySavedPoint.X, mySavedPoint.Y, mySavedPoint.Z, 5#, 5#, 5#)

      'select the line just created's endpoint

      '

        boolstatus = skLine.GetStartPoint2.Select4(False, swSelData)

        'Or other end '''boolstatus = skLine.GetEndPoint2.Select4(True, swSelData)

        ' So now the line endpoint is selected.

        ' And I have the persistant data from the first sketch's point...

            Debug.Print mySavedPoint.skName & vbCrLf & "IDPt = [" & _

                     mySavedPoint.id0 & ", " & _

                     mySavedPoint.id1 & "]" & vbCrLf & _

                     "Cordinate (" & mySavedPoint.X * 1000# & ", " & _

                     mySavedPoint.Y * 1000# & ", " & _

                     mySavedPoint.Z * 1000# & ") mm  "

      ''' How to add this point to the selection set and make the 2 coincident??

       

      MsgBox ("How now to select the first sketch point and make coincident?")

       

      swModel.ClearSelection2 True

      swSketchMgr.Insert3DSketch True

       

      End Sub

        • Re: Please show me how to select the "known" point in the first sketch so I can add the relation. VBA
          Richard Cook

          So finally....  How "GetID function can be used in Selecting a known point"

          I included some code snips below ....

           

          The GetID Method (ISketchPoint) returns an Array with two longs or two integers...

           

          The second element in the return array seems to correspond to the number in the selection name...

          So in the plotpoints function (below) the sketch point name is...

           

          myPointCount(i).skPtName = "Point" + CStr(vSketchPtID(1)) + "@" + swSketchMgr.ActiveSketch.Name

          .. Becomes something like "Point5@Sketch1"

           

          So now to select it later (add to selection set) by the exact name ...

          boolstatus = swModelDocExt.SelectByID2(myPointCount(i).skName, "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

           

          --- So far this has worked..

          '---------------------------------------------------------------------------------------------------

          Public Type PointStore

              id0 As Long

              id1 As Long

              skPtName As String

              X As Double

              Y As Double

              Z As Double

          End Type

          Dim myPointCount(20) As PointStore

          '-------------------------------------------------------------------------------------

          Main()

          vSketchPtID = plotPoints(0, 0, 0, 0, swSketchMgr.ActiveSketch.Name)

          .....

          '-------------------------------------------------------------------------------------

          Function plotPoints(i, X, Y, Z, skName) As Variant

           

          Set skPoint = swModel.SketchManager.CreatePoint(X, Y, Z)

          swModel.SketchAddConstraints "sgFIXED"

          vSketchPtID = skPoint.GetID

                Debug.Print "  IDPt(" & i & ") = [" & _

                        vSketchPtID(0) & ", " & _

                        vSketchPtID(1) & "]"

                Debug.Print "    (" & _

                        skPoint.X * 1000# & ", " & _

                        skPoint.Y * 1000# & ", " & _

                        skPoint.Z * 1000# & ") mm"

            myPointCount(i).id0 = vSketchPtID(0)

            myPointCount(i).id1 = vSketchPtID(1)

            myPointCount(i).skPtName = "Point" + CStr(vSketchPtID(1)) + "@" + swSketchMgr.ActiveSketch.Name

            myPointCount(i).X = skPoint.X

            myPointCount(i).Y = skPoint.Y

            myPointCount(i).Z = skPoint.Z

          plotPoints = vSketchPtID

          End Function

          • Re: Please show me how to select the "known" point in the first sketch so I can add the relation. VBA
            Richard Cook

            Also...

             

            The GetID Method (ISketchPoint) returns an Array with two longs or two integers...

             

            Some more guessing on my part about the array values..

            The second element in the return array correspond to the number in the selection name -- like "Point2@Sketch1"

            The first element seems to be the current position (order) in the sketch..

             

            So if you created 5 points in a sketch, the values returned would be (0,1),(1,2),(2,3),(3,4),(4,5)..

            If you delete those points and create 5 more, the new ID's would be (0,6),(1,7),(2,8),(3,9),(4,10).

             

            ....................