10 Replies Latest reply on Mar 23, 2017 4:54 PM by Christian Chu

    Changing which layers print via Macro

    Peter Kennedy

      We have a large set of drawings that we outsource for manufacturing however we don't want to share all the information on the drawing. I need two versions of the drawing an internal and external copy, the internal has all the information, external is only partial.


      If there a way to make a macro to print to pdf, but it will turn off the layer which has the additional internal information? Also since these drawings have a minimum of 2 sheets, up to 4 with the last always being the dxf of the part can I have the system always save the last sheet as a dxf and the remaining sheets into one PDF file?


      I just don't know where to start I tried to record a macro and go from there however it was unsuccessful.



        • Re: Changing which layers print via Macro
          Peter Brinkhuis

          The real art of programming is deconstructing your problem in pieces so small that a computer can execute them. Start small and don't try to everything at once.


          It this case that would mean:

          1. Connect to Solidworks (check any macro on this forum for an example, the macro recorder result isn't great)
          2. Get the current document (see 1)
          3. Make sure a document is open (check to see if the model from step 2 isn't null), otherwise exit
          4. Make sure the document is a drawing, otherwise exit
          5. Get the number of sheets (n) of this drawing
          6. Get the names of sheets in an array
          7. Loop through the sheets until page n, so:
            1. Get the name of the current sheet
            2. Activate the sheet using the name
            3. Make the right layers visible (just checked, this needs to be done per page)
          8. Save sheets 1 to n-1 to PDF
          9. Save sheet n to DXF


          When you want to deal with multiple files at once, you can either write you own software to do that or use existing software like BatchProcess of #Task. I don't think #task is very user friendly, but it has some smart features to prevent timeouts and other cock-ups.


          It seems that every single company has trouble printing documents the right way. Does nobody know any software that can do this properly? Otherwise I'm gonna build it myself soon.

          • Re: Changing which layers print via Macro
            Jason Kerns

            The SOLIDWORKS API Help section looks like it has an example written that you can reference


            This example shows how to get the layers in a drawing document.



            Option Explicit

            Public Enum swLineStyles_e

                swLineCONTINUOUS = 0

                swLineHIDDEN = 1

                swLinePHANTOM = 2

                swLineCHAIN = 3

                swLineCENTER = 4

                swLineSTITCH = 5

                swLineCHAINTHICK = 6

                swLineDEFAULT = 7

            End Enum

            Public Enum swLineWeights_e

                swLW_NONE = -1

                swLW_THIN = 0

                swLW_NORMAL = 1

                swLW_THICK = 2

                swLW_THICK2 = 3

                swLW_THICK3 = 4

                swLW_THICK4 = 5

                swLW_THICK5 = 6

                swLW_THICK6 = 7

                swLW_NUMBER = 8

                swLW_LAYER = 9

            End Enum

            Sub main()

                Dim swApp                       As SldWorks.SldWorks

                Dim swModel                     As SldWorks.ModelDoc2

                Dim swLayerMgr                  As SldWorks.LayerMgr

                Dim vLayerArr                   As Variant

                Dim vLayer                      As Variant

                Dim swLayer                     As SldWorks.Layer


                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swLayerMgr = swModel.GetLayerManager


                Debug.Print "File = " & swModel.GetPathName

                Debug.Print "  Current Layer = " & swLayerMgr.GetCurrentLayer

                Debug.Print ""


                vLayerArr = swLayerMgr.GetLayerList

                For Each vLayer In vLayerArr

                    Set swLayer = swLayerMgr.GetLayer(vLayer)


                    Debug.Print "  " & swLayer.Name

                    Debug.Print "    Color          = " & swLayer.Color

                    Debug.Print "    Description    = " & swLayer.Description

                    Debug.Print "    GetId          = " & swLayer.GetId

                    Debug.Print "    Style          = " & swLayer.Style

                    Debug.Print "    Visible        = " & swLayer.Visible

                    Debug.Print "    Width          = " & swLayer.Width


            End Sub