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