33 Replies Latest reply on Mar 1, 2016 7:48 AM by Robert Redmond

    Macro help?

    Robert Redmond

      Anyone have any ideas on how to create a macro for Sheet Metal Thickness?

       

      Any help is very much appreciated.

        • Re: Macro for sheet metal thickness?
          Akshay Abitkar

          Dim swap As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swFeat As SldWorks.Feature

           

          Sub main()

          Set swApp = Application.SldWorks

           

          Set swModel = swApp.ActiveDoc

           

          Set swSelMgr = swModel.SelectionManager

           

          Set swFeat = swModel.FirstFeature

           

          Do While Not swFeat Is Nothing

           

          If(swFeat.GetTypeName = swTnSheetMetal) Then

           

               Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                Set swSheetMetal = swFeat.GetDefinition

               Debug.Print "    Sheet metal thickness = " & swSheetMetal. Thickness * 1000# & "mm"

          End If

           

          Set swFeat = swFeat.GetNextFeature

          Loop

           

          End Sub

            • Re: Macro for sheet metal thickness?
              Akshay Abitkar

              If you want to change thickness of sheetmetal features then you should add following code

              swSheetMetal.Thickness = <thickness value in double>

               

              For example:

              swSheetMetal.Thickness = 0.2

                • Re: Macro for sheet metal thickness?
                  Akshay Abitkar

                  Also,

                  See below Solidworks Task scheduler process

                   

                  1. Write a macro with hard coded input folder
                  path. This macro will open the files in a loop, update it (Change thickness) and
                  then save it.

                  2. Copy the code from the VBA editor to Notepad
                  and change the hard coded FolderPath value to $$$INFOLDER_PATH$$$

                  Const strInputFolderPath As String =
                  $$$INFOLDER_PATH$$$

                  Note: For numeric parameters use ### instead of
                  $$$

                  3. Save
                  the text file as a .swb file.

                  4. Open Task Scheduler (START>>All
                  Programs>>SolidWorks ‘Ver xxxx’>>SolidWorks Tools>>
                  SolidWorks Task Scheduler).

                  5.
                  Choose Run Custom Task

                  • Re: Macro for sheet metal thickness?
                    Robert Redmond

                    Hello Akshay

                     

                    Sorry for my stupidity, im still a novice with the API, would i put in that code:

                    If (swFeat.GetTypeName = SheetMetal(Here) Then

                     

                         Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                          Set swSheetMetal = swFeat.GetDefinition

                         Debug.Print "    Sheet metal thickness = " & swSheetMetal.Thickness * 1000# & "mm"

                     

                    Thanks for the reply

                      • Re: Macro for sheet metal thickness?
                        Akshay Abitkar

                        Hi Robert,

                        You should set the value that you want to change for thickness property like below one.

                        If (swFeat.GetTypeName = SheetMetal(Here) Then

                         

                             Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                              Set swSheetMetal = swFeat.GetDefinition

                              swSheetMetal.Thickness = <value to update>

                               ' swSheetMetal.Thickness = 0.2

                        This will change thickness value to 0.2

                          • Re: Macro for sheet metal thickness?
                            Robert Redmond

                            Is that how it should, is it okay if it is green??

                             

                            Thanks Akshay

                              • Re: Macro for sheet metal thickness?
                                Akshay Abitkar

                                The content following symbol ' is commented section of code. To work with it you should remove symbol ' (removing of comment).

                                See code snippet as below.

                                 

                                If (swFeat.GetTypeName = "SheetMetal") Then

                                    Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                                    Set swSheetMetal = swFeat.GetDefinition

                                     swSheetMetal.Thickness = 0.2

                                  

                                End If

                                  • Re: Macro for sheet metal thickness?
                                    Robert Redmond

                                    Akshay

                                     

                                    Do i Leave : Debug.Print "    Sheet metal thickness = " & swSheetMetal.Thickness * 1000# & "mm"

                                    Or will this cause conflicts?

                                     

                                    Regards

                                     

                                    Robert

                                      • Re: Macro for sheet metal thickness?
                                        Akshay Abitkar

                                        Yes, you can remove Debug.Print "    Sheet metal thickness = " & swSheetMetal.Thickness * 1000# & "mm". I have putted there for debugging purpose. As Thickness is property.  We can get/set it.

                                        Debug.Print "    Sheet metal thickness = " & swSheetMetal.Thickness * 1000# & "mm". statement will print the thicknees value. while

                                        swSheetMetal.thickness = 0.2 will change/set the thickness value to 0.2.

                                          • Re: Macro for sheet metal thickness?
                                            Robert Redmond

                                            Akshay

                                             

                                            Thank you very much. By using the Folder Strings and parameters will this tell Solidworks where to look for the files i need to change?

                                             

                                            Rob

                                              • Re: Macro for sheet metal thickness?
                                                Akshay Abitkar

                                                ' Change the FolderPath variable to point to the folder on your local drive
                                                ' Assumes files open are part files. Modifications will be needed to open other file types
                                                ' Add Microsoft Scripting Runtime as a reference

                                                Dim swApp As SldWorks.SldWorks
                                                Dim swModel As SldWorks.ModelDoc2

                                                Sub main()
                                                    Dim FSObject As New FileSystemObject
                                                    Dim FileName As Object
                                                    Dim Source As Object
                                                    Dim FolderPath As String
                                                    Dim lErrors As Long
                                                    Dim lWarnings As Long
                                                    Dim bRet As Boolean
                                                    Dim options As Long
                                                    Dim swSelMgr As SldWorks.SelectionMgr
                                                    Dim swFeat As SldWorks.Feature

                                                  

                                                    Set swApp = Application.SldWorks

                                                    ' The folder to loop through
                                                    FolderPath = "E:\Siebel\2016\February"
                                                   
                                                    Set Source = FSObject.GetFolder(FolderPath)
                                                       
                                                    ' Loop through all files in the folder
                                                    For Each FileName In Source.Files
                                                        Debug.Print "Process Filename: " & FileName.Path
                                                   
                                                        ' Open each file, assumes PART files
                                                        Set swModel = swApp.OpenDoc6(FileName.Path, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", lErrors, lWarnings)
                                                        If swModel Is Nothing Then
                                                            Debug.Print "Model is nothing, error opening file"
                                                        End If
                                                       
                                                        Set swSelMgr = swModel.SelectionManager
                                                        Set swFeat = swModel.FirstFeature
                                                       
                                                        Do While Not swFeat Is Nothing

                                                        If (swFeat.GetTypeName = swTnSheetMetal) Then

                                                        Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                                                        Set swSheetMetal = swFeat.GetDefinition
                                                       
                                                        bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet
                                                       
                                                        swSheetMetal.Thickness = 0.2
                                                       
                                                        bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet

                                                    End If

                                                    Set swFeat = swFeat.GetNextFeature

                                                Loop

                                                     
                                                    Next
                                                End Sub

                                                 

                                                1> Open notepad , copy paste above code in it. Change the FolderPath variable to point to the folder on your local drive

                                                2>Save the text file as a .swb file.

                                                4>Open Task Scheduler (START>>AllPrograms>>SolidWorks ‘Ver xxxx’>>SolidWorks Tools>> SolidWorks Task Scheduler).
                                                5>Choose Run Custom Task

                                                6> thikness value will be change to 0.2 for sheet metal parts

                                • Re: Macro for sheet metal thickness?
                                  Robert Redmond

                                  Hello Akshay

                                   

                                  ' Add Microsoft Scripting Runtime as a reference----**** How do i do this?****

                                  Dim swApp As SldWorks.SldWorks
                                  Dim swModel As SldWorks.ModelDoc2

                                  Sub main()
                                       Dim FSObject As New FileSystemObject
                                       Dim FileName As Object
                                       Dim Source As Object
                                       Dim FolderPath As String
                                       Dim lErrors As Long
                                       Dim lWarnings As Long
                                       Dim bRet As Boolean
                                       Dim options As Long
                                       Dim swSelMgr As SldWorks.SelectionMgr
                                       Dim swFeat As SldWorks.Feature

                                    

                                      Set swApp = Application.SldWorks

                                      ' The folder to loop through
                                       FolderPath = "E:\Siebel\2016\February"--- I Have inputted $$$E:\Siebel\2016\February$$$-- Is this correct?
                                      
                                       Set Source = FSObject.GetFolder(FolderPath)
                                          
                                       ' Loop through all files in the folder
                                       For Each FileName In Source.Files
                                           Debug.Print "Process Filename: " & FileName.Path
                                      
                                           ' Open each file, assumes PART files
                                           Set swModel = swApp.OpenDoc6(FileName.Path, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", lErrors, lWarnings)
                                           If swModel Is Nothing Then
                                               Debug.Print "Model is nothing, error opening file"
                                           End If
                                          
                                           Set swSelMgr = swModel.SelectionManager
                                           Set swFeat = swModel.FirstFeature
                                          
                                           Do While Not swFeat Is Nothing

                                          If (swFeat.GetTypeName = swTnSheetMetal) Then

                                          Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                                          Set swSheetMetal = swFeat.GetDefinition
                                          
                                           bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet
                                          
                                           swSheetMetal.Thickness = 2
                                          
                                           bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet

                                      End If

                                      Set swFeat = swFeat.GetNextFeature

                                  Loop

                                       
                                       Next
                                  End Sub

                                   

                                  I have done everything correctly i think but it still will not work Akshay/ anyone else who could help

                                  When i run it as a custom task, for the Parameter String i put the "Value" i want to change the sheet metal to?

                                   

                                  Regards

                                   

                                  Thank you guys

                                    • Re: Macro for sheet metal thickness?
                                      Deepak Gupta

                                      Robert Redmond wrote:

                                       

                                      ' Add Microsoft Scripting Runtime as a reference----**** How do i do this?****


                                      Edit the macro and follow the picture below

                                       

                                        • Re: Macro for sheet metal thickness?
                                          Robert Redmond

                                          Deepak

                                           

                                          When i run the Macro in Solidworks, it opens all of the parts as it should but it does not change the sheet metal thickness, do you know any reason why this is happening? When i go to run it as a custom task, it doesn't open any of the parts and just says "Timed Out"

                                           

                                          Thank you in advance

                                            • Re: Macro help?
                                              Deepak Gupta

                                              This is bug as reported under SPR #: 422119 which states SheetMetalFeatureData does not set the thickness. So as a workaround, use the codes as stated by Scott Stuart here: Re: How to change thickness of a sheet metal part via API

                                                • Re: Macro help?
                                                  Artem Taturevych

                                                  In #TASK we have implemented the another workaround (by setting the value of the display dimension of a sheet metal feature) so the thickness value can be changed on the top level feature rather than on individual bends. So the result is equal to setting the ::Thickness property (considering it works).

                                                    • Re: Macro help?
                                                      Deepak Gupta

                                                      OK.

                                                       

                                                      The method suggested by Scott Stuart also works as that changes the top level thickness.

                                                        • Re: Macro help?
                                                          Robert Redmond

                                                          Deepak

                                                           

                                                          Where would you insert the code from Scott?

                                                           

                                                          Dim swApp As SldWorks.SldWorks

                                                          Dim swModel As SldWorks.ModelDoc2

                                                          Sub main()

                                                              Dim FSObject As New FileSystemObject

                                                              Dim FileName As Object

                                                              Dim Source As Object

                                                              Dim FolderPath As String

                                                              Dim lErrors As Long

                                                              Dim lWarnings As Long

                                                              Dim bRet As Boolean

                                                              Dim options As Long

                                                              Dim swSelMgr As SldWorks.SelectionMgr

                                                              Dim swFeat As SldWorks.Feature

                                                            

                                                              Set swApp = Application.SldWorks

                                                              ' The folder to loop through

                                                              FolderPath = "C:\Siebel\2016\February"

                                                             

                                                              Set Source = FSObject.GetFolder(FolderPath)

                                                                 

                                                              ' Loop through all files in the folder

                                                              For Each FileName In Source.Files

                                                                  Debug.Print "Process Filename: " & FileName.Path

                                                             

                                                                  ' Open each file, assumes PART files

                                                                  Set swModel = swApp.OpenDoc6(FileName.Path, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", lErrors, lWarnings)

                                                                  If swModel Is Nothing Then

                                                                      Debug.Print "Model is nothing, error opening file"

                                                                  End If

                                                                 

                                                                  Set swSelMgr = swModel.SelectionManager

                                                                  Set swFeat = swModel.FirstFeature

                                                                 

                                                                  Do While Not swFeat Is Nothing

                                                                  If (swFeat.GetTypeName = swTnSheetMetal) Then

                                                                  Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                                                                  Set swSheetMetal = swFeat.GetDefinition

                                                                 

                                                                  bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet

                                                                 

                                                                  swSheetMetal.Thickness = 0.2

                                                                 

                                                                  bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet

                                                              End If

                                                              Set swFeat = swFeat.GetNextFeature

                                                          Loop

                                                               

                                                              Next

                                                          End Sub

                                                           

                                                          Thank you again

                                                            • Re: Macro help?
                                                              Deepak Gupta

                                                              Robert Redmond wrote:

                                                               

                                                              Set swFeat = swModel.FirstFeature

                                                              Do While Not swFeat Is Nothing

                                                              If (swFeat.GetTypeName = swTnSheetMetal) Then

                                                              Dim swSheetMetal As SldWorks.SheetMetalFeatureData

                                                              Set swSheetMetal = swFeat.GetDefinition

                                                              bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet

                                                              swSheetMetal.Thickness = 0.2

                                                              bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet

                                                              End If

                                                              Set swFeat = swFeat.GetNextFeature

                                                              Loop

                                                               

                                                               

                                                              Replace above with these lines

                                                               

                                                              Dim swBaseFlange As SldWorks.BaseFlangeFeatureData

                                                              Set swFeat = swModel.FirstFeature

                                                              Do While Not swFeat Is Nothing

                                                                  If swFeat.GetTypeName = "SMBaseFlange" Then

                                                                      Set swBaseFlange = swFeat.GetDefinition

                                                                      swBaseFlange.AccessSelections swModel, Nothing

                                                                      swBaseFlange.Thickness = 2 / 1000 ' This would set the thickness as 2mm

                                                                      swFeat.ModifyDefinition swBaseFlange, swModel, Nothing

                                                                  End If

                                                                  Set swFeat = swFeat.GetNextFeature

                                                              Loop

                                                              Also note that macro uses meter for linear values. So in case you want to change other unit like mm or inch, then divide by factor accordingly.

                                              • Re: Macro for sheet metal thickness?
                                                Deepak Gupta

                                                What exactly you need to do with that macro; get or set thickness?

                                                • Re: Macro for sheet metal thickness?
                                                  Viktor Bovzdarenko

                                                  Hi Robert

                                                  The macro from @Akshay Abitkar perfectly reports the thickness of sheet metal if you modify it a bit:

                                                  replace swTnSheetMetal by "SheetMetal"

                                                  see example:

                                                  If (swFeat.GetTypeName = "SheetMetal") Then

                                                      Dim swSheetMetal  As SldWorks.SheetMetalFeatureData

                                                      Set swSheetMetal = swFeat.GetDefinition

                                                      Debug.Print "    Sheet metal thickness = " & swSheetMetal.Thickness * 1000# & "mm"

                                                  End If

                                                   

                                                  but what do you want? only see the thickness or change as well?