13 Replies Latest reply on Nov 10, 2016 8:14 AM by Shaun Jalbert

    Print tasks - Lookup Sheet Size to Print Landscape or Portrait

    Shaun Jalbert

      We've had our drawing templates in a "Landscape" format (34"L x 22"H) as long as we've been using SolidWorks.

      We've also been using Print Tasks and our API was controlled with a tidbit of code that forced the task to use the "Landscape" format.

      Below is a snippit of that code.

       

      '************* Start of Print task API Specs *************

      ' Sheet Size Option  (34"x22" = 132)

      swPageSetup.PrinterPaperSize = 132

       

      ' Printer Source Option (Adobe Distiller = 7)

      swPageSetup.PrinterPaperSource = 7

       

      ' Scale Percentage Option (100% = 100)

      swPageSetup.Scale2 = 100

       

      ' Scale To Fit Option (True, False)

      'swPageSetup.ScaleToFit = True

       

      ' Sheet Orientation Option  (1=Portrait,  '2 = Landscape)

      swPageSetup.Orientation = 2

      '************* End of Print task API Specs *************

       

      Because of some formatting issues within SolidWorks not liking rotated views, we're now using "Portrait" formats (22"L x 34"H) for some drawings.

      Because of this, our code is still forcing it to print in Landscape and only half of the drawing shows up.

      Is there a way to put in a lookup function for the sheet width and and if statement based on that function?

       

      Look up Sheet Width = x

      If x = 34", then swPageSetup.Orientation = 2

      If x = 22", then swPageSetup.Orientation = 1

        • Re: Print tasks - Lookup Sheet Size to Print Landscape or Portrait
          Wesley Prins

          i think you can find your answer in this example

          2016 SOLIDWORKS API Help - Get Name of Drawing Sheet Template Example (VBA)

          But maybe its easier for you to get the template name? and change the orientation with that information.

            • Re: Print tasks - Lookup Sheet Size to Print Landscape or Portrait
              Shaun Jalbert

              Thanks Wesley:

               

              I'm not trained to program in any language so I'm struggling to understand how to apply your example.

              So in my snippet above, I would replace This line:

              ' Sheet Orientation Option  (1=Portrait,  '2 = Landscape)

              swPageSetup.Orientation = 2

               

              With This?

              ' Sheet Orientation Option  (1=Portrait,  '2 = Landscape)

              Dim swDraw As SldWorks.DrawingDoc

              Dim swSheet As SldWorks.Sheet

              Dim vSheetProps As Variant

               

              Set swDraw = swModel

              If vSheetProps = "17" Then

                  swPageSetup.Orientation = 1

              Else

                  If vSheetProps = "34" Then

                        swPageSetup.Orientation = 2

              End If

               

              Am I on the right track?

              Does the vSheetProps return a value in inches, millimeters or light years? I've assume inches for the example above, hence the 17 and 34 values.

              The code needs to recognize a both a metric and imperial template.

                • Re: Print tasks - Lookup Sheet Size to Print Landscape or Portrait
                  Wesley Prins

                  'get active drawing and sheet

                   

                  Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    Set swDraw = swModel

                    Set swSheet = swDraw.GetCurrentSheet

                   

                  'Get props of drawing

                  Dim vSheetProps As Variant

                  vSheetProps = swSheet.GetProperties

                   

                  'nr 5 of props is width and 6 is heigh

                  'if width is greater then height landscape else portrait

                   

                  if vsheetprops(5) > vheetprops(6) then

                  swpagesetup.orientation = 2

                  else

                  swpagesetup.orientation = 1

                  End if

                   

                   

                   

                    • Re: Print tasks - Lookup Sheet Size to Print Landscape or Portrait
                      Shaun Jalbert

                      Hi Wesley Prins

                       

                      Hmmm.... I copied and pasted your script and proceeded to run it, but the print task failed.  No error code other than

                      what I pulled from our EPDM Task List Admin Screen...

                       

                      I made a few more declarations (as per the code hi-lighted in purple below), but I still can't get the script to run.

                      It just keeps coming up as an error. Any idea's?

                       

                       

                      ' Sheet Orientation Option  (1=Portrait,  '2 = Landscape)

                      ' swPageSetup.Orientation = 1

                       

                        Dim swApp As SldWorks.SldWorks

                        Dim swModel As SldWorks.ModelDoc2

                        Dim swDraw As SldWorks.DrawingDoc

                        Dim swSheet As SldWorks.Sheet

                        Dim vSheetProps As Variant

                       

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swDraw = swModel

                        Set swSheet = swDraw.GetCurrentSheet

                       

                        'Get props of drawing

                        vSheetProps = swSheet.GetProperties

                       

                        'nr 5 of props is width and 6 is height

                        'if width is greater then height landscape else portrait

                        if vsheetprops(5) > vheetprops(6) then

                            swPageSetup.Orientation = 1

                            else

                            swPageSetup.Orientation = 2

                        End if

                       

                      Below is the full print task script in case this helps. I've also attached in a text document in case the script below doesn't paste in correctly.

                      Perhaps there's a conflict with another declaration.

                       

                       

                      'Enterprise PDM 2014 - Default Print Task Script
                      'SPR Updates:
                      '<None>
                      '-----------------------------------------------
                      
                      
                      
                      Dim swApp As Object
                      Dim swModel As SldWorks.ModelDoc2
                      Dim swExtension As SldWorks.ModelDocExtension
                      Dim swPageSetup As SldWorks.PageSetup
                      Dim swDocSpecification As SldWorks.DocumentSpecification
                      Dim errors As Long
                      Dim warnings As Long
                      
                      
                      #If VBA7 Then
                          Private Declare PtrSafe Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
                          Private Declare PtrSafe Function PathIsRelative Lib "shlwapi.dll" Alias "PathIsRelativeA" (ByVal pszPath As String) As Long
                      #Else
                          Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
                          Private Declare Function PathIsRelative Lib "shlwapi.dll" Alias "PathIsRelativeA" (ByVal pszPath As String) As Long
                      #End If
                      
                      
                      Function PathAppend(path, more) As String
                          If Not Right(path, 1) = "\" Then
                              path = path & "\"
                          End If
                          If Left(more, 1) = "\" Then
                              more = Mid(more, 2)
                          End If
                          PathAppend = path & more
                      End Function
                      
                      Sub Log(message)
                          ' Determine error log output path
                          errorLogFolder = "[ErrorLogPath]"
                          
                          ' Trim \ from the start
                          If Left(errorLogFolder, 1) = "\" Then
                              errorLogFolder = Mid(errorLogFolder, 2)
                          End If
                      
                          ' Build full root
                          If PathIsRelative(errorLogFolder) = 1 Then
                              errorLogPath = PathAppend("<VaultPath>", errorLogFolder)
                          Else
                              errorLogPath = errorLogFolder
                          End If
                          
                          ' Create directory if not exists
                          SHCreateDirectoryEx ByVal 0&, errorLogPath, ByVal 0&
                          errorLogPath = PathAppend(errorLogPath, "<TaskInstanceGuid>.log")
                      
                          ' Write error to output file
                          Open errorLogPath For Append As #1
                          Print #1, message
                          Close #1
                      End Sub
                      
                      Function GetPrinter(paperSize)
                          If [__printer_prseltype] = 2 Then
                              Select Case paperSize
                                  Case 0
                                      GetPrinter = "[__printer_papsize_A]"
                                  Case 1
                                      GetPrinter = "[__printer_papsize_A]"
                                  Case 2
                                      GetPrinter = "[__printer_papsize_B]"
                                  Case 3
                                      GetPrinter = "[__printer_papsize_C]"
                                  Case 4
                                      GetPrinter = "[__printer_papsize_D]"
                                  Case 5
                                      GetPrinter = "[__printer_papsize_E]"
                                  Case 6
                                      GetPrinter = "[__printer_papsize_A0]"
                                  Case 7
                                      GetPrinter = "[__printer_papsize_A1]"
                                  Case 8
                                      GetPrinter = "[__printer_papsize_A2]"
                                  Case 9
                                      GetPrinter = "[__printer_papsize_A3]"
                                  Case 10
                                      GetPrinter = "[__printer_papsize_A4]"
                                  Case 11
                                      GetPrinter = "[__printer_papsize_A4]"
                                  Case 12
                                      GetPrinter = "[__printer_papsize_DEF]"
                              End Select
                          Else
                              GetPrinter = "[__printer_prname]"
                          End If
                      End Function
                      
                      Function GetPaperSize(paperSize)
                          Select Case paperSize
                              Case 0, 1
                                  paperSizeNum = 256 + 6 'DMPAPER_ASHEET     DMPAPER_USER+6
                              Case 2
                                  paperSizeNum = 256 + 7 'DMPAPER_BSHEET     DMPAPER_USER+7
                              Case 3
                                  paperSizeNum = 24 'DMPAPER_CSHEET
                              Case 4
                                  paperSizeNum = 173
                              Case 5
                                  paperSizeNum = 26 'DMPAPER_ESHEET
                              Case 6
                                  paperSizeNum = 256 + 4 'DMPAPER_A0  DMPAPER_USER+4
                              Case 7
                                  paperSizeNum = 256 + 5 'DMPAPER_A1  DMPAPER_USER+5
                              Case 8
                                  paperSizeNum = 66 'DMPAPER_A2
                              Case 9
                                  paperSizeNum = 8 'DMPAPER_A3
                              Case 10
                                  paperSizeNum = 9 'DMPAPER_A4
                              Case 11
                                  paperSizeNum = 77 'DMPAPER_A4_ROTATED
                              Case Else
                                  paperSizeNum = 9 'DMPAPER_A4
                          End Select
                          GetPaperSize = paperSizeNum
                      End Function
                      
                      Function bIsSupportedExtension(oExtension) As Boolean
                          
                          oExtension = LCase(oExtension)
                          
                          If oExtension = "prt" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "asm" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "drw" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "dxf" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "dwg" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "psd" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "ai" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "lfp" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "sldlfp" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "prtdot" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "asmdot" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "drwdot" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "x_t" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "x_b" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "xmt_txt" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "xmt_bin" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "igs" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "iges" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "step" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "stp" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "sat" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "vda" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "wrl" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "stl" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "cgr" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "wrl" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "xpr" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "xas" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "ipt" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "iam" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "par" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "psm" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "ckd" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "emn" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "brd" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "bdf" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "idb" Then
                             bIsSupportedExtension = True
                          ElseIf oExtension = "3dm" Then
                             bIsSupportedExtension = True
                          Else
                              bIsSupportedExtension = False
                          End If
                             
                      End Function
                      
                      Sub main()
                      
                          On Error GoTo Fail:
                      
                          Dim docFileName As String
                          Dim docCfgOrSheet As String
                          Dim sNumCopies As String
                          
                          docFileName = "<Filepath>"
                          docCfgOrSheet = "<SelCfgOrSheet>"
                          sNumCopies = "[__printer_copycount]"
                          
                          Dim iNumCopies As Integer
                          iNumCopies = 1
                          If (Len(sNumCopies) > 0) Then
                              iNumCopies = CInt(sNumCopies)
                          End If
                          
                          ' Get SW interface object
                          Set swApp = Application.SldWorks
                      swApp.visible =true
                          
                          ' Determine type of SolidWorks file based on file extension
                          If LCase(Right(docFileName, 7)) = ".sldprt" Or LCase(Right(docFileName, 4)) = ".prt" Then
                              docType = swDocPART
                          ElseIf LCase(Right(docFileName, 7)) = ".sldasm" Or LCase(Right(docFileName, 4)) = ".asm" Then
                              docType = swDocASSEMBLY
                          ElseIf LCase(Right(docFileName, 7)) = ".slddrw" Or LCase(Right(docFileName, 4)) = ".drw" Then
                              docType = swDocDRAWING
                          Else
                              docType = swDocNONE
                              If bIsSupportedExtension(Mid(docFileName, InStrRev(docFileName, ".") + 1)) = False Then
                                   Log "The file extension '" & Mid(docFileName, InStrRev(docFileName, ".") + 1) & "' is not supported."
                                   Exit Sub
                              End If
                          End If
                          
                          ' Open document
                          If docType = swDocNONE Then
                              Set swModel = swApp.LoadFile4(docFileName, "", Nothing, errors)
                      
                      '***********TEST START***********************
                      'swModel.ForceRebuild3 (false)
                      '***********TEST END***********************
                          Else
                              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
                      '***********TEST START***********************
                      swModel.ForceRebuild3 (false)
                      '***********TEST END***********************
                              'Set swModel = swApp.OpenDoc6(docFileName, docType, swOpenDocOptions_Silent Or swOpenDocOptions_ReadOnly, "", errors, warnings)
                          End If
                      
                          ' Load failed?
                          If swModel Is Nothing Then
                              Log "Method call ModelDoc2::OpenDoc6 for document '" & docFileName & "' failed. Error code " & errors & " returned."
                              Exit Sub
                          End If
                      
                          'If configuration is specified, activate it for part and assembly
                          If Len(docCfgOrSheet) > 0 Then
                              If docType = swDocPART Or docType = swDocASSEMBLY Then
                                  swModel.ShowConfiguration docCfgOrSheet
                      '***********TEST START***********************
                      swModel.ForceRebuild3 (false)
                      '***********TEST END***********************
                              End If
                          End If
                          
                          Set swPageSetup = swModel.PageSetup
                          
                          ' Printer
                          Printer = GetPrinter([__printer_defpapsize])
                          
                          If Len(Printer) = 0 Then
                              If Len(docCfgOrSheet) > 0 Then
                                  If docType = swDocDRAWING Then
                                      Log "Could not print document '" & docFileName & "' and sheet '" & docCfgOrSheet & "'. No printer was selected."
                                  Else
                                      Log "Could not print document '" & docFileName & "' and configuration '" & docCfgOrSheet & "'. No printer was selected."
                                  End If
                              Else
                                  Log "Could not print document '" & docFileName & "'. No printer was selected."
                              End If
                              Exit Sub
                          End If
                      
                      '************* Start Shaun Jalbert Edit *************
                      
                      ' Sheet Size Option  (34"x17" = 132)
                      swPageSetup.PrinterPaperSize = 132
                      
                      ' Printer Source Option (Adobe Distiller = 7)
                      swPageSetup.PrinterPaperSource = 7
                      
                      ' Scale Percentage Option (100% = 100)
                      swPageSetup.Scale2 = 100
                      
                      ' Scale To Fit Option (True, False)
                      'swPageSetup.ScaleToFit = True
                      
                      ' Sheet Orientation Option  (1=Portrait,  '2 = Landscape)
                      ' swPageSetup.Orientation = 1
                      
                         Dim swApp As SldWorks.SldWorks
                         Dim swModel As SldWorks.ModelDoc2
                         Dim swDraw As SldWorks.DrawingDoc
                         Dim swSheet As SldWorks.Sheet
                         Dim vSheetProps As Variant
                      
                         Set swApp = Application.SldWorks
                         Set swModel = swApp.ActiveDoc
                         Set swDraw = swModel
                         Set swSheet = swDraw.GetCurrentSheet
                      
                         'Get props of drawing
                         vSheetProps = swSheet.GetProperties
                      
                         'nr 5 of props is width and 6 is height
                         'if width is greater then height landscape else portrait
                         if vsheetprops(5) > vheetprops(6) then
                            swPageSetup.Orientation = 1
                            else
                            swPageSetup.Orientation = 2
                         End if
                      
                      '************* Finish Shaun Jalbert Edit *************
                      
                      'Shaun commented out to force Sheet Orientation Option
                          ' Paper size and orientation
                          'If Not ([__printer_usedrwnfo] And docType = swDocDRAWING) Then
                          '    swPageSetup.PrinterPaperSize = GetPaperSize( [__printer_defpapsize] )
                          '    swPageSetup.Orientation = [__printer_deforientation] + 1
                          'End If
                      
                          ' Margins/Headers
                          If [__printer_margintype] = 0 Then
                              swApp.SetUserPreferenceToggle swPageSetupPrinterUsePrinterMargin, True
                          Else
                              swApp.SetUserPreferenceToggle swPageSetupPrinterUsePrinterMargin, False
                              ' Convert m => mm
                              swApp.SetUserPreferenceDoubleValue swPageSetupPrinterTopMargin, [__printer_margintop] / 1000
                              swApp.SetUserPreferenceDoubleValue swPageSetupPrinterBottomMargin, [__printer_marginbottom] / 1000
                              swApp.SetUserPreferenceDoubleValue swPageSetupPrinterLeftMargin, [__printer_marginleft] / 1000
                              swApp.SetUserPreferenceDoubleValue swPageSetupPrinterRightMargin, [__printer_marginright] / 1000
                          End If
                          
                          If [__printer_headertype] = 2 Then
                              swPageSetup.SetHeader "[__printer_headerleft]", "[__printer_headercenter]", "[__printer_headerright]"
                          End If
                          
                          If [__printer_footertype] = 2 Then
                              swPageSetup.SetFooter "[__printer_footerleft]", "[__printer_footercenter]", "[__printer_footerright]"
                          End If
                          
                          
                          Set swExtension = swModel.Extension
                          
                          Dim vPrintSheets As Variant
                      
                          'If sheet is specified, find out its page number and add it to the array of sheets to be printed.
                          If Len(docCfgOrSheet) > 0 And docType = swDocDRAWING Then
                              Dim vSheetNames As Variant
                              Dim nPrintSheets(0) As Long
                              Dim swDraw As SldWorks.DrawingDoc
                              Dim swSheet As SldWorks.Sheet
                              Set swDraw = swModel
                              vSheetNames = swDraw.GetSheetNames
                              For i = 0 To UBound(vSheetNames)
                                  If LCase(vSheetNames(i)) = LCase(docCfgOrSheet) Then
                                      nPrintSheets(0) = i + 1
                                      vPrintSheets = nPrintSheets
                                      Exit For
                                  End If
                              Next i
                          End If
                          
                         'set the printer
                          swModel.Printer = Printer
                          
                          'check that the printer is valid
                          If swModel.Printer <> Printer Then
                              Log "The printer '" & Printer & "' for the Document  '" & docFileName & "' is not a valid printer."
                              Exit Sub
                          End If
                          
                          swExtension.UsePageSetup = swPageSetupInUse_Document
                      
                          ' Clear error messages
                          Dim vMsgs As Variant
                          Dim vMsgIDs As Variant
                          Dim vMsgTypes As Variant
                          
                          swApp.GetErrorMessages vMsgs, vMsgIDs, vMsgTypes
                          vMsgs = Empty
                      
                          ' Print file
                          swExtension.PrintOut2 vPrintSheets, iNumCopies, False, Printer, ""
                      
                          ' Check for errors
                          swApp.GetErrorMessages vMsgs, vMsgIDs, vMsgTypes
                          
                          If Not IsEmpty(vMsgs) Then
                              msg = "Could not print file '" & docFileName & "'. The following errors occured: " & vbCrLf
                              For i = 0 To UBound(vMsgs)
                                  msg = msg & vMsgs(i) & vbCrLf
                              Next i
                              Log msg
                          End If
                      
                          ' Close document
                          swApp.QuitDoc swModel.GetTitle
                      
                          Exit Sub
                             
                      Fail:
                          Log "Error while printing file '" & docFileName & "': " & vbCrLf & _
                              "An unexpected error occurred while executing the generated script. Script syntax error?" & vbCrLf & _
                              "Error number: " & Err.Number & vbCrLf & _
                              "Error description: '" & Err.Description & "'" & vbCrLf
                          
                      End Sub