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 (C) 1992-2024 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{
42 m_Mirror = false;
43}
44
45
47{
48 PRINTOUT_SETTINGS::Load( aConfig );
49
51
52 for( int layer : aConfig->m_Printing.layers )
53 m_LayerSet.set( layer, true );
54}
55
56
58{
59 PRINTOUT_SETTINGS::Save( aConfig );
60
61 aConfig->m_Printing.layers.clear();
62
63 for( unsigned layer = 0; layer < m_LayerSet.size(); ++layer )
64 if( m_LayerSet.test( layer ) )
65 aConfig->m_Printing.layers.push_back( layer );
66}
67
68
70 const KIGFX::VIEW* aView, const wxString& aTitle ) :
71 wxPrintout( aTitle ),
72 m_settings( aParams )
73{
74 m_view = aView;
75 m_gerbviewPrint = false;
76}
77
78
79void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
80{
81 *minPage = 1;
82 *selPageFrom = 1;
83
84 *maxPage = m_settings.m_pageCount;
85 *selPageTo = m_settings.m_pageCount;
86}
87
88
89void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
90{
91 wxDC* dc = GetDC();
93 std::unique_ptr<KIGFX::GAL_PRINT> galPrint = KIGFX::GAL_PRINT::Create( options, dc );
94 KIGFX::GAL* gal = galPrint->GetGAL();
95 KIGFX::PRINT_CONTEXT* printCtx = galPrint->GetPrintCtx();
96 std::unique_ptr<KIGFX::PAINTER> painter = getPainter( gal );
97 std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
98
99 // Target paper size
100 wxRect pageSizePx = GetLogicalPageRect();
101 const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
102 (double) pageSizePx.height / dc->GetPPI().y );
103 const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
104
105 galPrint->SetSheetSize( pageSizeIn );
106
107 view->SetGAL( gal );
108 view->SetPainter( painter.get() );
109 view->SetScaleLimits( 10e9, 0.0001 );
110 view->SetScale( 1.0 );
111
112
113 // Set the color scheme
114 RENDER_SETTINGS* dstSettings = view->GetPainter()->GetSettings();
115 dstSettings->LoadColors( m_settings.m_colorSettings );
116
118 {
119 for( int i = 0; i < LAYER_ID_COUNT; ++i )
120 dstSettings->SetLayerColor( i, COLOR4D::BLACK );
121
122 // In B&W mode, draw the background only in wxhite, because any other color
123 // will be replaced by a black background
124 dstSettings->SetBackgroundColor( COLOR4D::WHITE );
125 }
126 else // color enabled
127 {
128 for( int i = 0; i < LAYER_ID_COUNT; ++i )
129 {
130 // Cairo does not support translucent colors on PostScript surfaces
131 // see 'Features support by the PostScript surface' on
132 // https://www.cairographics.org/documentation/using_the_postscript_surface/
133 dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
134 }
135 }
136
137 dstSettings->SetIsPrinting( true );
138
139 setupPainter( *painter );
141 dstSettings->SetPrintLayers( m_settings.m_LayerSet );
142
143 dstSettings->SetLayerName( aLayerName );
144
145 VECTOR2I sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
146 VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.x ),
147 milsToIU( sheetSizeMils.y ) );
148 BOX2I drawingAreaBBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
149
150 // When printing the board without worksheet items, move board center to the
151 // drawing area center.
153 drawingAreaBBox = getBoundingBox();
154
155 view->SetLayerVisible( LAYER_DRAWINGSHEET, m_settings.PrintBorderAndTitleBlock() );
156
157 // Fit to page (drawingAreaBBox)
158 if( m_settings.m_scale <= 0.0 )
159 {
160 if( drawingAreaBBox.GetWidth() == 0 || drawingAreaBBox.GetHeight() == 0 )
161 {
162 // Nothing to print (empty board and no worksheet)
163 m_settings.m_scale = 1.0;
164 }
165 else
166 {
167 double scaleX = (double) pageSizeIU.x / drawingAreaBBox.GetWidth();
168 double scaleY = (double) pageSizeIU.y / drawingAreaBBox.GetHeight();
169 m_settings.m_scale = std::min( scaleX, scaleY );
170 }
171 }
172
173 setupGal( gal );
174 galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
175 gal->SetLookAtPoint( drawingAreaBBox.Centre() );
177 gal->SetClearColor( dstSettings->GetBackgroundColor() );
178 gal->ResizeScreen( pageSizePx.GetWidth(),pageSizePx.GetHeight() );
179 gal->ClearScreen();
180
181 if( m_gerbviewPrint )
182 // Mandatory in Gerbview to use the same order for printing as for screen redraw
183 // due to negative objects that need a specific order
184 view->UseDrawPriority( true );
185
186 {
188 view->Redraw();
189 }
190}
191
192
193void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
194{
195 // Disable all layers by default, let specific implementations enable required layers
196 for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
197 {
198 aView.SetLayerVisible( i, false );
199 aView.SetTopLayer( i, false );
201 }
202}
203
204
206{
208 aPainter.GetSettings()->SetBackgroundColor( COLOR4D::WHITE );
209}
210
211
213{
214 aGal->SetFlip( m_settings.m_Mirror, false );
215}
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
BASE_SET & reset(size_t pos)
Definition: base_set.h:142
BASE_SET & set(size_t pos)
Definition: base_set.h:115
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)
Configures GAL 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
Source VIEW object (note that actual printing only refers to this object)
const KIGFX::VIEW * m_view
Printout parameters.
virtual void setupViewLayers(KIGFX::VIEW &aView, const LSET &aLayerSet)
Configures PAINTER object 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)
Returns bounding box of the printed objects (excluding drawing-sheet frame)
BOARD_PRINTOUT_SETTINGS m_settings
virtual BOX2I getBoundingBox()=0
Returns a PAINTER instance used to draw the items.
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
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
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)
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)
void SetPrintLayers(LSET aLayerSet)
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
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:68
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:494
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:397
static constexpr int VIEW_MAX_LAYERS
Rendering order modifier for layers that are marked as top layers.
Definition: view.h:742
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1586
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:829
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:59
const VECTOR2D & GetSizeMils() const
Definition: page_info.h:144
#define LAYER_ID_COUNT
Must update this if you add any enums after GerbView!
Definition: layer_ids.h:481
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:218
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition: definitions.h:38
std::vector< int > layers
List of enabled layers for printing.
Definition: app_settings.h:146
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.
Handle the parameters used to print a board drawing.
Definition: printout.h:32
bool PrintBorderAndTitleBlock() const
Returns true if the drawing border and title block should be printed.
Definition: printout.h:55
COLOR_SETTINGS * m_colorSettings
The color settings to be used for printing.
Definition: printout.h:66
virtual void Save(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:24
bool m_blackWhite
Print in B&W or Color.
Definition: printout.h:60
int m_pageCount
Number of pages to print.
Definition: printout.h:61
const PAGE_INFO & m_pageInfo
Definition: printout.h:63
bool m_background
Print background color.
Definition: printout.h:62
virtual void Load(APP_SETTINGS_BASE *aConfig)
Definition: printout.cpp:32
double m_scale
Printing scale.
Definition: printout.h:58
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691