1 Reply Latest reply on Feb 17, 2015 2:56 PM by Ivana Kolin

    Sketch Text Count

    David Dewey

      I am running the following script. The script looks at all the sketch text within a sketch and renumbers them based on their insertion order starting from zero. There is a maximum of 144 text entities and a minimum of some number under that. What I'm unsure of is how to get the actual count of entities so I can run my FOR loop without going out of range and popping up an error dialogue each time. Any help on how to get this count would be appreciated!

       

       

       

       

      ' Preconditions:

      '           (1) Part document is open and contains

      '               sketch text.

      '           (2) Feature for sketch text is selected.

      ' Postconditions: All text is updated in numeric order starting from 0.

      '

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

       

       

      Option Explicit

       

       

       

       

       

      Dim swApp As SldWorks.SldWorks

       

       

      Dim swModel As Object

       

       

      Dim swSelMgr As SldWorks.SelectionMgr

       

       

      Dim swFeat As SldWorks.Feature

       

       

      Dim swSketch As SldWorks.Sketch

       

       

      Dim swSketchText As SldWorks.SketchText

       

       

      Dim params As Variant

      Dim i As Integer

      Dim Sketchid As Integer

      Dim TextCount As Integer

       

      Sub main()

       

       

      Set swApp = Application.SldWorks

       

       

      Set swModel = swApp.ActiveDoc

       

       

      Set swSelMgr = swModel.SelectionManager

       

       

      ' Get the selected feature of the sketch text

       

       

      Set swFeat = swSelMgr.GetSelectedObject6(1, 0)

       

       

      Set swSketch = swFeat.GetSpecificFeature2

       

       

      ' Edit the sketch of the sketch text

       

       

      swModel.EditSketch

       

       

       

      'vSketchTextSegments = swSketch.GetSketchTextSegments

       

       

      ' Get the sketch text

       

       

      params = swSketch.GetSketchTextSegments

       

       

      ' Only one instance of sketch text so

       

       

      ' set SketchText to that instance

       

       

      'GET TEXTCOUNT

      'TextCount =

       

      'ATTEMPTED FOR LOOP

      For i = 0 To 144

          Set swSketchText = params(i)

          swSketchText.Text = i

      Next i

       

      ' Insert the new text in the sketch,

       

       

      ' rebuild the part with any changes

       

       

      ' made to the sketch, and

       

       

      ' exit sketch mode

       

       

      swModel.InsertSketch2 True

       

      End Sub

        • Re: Sketch Text Count
          Ivana Kolin
          Option Explicit
          
          Dim swApp As SldWorks.SldWorks
          Dim swModel As Object
          Dim swSelMgr As SldWorks.SelectionMgr
          Dim swFeat As SldWorks.Feature
          Dim swSketch As SldWorks.Sketch
          Dim swSketchText As SldWorks.SketchText
          Dim params As Variant
          Dim i As Integer
          Dim Sketchid As Integer
          Dim TextCount As Integer
          
          Sub main()
          
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
              ' Get the selected feature of the sketch text
              Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
              Set swSketch = swFeat.GetSpecificFeature2
              ' Edit the sketch of the sketch text
              swModel.EditSketch
               
              'vSketchTextSegments = swSketch.GetSketchTextSegments
               
              ' Get the sketch text
              params = swSketch.GetSketchTextSegments
              ' Only one instance of sketch text so
              ' set SketchText to that instance
               
              'GET TEXTCOUNT
              '  ***** solution a
              Dim param As Variant
              TextCount = 0
              If (Not IsEmpty(params)) Then
                  For Each param In params
                      Set swSketchText = param
                      swSketchText.Text = TextCount
                      TextCount = TextCount + 1
                  Next
              End If
              '  ***** end   solution a
              
              '  ***** solution b
              TextCount = 0
              
              If (Not IsEmpty(params)) Then
                  TextCount = UBound(params)
              End If
          '    'ATTEMPTED FOR LOOP
              For i = 0 To TextCount
                  Set swSketchText = params(i)
                  swSketchText.Text = i
              Next i
              '  ***** end solution b
          
          
              ' Insert the new text in the sketch,
                 
              ' rebuild the part with any changes
                   
              ' made to the sketch, and
                    
              ' exit sketch mode
                    
              swModel.InsertSketch2 True
               
          End Sub