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 ), m_settings( aParams )
70 {
71  m_view = aView;
72 }
73 
74 
75 void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
76 {
77  *minPage = 1;
78  *selPageFrom = 1;
79 
80  *maxPage = m_settings.m_pageCount;
81  *selPageTo = m_settings.m_pageCount;
82 }
83 
84 
85 void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
86 {
87  auto dc = GetDC();
89  auto galPrint = KIGFX::GAL_PRINT::Create( options, dc );
90  auto gal = galPrint->GetGAL();
91  auto printCtx = galPrint->GetPrintCtx();
92  auto painter = getPainter( gal );
93  std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
94 
95  // Target paper size
96  wxRect pageSizePx = GetLogicalPageRect();
97  const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
98  (double) pageSizePx.height / dc->GetPPI().y );
99  galPrint->SetSheetSize( pageSizeIn );
100  const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
101 
102  view->SetGAL( gal );
103  view->SetPainter( painter.get() );
104  view->SetScaleLimits( 10e9, 0.0001 );
105  view->SetScale( 1.0 );
106 
107 
108  // Set the color scheme
109  auto dstSettings = view->GetPainter()->GetSettings();
110  dstSettings->LoadColors( m_settings.m_colorSettings );
111 
113  {
114  for( int i = 0; i < LAYER_ID_COUNT; ++i )
115  dstSettings->SetLayerColor( i, COLOR4D::BLACK );
116  }
117  else // color enabled
118  {
119  for( int i = 0; i < LAYER_ID_COUNT; ++i )
120  {
121  // Cairo does not support translucent colors on PostScript surfaces
122  // see 'Features support by the PostScript surface' on
123  // https://www.cairographics.org/documentation/using_the_postscript_surface/
124  dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
125  }
126  }
127 
129  setupPainter( *painter );
130 
131  auto sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
132  VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.GetWidth() ), milsToIU( sheetSizeMils.GetHeight() ) );
133  BOX2I bBox;
134 
135  // Determine printout bounding box
137  {
138  bBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
139  view->SetLayerVisible( LAYER_WORKSHEET, true );
140  }
141  else
142  {
143  EDA_RECT targetBbox = getBoundingBox();
144  bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
145  view->SetLayerVisible( LAYER_WORKSHEET, false );
146  }
147 
148 
149  // Fit to page
150  if( m_settings.m_scale <= 0.0 )
151  {
152  if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
153  {
154  // Nothing to print
155  m_settings.m_scale = 1.0;
156  }
157  else
158  {
159  double scaleX = (double) pageSizeIU.x / bBox.GetWidth();
160  double scaleY = (double) pageSizeIU.y / bBox.GetHeight();
161  m_settings.m_scale = std::min( scaleX, scaleY );
162  }
163  }
164 
165  view->SetPrintMode( 1 );
166 
167  setupGal( gal );
168  galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
169  gal->SetLookAtPoint( bBox.Centre() );
170  gal->SetZoomFactor( m_settings.m_scale );
171 
172  gal->SetClearColor( dstSettings->GetBackgroundColor() );
173  gal->ClearScreen();
174 
175  {
176  KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
177  view->Redraw();
178  }
179 
180  view->SetPrintMode( 0 );
181 }
182 
183 
184 void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
185 {
186  // Disable all layers by default, let specific implementions enable required layers
187  for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
188  {
189  aView.SetLayerVisible( i, false );
190  aView.SetTopLayer( i, false );
192  }
193 }
194 
195 
197 {
198  if( !m_settings.m_background )
200 }
201 
202 
204 {
205  aGal->SetFlip( m_settings.m_mirror, false );
206 }
bool m_background
Print background color.
Definition: printout.h:55
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
const PAGE_INFO & m_pageInfo
Definition: printout.h:56
void Save(APP_SETTINGS_BASE *aConfig) override
virtual void Save(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:24
PRINT_PARAMETERS handles the parameters used to print a board drawing.
Definition: printout.h:31
virtual void SetTopLayer(int aLayer, bool aEnabled=true)
Function SetTopLayer() Sets given layer to be displayed on the top or sets back the default order of ...
Definition: view.cpp:864
virtual std::unique_ptr< KIGFX::PAINTER > getPainter(KIGFX::GAL *aGal)=0
Returns a PAINTER instance used to draw the items.
const KIGFX::VIEW * m_view
Source VIEW object (note that actual printing only refers to this object)
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
void Load(APP_SETTINGS_BASE *aConfig) override
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:594
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:706
Auxiliary rendering target (noncached)
Definition: definitions.h:49
BOARD_PRINTOUT_SETTINGS m_settings
Printout parameters
virtual void Load(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:32
double m_scale
Printing scale.
Definition: printout.h:51
Board plot function definition file.
COLOR_SETTINGS * m_colorSettings
The color settings to be used for printing.
Definition: printout.h:59
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Function SetLayerTarget() Changes the rendering target for a particular layer.
Definition: view.h:434
PAINTER contains all the knowledge about how to draw graphical object onto any particular output devi...
Definition: painter.h:58
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:92
virtual int milsToIU(double aMils) const =0
Convert mils to internal units
virtual void setupPainter(KIGFX::PAINTER &aPainter)
Configures PAINTER object for a printout
bool m_mirror
Print mirrored.
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
coord_type GetWidth() const
Definition: box2.h:197
std::vector< int > layers
List of enabled layers for printing.
Definition: app_settings.h:124
virtual void setupGal(KIGFX::GAL *aGal)
Configures GAL object for a printout
void SetLayerVisible(int aLayer, bool aVisible=true)
Function SetLayerVisible() Controls the visibility of a particular layer.
Definition: view.h:399
bool PrintBorderAndTitleBlock() const
Returns true if the drawing border and title block should be printed.
Definition: printout.h:64
const wxSize & GetSizeMils() const
Definition: page_info.h:143
#define LAYER_ID_COUNT
Must update this if you add any enums after GerbView!
virtual void setupViewLayers(KIGFX::VIEW &aView, const LSET &aLayerSet)
Enables layers visibility for a printout
Definition: color4d.h:48
bool m_blackWhite
Print in B&W or Color.
Definition: printout.h:53
std::unique_ptr< VIEW > DataReference() const
Returns a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1480
BOARD_PRINTOUT_SETTINGS(const PAGE_INFO &aPageInfo)
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter Returns pointer to current settings that are going to be used when drawing items.
virtual void SetBackgroundColor(const COLOR4D &aColor)=0
Sets the background color.
Vec Centre() const
Definition: box2.h:79
int m_pageCount
Number of pages to print.
Definition: printout.h:54
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
coord_type GetHeight() const
Definition: box2.h:198
Board print handler definition file.
BOARD_PRINTOUT(const BOARD_PRINTOUT_SETTINGS &aParams, const KIGFX::VIEW *aView, const wxString &aTitle)
VIEW.
Definition: view.h:61
virtual EDA_RECT getBoundingBox()=0
Returns bounding box of the printed objects (excluding worksheet frame)
LSET m_layerSet
Layers to print.
const wxSize GetSize() const
Definition: eda_rect.h:103
Class GAL is the abstract interface for drawing on a 2D-surface.