From 32c8fdad3726cf9d1ff07f6e6045abe73cbdbe65 Mon Sep 17 00:00:00 2001
From: Maciej Suminski <maciej.suminski@cern.ch>
Date: Thu, 26 Mar 2015 18:23:17 +0100
Subject: [PATCH] Fix for printing non-consecutive layers or starting with
 layer > 1 in gerbview.

---
 gerbview/class_gbr_layout.h                   |  8 +++----
 .../dialogs/dialog_print_using_printer.cpp    | 24 +++++++++----------
 gerbview/draw_gerber_screen.cpp               | 21 ++++++++++++----
 gerbview/printout_control.cpp                 | 11 ++++-----
 4 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/gerbview/class_gbr_layout.h b/gerbview/class_gbr_layout.h
index 8388bf5b17..e5610ecd41 100644
--- a/gerbview/class_gbr_layout.h
+++ b/gerbview/class_gbr_layout.h
@@ -106,15 +106,15 @@ public:
      * @param aPrintBlackAndWhite = true to force black and white insdeat of color
      *        useful only to print/plot gebview layers
      */
-    void    Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
-                  GR_DRAWMODE aDrawMode, const wxPoint& aOffset,
-                  bool aPrintBlackAndWhite = false );
+    void Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
+               GR_DRAWMODE aDrawMode, const wxPoint& aOffset,
+               bool aPrintBlackAndWhite = false );
     /**
      * Function SetPrintableLayers
      * changes the list of printable layers
      * @param aLayerMask = The new bit-mask of printable layers
      */
-    void    SetPrintableLayers( const std::bitset <GERBER_DRAWLAYERS_COUNT>& aLayerMask  )
+    void SetPrintableLayers( const std::bitset <GERBER_DRAWLAYERS_COUNT>& aLayerMask  )
     {
         m_printLayersMask = aLayerMask;
     }
diff --git a/gerbview/dialogs/dialog_print_using_printer.cpp b/gerbview/dialogs/dialog_print_using_printer.cpp
index 2f4ff2ec8d..b2a17b7d42 100644
--- a/gerbview/dialogs/dialog_print_using_printer.cpp
+++ b/gerbview/dialogs/dialog_print_using_printer.cpp
@@ -78,8 +78,8 @@ private:
     void OnScaleSelectionClick( wxCommandEvent& event );
 
     void OnButtonCancelClick( wxCommandEvent& event ) { Close(); }
-    void SetPrintParameters( );
-    void InitValues( );
+    void SetPrintParameters();
+    void InitValues();
 
 public:
     bool IsMirrored() { return m_Print_Mirror->IsChecked(); }
@@ -151,8 +151,8 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
         s_pageSetupData = new wxPageSetupDialogData;
         // Set initial page margins.
         // Margins are already set in Gerbview, so we can use 0
-        s_pageSetupData->SetMarginTopLeft(wxPoint(0, 0));
-        s_pageSetupData->SetMarginBottomRight(wxPoint(0, 0));
+        s_pageSetupData->SetMarginTopLeft( wxPoint( 0, 0 ) );
+        s_pageSetupData->SetMarginBottomRight( wxPoint( 0, 0 ) );
     }
 
     s_Parameters.m_PageSetupData = s_pageSetupData;
@@ -175,7 +175,6 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
                                             wxGROW | wxLEFT | wxRIGHT | wxTOP );
     }
 
-
     // Read the scale adjust option
     int scale_idx = 4; // default selected scale = ScaleList[4] = 1.000
 
@@ -184,8 +183,8 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
         m_Config->Read( OPTKEY_PRINT_X_FINESCALE_ADJ, &s_Parameters.m_XScaleAdjust );
         m_Config->Read( OPTKEY_PRINT_Y_FINESCALE_ADJ, &s_Parameters.m_YScaleAdjust );
         m_Config->Read( OPTKEY_PRINT_SCALE, &scale_idx );
