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 
34 
36  : PRINTOUT_SETTINGS( aPageInfo )
37 {
38  m_layerSet.set();
39  m_mirror = false;
40 }
41 
42 
43 void BOARD_PRINTOUT_SETTINGS::Load( wxConfigBase* aConfig )
44 {
45  PRINTOUT_SETTINGS::Load( aConfig );
46 
47  for( unsigned layer = 0; layer < m_layerSet.size(); ++layer )
48  {
49  int tmp;
50  wxString key = wxString::Format( OPTKEY_LAYERBASE, layer );
51  aConfig->Read( key, &tmp, 1 );
52  m_layerSet.set( layer, tmp );
53  }
54 }
55 
56 
57 void BOARD_PRINTOUT_SETTINGS::Save( wxConfigBase* aConfig )
58 {
59  PRINTOUT_SETTINGS::Save( aConfig );
60 
61  for( unsigned layer = 0; layer < m_layerSet.size(); ++layer )
62  {
63  wxString key = wxString::Format( OPTKEY_LAYERBASE, layer );
64  aConfig->Write( key, m_layerSet.test( layer ) );
65  }
66 }
67 
68 
70  const KIGFX::VIEW* aView, const wxString& aTitle ) :
71  wxPrintout( aTitle ), m_settings( aParams )
72 {
73  m_view = aView;
74 }
75 
76 
77 void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
78 {
79  *minPage = 1;
80  *selPageFrom = 1;
81 
82  *maxPage = m_settings.m_pageCount;
83  *selPageTo = m_settings.m_pageCount;
84 }
85 
86 
87 void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
88 {
89  auto dc = GetDC();
91  auto galPrint = KIGFX::GAL_PRINT::Create( options, dc );
92  auto gal = galPrint->GetGAL();
93  auto printCtx = galPrint->GetPrintCtx();
94  auto painter = getPainter( gal );
95  std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
96 
97  // Target paper size
98  wxRect pageSizePx = GetLogicalPageRect();
99  const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
100  (double) pageSizePx.height / dc->GetPPI().y );
101  galPrint->SetSheetSize( pageSizeIn );
102  const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
103 
104  view->SetGAL( gal );
105  view->SetPainter( painter.get() );
106  view->SetScaleLimits( 10e9, 0.0001 );
107  view->SetScale( 1.0 );
108 
109 
110  // Set the color scheme
111  auto srcSettings = m_view->GetPainter()->GetSettings();
112  auto dstSettings = view->GetPainter()->GetSettings();
113 
115  {
116  for( int i = 0; i < LAYER_ID_COUNT; ++i )
117  dstSettings->SetLayerColor( i, COLOR4D::BLACK );
118  }
119  else // color enabled
120  {
121  for( int i = 0; i < LAYER_ID_COUNT; ++i )
122  {
123  // Cairo does not support translucent colors on PostScript surfaces
124  // see 'Features support by the PostScript surface' on
125  // ttps://www.cairographics.org/documentation/using_the_postscript_surface/
126  dstSettings->SetLayerColor( i, srcSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
127  }
128  }
129 
130 
132  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_WORKSHEET, true );
143  }
144  else
145  {
146  EDA_RECT targetBbox = getBoundingBox();
147  bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
148  view->SetLayerVisible( LAYER_WORKSHEET, 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  setupGal( gal );
169  galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
170  gal->SetLookAtPoint( bBox.Centre() );
171  gal->SetZoomFactor( m_settings.m_scale );
172 
173  {
174  KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
175  view->Redraw();
176  }
177 }
178 
179 
180 void BOARD_PRINTOUT::setupViewLayers( const std::unique_ptr<KIGFX::VIEW>& aView,
181  const LSET& aLayerSet )
182 {
183  // Disable all layers by default, let specific implementions enable required layers
184  for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
185  {
186  aView->SetLayerVisible( i, false );
187  aView->SetTopLayer( i, false );
188  aView->SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
189  }
190 }
191 
192 
193 void BOARD_PRINTOUT::setupPainter( const std::unique_ptr<KIGFX::PAINTER>& aPainter )
194 {
195  aPainter->GetSettings()->SetBackgroundColor( COLOR4D::WHITE );
196 }
197 
198 
200 {
201  aGal->SetFlip( m_settings.m_mirror, false );
202 }
BOX2< VECTOR2I > BOX2I
Definition: box2.h:520
const PAGE_INFO & m_pageInfo
Definition: printout.h:48
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
#define OPTKEY_LAYERBASE
Definition: pcbplot.h:53
Class PRINT_PARAMETERS handles the parameters used to print a board drawing.
Definition: printout.h:30
virtual std::unique_ptr< KIGFX::PAINTER > getPainter(KIGFX::GAL *aGal)=0
Returns a PAINTER instance used to draw the items.
const wxSize GetSize() const
Definition: eda_rect.h:101
virtual void Save(wxConfigBase *aConfig)
Definition: printout.cpp:23
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.
virtual void DrawPage(const wxString &aLayerName=wxEmptyString, int aPageNum=1, int aPageCount=1)
Print a page (or a set of pages).
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:699
Auxiliary rendering target (noncached)
Definition: definitions.h:42
BOARD_PRINTOUT_SETTINGS m_settings
Printout parameters
const wxSize & GetSizeMils() const
Definition: page_info.h:142
double m_scale
Printing scale.
Definition: printout.h:44
Board plot function definition file.
coord_type GetWidth() const
Definition: box2.h:195
Class LSET is a set of PCB_LAYER_IDs.
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
bool PrintBorderAndTitleBlock() const
Returns true if the drawing border and title block should be printed.
Definition: printout.h:56
virtual int milsToIU(double aMils) const =0
Convert mils to internal units
bool m_mirror
Print mirrored.
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:54
virtual void setupGal(KIGFX::GAL *aGal)
Configures GAL object for a printout
void Load(wxConfigBase *aConfig) override
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Function SetLayerColor Changes the color used to draw a layer.
Definition: painter.h:230
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
virtual void setupPainter(const std::unique_ptr< KIGFX::PAINTER > &aPainter)
Configures PAINTER object for a printout
#define LAYER_ID_COUNT
Must update this if you add any enums after GerbView!
Vec Centre() const
Definition: box2.h:77
virtual void Load(wxConfigBase *aConfig)
Definition: printout.cpp:31
std::unique_ptr< VIEW > DataReference() const
Returns a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1466
bool m_blackWhite
Print in B&W or Color.
Definition: printout.h:46
BOARD_PRINTOUT_SETTINGS(const PAGE_INFO &aPageInfo)
coord_type GetHeight() const
Definition: box2.h:196
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
virtual void setupViewLayers(const std::unique_ptr< KIGFX::VIEW > &aView, const LSET &aLayerSet)
Enables layers visibility for a printout
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int m_pageCount
Number of pages to print.
Definition: printout.h:47
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Definition: colors.h:49
Board print handler definition file.
BOARD_PRINTOUT(const BOARD_PRINTOUT_SETTINGS &aParams, const KIGFX::VIEW *aView, const wxString &aTitle)
Definition: colors.h:45
Class VIEW.
Definition: view.h:61
void Save(wxConfigBase *aConfig) override
virtual EDA_RECT getBoundingBox()=0
Returns bounding box of the printed objects (excluding worksheet frame)
LSET m_layerSet
Layers to print.
#define min(a, b)
Definition: auxiliary.h:85
Class GAL is the abstract interface for drawing on a 2D-surface.