4 Replies Latest reply on Nov 24, 2016 9:56 PM by Justin Logan

    macros and layers

    David Spilka

      i am trying to make macros that will save endless time for us.  i can get the macros to work if there are drop down menus (like save as to PDF or send to printer), but the macro record will not recognize the layer box in the command manager.  if i click on it and open it up and turn a layer on or off, the recording of the macro does not understand what is going on.  how do i solve this?  also, if the macro opens the print dialog box and i change the print quantity to 2 from 1, how do i get the macro to do this for me?

        • Re: macros and layers
          Deepak Gupta

          Try these codes and use as needed


          'Demonstrates how to open the layer and print windows

          'Preconditions: Active file is a Drawing


          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2


          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc


              If (swModel Is Nothing) Then

                  MsgBox " Please open a drawing document. "


              End If


               If (swModel.GetType <> swDocDRAWING) Then

                  MsgBox " Please open a drawing document. "


              End If


              'Open SOLIDWORKS Layers Window

             swModel.Extension.RunCommand swCommands_LayerProperties, ""


              'Open SOLIDWORKS Print Window

              swModel.Extension.RunCommand swCommands_Print, ""


          End Sub

          • Re: macros and layers
            Peter Brinkhuis

            Deepak's answer highlights the fact that the macro recorder can't record all actions, but that the API does have functions for most of the possibilities of SolidWorks. It seems that any sketching, modeling and drawing creation work can be recorded (albeit poorly) and any work regarding windows and screens cannot be recorded.

            • Re: macros and layers
              Keith Rice

              The macro recorder is not an effective way to create a macro in many instances, since it:

              • Can't record certain actions
              • Can't record logic
              • Can't get user input

              Other downsides:

              • The resulting code is usually messy and might not use the best API calls or show you the ideal way to automate via API a certain task


              In short, it isn't a tool that professional API developers like myself use very often. But in your case, I understand why you want to use it, since it gives the impression of being able to record the macro you need. The name "Macro Recorder", then is somewhat of a misnomer.


              Here are some API calls that might be necessary in the macro you are looking for:

              • IDrawingDoc::SetCurrentLayer
              • IDrawingDoc::CreateLayer2
              • IModelDocExtension::PrintOut4


              The names should be self-explanatory. Needless to say, these API calls will perform the same actions you would normally do in GUI, but without the GUI. The options you set in the GUI are passed in as arguments to those API calls, roughly speaking.


              If you need help writing this, please explain more thoroughly what you are trying to automate. Please see How to ask a good question in the API forum for reference.



              SolidWorks API Training and Services

              • Re: macros and layers
                Justin Logan

                I came across this Sub (can't remember what post it came from on the Forum but thank you to whoever posted it!) that can be put into the standard Print and Convert tasks (though I am not sure of your final application):


                Sub LayerVisibility()

                    Dim Part As Object

                    Dim swLayerMgr As Object

                    Dim swLayer As Object


                    Set swApp = CreateObject("SldWorks.Application")

                    Set Part = swApp.ActiveDoc

                    Set swLayerMgr = Part.GetLayerManager


                    ' Selecting QC-Balloons Layer  (Layer name can be changed to whatever layer is desired)

                    Set swLayer = swLayerMgr.GetLayer("QC-Balloons")


                    ' Turn layer OFF (False) or ON (True)

                    swLayer.Visible = False


                    Set swLayer = Nothing

                    Set swLayerMgr = Nothing

                    Set Part = Nothing

                End Sub


                Paste this entire Sub into the script of the Task and call on it as needed.


                For the Print Task I put it into the Open Document with an If statement - though not sure if the If this is required:


                    ' Open document

                    If docType = swDocNONE Then

                        Set swModel = swApp.LoadFile4(docFileName, "", Nothing, errors)


                        Set swDocSpecification = swApp.GetOpenDocSpec(docFileName)

                        swDocSpecification.DocumentType = docType

                        swDocSpecification.ReadOnly = True

                        swDocSpecification.Silent = True

                        swDocSpecification.ConfigurationName = ""

                        swDocSpecification.DisplayState = ""

                        Set swModel = swApp.OpenDoc7(swDocSpecification)

                        errors = swDocSpecification.Error


                        'Set swModel = swApp.OpenDoc6(docFileName, docType, swOpenDocOptions_Silent Or swOpenDocOptions_ReadOnly, "", errors, warnings)


                        If docType = swDocDRAWING Then


                        End If


                    End If


                For the Convert Task it was a bit simpler:


                    If docType = swDocDRAWING Then

                        Dim vSheetNames As Variant

                        Set swDrawing = swModel






                Please note, Red Text is existing scripting in the Task, Green Text is what should be added.


                Hope this helps!