-        m_Config->Read( OPTKEY_PRINT_PAGE_FRAME, &s_Parameters.m_Print_Sheet_Ref, 1);
-        m_Config->Read( OPTKEY_PRINT_MONOCHROME_MODE, &s_Parameters.m_Print_Black_and_White, 1);
+        m_Config->Read( OPTKEY_PRINT_PAGE_FRAME, &s_Parameters.m_Print_Sheet_Ref, 1 );
+        m_Config->Read( OPTKEY_PRINT_MONOCHROME_MODE, &s_Parameters.m_Print_Black_and_White, 1 );
 
         // Test for a reasonnable scale value. Set to 1 if problem
         if( s_Parameters.m_XScaleAdjust < MIN_SCALE ||
@@ -207,8 +206,8 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
 
     m_ScaleOption->SetSelection( scale_idx );
     scale_idx = m_ScaleOption->GetSelection();
-    s_Parameters.m_PrintScale =  s_ScaleList[scale_idx];
-    m_Print_Mirror->SetValue(s_Parameters.m_PrintMirror);
+    s_Parameters.m_PrintScale = s_ScaleList[scale_idx];
+    m_Print_Mirror->SetValue( s_Parameters.m_PrintMirror );
 
 
     if( s_Parameters.m_Print_Black_and_White )
@@ -230,6 +229,7 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
     m_FineAdjustYscaleOpt->Enable(enable);
 }
 
+
 int DIALOG_PRINT_USING_PRINTER::SetLayerSetFromListSelection()
 {
     int page_count = 0;
@@ -275,7 +275,7 @@ void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
 }
 
 
