2 Replies Latest reply on Mar 30, 2016 1:47 PM by Daniel Egebjerg

    Batch print of drawings in a specific state Issue

    Daniel Egebjerg

      Hi Boys and Girls....

      I´ve been trying to make a print task, that only print files in the Workflow state "produktion" but i cannot seem to get it to work... Before i added the state part of the script it worked ( text marked in RED), but printed all referenced files, which i wanted to counter (Our production people, does not need a drawing of a purchased item, and i am to lazy to sort a stack of drawings:))

       

       

      Any ideas how to solve the problem??

       

      (Please note that i am fairly new in making scripts...)

       

      //Daniel

       

      '-----------------------------------------------

      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 FileState As IEdmFile5.CurrentState

      Dim errors As Long

      Dim warnings As Long

      Dim Width As Double

      Dim Height As Double

       

       

      Dim DrwPaperSize As Integer

      Dim PrintPaperSize As String

       

       

      Dim A0PrintSize As String

      Dim A0ScaleToFit As String

      Dim A0Printer As String

      Dim A1PrintSize As String

      Dim A1ScaleToFit As String

      Dim A1Printer As String

      Dim A2PrintSize As String

      Dim A2ScaleToFit As String

      Dim A2Printer As String

      Dim A3PrintSize As String

      Dim A3ScaleToFit As String

      Dim A3Printer As String

      Dim A4PrintSize As String

      Dim A4ScaleToFit As String

      Dim A4Printer As String

      Dim OtherPrintSize As String

      Dim OtherScaleToFit As String

      Dim OtherPrinter As String

       

       

      Dim FileWorkFlowStateName As String

      Dim ExcludeSheet As String

       

      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)

          Select Case paperSize

              Case 11

        SheetPrinter = A0Printer

              Case 10

                  SheetPrinter = A1Printer

              Case 9

                  SheetPrinter = A2Printer

              Case 8

                  SheetPrinter = A3Printer

              Case 6

                  SheetPrinter = A4Printer

              Case 7

                  SheetPrinter = A4Printer

        Case Else

        SheetPrinter = OtherPrinter

        End Select

         GetPrinter = SheetPrinter

        End Function

       

       

        Function GetPrintPaperSize(paperSize)

          Select Case paperSize

              Case 11

        ConvPaperSize = A0PrintSize

              Case 10

                  ConvPaperSize = A1PrintSize

              Case 9

                  ConvPaperSize = A2PrintSize

              Case 8

                  ConvPaperSize = A3PrintSize

              Case 6

                  ConvPaperSize = A4PrintSize

              Case 7

                  ConvPaperSize = A4PrintSize

        Case Else

        ConvPaperSize = OtherPrintSize

        End Select

         GetPrintPaperSize = ConvPaperSize

        End Function

       

      Function GetPaperSize(NewPaperSize)

          Select Case NewPaperSize

              Case "A0"

                  paperSizeNum = 256 + 4 'DMPAPER_A0  DMPAPER_USER+4

              Case A1

                  paperSizeNum = 256 + 5 'DMPAPER_A1  DMPAPER_USER+5

              Case "A2"

                  paperSizeNum = 66 'DMPAPER_A2

              Case "A3"

                  paperSizeNum = 8 'DMPAPER_A3

              Case "A4"

                  paperSizeNum = 9 'DMPAPER_A4

              Case Else

                  paperSizeNum = 9 'DMPAPER_A4

          End Select

          GetPaperSize = paperSizeNum

      End Function

       

       

      Function GetPaperOrietation(paperSize) ' 1 = Portrait 2 = Landscape

          Select Case paperSize

              Case 11

                  paperorientation = 2 'PAPER_A0

              Case 10

                  paperorientation = 2 'PAPER_A1

              Case 9

                  paperorientation = 2 'PAPER_A2

              Case 8

                  paperorientation = 2 'PAPER_A3

              Case 6

                  paperorientation = 2 'PAPER_A4L

              Case 7

                  paperorientation = 1 'PAPER_A4P

              Case Else

                  paperorientation = 2 'PAPER_A4

          End Select

          GetPaperOrietation = paperorientation

      End Function

       

       

      Function GetPrintScale(paperSize) ' 1= Portrait = Landscape

          Select Case paperSize

              Case 11

                  PrintScaleToFit = A0ScaleToFit 'PAPER_A0

              Case 10

                  PrintScaleToFit = A1ScaleToFit 'PAPER_A1

              Case 9

                  PrintScaleToFit = A2ScaleToFit 'PAPER_A2

              Case 8

                  PrintScaleToFit = A3ScaleToFit 'PAPER_A3

              Case 6

                  PrintScaleToFit = A4ScaleToFit 'PAPER_A4P

              Case 7

                  PrintScaleToFit = A4ScaleToFit 'PAPER_A4L

              Case Else

                  PrintScaleToFit = OtherScaleToFit 'PAPER_A4

          End Select

          GetPrintScale = PrintScaleToFit

      End Function

       

       

      Sub main()

       

       

      A0PrintSize = "A0"

      A0ScaleToFit = False ' True

      A0Printer = "\\sbs2011\HP Designjet T770 44in HPGL2"

       

       

      A1PrintSize = "A1"

      A1ScaleToFit = False ' True

      A1Printer = "\\sbs2011\HP Designjet T770 44in HPGL2"

       

       

      A2PrintSize = "A2"

      A2ScaleToFit = False ' True

      A2Printer = "\\sbs2011\HP Designjet T770 44in HPGL2"

       

       

      A3PrintSize = "A3"

      A3ScaleToFit = False 'True False

      A3Printer = "\\sbs2011\Hvid Papir G36C-1 på sbs2011 "

       

       

      A4PrintSize = "A4"

      A4ScaleToFit = True ' False

      A4Printer = "\\sbs2011\Hvid Papir G36C-1 på sbs2011"

       

       

      OtherPrintSize = "A4"

      OtherScaleToFit = True ' False

      OtherPrinter = "\\sbs2011\Hvid Papir G36C-1 på sbs2011"

       

       

       

       

      ExcludeSheet = "dxf"

      FileWorkFlowStateName = "Produktion"

       

       

          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 and the files Workflow state

          If LCase(Right(docFileName, 7)) = ".slddrw" Or LCase(Right(docFileName, 4)) = ".drw" And Filestate = FileWorkFlowStateName Then

        docType = swDocDRAWING

          Else

              docType = swDocNONE

          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

       

       

       

          Set swPageSetup = swModel.PageSetup

          swPageSetup.Scale2 = 100

       

       

          ' 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

       

          ' Clear error messages

          Dim vMsgs As Variant

          Dim vMsgIDs As Variant

          Dim vMsgTypes As Variant

       

          swApp.GetErrorMessages vMsgs, vMsgIDs, vMsgTypes

          vMsgs = Empty

       

       

       

       

          Dim vPrintSheets As Variant

       

       

          'Print one sheet at at a time.

          If 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

        If LCase(vSheetNames(i)) <> LCase(ExcludeSheet) Then

        ' MsgBox "  SheetName[" & i & "] = " & vSheetNames(i)

        DrwPaperSize = swDraw.Sheet(vSheetNames(i)).GetSize(Width, Height)

        'set the printer

        printer = GetPrinter(DrwPaperSize)

        swModel.printer = printer

        PrintPaperSize = GetPrintPaperSize(DrwPaperSize)

        swPageSetup.PrinterPaperSize = GetPaperSize(PrintPaperSize)

        swPageSetup.Orientation = GetPaperOrietation(DrwPaperSize)

        swPageSetup.ScaleToFit = GetPrintScale(DrwPaperSize)

        'msgbox swPageSetup.Scale2 & " - " & swPageSetup.ScaleToFit & " - " & swPageSetup.PrinterPaperSize & " - " & swModel.printer

        'msgbox swPageSetup.Scale2 & " - " & swPageSetup.ScaleToFit & " - " & swPageSetup.PrinterPaperSize & " - " & swModel.printer

        ' Print sheet

        nPrintSheets(0) = 0

        nPrintSheets(0) = i + 1

        vPrintSheets = nPrintSheets

        swExtension.PrintOut2 vPrintSheets, iNumCopies, False, printer, ""

        Else

        End If

        Else

        End If

              Next i

          End If

       

          ' 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