2 Replies Latest reply on Apr 10, 2018 12:26 PM by Rodney Michels

    Draftsight API Printing

    Rodney Michels

      I have generally liked the API help documents DraftSight has published.  One thing I cannot seem to figure out though, how to send a document to the printer!  I can "Get" and "Set" Printer options.  Nothing I have read jumps out at me stating, "Sends document to Printer"!

       

      Any Help would be great!

       

      Rod

        • Re: Draftsight API Printing
          Michael Spens

          Hi Rodney,

          Try the PrintOut method of the PrintManager interface.

           

          Mike

            • Re: Draftsight API Printing
              Rodney Michels

              Thanks Mike!  I actually got past that finally, but struggling setting the correct options to print my drawing.

               

              Within Draftsight, the drawing is consider a "D" size print, so I need to print out on a 11 x 17 document.

               

              These are the settings I am trying to set, but have not been able to get the document printed out correctly.  I've attached my sample code.  I've tweaked it so much I'll probably need to start from scratch. 

               

               

               

               

              Sub GetSetPrintOptions(ByVal dsApp As Application, ByVal dsDoc As DraftSight.Interop.dsAutomation.Document)

                      Dim dsPrintMgr As PrintManager = Nothing

                      Dim printerName As String

                      'Dim dsVarPaperSizes As Object

                      Dim paperLength As Double

                      Dim paperWidth As Double

                      Dim rightLength As Double

                      Dim rightWidth As Double

                      Dim paperSize As String

                      Dim precision As Double

                      Dim xOffset As Double

                      Dim yOffset As Double

                      Dim printOrientation As dsPrintOrientation_e

                      Dim centerPrint As Boolean

                      Dim printQuality As Long

                      Dim scaleLineWeight As Boolean

                      Dim scaleToFit As Boolean

                      Dim styleTable As String

                      Dim userScale As Boolean

                      'Dim viewDisplayStyle As dsViewDisplayStyle_e

                      'Get PrintManager

               

               

                      dsPrintMgr = dsApp.GetPrintManager()

               

               

                      'Dim printArray() As String

                      'Dim i As Integer

                      'printArray = dsPrintMgr.GetAvailablePrinters()

               

               

               

               

                      If Not dsPrintMgr Is Nothing Then

                       

               

               

                          'Set the sheets to print

                          Dim sheets As New List(Of String)

                          'Dim sheets(0) As String

                          'sheets(0) = "layout1"

               

               

               

               

                          Dim dsSheet As Sheet

                          Dim sheetName As String

                          Dim dsVarSheets As Object

                          dsVarSheets = dsDoc.GetSheets2

               

               

                          'dsPrintMgr.SetSheets(dsVarSheets)

                          If IsArray(dsVarSheets) Then

                              For index = LBound(dsVarSheets) To UBound(dsVarSheets)

                                  dsSheet = dsVarSheets(index)

                                  sheetName = dsSheet.Name

                                  If Not sheetName = "Model" Then

                                      dsSheet.Activate()

               

               

                                      System.Windows.Forms.Application.DoEvents()

               

               

               

               

               

               

                                      'Get printer name

                                      printerName = "\\parshvprint01\655-Copier-B-SCAD"

                                      dsPrintMgr.Printer = printerName

               

               

               

               

               

               

                                      If dsPrintMgr.Printer = "" Then

                                          MsgBox("Failed to set IPrintManager.Printer property " & printerName & " value.")

                                      End If

               

               

               

               

                                      ''Get available paper sizes

                                      ''dsVarPaperSizes = dsPrintMgr.AvailablePaperSizes

                                      ''If IsArray(dsVarPaperSizes) And UBound(dsVarPaperSizes) = 0 Then

                                      ''    MsgBox("List of available paper sizes is empty for " & dsPrintMgr.Printer & " printer.")

                                      ''End If

               

               

                                      paperSize = "Tabloid"

                                      dsPrintMgr.PaperSize = paperSize

                                      If paperSize <> dsPrintMgr.PaperSize Then

                                          MsgBox("Failed to set IPrintManager.PaperSize property to " & paperSize & " value.")

                                      End If

               

               

               

               

               

               

                                      ' ''Get paper size

                                      ''rightLength = 134

                                      ''rightWidth = 87

                                      ''dsPrintMgr.GetPaperSize(paperLength, paperWidth)

                                      ' ''Verify paper length value

                                      ''precision = 0.000000001

                                      ''If Math.Abs(rightLength - paperLength) > precision Then

                                      ''    MsgBox("IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size.")

                                      ''End If

                                      ' ''Verify paper width value

                                      ''If Math.Abs(rightWidth - paperWidth) > precision Then

                                      ''    MsgBox("IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size.")

                                      ''End If

                                      ' ''Set paper size to "VGA_(640.00_x_480.00_Pixels)"

                                      ''paperSize = "VGA_(640.00_x_480.00_Pixels)"

                                      ''dsPrintMgr.paperSize = paperSize

                                      ''If paperSize <> dsPrintMgr.paperSize Then

                                      ''    MsgBox("Failed to set IPrintManager.PaperSize property to " & paperSize & " value.")

                                      ''End If

               

               

               

               

                                      ''Get paper size

                                      rightLength = 431.8

                                      rightWidth = 279.4

                                      dsPrintMgr.GetPaperSize(paperLength, paperWidth)

               

               

                                      dsPrintMgr.SetClosestValidPaperSize(rightLength, rightWidth)

               

               

               

               

                                      ' ''Verify paper length value

                                      'If Math.Abs(rightLength - paperLength) > precision Then

                                      '    MsgBox("IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size.")

                                      'End If

                                      ''Verify paper width value

                                      'If Math.Abs(rightWidth - paperWidth) > precision Then

                                      '    MsgBox("IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size.")

                                      'End If

               

               

                                      'Set Print margins

                                      Dim top As Double = 4.3180000000000014

                                      Dim bottom As Double = 4.3180000000000858

                                      Dim left As Double = 4.3180000000000014

                                      Dim right As Double = 4.3179999999999961

                                      dsPrintMgr.SetPrintMargins(top, bottom, left, right)

               

               

                                      'Get print margins

               

               

                                      dsPrintMgr.GetPrintMargins(top, bottom, left, right)

               

               

               

               

                                      'Set print offset

               

               

                                      xOffset = -0.000000000000028421709430404007

                                      yOffset = 2.3552973421817849

                                      dsPrintMgr.SetPrintOffset(xOffset, yOffset)

               

               

               

               

                                      'Get print margins

                                      Dim resultXOffset As Double

                                      Dim resultYOffset As Double

                                      dsPrintMgr.GetPrintOffset(resultXOffset, resultYOffset)

               

               

               

               

               

               

               

               

               

               

               

               

                                      ''Verify print offset values

                                      'If Math.Abs(resultXOffset - xOffset) > precision Then

                                      '    MsgBox("IPrintManager.GetPrintOffset method returns wrong XOffset value. It should be " & xOffset & ", but it is " & resultXOffset & ".")

                                      'End If

                                      'If Math.Abs(resultYOffset - yOffset) > precision Then

                                      '    MsgBox("IPrintManager.GetPrintOffset method returns wrong YOffset value. It should be " & yOffset & ", but it is " & resultYOffset & ".")

                                      'End If

               

               

               

               

                                      'Set print orientation

                                      printOrientation = dsPrintOrientation_e.dsPrintOrientation_Landscape

                                      dsPrintMgr.Orientation = printOrientation

                                      'Verify if print orientation was set

                                      If printOrientation <> dsPrintMgr.Orientation Then

                                          MsgBox("Failed to set IPrintManager.Orientation property. It should be " & printOrientation & ", but it is " & dsPrintMgr.Orientation & ".")

                                      End If

               

               

               

               

               

               

               

               

               

               

                                      ''sheets.Add(sheetName)

               

               

                                      ''dsPrintMgr.SetSheets(sheets)

               

               

               

               

                                      ''Verify the sheets to print

                                      ''Dim sheetObj As Object

                                      ''sheetObj = dsPrintMgr.GetSheets

                                      ''If IsNothing(sheetObj) Then

                                      ''    MsgBox("Failed to set sheets to print")

                                      ''End If

               

               

                                      'Set PrintOnCenter property

                                      centerPrint = True

                                      dsPrintMgr.PrintOnCenter = centerPrint

                                      If centerPrint <> dsPrintMgr.PrintOnCenter Then

                                          MsgBox("Failed to set IPrintManager.PrintOnCenter property to " & centerPrint)

                                      End If

               

               

               

               

                                      'Set print quality

                                      printQuality = 150

                                      dsPrintMgr.Quality = printQuality

               

               

               

               

                                      'Set ScaleLineWeight property

                                      scaleLineWeight = False

                                      dsPrintMgr.ScaleLineWeight = scaleLineWeight

                                      If scaleLineWeight <> dsPrintMgr.ScaleLineWeight Then

                                          MsgBox("Failed to set IPrintManager.ScaleLineWeight property.")

                                      End If

               

               

               

               

                                      'Set ScaleToFit property

                                      scaleToFit = False

                                      dsPrintMgr.ScaleToFit = scaleToFit

                                      If scaleToFit <> dsPrintMgr.ScaleToFit Then

                                          MsgBox("Failed to set IPrintManager.ScaleToFit property to " & scaleToFit)

                                      End If

               

               

               

               

                                      'Set StyleTable property

                                      styleTable = "monochrome.ctb"

                                      dsPrintMgr.StyleTable = styleTable

                                      If styleTable <> dsPrintMgr.StyleTable Then

                                          MsgBox("Failed to set IPrintManager.StyleTable property to " & styleTable)

                                      End If

               

               

               

               

                                      'Set UserScale property

                                      userScale = False

                                      dsPrintMgr.UserScale = userScale

               

               

               

               

                                      ''Set ViewDisplayStyle property

                                      'viewDisplayStyle = dsViewDisplayStyle_e.dsViewDisplayStyle_Rendered

                                      'dsPrintMgr.ViewDisplayStyle = viewDisplayStyle

                                      'If viewDisplayStyle <> dsPrintMgr.ViewDisplayStyle Then

                                      '    MsgBox("Failed to set IPrintManager.ViewDisplayStyle property to " & viewDisplayStyle)

                                      'End If

               

               

                                      'dsPrintMgr.SetStandardScale(DraftSight.Interop.dsAutomation.dsStandardPrintScale_e.dsStandardPrintScale_ScaleToFit)

                                      'dsPrintMgr.SetPrintRange(dsPreviewRange_e.dsPreviewRange_AllGeometry, "", True, 0, 0, 0, 0)

               

               

                                      System.Windows.Forms.Application.DoEvents()

               

               

                                      dsPrintMgr.PrintOut(1, dsDoc.GetPathName)

                                  End If

                              Next

                          End If

                        

               

               

               

               

                      Else

                          MsgBox("IDocument.GetPrintManager returns Nothing.")

                      End If

                  End Sub