-void DIALOG_PRINT_USING_PRINTER::SetPrintParameters( )
+void DIALOG_PRINT_USING_PRINTER::SetPrintParameters()
 {
     s_Parameters.m_PrintMirror = m_Print_Mirror->GetValue();
     s_Parameters.m_Print_Black_and_White =
@@ -288,7 +288,7 @@ void DIALOG_PRINT_USING_PRINTER::SetPrintParameters( )
     SetLayerSetFromListSelection();
 
     int idx = m_ScaleOption->GetSelection();
-    s_Parameters.m_PrintScale =  s_ScaleList[idx];
+    s_Parameters.m_PrintScale = s_ScaleList[idx];
 
     if( m_FineAdjustXscaleOpt )
     {
@@ -331,7 +331,7 @@ void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
 
 bool DIALOG_PRINT_USING_PRINTER::PreparePrintPrms()
 {
-    SetPrintParameters( );
+    SetPrintParameters();
 
     // If no layer selected, we have no plot. prompt user if it happens
     // because he could think there is a bug in Pcbnew:
diff --git a/gerbview/draw_gerber_screen.cpp b/gerbview/draw_gerber_screen.cpp
index e5dba34896..8b61629180 100644
--- a/gerbview/draw_gerber_screen.cpp
+++ b/gerbview/draw_gerber_screen.cpp
@@ -46,7 +46,7 @@
 void GERBVIEW_FRAME::PrintPage( wxDC* aDC, LSET aPrintMasklayer,
                                 bool aPrintMirrorMode, void* aData )
 {
-    wxCHECK_RET( aData != NULL, wxT( "aDate cannot be NULL." ) );
+    wxCHECK_RET( aData != NULL, wxT( "aData cannot be NULL." ) );
 
     // Save current draw options, because print mode has specific options:
     GBR_DISPLAY_OPTIONS imgDisplayOptions = m_DisplayOptions;
@@ -59,15 +59,28 @@ void GERBVIEW_FRAME::PrintPage( wxDC* aDC, LSET aPrintMasklayer,
     m_DisplayOptions.m_DisplayDCodes = false;
     m_DisplayOptions.m_IsPrinting = true;
 
-    PRINT_PARAMETERS* printParameters = (PRINT_PARAMETERS*)aData;
+    PRINT_PARAMETERS* printParameters = (PRINT_PARAMETERS*) aData;
+
+    // Find the layer to be printed
+    int page = printParameters->m_Flags;    // contains the page number (not necessarily layer number)
+    int layer = 0;
+
+    // Find the layer number for the printed page (search through the mask and count bits)
+    while( page > 0 )
+    {
+        if( printLayersMask[layer++] )
+            --page;
+    }
+    --layer;
+
     std::bitset <GERBER_DRAWLAYERS_COUNT> printCurrLayerMask;
     printCurrLayerMask.reset();
-    printCurrLayerMask.set(printParameters->m_Flags);   // m_Flags contains the draw layer number
+    printCurrLayerMask.set( layer );
     GetGerberLayout()->SetPrintableLayers( printCurrLayerMask );
     m_canvas->SetPrintMirrored( aPrintMirrorMode );
     bool printBlackAndWhite = printParameters->m_Print_Black_and_White;
 
-    GetGerberLayout()->Draw( m_canvas, aDC, UNSPECIFIED_DRAWMODE,
+    GetGerberLayout()->Draw( m_canvas, aDC, (GR_DRAWMODE) 0,
                              wxPoint( 0, 0 ), printBlackAndWhite );
 
     m_canvas->SetPrintMirrored( false );
diff --git a/gerbview/printout_control.cpp b/gerbview/printout_control.cpp
index 05cd9de4c4..1eb8ad1715 100644
--- a/gerbview/printout_control.cpp
+++ b/gerbview/printout_control.cpp
@@ -81,7 +81,7 @@ bool BOARD_PRINTOUT_CONTROLLER::OnPrintPage( int aPage )
 {
     // in gerbview, draw layers are always printed on separate pages
     // because handling negative objects when using only one page is tricky
-    m_PrintParams.m_Flags = aPage-1;    // = gerber draw layer id
+    m_PrintParams.m_Flags = aPage;
     DrawPage();
 
     return true;
@@ -233,12 +233,12 @@ void BOARD_PRINTOUT_CONTROLLER::DrawPage()
 
     if( m_PrintParams.PrintBorderAndTitleBlock() )
         m_Parent->DrawWorkSheet( dc, screen, m_PrintParams.m_PenDefaultSize,
-                                  IU_PER_MILS, titleblockFilename );
+                                 IU_PER_MILS, titleblockFilename );
 
     if( printMirror )
     {
         // To plot mirror, we reverse the x axis, and modify the plot x origin
-        dc->SetAxisOrientation( false, false);
+        dc->SetAxisOrientation( false, false );
 
         /* Plot offset x is moved by the x plot area size in order to have
          * the old draw area in the new draw area, because the draw origin has not moved
@@ -248,7 +248,7 @@ void BOARD_PRINTOUT_CONTROLLER::DrawPage()
         x_dc_offset = KiROUND( x_dc_offset  * userscale );
         dc->SetDeviceOrigin( x_dc_offset, 0 );
 
-        panel->SetClipBox( EDA_RECT( wxPoint( -MAX_VALUE/2, -MAX_VALUE/2 ),
+        panel->SetClipBox( EDA_RECT( wxPoint( -MAX_VALUE / 2, -MAX_VALUE / 2 ),
                                      panel->GetClipBox()->GetSize() ) );
     }
 
@@ -261,8 +261,7 @@ void BOARD_PRINTOUT_CONTROLLER::DrawPage()
     // B&W mode is handled in print page function
     GRForceBlackPen( false );
 
-    m_Parent->PrintPage( dc, m_PrintParams.m_PrintMaskLayer, printMirror,
-                         &m_PrintParams );
+    m_Parent->PrintPage( dc, m_PrintParams.m_PrintMaskLayer, printMirror, &m_PrintParams );
 
     m_Parent->SetDrawBgColor( bg_color );
     screen->m_IsPrinting = false;