1 Reply Latest reply on Oct 21, 2015 4:21 PM by Jonathan Sterling

    How to add or remove features from an existing Table Driven Pattern using VBA?

    Jonathan Sterling

      I have an existing Table Driven Pattern of a tapped hole and would like to add another feature to the pattern if it is required and remove the feature if it is not. I have dug into the SldWorks.TablePatternFeatureData but I cannot figure out to use the PatternFeatureArray or the ISetPatternFeatureArray. Any assistance/direction would be greatly appreciated.

        • Re: How to add or remove features from an existing Table Driven Pattern using VBA?
          Jonathan Sterling

          Finally, got the code to work. For any who are interested or might search for something similar I will post the code below.

           

          To summarize, I needed to create an array and set swTablePatternFeatData.PatternFeatureArray equal to that array. To remove the extra feature I just repeated the process without adding the feature to the array. I was able to get direction from the VBA help file for creating and adding features to a design library feature.

           

          Finally, any pointers to improving the code would be appreciated. A dabbler in VBA am I and I am certain I lack the finesse others possess.

           

          Private Sub main()

          'Declarations

              'Solidworks

              Dim SwApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swModelDocExt As SldWorks.ModelDocExtension

              Dim swSelMgr As SldWorks.SelectionMgr

              Dim swFeat As SldWorks.Feature

              Dim swTablePatternFeatData As SldWorks.TablePatternFeatureData

              'In general

              Dim Status As Boolean

              Dim Obj() As Object

              Dim selCount As Integer

              Dim i As Integer

              Dim vFeat As Variant

              Dim Feature As String

            

              Set SwApp = GetObject(, "SldWorks.Application")

              Set swModel = SwApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager

              Set swModelDocExt = swModel.Extension

           

           

          'Access the table pattern feature

                  Status = swModelDocExt.SelectByID2("Bolt Pattern", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

                  Set swFeat = swSelMgr.GetSelectedObject6(1, -1)

                  Set swTablePatternFeatData = swFeat.GetDefinition

                  swTablePatternFeatData.AccessSelections swModel, Nothing

          'Determine which design situation is desired

              Feature = "S"

              If Feature = "U" Then

                  'Add both features to pattern

                     'Create array

                      Status = swModelDocExt.SelectByID2("Main Bolt", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, 0)

                      Status = swModelDocExt.SelectByID2("Bolt Clearance", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, 0)

                      selCount = swSelMgr.GetSelectedObjectCount2(-1)

                      selCount = selCount - 1

                      ReDim Obj(selCount) As Object

                      For i = 0 To selCount

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

                      Next

                      vFeat = Obj

                    

                     'Set the table pattern features equal to the vFeat array

                      swTablePatternFeatData.PatternFeatureArray = vFeat

                    

                      'Save changes and exit table pattern feature

                      swFeat.ModifyDefinition swTablePatternFeatData, swModel, Nothing

                      swModel.ClearSelection2 True

              Else

                  'Add only tap to pattern

                      'Create array

                      Status = swModelDocExt.SelectByID2("Main Bolt", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, 0)

                      selCount = swSelMgr.GetSelectedObjectCount2(-1)

                      selCount = selCount - 1

                      ReDim Obj(selCount) As Object

                      For i = 0 To selCount

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

                      Next

                      vFeat = Obj

                    

                      'Set the table pattern features equal to the vFeat array

                      swTablePatternFeatData.PatternFeatureArray = vFeat

                    

                      'Save changes and exit table pattern feature

                      swFeat.ModifyDefinition swTablePatternFeatData, swModel, Nothing

                      swModel.ClearSelection2 True

              End If

          End Sub