5 Replies Latest reply on Jun 12, 2015 11:52 AM by Simon Turner

    How to Change a Table Driven Pattern's Coordinate System with VBA API

    Jonathan Sterling

      Hi, first time posting so if I have done anything not by the book please let me know. I tried doing an extensive amount of research on the question I am proposing and have not had much luck, on the forum or off. I am hoping someone here can assist me.


      I am working on a macro that takes a preexisting feature's sketch and moves it to a new location on the part file. The feature also has a table driven pattern child that needs to move as well (coordinate system).  Thus far I have been able to relocate the sketch to a new face and change the relations controlling the sketch entities. The issue I am having is finding anywhere that might discuss how to change the coordinate system in the table pattern to a separate coordinate system set for the other location. (There are two coordinate systems in the model for each location that the feature can be moved to).


      Is there a VBA command for switching between these two coordinate systems or is there a better method I am not considering.


      Thank you for any assistance with this matter.

        • Re: How to Change a Table Driven Pattern's Coordinate System with VBA API
          Simon Turner

          As far as I can tell, you just have to get the Feature corresponding to the pattern and convert it to a TablePatternFeatureData object (by Feature::GetDefinition).

          Then you can use TablePatternFeatureData::CoordinateSystem

          If you are changing the coordinate system, you will need to use the AccessSelections method (equivalent to Edit Feature in the UI), change the coordinate system, and then Feature::ModifyDefinition

            • Re: How to Change a Table Driven Pattern's Coordinate System with VBA API
              Jonathan Sterling

              Thank you Simon for the quick response.


              In my searching I did find examples of code for creating a new pattern and in that instance dictating the necessary elements that make up the pattern (coordinate system, seed feature, etc). I had considered just deleting the pattern and remaking it each time, but the pattern I am moving actually has a part pattern in an assembly file that moves with the feature pattern. So remaking the pattern each time would not be efficient.


              I also noticed the AccessSelections method but it looked like it was used to just get information from the pattern. Do you have any code showing how to modify or edit a feature (a table driven pattern feature would be excellent) using the AccessSelections method and ModifyDefinition?

                • Re: How to Change a Table Driven Pattern's Coordinate System with VBA API
                  Simon Turner

                  I don't have any examples to hand, but if you search the API help for "AccessSelections AND example", you will find plenty of examples.

                  For example:


                  2015 SOLIDWORKS API Help - Get and Set Seed Components Example (VB.NET)

                    • Re: How to Change a Table Driven Pattern's Coordinate System with VBA API
                      Jonathan Sterling

                      I actually forgot I had used the AccessSelections function to create a macro for changing out the text file that drove the patterns' locations.


                      I reviewed it and pulled the code into this macro, but I was unable to determine how to get the Coordinate System to be recognized as the correct object type and tried a slew of methods. I was about to post the code in the hopes you could assist in finding the issue and to make the debugging easier I was going through the different methods I used and what error codes I received. I was removing the ' to allow the macro to read the code and I must have modified the code after trying it because the second time I tried the section it worked.


                      All of that to say thank you Simon for setting me on the correct path, I am posting the code below and highlighting the section that was the subject of this posting:


                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swPart As SldWorks.PartDoc

                      Dim swModelDocExt As SldWorks.ModelDocExtension

                      Dim swSelMgr As SldWorks.SelectionMgr

                      Dim swFeat As SldWorks.Feature

                      Dim swFace As SldWorks.Face2

                      Dim swTablePatternFeatData As SldWorks.TablePatternFeatureData

                      Dim swCoordSys As SldWorks.Feature

                      Dim TurnOffScreen As ModelView

                      Dim SuppState As Variant

                      Dim ConfigNames As Variant

                      Dim boolstatus As Boolean

                      Sub ChangetoAngled()



                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swSelMgr = swModel.SelectionManager

                      Set swModelDocExt = swModel.Extension



                      'Turn off screen updating

                          Set TurnOffScreen = swModel.ActiveView

                          TurnOffScreen.EnableGraphicsUpdate = False

                          swModel.ClearSelection2 True



                      'Check if model is part file

                          If Not swModel Is Nothing Then

                              If Not swModel.GetType = SwConst.swDocumentTypes_e.swDocPART Then GoTo Wrongtype:

                          End If


                      'Activate Heater Hole Position Sketch for Editing

                          boolstatus = swModelDocExt.SelectByID2("HH Position", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)


                          'Select Hole Point and Remove Horizontal Relation

                              boolstatus = swModelDocExt.SelectByID2("Point1", "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)

                              swModel.SketchConstraintsDel 0, "sgHORIZONTALPOINTS2D"

                          'Select Point and add new Horizontal Relationship

                              boolstatus = swModelDocExt.SelectByID2("Point10@Angled Heater Cut and Heaters Position Sketch", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

                              swModel.SketchAddConstraints "sgHORIZONTALPOINTS2D"

                          'Exit sketch

                              swModel.SketchManager.InsertSketch True


                      'Select Angled Face and Heater Hole Position Sketch and redefine sketch plane

                          boolstatus = swModelDocExt.SelectByID2("HH Position", "SKETCH", 0, 0, 0, True, 0, Nothing, 0)

                          Set swFace = swModel.GetEntityByName("Angled Heater Face", 2)

                          swFace.Select2 True, True

                          boolstatus = swModelDocExt.ChangeSketchPlane(1, Nothing)


                      'Clear Selections and prepare to change Coordinate System

                          swModel.ClearSelection2 True


                      'Select the Table-Driven Pattern and access it to change coordinate system

                          boolstatus = swModelDocExt.SelectByID2("Heater Hole Pattern", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

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

                          Set swTablePatternFeatData = swFeat.GetDefinition

                          swTablePatternFeatData.AccessSelections swModel, Nothing

                          'Select new coordinate system and change to it

                              boolstatus = swModelDocExt.SelectByID2("Angled Heater CS", "COORDSYS", 0, 0, 0, False, 0, Nothing, 0)

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

                              swTablePatternFeatData.CoordinateSystem = swCoordSys

                          'Exit feature with changes intact

                              swFeat.ModifyDefinition swTablePatternFeatData, swModel, Nothing


                      'No issues with code > exit sub

                          TurnOffScreen.EnableGraphicsUpdate = True

                          Exit Sub


                      'Wrong file type error


                          TurnOffScreen.EnableGraphicsUpdate = True

                          MsgBox ("This macro reguires a SolidWorks Part file.")

                      End Sub