KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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) 2023 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "sch_printout.h"
22#include <sch_edit_frame.h>
23#include <math/vector2wx.h>
24#include <pgm_base.h>
27#include <sch_painter.h>
28
29#include <view/view.h>
30#include <gal/gal_print.h>
32#include <gal/painter.h>
33#include <zoom_defines.h>
34
35
36SCH_PRINTOUT::SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle, bool aUseCairo ) :
37 wxPrintout( aTitle )
38{
39 wxASSERT( aParent != nullptr );
40 m_parent = aParent;
41 m_useCairo = aUseCairo;
42 m_view = nullptr;
43}
44
45
46void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
47{
48 *minPage = *selPageFrom = 1;
49 *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
50}
51
52
53bool SCH_PRINTOUT::HasPage( int pageNum )
54{
55 return m_parent->Schematic().Root().CountSheets() >= pageNum;
56}
57
58
59bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
60{
61 if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
62 return false;
63
64 return true;
65}
66
67
69{
71
72 wxCHECK_MSG( page >= 1 && page <= (int)sheetList.size(), false,
73 wxT( "Cannot print invalid page number." ) );
74
75 wxCHECK_MSG( sheetList[ page - 1].LastScreen() != nullptr, false,
76 wxT( "Cannot print page with NULL screen." ) );
77
78 wxString msg;
79 msg.Printf( _( "Print page %d" ), page );
80 m_parent->SetMsgPanel( msg, wxEmptyString );
81
82 SCH_SCREEN* screen = m_parent->GetScreen();
83 SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
84 m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
89 PrintPage( screen );
90 m_parent->SetCurrentSheet( oldsheetpath );
93
94 return true;
95}
96
97
98int SCH_PRINTOUT::milsToIU( int aMils )
99{
100 return KiROUND( aMils * schIUScale.IU_PER_MILS );
101}
102
103/*
104 * This is the real print function: print the active screen
105 */
107{
108 if( !m_useCairo )
109 {
110 // Version using print to a wxDC
111 // Warning:
112 // When printing many pages, changes in the current wxDC will affect all next printings
113 // because all prints are using the same wxPrinterDC after creation
114 // So be careful and reinit parameters, especially when using offsets.
115
116 VECTOR2I tmp_startvisu;
117 wxSize pageSizeIU; // Page size in internal units
118 VECTOR2I old_org;
119 wxRect fitRect;
120 wxDC* dc = GetDC();
121
122 wxBusyCursor dummy;
123
124 // Save current offsets and clip box.
125 tmp_startvisu = aScreen->m_StartVisu;
126 old_org = aScreen->m_DrawOrg;
127
128 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
131
132 // Change scale factor and offset to print the whole page.
133 bool printDrawingSheet = cfg->m_Printing.title_block;
134
135 pageSizeIU = ToWxSize( aScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS ) );
136 FitThisSizeToPaper( pageSizeIU );
137
138 fitRect = GetLogicalPaperRect();
139
140 // When is the actual paper size does not match the schematic page size, the drawing will
141 // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
142 // paper draw area.
143 int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
144 int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
145
146 // Using a wxAffineMatrix2D has a big advantage: it handles different pages orientations
147 //(PORTRAIT/LANDSCAPE), but the affine matrix is not always supported
148 if( dc->CanUseTransformMatrix() )
149 {
150 wxAffineMatrix2D matrix; // starts from a unity matrix (the current wxDC default)
151
152 // Check for portrait/landscape mismatch:
153 if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
154 {
155 // Rotate the coordinates, and keep the draw coordinates inside the page
156 matrix.Rotate( M_PI_2 );
157 matrix.Translate( 0, -pageSizeIU.y );
158
159 // Recalculate the offsets and page sizes according to the page rotation
160 std::swap( pageSizeIU.x, pageSizeIU.y );
161 FitThisSizeToPaper( pageSizeIU );
162 fitRect = GetLogicalPaperRect();
163
164 xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
165 yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
166
167 // All the coordinates will be rotated 90 deg when printing,
168 // so the X,Y offset vector must be rotated -90 deg before printing
169 std::swap( xoffset, yoffset );
170 std::swap( fitRect.width, fitRect.height );
171 yoffset = -yoffset;
172 }
173
174 matrix.Translate( xoffset, yoffset );
175 dc->SetTransformMatrix( matrix );
176
177 fitRect.x -= xoffset;
178 fitRect.y -= yoffset;
179 }
180 else
181 {
182 SetLogicalOrigin( 0, 0 ); // Reset all offset settings made previously.
183 // When printing previous pages (all prints are using the same wxDC)
184 OffsetLogicalOrigin( xoffset, yoffset );
185 }
186
187 dc->SetLogicalFunction( wxCOPY );
188 GRResetPenAndBrush( dc );
189
190 COLOR4D savedBgColor = m_parent->GetDrawBgColor();
192
193 if( cfg->m_Printing.background )
194 {
195 if( cfg->m_Printing.use_theme && theme )
196 bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
197 }
198 else
199 {
200 bgColor = COLOR4D::WHITE;
201 }
202
203 m_parent->SetDrawBgColor( bgColor );
204
205 GRSFilledRect( dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(), fitRect.GetBottom(), 0,
206 bgColor, bgColor );
207
208 if( cfg->m_Printing.monochrome )
209 GRForceBlackPen( true );
210
212 renderSettings.SetPrintDC( dc );
213
214 if( cfg->m_Printing.use_theme && theme )
215 renderSettings.LoadColors( theme );
216
217 renderSettings.SetBackgroundColor( bgColor );
218
219 // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
220 // if they just use the PCB layer.
221 renderSettings.SetLayerColor( LAYER_DRAWINGSHEET,
223
224 renderSettings.SetDefaultFont( cfg->m_Appearance.default_font );
225
226 if( printDrawingSheet )
227 {
228 m_parent->PrintDrawingSheet( &renderSettings, aScreen, aScreen->Schematic()->GetProperties(),
229 schIUScale.IU_PER_MILS, aScreen->GetFileName(), wxEmptyString );
230 }
231
232 renderSettings.SetIsPrinting( true );
233
234 aScreen->Print( &renderSettings );
235
236 m_parent->SetDrawBgColor( savedBgColor );
237
238 GRForceBlackPen( false );
239
240 aScreen->m_StartVisu = tmp_startvisu;
241 aScreen->m_DrawOrg = old_org;
242 }
243 else
244 {
245 wxDC* dc = GetDC();
249 std::unique_ptr<KIGFX::GAL_PRINT> galPrint = KIGFX::GAL_PRINT::Create( options, dc );
250 KIGFX::GAL* gal = galPrint->GetGAL();
251 KIGFX::PRINT_CONTEXT* printCtx = galPrint->GetPrintCtx();
252 std::unique_ptr<KIGFX::SCH_PAINTER> painter = std::make_unique<KIGFX::SCH_PAINTER>( gal );
253 std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
254
255 painter->SetSchematic( &m_parent->Schematic() );
256
257 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
260
261 // Target paper size
262 wxRect pageSizePx = GetLogicalPageRect();
263 const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
264 (double) pageSizePx.height / dc->GetPPI().y );
265 const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
266
267 galPrint->SetSheetSize( pageSizeIn );
268
269 view->SetGAL( gal );
270 view->SetPainter( painter.get() );
271 view->SetScaleLimits( ZOOM_MAX_LIMIT_EESCHEMA, ZOOM_MIN_LIMIT_EESCHEMA );
272 view->SetScale( 1.0 );
274
275 // Init the SCH_RENDER_SETTINGS used by the painter used to print schematic
276 KIGFX::SCH_RENDER_SETTINGS* dstSettings = painter->GetSettings();
277
278 dstSettings->m_ShowPinsElectricalType = false;
279
280 // Set the color scheme
281 dstSettings->LoadColors( m_parent->GetColorSettings( false ) );
282
283 if( cfg->m_Printing.use_theme && theme )
284 dstSettings->LoadColors( theme );
285
286 bool printDrawingSheet = cfg->m_Printing.title_block;
287
289
290 if( cfg->m_Printing.background )
291 {
292 if( cfg->m_Printing.use_theme && theme )
293 bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
294 }
295 else
296 {
297 bgColor = COLOR4D::WHITE;
298 }
299
300 dstSettings->SetBackgroundColor( bgColor );
301
302 // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
303 // if they just use the PCB layer.
304 dstSettings->SetLayerColor( LAYER_DRAWINGSHEET,
306
307 dstSettings->SetDefaultFont( cfg->m_Appearance.default_font );
308
309 if( cfg->m_Printing.monochrome )
310 {
311 for( int i = 0; i < LAYER_ID_COUNT; ++i )
312 dstSettings->SetLayerColor( i, COLOR4D::BLACK );
313
314 // In B&W mode, draw the background only in white, because any other color
315 // will be replaced by a black background
316 dstSettings->SetBackgroundColor( COLOR4D::WHITE );
317 dstSettings->m_OverrideItemColors = true;
318
319 // Disable print some backgrounds
320 dstSettings->SetPrintBlackAndWhite( true );
321 }
322 else // color enabled
323 {
324 for( int i = 0; i < LAYER_ID_COUNT; ++i )
325 {
326 // Cairo does not support translucent colors on PostScript surfaces
327 // see 'Features support by the PostScript surface' on
328 // https://www.cairographics.org/documentation/using_the_postscript_surface/
329 dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
330 }
331 }
332
333 dstSettings->SetIsPrinting( true );
334
335 VECTOR2I sheetSizeIU = aScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
336 BOX2I drawingAreaBBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
337
338 // Enable all layers and use KIGFX::TARGET_NONCACHED to force update drawings
339 // for printing with current GAL instance
340 for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
341 {
342 view->SetLayerVisible( i, true );
343 view->SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
344 }
345
346 view->SetLayerVisible( LAYER_DRAWINGSHEET, printDrawingSheet );
347
348 // When is the actual paper size does not match the schematic page size,
349 // we need to adjust the print scale to fit the selected paper size (pageSizeIU)
350 double scaleX = (double) pageSizeIU.x / drawingAreaBBox.GetWidth();
351 double scaleY = (double) pageSizeIU.y / drawingAreaBBox.GetHeight();
352
353 double print_scale = std::min( scaleX, scaleY );
354
355 galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
356 gal->SetLookAtPoint( drawingAreaBBox.Centre() );
357 gal->SetZoomFactor( print_scale );
358 gal->SetClearColor( dstSettings->GetBackgroundColor() );
359 gal->ClearScreen();
360
361 // Needed to use the same order for printing as for screen redraw
362 view->UseDrawPriority( true );
363
364 {
366 view->Redraw();
367 }
368 }
369}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
BOX2< VECTOR2I > BOX2I
Definition: box2.h:853
VECTOR2I m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:88
VECTOR2I m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
coord_type GetHeight() const
Definition: box2.h:189
coord_type GetWidth() const
Definition: box2.h:188
Vec Centre() const
Definition: box2.h:71
Color settings are a bit different than most of the settings objects in that there can be more than o...
COLOR4D GetColor(int aLayer) const
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
virtual void SetDrawBgColor(const COLOR4D &aColor)
void PrintDrawingSheet(const RENDER_SETTINGS *aSettings, BASE_SCREEN *aScreen, const std::map< wxString, wxString > *aProperties, double aMils2Iu, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Prints the drawing-sheet (frame and title block).
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
CAIRO_ANTIALIASING_MODE cairo_antialiasing_mode
The grid style to draw the grid in.
static std::unique_ptr< GAL_PRINT > Create(GAL_DISPLAY_OPTIONS &aOptions, wxDC *aDC)
Abstract interface for drawing on a 2D-surface.
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 SetWorldUnitLength(double aWorldUnitLength)
Set the unit length.
void SetClearColor(const COLOR4D &aColor)
virtual bool HasNativeLandscapeRotation() const =0
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetDefaultFont(const wxString &aFont)
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetPrintBlackAndWhite(bool aPrintBlackAndWhite)
void SetPrintDC(wxDC *aDC)
void SetIsPrinting(bool isPrinting)
Store schematic specific render settings.
Definition: sch_painter.h:72
void SetBackgroundColor(const COLOR4D &aColor) override
Set the background color.
Definition: sch_painter.h:95
void LoadColors(const COLOR_SETTINGS *aSettings) override
const COLOR4D & GetBackgroundColor() const override
Return current background color settings.
Definition: sch_painter.h:90
static constexpr int VIEW_MAX_LAYERS
Rendering order modifier for layers that are marked as top layers.
Definition: view.h:729
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1536
const VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition: page_info.h:171
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
const std::map< wxString, wxString > * GetProperties()
Definition: schematic.h:93
SCH_SHEET & Root() const
Definition: schematic.h:105
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
EESCHEMA_SETTINGS * eeconfig() const
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
COLOR4D GetDrawBgColor() const override
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_SHEET_PATH & GetCurrentSheet() const
SCHEMATIC & Schematic() const
void RecomputeIntersheetRefs()
Update the schematic's page reference map for all global labels, and refresh the labels so that they ...
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
const KIGFX::VIEW * m_view
Definition: sch_printout.h:52
bool HasPage(int page) override
bool OnPrintPage(int page) override
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
SCH_PRINTOUT(SCH_EDIT_FRAME *aParent, const wxString &aTitle, bool aUseCairo)
SCH_EDIT_FRAME * m_parent
Source VIEW object (note that actual printing only refers to this object)
Definition: sch_printout.h:50
int milsToIU(int aMils)
void PrintPage(SCH_SCREEN *aScreen)
bool OnBeginDocument(int startPage, int endPage) override
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:131
void Print(const RENDER_SETTINGS *aSettings)
Print all the items in the screen to aDC.
const wxString & GetFileName() const
Definition: sch_screen.h:144
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:97
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
SCH_SCREEN * LastScreen()
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:788
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
#define _(s)
void GRForceBlackPen(bool flagforce)
Definition: gr_basic.cpp:159
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:73
void GRSFilledRect(wxDC *aDC, int x1, int y1, int x2, int y2, int aWidth, const COLOR4D &aColor, const COLOR4D &aBgColor)
Definition: gr_basic.cpp:422
#define LAYER_ID_COUNT
Must update this if you add any enums after GerbView!
Definition: layer_ids.h:477
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:220
@ LAYER_SCHEMATIC_DRAWINGSHEET
Definition: layer_ids.h:394
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:388
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition: definitions.h:49
see class PGM_BASE
constexpr double SCH_WORLD_UNIT(1e-7/0.0254)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
std::vector< FAB_LAYER_COLOR > dummy
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:129
bool background
Whether or not to print background color.
Definition: app_settings.h:128
wxString color_theme
Color theme to use for printing.
Definition: app_settings.h:132
bool title_block
Whether or not to print title block.
Definition: app_settings.h:133
bool use_theme
If false, display color theme will be used.
Definition: app_settings.h:131
const double IU_PER_MILS
Definition: base_units.h:78
#define M_PI_2
Definition: transline.cpp:40
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588
wxSize ToWxSize(const VECTOR2I &aSize)
Definition: vector2wx.h:55
#define ZOOM_MIN_LIMIT_EESCHEMA
Definition: zoom_defines.h:51
#define ZOOM_MAX_LIMIT_EESCHEMA
Definition: zoom_defines.h:50