2 Replies Latest reply on Dec 17, 2014 12:05 AM by Brandon Massoni

    How to store selected features in an array?

    Brandon Massoni

      How can I store selected features (edges in my case) in an array? I keep getting "error 9, subscript out of range."

       

      'Precondition: Edges are selected

      'Post-condition: Edges are stored in an array "swEdge()"

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim longEdgeCount As Long

      Dim swEdge() As SldWorks.Edge

      Dim swEnt As SldWorks.Entity

      Dim Bret As Boolean

      Dim i As Integer

       

      Sub main()

         

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

         

          'Zero edge counter, find edge count value,& subtract 1 to match array index

          longEdgeCount = 0

          longEdgeCount = swSelMgr.GetSelectedObjectCount2(-1) 'Select regardless of marks

          MsgBox longEdgeCount

          longEdgeCount = longEdgeCount - 1

         

          'Set selectedObjects to objEdge() array

          For i = 0 To longEdgeCount

              Set swEdge(i) = swSelMgr.GetSelectedObject6(i + 1, -1)

          Next

       

          'Check to see if swEdge is populated correctly

          swModel.ClearSelection2 True

          For i = 0 To longEdgeCount

              Set swEnt = swEdge(i)

              Bret = swEnt.Select4(True, swSelData): Debug.Assert Bret

          Next

            

      End Sub

        • Re: How to store selected features in an array?
          Keith Rice

          Brandon,

           

          The reason you are getting that error is because your array, swEdge, is dynamic, and dynamic arrays by default have no length. You need to use ReDim to specify an array size before you start assigning values to its elements. For example, this will resize the array so that it can hold the number of items selected:

           

          ReDim swEdge(swSelMgr.GetSelectedObjectCount2(-1) - 1)

           

          ReDim resets the entire array. You will need to use ReDim Preserve if you want to preserve existing elements.

           

          But wait! I would not recommend that you use arrays at all. Rather, use the Collection class, which is much easier to work with. When you declare a collection you will need to use the New keyword, as shown below, because a collection is a class, and classes need to be instantiated. Also be sure to reset your collection each time you run your program.

           

          Here is how I would recommend you write this macro:

           

          =======

           

          'Precondition: Edges are selected

          'Post-condition: Edges are stored in collection "colEdges"

           

          Option Explicit

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim colEdges As New Collection

          Dim swEnt As SldWorks.Entity

          Dim i As Integer

           

          Sub main()

              Set colEdges = Nothing

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager

            

              For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

                  colEdges.Add swSelMgr.GetSelectedObject6(i, -1)

              Next

            

              swModel.ClearSelection2 True

            

              For i = 1 To colEdges.Count

                  Set swEnt = colEdges.Item(i)

                  swEnt.Select4 True, Nothing

              Next

            

              Stop

            

          End Sub

           

          =======

           

          Finally, on a semantic note: "features" refer only to items that reside in a FeatureManager tree. Since edges cannot reside there, they are not considered features. Technically, an edge is an topological entity.

           

          I hope this helps.

           

          Keith

          SolidWorks API Video Tutorials

          1 person found this helpful