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 <wxstruct.h>
37 #include <class_base_screen.h>
38 
39 #include <gerbview_frame.h>
42 
43 #include <printout_controler.h>
44 
45 
46 
48 {
49  m_PenDefaultSize = Millimeter2iu( 0.2 ); // A reasonable default value to draw items
50  // which do not have a specified line width
51  m_PrintScale = 1.0;
52  m_XScaleAdjust = 1.0;
53  m_YScaleAdjust = 1.0;
54  m_Print_Sheet_Ref = false;
55  m_PrintMaskLayer.set();
56  m_PrintMirror = false;
59  m_PageCount = 1;
60  m_ForceCentered = false;
61  m_Flags = 0;
63  m_PageSetupData = NULL;
64 }
65 
66 
68  EDA_DRAW_FRAME* aParent,
69  const wxString& aTitle ) :
70  wxPrintout( aTitle )
71 {
72  m_PrintParams = aParams; // Make a local copy of the print parameters.
73  m_Parent = aParent;
74 }
75 
76 
78 {
79  // in gerbview, draw layers are always printed on separate pages
80  // because handling negative objects when using only one page is tricky
81  m_PrintParams.m_Flags = aPage;
82 
83  // The gerber filename of the page to print will be printed to the worksheet.
84  // Find this filename:
85  // Find the graphic layer number for the page to print
86  std::vector<int> printList =
87  ((GERBVIEW_FRAME*) m_Parent)->GetGerberLayout()->GetPrintableLayers();
88 
89  if( printList.size() < 1 ) // Should not occur
90  return false;
91 
92  int graphiclayer = printList[aPage-1];
94  GERBER_FILE_IMAGE* gbrImage = gbrImgList.GetGbrImage( graphiclayer );
95  wxString gbr_filename;
96 
97  if( gbrImage )
98  gbr_filename = gbrImage->m_FileName;
99 
100  DrawPage( gbr_filename, aPage, m_PrintParams.m_PageCount );
101 
102  return true;
103 }
104 
105 
106 void BOARD_PRINTOUT_CONTROLLER::GetPageInfo( int* minPage, int* maxPage,
107  int* selPageFrom, int* selPageTo )
108 {
109  *minPage = 1;
110  *selPageFrom = 1;
111 
112  int icnt = 1;
113 
115  icnt = m_PrintParams.m_PageCount;
116 
117  *maxPage = icnt;
118  *selPageTo = icnt;
119 }
120 
121 
122 void BOARD_PRINTOUT_CONTROLLER::DrawPage( const wxString& aLayerName,
123  int aPageNum, int aPageCount)
124 {
125  wxPoint offset;
126  double userscale;
127  EDA_RECT boardBoundingBox;
128  EDA_RECT drawRect;
129  wxDC* dc = GetDC();
130  BASE_SCREEN* screen = m_Parent->GetScreen();
131  bool printMirror = m_PrintParams.m_PrintMirror;
132  wxSize pageSizeIU = m_Parent->GetPageSizeIU();
133 
134  wxBusyCursor dummy;
135 
136  boardBoundingBox = ((GERBVIEW_FRAME*) m_Parent)->GetGerberLayoutBoundingBox();
137  wxString titleblockFilename = aLayerName; // TODO see if we uses the gerber file name
138 
139  // Use the page size as the drawing area when the board is shown or the user scale
140  // is less than 1.
142  boardBoundingBox = EDA_RECT( wxPoint( 0, 0 ), pageSizeIU );
143 
144  // Compute the PCB size in internal units
145  userscale = m_PrintParams.m_PrintScale;
146 
147  if( m_PrintParams.m_PrintScale == 0 ) // fit in page option
148  {
149  if(boardBoundingBox.GetWidth() && boardBoundingBox.GetHeight())
150  {
151  int margin = Millimeter2iu( 10.0 ); // add a margin around the drawings
152  double scaleX = (double)(pageSizeIU.x - (2 * margin)) /
153  boardBoundingBox.GetWidth();
154  double scaleY = (double)(pageSizeIU.y - (2 * margin)) /
155  boardBoundingBox.GetHeight();
156  userscale = (scaleX < scaleY) ? scaleX : scaleY;
157  }
158  else
159  userscale = 1.0;
160  }
161 
162  wxSize scaledPageSize = pageSizeIU;
163  drawRect.SetSize( scaledPageSize );
164  scaledPageSize.x = wxRound( scaledPageSize.x / userscale );
165  scaledPageSize.y = wxRound( scaledPageSize.y / userscale );
166 
167 
169  {
170  // Always scale to the size of the paper.
171  FitThisSizeToPageMargins( scaledPageSize, *m_PrintParams.m_PageSetupData );
172  }
173 
174  // Compute Accurate scale 1
175  if( m_PrintParams.m_PrintScale == 1.0 )
176  {
177  // We want a 1:1 scale, regardless the page setup
178  // like page size, margin ...
179  MapScreenSizeToPaper(); // set best scale and offset (scale is not used)
180  int w, h;
181  GetPPIPrinter( &w, &h );
182  double accurate_Xscale = (double) w / (IU_PER_MILS*1000);
183  double accurate_Yscale = (double) h / (IU_PER_MILS*1000);
184 
185  if( IsPreview() ) // Scale must take in account the DC size in Preview
186  {
187  // Get the size of the DC in pixels
188  wxSize PlotAreaSize;
189  dc->GetSize( &PlotAreaSize.x, &PlotAreaSize.y );
190  GetPageSizePixels( &w, &h );
191  accurate_Xscale *= (double)PlotAreaSize.x / w;
192  accurate_Yscale *= (double)PlotAreaSize.y / h;
193  }
194  // Fine scale adjust
195  accurate_Xscale *= m_PrintParams.m_XScaleAdjust;
196  accurate_Yscale *= m_PrintParams.m_YScaleAdjust;
197 
198  // Set print scale for 1:1 exact scale
199  dc->SetUserScale( accurate_Xscale, accurate_Yscale );
200  }
201 
202  // Get the final size of the DC in pixels
203  wxSize PlotAreaSizeInPixels;
204  dc->GetSize( &PlotAreaSizeInPixels.x, &PlotAreaSizeInPixels.y );
205 
206  double scalex, scaley;
207  dc->GetUserScale( &scalex, &scaley );
208 
209  // In some cases the plot origin is the centre of the board outline rather than the center
210  // of the selected paper size.
212  {
213  // Here we are only drawing the board and it's contents.
214  drawRect = boardBoundingBox;
215  offset.x += wxRound( (double) -scaledPageSize.x / 2.0 );
216  offset.y += wxRound( (double) -scaledPageSize.y / 2.0 );
217 
218  wxPoint center = boardBoundingBox.Centre();
219 
220  if( printMirror )
221  {
222  // Calculate the mirrored center of the board.
223  center.x = m_Parent->GetPageSizeIU().x - boardBoundingBox.Centre().x;
224  }
225 
226  offset += center;
227  }
228 
229  GRResetPenAndBrush( dc );
230 
231  EDA_DRAW_PANEL* panel = m_Parent->GetCanvas();
232  EDA_RECT tmp = *panel->GetClipBox();
233 
234  // Set clip box to the max size
235  #define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
236  // and that allows calculations without overflow
237  panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
238 
239  screen->m_IsPrinting = true;
240  COLOR4D bg_color = m_Parent->GetDrawBgColor();
241 
242  // Print frame reference, if requested, before printing draw layers
244  GRForceBlackPen( true );
245 
247  {
248  int tempScreenNumber = screen->m_ScreenNumber;
249  int tempNumberOfScreens = screen->m_NumberOfScreens;
250  screen->m_ScreenNumber = aPageNum;
251  screen->m_NumberOfScreens = aPageCount;
253  IU_PER_MILS, titleblockFilename );
254  screen->m_ScreenNumber = tempScreenNumber;
255  screen->m_NumberOfScreens = tempNumberOfScreens;
256  }
257 
258  if( printMirror )
259  {
260  // To plot mirror, we reverse the x axis, and modify the plot x origin
261  dc->SetAxisOrientation( false, false );
262 
263  /* Change plot offset in order to have the draw area at the same location.
264  * The plot origin X is just moved from 0 to PlotAreaSizeInPixels.x.
265  * just set offset x at PlotAreaSizeInPixels.x.
266  */
267  int x_dc_offset = PlotAreaSizeInPixels.x;
268  x_dc_offset = KiROUND( x_dc_offset * userscale );
269  dc->SetDeviceOrigin( x_dc_offset, 0 );
270 
271  panel->SetClipBox( EDA_RECT( wxPoint( -MAX_VALUE / 2, -MAX_VALUE / 2 ),
272  panel->GetClipBox()->GetSize() ) );
273  }
274 
275  // screen->m_DrawOrg = offset;
276  dc->SetLogicalOrigin( offset.x, offset.y );
278 
279  // Never force black pen to print draw layers
280  // because negative objects need a white pen, not a black pen
281  // B&W mode is handled in print page function
282  GRForceBlackPen( false );
283 
285 
286  m_Parent->SetDrawBgColor( bg_color );
287  screen->m_IsPrinting = false;
288  panel->SetClipBox( tmp );
289 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:196
BOARD_PRINTOUT_CONTROLLER(const PRINT_PARAMETERS &aParams, EDA_DRAW_FRAME *aParent, const wxString &aTitle)
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
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:337
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
virtual void SetDrawBgColor(COLOR4D aColor)
Definition: draw_frame.h:329
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:347
virtual COLOR4D GetDrawBgColor() const
Definition: draw_frame.h:324
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:405
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:277
Base window classes and related definitions.
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
bool OnPrintPage(int aPage) override
static GERBER_FILE_IMAGE_LIST & GetImagesList()
EDA_RECT * GetClipBox()
DrillShapeOptT m_DrillShapeOpt
wxPoint Centre() const
wxPageSetupDialogData * m_PageSetupData
void SetSize(const wxSize &size)
see class PGM_BASE
BASE_SCREEN class implementation.
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.
int GetWidth() const
bool CenterOnBoardOutline() const
Function CenterOnBoardOutline returns true if the print should be centered by the board outline inste...
Definition: colors.h:49
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:77
const wxSize & GetSize() const
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define MAX_VALUE