5 Replies Latest reply on Mar 7, 2018 3:44 PM by Grant Kirkland

    PDM print task not scaling drawings to fit paper

    Ryan Sullivan

      I have been testing the print function within PDM professional tasks and it's going to be awesome but...  I can't get all of the drawings (size B) to scale to fit a size A sheet.  Any one have this happen to them?  I have not seen any settings with in the task set up to allow a scale to fit option but that seems like a great idea.

       

      Thanks,

      Ryan

        • Re: PDM print task not scaling drawings to fit paper
          Dusty Peterson

          I'm having a similar issue.  When I print to an A size it doesn't zoom to fit.  So I end up with a drawing only on a 1/4 of my page.

          • Re: PDM print task not scaling drawings to fit paper
            Daniel Egebjerg

            Hey Ryan.

             

            Can you post the Task code, then i´d gladly look at it

              • Re: PDM print task not scaling drawings to fit paper
                Ryan Sullivan

                'SOLIDWORKS PDM 2017 – 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 'A

                                GetPrinter = "[__printer_papsize_A]"

                            Case 1 'A

                                GetPrinter = "[__printer_papsize_A]"

                            Case 2 'B

                                GetPrinter = "[__printer_papsize_B]"

                            Case 3 'C

                                GetPrinter = "[__printer_papsize_C]"

                            Case 4 'D

                                GetPrinter = "[__printer_papsize_D]"

                            Case 5 'E

                                GetPrinter = "[__printer_papsize_E]"

                            Case 6 'A0

                                GetPrinter = "[__printer_papsize_A0]"

                            Case 7 'A1

                                GetPrinter = "[__printer_papsize_A1]"

                            Case 8 'A2

                                GetPrinter = "[__printer_papsize_A2]"

                            Case 9 'A3

                                GetPrinter = "[__printer_papsize_A3]"

                            Case 10 'A4

                                GetPrinter = "[__printer_papsize_A4]"

                            Case 11 'A4

                                GetPrinter = "[__printer_papsize_A4]"

                            Case 12 'DEF

                                GetPrinter = "[__printer_papsize_DEF]"

                        End Select

                    Else

                        'Printer defined in Printer page "Always use the following printer name"

                        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 = 25 'DMPAPER_DSHEET

                        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

                   

                    ' 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)

                    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

                   

                        '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

                        End If

                    End If

                   

                    Set swPageSetup = swModel.PageSetup

                   

                    'Check if "Use paper size and orientation in drawing sheet" is disabled, if so use specified values in page

                    If [__printer_usedrwnfo] = False And docType = swDocDRAWING Then

                        ' Printer (Default size in Paper and size page)

                        printer = GetPrinter( [__printer_defpapsize] )

                       

                        If Len(printer) = 0 Then

                            If Len(docCfgOrSheet) > 0 Then

                                Log "Could not print document '" & docFileName & "' and sheet '" & docCfgOrSheet & "'. No printer was selected."          

                            Else

                                Log "Could not print document '" & docFileName & "'. No printer was selected."

                            End If

                            Exit Sub

                        End If

                    Else

                    'Not a drawing - use default printer instead

                    If Not docType = swDocDRAWING Then

                        'Printer (Default size in Paper and size page)

                        printer = "[__printer_papsize_DEF]"

                        'If printer name is empty at this point - pick default "Always use the following printer name" defined in Printer page

                        If Printer = "" Then

                            printer = "[__printer_prname]"

                        End If

                       

                        If Len(printer) = 0 Then

                            If Len(docCfgOrSheet) > 0 Then

                                Log "Could not print document '" & docFileName & "' and configuration '" & docCfgOrSheet & "'. No printer was selected."          

                            Else

                                Log "Could not print document '" & docFileName & "'. No printer was selected."

                            End If

                            Exit Sub

                        End If

                    End If

                    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 vSheetProps 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

                        'Check if "Use paper size and orientation in drawing sheet" is enabled, if so the sheet properties need to be retrieved

                        If [__printer_usedrwnfo] = True Then

                        'Get sheet specific properties for the found sheet

                        Set swSheet = swDraw.Sheet(vSheetNames(i))

                        vSheetProps = swSheet.GetProperties

                       

                                        'Check if "Select printer based on paper size" is enabled - if so find sheet size in drawing

                                If ([__printer_prseltype] = 2) and ([__printer_usedrwnfo] = True) Then

                                    'Returned size value from API is different from predefined parameters in script for A formats. Select appropriate printer.

                                    Select Case vSheetProps(0)

                                        Case 6, 7 'A4

                                            Printer = GetPrinter(10)

                                        Case 8 'A3

                                            Printer = GetPrinter(9)

                                        Case 9 'A2

                                            Printer = GetPrinter(8)

                                        Case 10 'A1

                                            Printer = GetPrinter(7)

                                        Case 11 'A0

                                            Printer = GetPrinter(6)

                                        Case Else

                                            Printer = GetPrinter(vSheetProps(0))

                                    End Select

                                   

                                    'If printer name is empty at this point - pick default "Always use the following printer name" defined in Printer page

                                    If Printer = "" Then

                                        printer = "[__printer_prname]"

                                    End If

                                Else 'Use the default or selected printer instead

                                                Printer = "[__printer_prname]"

                                End If

                        End If

                 

                 

                                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

                 

                 

                   

                    If Not ([__printer_usedrwnfo] And docType = swDocDRAWING) Then

                    'Find paper size and orientation as defined by user

                        swPageSetup.PrinterPaperSize = GetPaperSize( [__printer_defpapsize] )   

                        swPageSetup.Orientation = [__printer_deforientation] + 1

                    Else

                    'Find paper size as defined in the drawing

                        Select Case vSheetProps(0)

                                        Case 6,7 'A4

                                            swPageSetup.PrinterPaperSize = GetPaperSize(10)

                                        'Case 7 'A4 rotated

                                        '    swPageSetup.PrinterPaperSize = GetPaperSize(11)

                                        Case 8 'A3

                                            swPageSetup.PrinterPaperSize = GetPaperSize(9)

                                        Case 9 'A2

                                            swPageSetup.PrinterPaperSize = GetPaperSize(8)

                                        Case 10 'A1

                                            swPageSetup.PrinterPaperSize = GetPaperSize(7)

                                        Case 11 'A0

                                            swPageSetup.PrinterPaperSize = GetPaperSize(6)

                                        Case Else

                                            swPageSetup.PrinterPaperSize = GetPaperSize(vSheetProps(0))

                                    End Select

                   

                        'Decide sheet orientation. If height is bigger than width, use landscape

                            If vSheetProps(6) > vSheetProps(5) Then

                                swPageSetup.Orientation = 1

                            Else

                                swPageSetup.Orientation = 2

                            End If

                 

                 

                        'Switch orientation if using PDF printer

                        'If you experience some troubles with page orientation on PDF printers, please uncomment following 7 lines

                        'If InStrRev(Printer, "PDF") > 0 Then

                        '    If vSheetProps(6) > vSheetProps(5) Then

                        '        swPageSetup.Orientation = 2

                        '    Else

                        '        swPageSetup.Orientation = 1

                        '    End If

                        'End If

                           

                   End If

                 

                 

                    ' 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

              • Re: PDM print task not scaling drawings to fit paper
                Dwayne Parrott

                FROM THE ONLINE HELP, HOPE THIS HELPS!

                YOU CAN CONFIGURE SOME OF THE PROPERTIES OF THE PRINTER.....

                 

                Configuring the Print Task

                 

                You configure the Print task provided by the SOLIDWORKS Task Add-in to specify the computer, printer, and print settings to use.

                You can configure the print properties so that the task runs silently when executed or so that a user who initiates the task can select all or some of the printer properties.

                To navigate in the dialog box, click a feature in the left pane to configure it or click Back and Next.

                2014 SOLIDWORKS PDM Help - Configuring the Print Task