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, 2024 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 <tool/tool_manager.h>
24#include <sch_edit_frame.h>
25#include <math/vector2wx.h>
26#include <pgm_base.h>
29#include <sch_painter.h>
30
31#include <view/view.h>
32#include <gal/gal_print.h>
34#include <gal/painter.h>
35#include <zoom_defines.h>
37#include <string_utils.h>
38
39
40SCH_PRINTOUT::SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle, bool aUseCairo ) :
41 wxPrintout( aTitle )
42{
43 wxASSERT( aParent != nullptr );
44 m_parent = aParent;
45 m_useCairo = aUseCairo;
46 m_view = nullptr;
47}
48
49
50void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
51{
52 *minPage = *selPageFrom = 1;
53 *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
54}
55
56
57bool SCH_PRINTOUT::HasPage( int pageNum )
58{
59 return m_parent->Schematic().Root().CountSheets() >= pageNum;
60}
61
62
63bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
64{
65 if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
66 return false;
67
68 return true;
69}
70
71
73{
75 sheetList.SortByPageNumbers( false );
76
77 wxCHECK_MSG( page >= 1 && page <= (int)sheetList.size(), false,
78 wxT( "Cannot print invalid page number." ) );
79
80 wxCHECK_MSG( sheetList[ page - 1].LastScreen() != nullptr, false,
81 wxT( "Cannot print page with NULL screen." ) );
82
83 wxString msg;
84 msg.Printf( _( "Print page %d" ), page );
85 m_parent->SetMsgPanel( msg, wxEmptyString );
86
87 SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
88
89 // Switch to the new current sheet
90 m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
95 // Ensure the displayed page number is updated:
96 KIGFX::SCH_VIEW* sch_view = m_parent->GetCanvas()->GetView();
97 sch_view->GetDrawingSheet()->SetPageNumber( TO_UTF8( screen->GetPageNumber() ) );
98
99 PrintPage( screen );
100
101 // Restore the initial current sheet
102 m_parent->SetCurrentSheet( oldsheetpath );
105 screen = m_parent->GetCurrentSheet().LastScreen();
106 sch_view->GetDrawingSheet()->SetPageNumber( TO_UTF8( screen->GetPageNumber() ) );
107
108 return true;
109}
110
111
113{
114 return KiROUND( aMils * schIUScale.IU_PER_MILS );
115}
116
117
118/*
119 * This is the real print function: print the active screen
120 */
122{
123 if( !m_useCairo )
124 {
125 // Version using print to a wxDC
126 // Warning:
127 // When printing many pages, changes in the current wxDC will affect all next printings
128 // because all prints are using the same wxPrinterDC after creation
129 // So be careful and reinit parameters, especially when using offsets.
130
131 VECTOR2I tmp_startvisu;
132 wxSize pageSizeIU; // Page size in internal units
133 VECTOR2I old_org;
134 wxRect fitRect;
135 wxDC* dc = GetDC();
136
137 wxBusyCursor dummy;
138
139 // Save current offsets and clip box.
140 tmp_startvisu = aScreen->m_StartVisu;
141 old_org = aScreen->m_DrawOrg;
142
146
147 // Change scale factor and offset to print the whole page.
148 bool printDrawingSheet = cfg->m_Printing.title_block;
149
150 pageSizeIU = ToWxSize( aScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS ) );
151 FitThisSizeToPaper( pageSizeIU );
152
153 fitRect = GetLogicalPaperRect();
154
155 // When is the actual paper size does not match the schematic page size, the drawing will
156 // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
157 // paper draw area.
158 int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
159 int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
160
161 // Using a wxAffineMatrix2D has a big advantage: it handles different pages orientations
162 //(PORTRAIT/LANDSCAPE), but the affine matrix is not always supported
163 if( dc->CanUseTransformMatrix() )
164 {
165 wxAffineMatrix2D matrix; // starts from a unity matrix (the current wxDC default)
166
167 // Check for portrait/landscape mismatch:
168 if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
169 {
170 // Rotate the coordinates, and keep the draw coordinates inside the page
171 matrix.Rotate( M_PI_2 );
172 matrix.Translate( 0, -pageSizeIU.y );
173
174 // Recalculate the offsets and page sizes according to the page rotation
175 std::swap( pageSizeIU.x, pageSizeIU.y );
176 FitThisSizeToPaper( pageSizeIU );
177 fitRect = GetLogicalPaperRect();
178
179 xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
180 yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
181
182 // All the coordinates will be rotated 90 deg when printing,
183 // so the X,Y offset vector must be rotated -90 deg before printing
184 std::swap( xoffset, yoffset );
185 std::swap( fitRect.width, fitRect.height );
186 yoffset = -yoffset;
187 }
188
189 matrix.Translate( xoffset, yoffset );
190 dc->SetTransformMatrix( matrix );
191
192 fitRect.x -= xoffset;
193 fitRect.y -= yoffset;
194 }
195 else
196 {
197 SetLogicalOrigin( 0, 0 ); // Reset all offset settings made previously.
198 // When printing previous pages (all prints are using the same wxDC)
199 OffsetLogicalOrigin( xoffset, yoffset );
200 }
201
202 dc->SetLogicalFunction( wxCOPY );
203 GRResetPenAndBrush( dc );
204
205 COLOR4D savedBgColor = m_parent->GetDrawBgColor();
207
208 if( cfg->m_Printing.background )
209 {
210 if( cfg->m_Printing.use_theme && theme )
211 bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
212 }
213 else
214 {
215 bgColor = COLOR4D::WHITE;
216 }
217
218 m_parent->SetDrawBgColor( bgColor );
219
220 GRSFilledRect( dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(), fitRect.GetBottom(), 0,
221 bgColor, bgColor );
222
223 if( cfg->m_Printing.monochrome )
224 GRForceBlackPen( true );
225
226 SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
227 renderSettings.SetPrintDC( dc );
228
229 if( cfg->m_Printing.use_theme && theme )
230 renderSettings.LoadColors( theme );
231
232 renderSettings.SetBackgroundColor( bgColor );
233
234 // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
235 // if they just use the PCB layer.
236 renderSettings.SetLayerColor( LAYER_DRAWINGSHEET,
238
239 renderSettings.SetDefaultFont( cfg->m_Appearance.default_font );
240
241 if( printDrawingSheet )
242 {
243 m_parent->PrintDrawingSheet( &renderSettings, aScreen, aScreen->Schematic()->GetProperties(),
244 schIUScale.IU_PER_MILS, aScreen->GetFileName(), wxEmptyString );
245 }
246
247 renderSettings.SetIsPrinting( true );
248
249 aScreen->Print( &renderSettings );
250
251 m_parent->SetDrawBgColor( savedBgColor );
252
253 GRForceBlackPen( false );
254
255 aScreen->m_StartVisu = tmp_startvisu;
256 aScreen->m_DrawOrg = old_org;
257 }
258 else
259 {
260 wxDC* dc = GetDC();
264 std::unique_ptr<KIGFX::GAL_PRINT> galPrint = KIGFX::GAL_PRINT::Create( options, dc );
265 KIGFX::GAL* gal = galPrint->GetGAL();
266 KIGFX::PRINT_CONTEXT* printCtx = galPrint->GetPrintCtx();
267 std::unique_ptr<KIGFX::SCH_PAINTER> painter = std::make_unique<KIGFX::SCH_PAINTER>( gal );
268 std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
269
270 painter->SetSchematic( &m_parent->Schematic() );
271
276
277 // Target paper size
278 wxRect pageSizePx = GetLogicalPageRect();
279 const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
280 (double) pageSizePx.height / dc->GetPPI().y );
281 const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
282
283 galPrint->SetSheetSize( pageSizeIn );
284
285 view->SetGAL( gal );
286 view->SetPainter( painter.get() );
287 view->SetScaleLimits( ZOOM_MAX_LIMIT_EESCHEMA, ZOOM_MIN_LIMIT_EESCHEMA );
288 view->SetScale( 1.0 );
290
291 // Init the SCH_RENDER_SETTINGS used by the painter used to print schematic
292 SCH_RENDER_SETTINGS* dstSettings = painter->GetSettings();
293
294 dstSettings->m_ShowPinsElectricalType = false;
295
296 // Set the color scheme
297 dstSettings->LoadColors( m_parent->GetColorSettings( false ) );
298
299 if( cfg->m_Printing.use_theme && theme )
300 dstSettings->LoadColors( theme );
301
302 bool printDrawingSheet = cfg->m_Printing.title_block;
303
305
306 if( cfg->m_Printing.background )
307 {
308 if( cfg->m_Printing.use_theme && theme )
309 bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
310 }
311 else
312 {
313 bgColor = COLOR4D::WHITE;
314 }
315
316 dstSettings->SetBackgroundColor( bgColor );
317
318 // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
319 // if they just use the PCB layer.
320 dstSettings->SetLayerColor( LAYER_DRAWINGSHEET,
322
323 dstSettings->SetDefaultFont( cfg->m_Appearance.default_font );
324
325 if( cfg->m_Printing.monochrome )
326 {
327 for( int i = 0; i < LAYER_ID_COUNT; ++i )
328 dstSettings->SetLayerColor( i, COLOR4D::BLACK );
329
330 // In B&W mode, draw the background only in white, because any other color
331 // will be replaced by a black background
332 dstSettings->SetBackgroundColor( COLOR4D::WHITE );
333 dstSettings->m_OverrideItemColors = true;
334
335 // Disable print some backgrounds
336 dstSettings->SetPrintBlackAndWhite( true );
337 }
338 else // color enabled
339 {
340 for( int i = 0; i < LAYER_ID_COUNT; ++i )
341 {
342 // Cairo does not support translucent colors on PostScript surfaces
343 // see 'Features support by the PostScript surface' on
344 // https://www.cairographics.org/documentation/using_the_postscript_surface/
345 dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
346 }
347 }
348
349 dstSettings->SetIsPrinting( true );
350
351 VECTOR2I sheetSizeIU = aScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
352 BOX2I drawingAreaBBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
353
354 // Enable all layers and use KIGFX::TARGET_NONCACHED to force update drawings
355 // for printing with current GAL instance
356 for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
357 {
358 view->SetLayerVisible( i, true );
359 view->SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
360 }
361
362 view->SetLayerVisible( LAYER_DRAWINGSHEET, printDrawingSheet );
363
364 // Don't draw the selection if it's not from the current screen
365 for( EDA_ITEM* item : selTool->GetSelection() )
366 {
367 if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( item ) )
368 {
369 if( !m_parent->GetScreen()->CheckIfOnDrawList( schItem ) )
370 view->SetLayerVisible( LAYER_SELECT_OVERLAY, false );
371
372 break;
373 }
374 }
375
376 // When is the actual paper size does not match the schematic page size,
377 // we need to adjust the print scale to fit the selected paper size (pageSizeIU)
378 double scaleX = (double) pageSizeIU.x / drawingAreaBBox.GetWidth();
379 double scaleY = (double) pageSizeIU.y / drawingAreaBBox.GetHeight();
380
381 double print_scale = std::min( scaleX, scaleY );
382
383 galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
384 gal->SetLookAtPoint( drawingAreaBBox.Centre() );
385 gal->SetZoomFactor( print_scale );
386 gal->SetClearColor( dstSettings->GetBackgroundColor() );
387 gal->ClearScreen();
388
389 // Needed to use the same order for printing as for screen redraw
390 view->UseDrawPriority( true );
391
392 {
394 view->Redraw();
395 }
396 }
397}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
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
const wxString & GetPageNumber() const
Definition: base_screen.cpp:70
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
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 SetPageNumber(const std::string &aPageNumber)
Changes the page number displayed in the title block.
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 base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
EE_SELECTION & GetSelection()
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)
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: sch_view.h:103
static constexpr int VIEW_MAX_LAYERS
Rendering order modifier for layers that are marked as top layers.
Definition: view.h:735
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1604
const VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition: page_info.h:171
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
const std::map< wxString, wxString > * GetProperties()
Definition: schematic.h:95
SCH_SHEET & Root() const
Definition: schematic.h:121
SCH_RENDER_SETTINGS * GetRenderSettings()
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
EESCHEMA_SETTINGS * eeconfig() const
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)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
bool HasPage(int page) override
bool OnPrintPage(int page) override
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
const KIGFX::SCH_VIEW * m_view
Definition: sch_printout.h:52
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
void SetBackgroundColor(const COLOR4D &aColor) override
Set the background color.
const KIGFX::COLOR4D & GetBackgroundColor() const override
Return current background color settings.
void LoadColors(const COLOR_SETTINGS *aSettings) override
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:130
const wxString & GetFileName() const
Definition: sch_screen.h:143
void Print(const SCH_RENDER_SETTINGS *aSettings)
Print all the items in the screen to aDC.
Definition: sch_screen.cpp:854
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:100
bool CheckIfOnDrawList(const SCH_ITEM *aItem) const
Definition: sch_screen.cpp:386
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sort the list of sheets by page number.
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:845
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#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:481
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:218
@ LAYER_SELECT_OVERLAY
currently selected items overlay
Definition: layer_ids.h:220
@ LAYER_SCHEMATIC_DRAWINGSHEET
Definition: layer_ids.h:398
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:391
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition: definitions.h:49
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
constexpr double SCH_WORLD_UNIT(1e-7/0.0254)
std::vector< FAB_LAYER_COLOR > dummy
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:141
bool background
Whether or not to print background color.
Definition: app_settings.h:140
wxString color_theme
Color theme to use for printing.
Definition: app_settings.h:144
bool title_block
Whether or not to print title block.
Definition: app_settings.h:145
bool use_theme
If false, display color theme will be used.
Definition: app_settings.h:143
const double IU_PER_MILS
Definition: base_units.h:77
#define M_PI_2
Definition: transline.cpp:40
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
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