KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2018 CERN
7 * Author: Maciej Suminski <[email protected]>
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 <lset.h>
30#include <view/view.h>
31#include <gal/gal_print.h>
33#include <gal/painter.h>
34#include <pcbplot.h>
36
37
39 : PRINTOUT_SETTINGS( aPageInfo )
40{
41 m_LayerSet.set();
42 m_Mirror = false;
43}
44
45
47{
48 PRINTOUT_SETTINGS::Load( aConfig );
49
50 m_LayerSet.reset();
51
52 for( int layer : aConfig->m_Printing.layers )
53 m_LayerSet.set( layer, true );
54
55 m_Mirror = aConfig->m_Printing.mirror;
56}
57
58
60{
61 PRINTOUT_SETTINGS::Save( aConfig );
62
63 aConfig->m_Printing.layers.clear();
64
65 for( unsigned layer = 0; layer < m_LayerSet.size(); ++layer )
66 if( m_LayerSet.test( layer ) )
67 aConfig->m_Printing.layers.push_back( layer );
68
69 aConfig->m_Printing.mirror = m_Mirror;
70}
71
72
74 const KIGFX::VIEW* aView, const wxString& aTitle ) :
75 wxPrintout( aTitle ),
76 m_settings( aParams )
77{
78 m_view = aView;
79 m_gerbviewPrint = false;
80}
81
82
83void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
84{
85 *minPage = 1;
86 *selPageFrom = 1;
87
88 *maxPage = m_settings.m_pageCount;
89 *selPageTo = m_settings.m_pageCount;
90}
91
92
93void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
94{
95 wxDC* dc = GetDC();
97 std::unique_ptr<KIGFX::GAL_PRINT> galPrint = KIGFX::GAL_PRINT::Create( options, dc );
98 KIGFX::GAL* gal = galPrint->GetGAL();
99 KIGFX::PRINT_CONTEXT* printCtx = galPrint->GetPrintCtx();
100 std::unique_ptr<KIGFX::PAINTER> painter = getPainter( gal );
101 std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
102
103 // Target paper size
104 wxRect pageSizePx = GetLogicalPageRect();
105 const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
106 (double) pageSizePx.height / dc->GetPPI().y );
107 const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
108
109 galPrint->SetSheetSize( pageSizeIn );
110
111 view->SetGAL( gal );
112 view->SetPainter( painter.get() );
113 view->SetScaleLimits( 10e9, 0.0001 );
114 view->SetScale( 1.0 );
115
116
117 // Set the color scheme
118 RENDER_SETTINGS* dstSettings = view->GetPainter()->GetSettings();
119 dstSettings->LoadColors( m_settings.m_colorSettings );
120
121 if( m_settings.m_blackWhite )
122 {
123 for( int i = 0; i < LAYER_ID_COUNT; ++i )
124 dstSettings->SetLayerColor( i, COLOR4D::BLACK );
125
126 // In B&W mode, draw the background only in wxhite, because any other color
127 // will be replaced by a black background
128 dstSettings->SetBackgroundColor( COLOR4D::WHITE );
129 }
130 else // color enabled
131 {
132 for( int i = 0; i < LAYER_ID_COUNT; ++i )
133 {
134 // Cairo does not support translucent colors on PostScript surfaces
135 // see 'Features support by the PostScript surface' on
136 // https://www.cairographics.org/documentation/using_the_postscript_surface/
137 dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
138 }
139 }
140
141 dstSettings->SetIsPrinting( true );
142
143 setupPainter( *painter );
144 setupViewLayers( *view, m_settings.m_LayerSet );
145 dstSettings->SetPrintLayers( m_settings.m_LayerSet );
146
147 dstSettings->SetLayerName( aLayerName );
148
149 VECTOR2I sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
150 VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.x ),
151 milsToIU( sheetSizeMils.y ) );
152 BOX2I drawingAreaBBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
153
154 // When printing the board without worksheet items, move board center to the
155 // drawing area center.
156 if( !m_settings.PrintBorderAndTitleBlock() )
157 drawingAreaBBox = getBoundingBox();
158
159 view->SetLayerVisible( LAYER_DRAWINGSHEET, m_settings.PrintBorderAndTitleBlock() );
160
161 // Fit to page (drawingAreaBBox)
162 if( m_settings.m_scale <= 0.0 )
163 {
164 if( drawingAreaBBox.GetWidth() == 0 || drawingAreaBBox.GetHeight() == 0 )
165 {
166 // Nothing to print (empty board and no worksheet)
167 m_settings.m_scale = 1.0;
168 }
169 else
170 {
171 double scaleX = (double) pageSizeIU.x / drawingAreaBBox.GetWidth();
172 double scaleY = (double) pageSizeIU.y / drawingAreaBBox.GetHeight();
173 m_settings.m_scale = std::min( scaleX, scaleY );
174 }
175 }
176
177 setupGal( gal );
178 galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
179 gal->SetLookAtPoint( drawingAreaBBox.Centre() );
180 gal->SetZoomFactor( m_settings.m_scale );
181 gal->SetClearColor( dstSettings->GetBackgroundColor() );
182
183 // Clearing the screen for the background color needs the screen set to the page size
184 // in pixels. This can ?somehow? prevent some but not all foreground elements from being printed
185 // TODO: figure out what's going on here and fix printing. See also sch_printout
186 VECTOR2I size = gal->GetScreenPixelSize();
187 gal->ResizeScreen( pageSizePx.GetWidth(),pageSizePx.GetHeight() );
188 gal->ClearScreen();
189 gal->ResizeScreen( size.x, size.y );
190
191 if( m_gerbviewPrint )
192 // Mandatory in Gerbview to use the same order for printing as for screen redraw
193 // due to negative objects that need a specific order
194 view->UseDrawPriority( true );
195
196 {
198 view->Redraw();
199 }
200}
201
202
203void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
204{
205 // Disable all layers by default, let specific implementations enable required layers
206 for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
207 {
208 aView.SetLayerVisible( i, false );
209 aView.SetTopLayer( i, false );
211 }
212}
213
214
216{
217 if( !m_settings.m_background )
219}
220
221
223{
224 aGal->SetFlip( m_settings.m_Mirror, false );
225}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
virtual void DrawPage(const wxString &aLayerName=wxEmptyString, int aPageNum=1, int aPageCount=1)
Print a page (or a set of pages).
bool m_gerbviewPrint
True if the caller is Gerbview, false for Pcbnew.
virtual void setupPainter(KIGFX::PAINTER &aPainter)
Configure #PAINTER object for a printout.
virtual int milsToIU(double aMils) const =0
Convert mils to internal units.
virtual std::unique_ptr< KIGFX::PAINTER > getPainter(KIGFX::GAL *aGal)=0
Return the #PAINTER instance used to draw the items.
const KIGFX::VIEW * m_view
Source VIEW object (note that actual printing only refers to this object).
virtual void setupViewLayers(KIGFX::VIEW &aView, const LSET &aLayerSet)
Enable layers visibility for a printout.
BOARD_PRINTOUT(const BOARD_PRINTOUT_SETTINGS &aParams, const KIGFX::VIEW *aView, const wxString &aTitle)
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
virtual void setupGal(KIGFX::GAL *aGal)
Configure GAL object for a printout.
BOARD_PRINTOUT_SETTINGS m_settings
Printout parameters.
virtual BOX2I getBoundingBox()=0
Return bounding box of the printed objects (excluding drawing-sheet frame).
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr Vec Centre() const
Definition box2.h:97
constexpr size_type GetHeight() const
Definition box2.h:215
static const COLOR4D WHITE
Definition color4d.h:405
static const COLOR4D BLACK
Definition color4d.h:406
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:312
static std::unique_ptr< GAL_PRINT > Create(GAL_DISPLAY_OPTIONS &aOptions, wxDC *aDC)
Abstract interface for drawing on a 2D-surface.
virtual void ResizeScreen(int aWidth, int aHeight)
Resize the canvas.
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
void SetZoomFactor(double aZoomFactor)
void SetLookAtPoint(const VECTOR2D &aPoint)
Get/set the Point in world space to look at.
virtual void ClearScreen()
Clear the screen.
void SetClearColor(const COLOR4D &aColor)
const VECTOR2I & GetScreenPixelSize() const
Return GAL canvas size in pixels.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition painter.h:59
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
virtual bool HasNativeLandscapeRotation() const =0
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
virtual void LoadColors(const COLOR_SETTINGS *aSettings)
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetPrintLayers(const LSET &aLayerSet)
virtual void SetBackgroundColor(const COLOR4D &aColor)=0
Set the background color.
void SetLayerName(const wxString &aLayerName)
virtual const COLOR4D & GetBackgroundColor() const =0
Return current background color settings.
void SetIsPrinting(bool isPrinting)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:497
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:400
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:745
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:840
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition layer_ids.h:626
@ LAYER_DRAWINGSHEET
Sheet frame and title block.
Definition layer_ids.h:278
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition definitions.h:38
bool mirror
Print mirrored.
std::vector< int > layers
List of enabled layers for printing.
void Load(APP_SETTINGS_BASE *aConfig) override
BOARD_PRINTOUT_SETTINGS(const PAGE_INFO &aPageInfo)
LSET m_LayerSet
Layers to print.
void Save(APP_SETTINGS_BASE *aConfig) override
bool m_Mirror
Print mirrored.
virtual void Save(APP_SETTINGS_BASE *aConfig)
Definition printout.cpp:24
PRINTOUT_SETTINGS(const PAGE_INFO &aPageInfo)
Definition printout.h:33
virtual void Load(APP_SETTINGS_BASE *aConfig)
Definition printout.cpp:32
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694