KiCad PCB EDA Suite
printout_control.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include <fctsys.h>
32 #include <pgm_base.h>
33 #include <gr_basic.h>
34 #include <class_drawpanel.h>
35 #include <base_units.h>
36 #include <base_screen.h>
37 
38 #include <gerbview_frame.h>
39 #include <gerber_file_image.h>
40 #include <gerber_file_image_list.h>
41 
42 #include <printout_controler.h>
43 
44 
45 
47 {
48  m_PenDefaultSize = Millimeter2iu( 0.2 ); // A reasonable default value to draw items
49  // which do not have a specified line width
50  m_PrintScale = 1.0;
51  m_XScaleAdjust = 1.0;
52  m_YScaleAdjust = 1.0;
53  m_Print_Sheet_Ref = false;
54  m_PrintMaskLayer.set();
55  m_PrintMirror = false;
58  m_PageCount = 1;
59  m_ForceCentered = false;
60  m_Flags = 0;
62  m_PageSetupData = NULL;
63 }
64 
65 
67  EDA_DRAW_FRAME* aParent,
68  const wxString& aTitle ) :
69  wxPrintout( aTitle )
70 {
71  m_PrintParams = aParams; // Make a local copy of the print parameters.
72  m_Parent = aParent;
73 }
74 
75 
77 {
78  // in gerbview, draw layers are always printed on separate pages
79  // because handling negative objects when using only one page is tricky
80  m_PrintParams.m_Flags = aPage;
81 
82  // The gerber filename of the page to print will be printed to the worksheet.
83  // Find this filename:
84  // Find the graphic layer number for the page to print
85  std::vector<int> printList =
86  ((GERBVIEW_FRAME*) m_Parent)->GetGerberLayout()->GetPrintableLayers();
87 
88  if( printList.size() < 1 ) // Should not occur
89  return false;
90 
91  int graphiclayer = printList[aPage-1];
93  GERBER_FILE_IMAGE* gbrImage = gbrImgList.GetGbrImage( graphiclayer );
94  wxString gbr_filename;
95 
96  if( gbrImage )
97  gbr_filename = gbrImage->m_FileName;
98 
99  DrawPage( gbr_filename, aPage, m_PrintParams.m_PageCount );
100 
101  return true;
102 }
103 
104 
105 void BOARD_PRINTOUT_CONTROLLER::GetPageInfo( int* minPage, int* maxPage,
106  int* selPageFrom, int* selPageTo )
107 {
108  *minPage = 1;
109  *selPageFrom = 1;
110 
111  int icnt = 1;
112 
114  icnt = m_PrintParams.m_PageCount;
115 
116  *maxPage = icnt;
117  *selPageTo = icnt;
118 }
119 
120 
121 void BOARD_PRINTOUT_CONTROLLER::DrawPage( const wxString& aLayerName,
122  int aPageNum, int aPageCount)
123 {
124  wxPoint offset;
125  double userscale;
126  EDA_RECT boardBoundingBox;
127  EDA_RECT drawRect;
128  wxDC* dc = GetDC();
129  BASE_SCREEN* screen = m_Parent->GetScreen();
130  bool printMirror = m_PrintParams.m_PrintMirror;
131  wxSize pageSizeIU = m_Parent->GetPageSizeIU();
132 
133  wxBusyCursor dummy;
134 
135  boardBoundingBox = ((GERBVIEW_FRAME*) m_Parent)->GetGerberLayoutBoundingBox();
136  const wxString& titleblockFilename = aLayerName; // TODO see if we uses the gerber file name
137 
138  // Use the page size as the drawing area when the board is shown or the user scale
139  // is less than 1.
141  boardBoundingBox = EDA_RECT( wxPoint( 0, 0 ), pageSizeIU );
142 
143  // Compute the PCB size in internal units
144  userscale = m_PrintParams.m_PrintScale;
145 
146  if( m_PrintParams.m_PrintScale == 0 ) // fit in page option
147  {
148  if(boardBoundingBox.GetWidth() && boardBoundingBox.GetHeight())
149  {
150  int margin = Millimeter2iu( 10.0 ); // add a margin around the drawings
151  double scaleX = (double)(pageSizeIU.x - (2 * margin)) /
152  boardBoundingBox.GetWidth();
153  double scaleY = (double)(pageSizeIU.y - (2 * margin)) /
154  boardBoundingBox.GetHeight();
155  userscale = (scaleX < scaleY) ? scaleX : scaleY;
156  }
157  else
158  userscale = 1.0;
159  }
160 
161  wxSize scaledPageSize = pageSizeIU;
162  drawRect.SetSize( scaledPageSize );
163  scaledPageSize.x = wxRound( scaledPageSize.x / userscale );
164  scaledPageSize.y = wxRound( scaledPageSize.y / userscale );
165 
166 
168  {
169  // Always scale to the size of the paper.
170  FitThisSizeToPageMargins( scaledPageSize, *m_PrintParams.m_PageSetupData );
171  }
172 
173  // Compute Accurate scale 1
174  if( m_PrintParams.m_PrintScale == 1.0 )
175  {
176  // We want a 1:1 scale, regardless the page setup
177  // like page size, margin ...
178  MapScreenSizeToPaper(); // set best scale and offset (scale is not used)
179  int w, h;
180  GetPPIPrinter( &w, &h );
181  double accurate_Xscale = (double) w / (IU_PER_MILS*1000);
182  double accurate_Yscale = (double) h / (IU_PER_MILS*1000);
183 
184  if( IsPreview() ) // Scale must take in account the DC size in Preview
185  {
186  // Get the size of the DC in pixels
187  wxSize PlotAreaSize;
188  dc->GetSize( &PlotAreaSize.x, &PlotAreaSize.y );
189  GetPageSizePixels( &w, &h );
190  accurate_Xscale *= (double)PlotAreaSize.x / w;
191  accurate_Yscale *= (double)PlotAreaSize.y / h;
192  }
193  // Fine scale adjust
194  accurate_Xscale *= m_PrintParams.m_XScaleAdjust;
195  accurate_Yscale *= m_PrintParams.m_YScaleAdjust;
196 
197  // Set print scale for 1:1 exact scale
198  dc->SetUserScale( accurate_Xscale, accurate_Yscale );
199  }
200 
201  // Get the final size of the DC in pixels
202  wxSize PlotAreaSizeInPixels;
203  dc->GetSize( &PlotAreaSizeInPixels.x, &PlotAreaSizeInPixels.y );
204 
205  double scalex, scaley;
206  dc->GetUserScale( &scalex, &scaley );
207 
208  // In some cases the plot origin is the centre of the board outline rather than the center
209  // of the selected paper size.
211  {
212  // Here we are only drawing the board and it's contents.
213  drawRect = boardBoundingBox;
214  offset.x += wxRound( (double) -scaledPageSize.x / 2.0 );
215  offset.y += wxRound( (double) -scaledPageSize.y / 2.0 );
216 
217  wxPoint center = boardBoundingBox.Centre();
218 
219  if( printMirror )
220  {
221  // Calculate the mirrored center of the board.
222  center.x = m_Parent->GetPageSizeIU().x - boardBoundingBox.Centre().x;
223  }
224 
225  offset += center;
226  }
227 
228  GRResetPenAndBrush( dc );
229 
230  EDA_DRAW_PANEL* panel = m_Parent->GetCanvas();
231  EDA_RECT tmp = *panel->GetClipBox();
232 
233  // Set clip box to the max size
234  #define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
235  // and that allows calculations without overflow
236  panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
237 
238  screen->m_IsPrinting = true;
239  COLOR4D bg_color = m_Parent->GetDrawBgColor();
240 
241  // Print frame reference, if requested, before printing draw layers
243  GRForceBlackPen( true );
244 
246  {
247  int tempScreenNumber = screen->m_ScreenNumber;
248  int tempNumberOfScreens = screen->m_NumberOfScreens;
249  screen->m_ScreenNumber = aPageNum;
250  screen->m_NumberOfScreens = aPageCount;
252  IU_PER_MILS, titleblockFilename );
253  screen->m_ScreenNumber = tempScreenNumber;
254  screen->m_NumberOfScreens = tempNumberOfScreens;
255  }
256 
257  if( printMirror )
258  {
259  // To plot mirror, we reverse the x axis, and modify the plot x origin
260  dc->SetAxisOrientation( false, false );
261 
262  /* Change plot offset in order to have the draw area at the same location.
263  * The plot origin X is just moved from 0 to PlotAreaSizeInPixels.x.
264  * just set offset x at PlotAreaSizeInPixels.x.
265  */
266  int x_dc_offset = PlotAreaSizeInPixels.x;
267  x_dc_offset = KiROUND( x_dc_offset * userscale );
268  dc->SetDeviceOrigin( x_dc_offset, 0 );
269 
270  panel->SetClipBox( EDA_RECT( wxPoint( -MAX_VALUE / 2, -MAX_VALUE / 2 ),
271  panel->GetClipBox()->GetSize() ) );
272  }
273 
274  // screen->m_DrawOrg = offset;
275  dc->SetLogicalOrigin( offset.x, offset.y );
277 
278  // Never force black pen to print draw layers
279  // because negative objects need a white pen, not a black pen
280  // B&W mode is handled in print page function
281  GRForceBlackPen( false );
282 
284 
285  m_Parent->SetDrawBgColor( bg_color );
286  screen->m_IsPrinting = false;
287  panel->SetClipBox( tmp );
288 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:218
BOARD_PRINTOUT_CONTROLLER(const PRINT_PARAMETERS &aParams, EDA_DRAW_FRAME *aParent, const wxString &aTitle)
int m_ScreenNumber
Definition: base_screen.h:216
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
Board print handler definition file.
Implementation of conversion functions that require both schematic and board internal units...
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:342
const wxSize GetSize() const
Definition: eda_rect.h:101
Class PRINT_PARAMETERS handles the parameters used to print a board drawing.
bool PrintBorderAndTitleBlock() const
Function PrintBorderAndTitleBlock returns true if the drawing border and title block should be printe...
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
int GetHeight() const
Definition: eda_rect.h:118
virtual void SetDrawBgColor(COLOR4D aColor)
Definition: draw_frame.h:334
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
void DrawPage(const wxString &aLayerName=wxEmptyString, int aPageNum=1, int aPageCount=1)
Print a page ( or a set of pages ).
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:54
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
virtual const wxSize GetPageSizeIU() const =0
Function GetPageSizeIU works off of GetPageSettings() to return the size of the paper page in the int...
void SetClipBox(const EDA_RECT &aRect)
virtual BASE_SCREEN * GetScreen() const
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: draw_frame.h:352
virtual COLOR4D GetDrawBgColor() const
Definition: draw_frame.h:329
virtual void PrintPage(wxDC *aDC, LSET aPrintMask, bool aPrintMirrorMode, void *aData=NULL)
Function PrintPage used to print a page Print the page pointed by current screen, set by the calling ...
Definition: draw_frame.cpp:408
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:299
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
bool OnPrintPage(int aPage) override
static GERBER_FILE_IMAGE_LIST & GetImagesList()
EDA_RECT * GetClipBox()
DrillShapeOptT m_DrillShapeOpt
wxPoint Centre() const
Definition: eda_rect.h:60
bool m_IsPrinting
Definition: base_screen.h:220
wxPageSetupDialogData * m_PageSetupData
void SetSize(const wxSize &size)
Definition: eda_rect.h:126
see class PGM_BASE
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int GetWidth() const
Definition: eda_rect.h:117
bool CenterOnBoardOutline() const
Function CenterOnBoardOutline returns true if the print should be centered by the board outline inste...
Definition: colors.h:49
int m_NumberOfScreens
Definition: base_screen.h:217
void DrawWorkSheet(wxDC *aDC, BASE_SCREEN *aScreen, int aLineWidth, double aScale, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Function DrawWorkSheet Draws on screen the page layout with the frame and the basic inscriptions...
Definition: worksheet.cpp:76
BASE_SCREEN class implementation.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define MAX_VALUE