KiCad PCB EDA Suite
board_printout.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) 2009 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2018 CERN
7  * Author: Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <board_printout.h>
28 
29 #include <view/view.h>
30 #include <gal/gal_print.h>
31 #include <painter.h>
32 #include <pcbplot.h>
33 #include <settings/app_settings.h>
34 
35 
37  : PRINTOUT_SETTINGS( aPageInfo )
38 {
39  m_LayerSet.set();
40  m_Mirror = false;
41 }
42 
43 
45 {
46  PRINTOUT_SETTINGS::Load( aConfig );
47 
48  m_LayerSet.reset();
49 
50  for( int layer : aConfig->m_Printing.layers )
51  m_LayerSet.set( layer, true );
52 }
53 
54 
56 {
57  PRINTOUT_SETTINGS::Save( aConfig );
58 
59  aConfig->m_Printing.layers.clear();
60 
61  for( unsigned layer = 0; layer < m_LayerSet.size(); ++layer )
62  if( m_LayerSet.test( layer ) )
63  aConfig->m_Printing.layers.push_back( layer );
64 }
65 
66 
68  const KIGFX::VIEW* aView, const wxString& aTitle ) :
69  wxPrintout( aTitle ),
70  m_settings( aParams )
71 {
72  m_view = aView;
73 }
74 
75 
76 void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
77 {
78  *minPage = 1;
79  *selPageFrom = 1;
80 
81  *maxPage = m_settings.m_pageCount;
82  *selPageTo = m_settings.m_pageCount;
83 }
84 
85 
86 void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
87 {
88  auto dc = GetDC();
90  auto galPrint = KIGFX::GAL_PRINT::Create( options, dc );
91  auto gal = galPrint->GetGAL();
92  auto printCtx = galPrint->GetPrintCtx();
93  auto painter = getPainter( gal );
94  std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
95 
96  // Target paper size
97  wxRect pageSizePx = GetLogicalPageRect();
98  const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
99  (double) pageSizePx.height / dc->GetPPI().y );
100  galPrint->SetSheetSize( pageSizeIn );
101  const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
102 
103  view->SetGAL( gal );
104  view->SetPainter( painter.get() );
105  view->SetScaleLimits( 10e9, 0.0001 );
106  view->SetScale( 1.0 );
107 
108 
109  // Set the color scheme
110  RENDER_SETTINGS* dstSettings = view->GetPainter()->GetSettings();
111  dstSettings->LoadColors( m_settings.m_colorSettings );
112 
114  {
115  for( int i = 0; i < LAYER_ID_COUNT; ++i )
116  dstSettings->SetLayerColor( i, COLOR4D::BLACK );
117  }
118  else // color enabled
119  {
120  for( int i = 0; i < LAYER_ID_COUNT; ++i )
121  {
122  // Cairo does not support translucent colors on PostScript surfaces
123  // see 'Features support by the PostScript surface' on
124  // https://www.cairographics.org/documentation/using_the_postscript_surface/
125  dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
126  }
127  }
128 
129  dstSettings->SetIsPrinting( true );
130 
131  setupPainter( *painter );
133 
134  auto sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
135  VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.GetWidth() ), milsToIU( sheetSizeMils.GetHeight() ) );
136  BOX2I bBox;
137 
138  // Determine printout bounding box
140  {
141  bBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
142  view->SetLayerVisible( LAYER_DRAWINGSHEET, true );
143  }
144  else
145  {
146  EDA_RECT targetBbox = getBoundingBox();
147  bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
148  view->SetLayerVisible( LAYER_DRAWINGSHEET, false );
149  }
150 
151 
152  // Fit to page
153  if( m_settings.m_scale <= 0.0 )
154  {
155  if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
156  {
157  // Nothing to print
158  m_settings.m_scale = 1.0;
159  }
160  else
161  {
162  double scaleX = (double) pageSizeIU.x / bBox.GetWidth();
163  double scaleY = (double) pageSizeIU.y / bBox.GetHeight();
164  m_settings.m_scale = std::min( scaleX, scaleY );
165  }
166  }
167 
168  view->SetPrintMode( 1 );
169 
170  setupGal( gal );
171  galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
172  gal->SetLookAtPoint( bBox.Centre() );
173  gal->SetZoomFactor( m_settings.m_scale );
174 
175  gal->SetClearColor( dstSettings->GetBackgroundColor() );
176  gal->ClearScreen();
177 
178  {
179  KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
180  view->Redraw();
181  }
182 
183  view->SetPrintMode( 0 );
184 }
185 
186 
187 void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
188 {
189  // Disable all layers by default, let specific implementations enable required layers
190  for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
191  {
192  aView.SetLayerVisible( i, false );
193  aView.SetTopLayer( i, false );
195  }
196 }
197 
198 
200 {
201  if( !m_settings.m_background )
203 }
204 
205 
207 {
208  aGal->SetFlip( m_settings.m_Mirror, false );
209 }
bool m_background
Print background color.
Definition: printout.h:62
BOX2< VECTOR2I > BOX2I
Definition: box2.h:512
const PAGE_INFO & m_pageInfo
Definition: printout.h:63
void Save(APP_SETTINGS_BASE *aConfig) override
virtual void Save(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:24
Handle the parameters used to print a board drawing.
Definition: printout.h:31
virtual void SetTopLayer(int aLayer, bool aEnabled=true)
Set given layer to be displayed on the top or sets back the default order of layers.
Definition: view.cpp:838
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
virtual std::unique_ptr< KIGFX::PAINTER > getPainter(KIGFX::GAL *aGal)=0
Source VIEW object (note that actual printing only refers to this object)
virtual const COLOR4D & GetBackgroundColor()=0
Return current background color settings.
const KIGFX::VIEW * m_view
Printout parameters.
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void Load(APP_SETTINGS_BASE *aConfig) override
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:305
LSET m_LayerSet
Layers to print.
virtual void DrawPage(const wxString &aLayerName=wxEmptyString, int aPageNum=1, int aPageCount=1)
Print a page (or a set of pages).
Definition: color4d.h:44
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:701
Auxiliary rendering target (noncached)
Definition: definitions.h:49
BOARD_PRINTOUT_SETTINGS m_settings
virtual void Load(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:32
double m_scale
Printing scale.
Definition: printout.h:58
Board plot function definition file.
COLOR_SETTINGS * m_colorSettings
The color settings to be used for printing.
Definition: printout.h:66
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:427
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:57
LSET is a set of PCB_LAYER_IDs.
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
virtual int milsToIU(double aMils) const =0
< Convert mils to internal units
virtual void setupPainter(KIGFX::PAINTER &aPainter)
Configures GAL object for a printout.
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
coord_type GetWidth() const
Definition: box2.h:187
std::vector< int > layers
List of enabled layers for printing.
Definition: app_settings.h:139
virtual void setupGal(KIGFX::GAL *aGal)
Returns bounding box of the printed objects (excluding drawing-sheet frame)
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:387
bool PrintBorderAndTitleBlock() const
Returns true if the drawing border and title block should be printed.
Definition: printout.h:55
const wxSize & GetSizeMils() const
Definition: page_info.h:135
#define LAYER_ID_COUNT
Must update this if you add any enums after GerbView!
virtual void setupViewLayers(KIGFX::VIEW &aView, const LSET &aLayerSet)
Configures PAINTER object for a printout.
Definition: color4d.h:48
bool m_blackWhite
Print in B&W or Color.
Definition: printout.h:60
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1440
BOARD_PRINTOUT_SETTINGS(const PAGE_INFO &aPageInfo)
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
virtual void SetBackgroundColor(const COLOR4D &aColor)=0
Set the background color.
Vec Centre() const
Definition: box2.h:69
int m_pageCount
Number of pages to print.
Definition: printout.h:61
void SetIsPrinting(bool isPrinting)
Handle the component boundary box.
Definition: eda_rect.h:42
coord_type GetHeight() const
Definition: box2.h:188
Board print handler definition file.
BOARD_PRINTOUT(const BOARD_PRINTOUT_SETTINGS &aParams, const KIGFX::VIEW *aView, const wxString &aTitle)
drawingsheet frame and titleblock
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
virtual void LoadColors(const COLOR_SETTINGS *aSettings)
bool m_Mirror
Print mirrored.
virtual EDA_RECT getBoundingBox()=0
Returns a PAINTER instance used to draw the items.
const wxSize GetSize() const
Definition: eda_rect.h:91
Abstract interface for drawing on a 2D-surface.