KiCad PCB EDA Suite
plot_schematic_PDF.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2010 Jean-Pierre Charras jp.charras at wanadoo.fr
8  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <fctsys.h>
29 #include <plotter.h>
30 #include <sch_edit_frame.h>
31 #include <base_units.h>
32 #include <sch_sheet_path.h>
33 #include <sch_painter.h>
34 #include <schematic.h>
35 #include <pgm_base.h>
36 #include <project.h>
37 #include <general.h>
39 
40 #include <reporter.h>
41 
42 #include <dialog_plot_schematic.h>
43 #include <wx_html_report_panel.h>
44 
45 void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotFrameRef,
46  RENDER_SETTINGS* aRenderSettings )
47 {
48  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
49 
50  /* When printing all pages, the printed page is not the current page. In
51  * complex hierarchies, we must update component references and others
52  * parameters in the given printed SCH_SCREEN, accordint to the sheet path
53  * because in complex hierarchies a SCH_SCREEN (a drawing ) is shared
54  * between many sheets and component references depend on the actual sheet
55  * path used
56  */
57  SCH_SHEET_LIST sheetList;
58 
59  if( aPlotAll )
60  sheetList.BuildSheetList( &m_parent->Schematic().Root() );
61  else
62  sheetList.push_back( m_parent->GetCurrentSheet() );
63 
64  // Allocate the plotter and set the job level parameter
65  PDF_PLOTTER* plotter = new PDF_PLOTTER();
66  plotter->SetRenderSettings( aRenderSettings );
67  plotter->SetColorMode( getModeColor() );
68  plotter->SetCreator( wxT( "Eeschema-PDF" ) );
69  plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
70 
71  wxString msg;
72  wxFileName plotFileName;
73  REPORTER& reporter = m_MessagesBox->Reporter();
74  LOCALE_IO toggle; // Switch the locale to standard C
75 
76  for( unsigned i = 0; i < sheetList.size(); i++ )
77  {
78  m_parent->SetCurrentSheet( sheetList[i] );
82 
83  if( i == 0 )
84  {
85 
86  try
87  {
88  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
89  wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
90  plotFileName = createPlotFileName( fname, ext, &reporter );
91 
92  if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
93  {
94  msg.Printf( _( "Unable to create file \"%s\".\n" ), plotFileName.GetFullPath() );
95  reporter.Report( msg, RPT_SEVERITY_ERROR );
96  delete plotter;
97  return;
98  }
99 
100  // Open the plotter and do the first page
101  setupPlotPagePDF( plotter, screen );
102  plotter->StartPlot();
103  }
104  catch( const IO_ERROR& e )
105  {
106  // Cannot plot PDF file
107  msg.Printf( wxT( "PDF Plotter exception: %s" ), e.What() );
108  reporter.Report( msg, RPT_SEVERITY_ERROR );
109 
110  restoreEnvironment( plotter, oldsheetpath );
111  return;
112  }
113 
114  }
115  else
116  {
117  /* For the following pages you need to close the (finished) page,
118  * reconfigure, and then start a new one */
119  plotter->ClosePage();
120  setupPlotPagePDF( plotter, screen );
121  plotter->StartPage();
122  }
123 
124  plotOneSheetPDF( plotter, screen, aPlotFrameRef );
125  }
126 
127  // Everything done, close the plot and restore the environment
128  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
129  reporter.Report( msg, RPT_SEVERITY_ACTION );
130 
131  restoreEnvironment( plotter, oldsheetpath );
132 }
133 
134 
136  SCH_SHEET_PATH& aOldsheetpath )
137 {
138  aPlotter->EndPlot();
139  delete aPlotter;
140 
141  // Restore the previous sheet
142  m_parent->SetCurrentSheet( aOldsheetpath );
145 }
146 
147 
149  SCH_SCREEN* aScreen,
150  bool aPlotFrameRef )
151 {
152  if( m_plotBackgroundColor->GetValue() )
153  {
154  aPlotter->SetColor( aPlotter->RenderSettings()->GetBackgroundColor() );
155  wxPoint end( aPlotter->PageSettings().GetWidthIU(),
156  aPlotter->PageSettings().GetHeightIU() );
157  aPlotter->Rect( wxPoint( 0, 0 ), end, FILLED_SHAPE, 1.0 );
158  }
159 
160  if( aPlotFrameRef )
161  {
162  PlotWorkSheet( aPlotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
165  aScreen->GetFileName(),
166  aPlotter->GetColorMode() ?
168  COLOR4D::BLACK );
169  }
170 
171  aScreen->Plot( aPlotter );
172 }
173 
174 
176 {
177  PAGE_INFO plotPage; // page size selected to plot
178  // Considerations on page size and scaling requests
179  const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
180 
181  switch( m_pageSizeSelect )
182  {
183  case PAGE_SIZE_A:
184  plotPage.SetType( wxT( "A" ) );
185  plotPage.SetPortrait( actualPage.IsPortrait() );
186  break;
187 
188  case PAGE_SIZE_A4:
189  plotPage.SetType( wxT( "A4" ) );
190  plotPage.SetPortrait( actualPage.IsPortrait() );
191  break;
192 
193  case PAGE_SIZE_AUTO:
194  default:
195  plotPage = actualPage;
196  break;
197  }
198 
199  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
200  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
201  double scale = std::min( scalex, scaley );
202  aPlotter->SetPageSettings( plotPage );
203  // Currently, plot units are in decimil
204  aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
205 }
206 
static wxString GetDefaultFileExtension()
Definition: plotter.h:866
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
SCH_SHEET_LIST.
const wxString & GetFileName() const
Definition: sch_screen.h:185
int m_ScreenNumber
Definition: base_screen.h:79
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:164
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:216
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
virtual bool EndPlot() override
Implementation of conversion functions that require both schematic and board internal units.
virtual const COLOR4D & GetBackgroundColor()=0
Function GetBackgroundColor Returns current background color settings.
virtual void SetColor(COLOR4D color)=0
int GetHeightMils() const
Definition: page_info.h:141
virtual void StartPage()
Starts a new page in the PDF document.
virtual void SetTitle(const wxString &aTitle)
Definition: plotter.h:169
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:143
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition: plotter.h:146
bool SetType(const wxString &aStandardPageDescriptionName, bool aIsPortrait=false)
Function SetType sets the name of the page type and also the sizes and margins commonly associated wi...
Definition: page_info.cpp:119
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: color4d.h:44
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:180
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void restoreEnvironment(PDF_PLOTTER *aPlotter, SCH_SHEET_PATH &aOldsheetpath)
Everything done, close the plot and restore the environment.
void setupPlotPagePDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen)
wxString GetScreenDesc() const override
Return a human-readable description of the current screen.
virtual void ClosePage()
Close the current page in the PDF document (and emit its compressed stream)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
void PlotWorkSheet(PLOTTER *plotter, const PROJECT *aProject, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, int aSheetNumber, int aNumberOfSheets, const wxString &aSheetDesc, const wxString &aFilename, COLOR4D aColor)
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
REPORTER & Reporter()
returns the reporter object that reports to this panel
SCHEMATIC & Schematic() const
SCH_SHEET_PATH.
void BuildSheetList(SCH_SHEET *aSheet)
Function BuildSheetList builds the list of sheets and their sheet path from aSheet.
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror)=0
Set the plot offset and scaling for the current plot.
void Plot(PLOTTER *aPlotter)
Plot all the schematic objects to aPlotter.
Definition: sch_screen.cpp:781
const PAGE_INFO & GetPageSettings() const override
Base plotter engine class.
Definition: plotter.h:114
SCH_SHEET & Root() const
Definition: schematic.h:94
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:147
const int scale
see class PGM_BASE
SCH_SCREEN * LastScreen()
Function LastScreen.
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
bool IsPortrait() const
Definition: page_info.h:122
#define _(s)
Definition: 3d_actions.cpp:33
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:91
const TITLE_BLOCK & GetTitleBlock() const override
void createPDFFile(bool aPlotAll, bool aPlotFrameRef, RENDER_SETTINGS *aRenderSettings)
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:77
#define IU_PER_MILS
Definition: plotter.cpp:138
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
wxString GetUniqueFilenameForCurrentSheet()
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
int m_NumberOfScreens
Definition: base_screen.h:80
SCH_SHEET_PATH & GetCurrentSheet() const
const wxString & GetTitle() const
Definition: title_block.h:65
WX_HTML_REPORT_PANEL * m_MessagesBox
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
virtual bool StartPlot() override
The PDF engine supports multiple pages; the first one is opened 'for free' the following are to be cl...
PAGE_INFO & PageSettings()
Definition: plotter.h:150
void SetPortrait(bool aIsPortrait)
Function SetPortrait will rotate the paper page 90 degrees.
Definition: page_info.cpp:186
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
wxFileName createPlotFileName(wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
void plotOneSheetPDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen, bool aPlotFrameRef)
int GetWidthMils() const
Definition: page_info.h:138
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:149
bool GetColorMode() const
Definition: plotter.h:144