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-2017 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 <project.h>
34 
35 #include <reporter.h>
36 
37 #include <dialog_plot_schematic.h>
38 #include <wx_html_report_panel.h>
39 
40 void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotFrameRef )
41 {
42  SCH_SCREEN* screen = m_parent->GetScreen();
43  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
44 
45  /* When printing all pages, the printed page is not the current page. In
46  * complex hierarchies, we must update component references and others
47  * parameters in the given printed SCH_SCREEN, accordint to the sheet path
48  * because in complex hierarchies a SCH_SCREEN (a drawing ) is shared
49  * between many sheets and component references depend on the actual sheet
50  * path used
51  */
52  SCH_SHEET_LIST sheetList;
53 
54  if( aPlotAll )
55  sheetList.BuildSheetList( g_RootSheet );
56  else
57  sheetList.push_back( m_parent->GetCurrentSheet() );
58 
59  // Allocate the plotter and set the job level parameter
60  PDF_PLOTTER* plotter = new PDF_PLOTTER();
62  plotter->SetColorMode( getModeColor() );
63  plotter->SetCreator( wxT( "Eeschema-PDF" ) );
64  plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
65 
66  wxString msg;
67  wxFileName plotFileName;
68  REPORTER& reporter = m_MessagesBox->Reporter();
69  LOCALE_IO toggle; // Switch the locale to standard C
70 
71  for( unsigned i = 0; i < sheetList.size(); i++ )
72  {
73  m_parent->SetCurrentSheet( sheetList[i] );
76  screen = m_parent->GetCurrentSheet().LastScreen();
77 
78  if( i == 0 )
79  {
80 
81  try
82  {
83  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
84  wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
86  fname, ext, &reporter );
87 
88  if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
89  {
90  msg.Printf( _( "Unable to create file \"%s\".\n" ),
91  GetChars( plotFileName.GetFullPath() ) );
92  reporter.Report( msg, REPORTER::RPT_ERROR );
93  delete plotter;
94  return;
95  }
96 
97  // Open the plotter and do the first page
98  setupPlotPagePDF( plotter, screen );
99  plotter->StartPlot();
100  }
101  catch( const IO_ERROR& e )
102  {
103  // Cannot plot PDF file
104  msg.Printf( wxT( "PDF Plotter exception: %s" ), GetChars( e.What() ) );
105  reporter.Report( msg, REPORTER::RPT_ERROR );
106 
107  restoreEnvironment( plotter, oldsheetpath );
108  return;
109  }
110 
111  }
112  else
113  {
114  /* For the following pages you need to close the (finished) page,
115  * reconfigure, and then start a new one */
116  plotter->ClosePage();
117  setupPlotPagePDF( plotter, screen );
118  plotter->StartPage();
119  }
120 
121  plotOneSheetPDF( plotter, screen, aPlotFrameRef );
122  }
123 
124  // Everything done, close the plot and restore the environment
125  msg.Printf( _( "Plot: \"%s\" OK.\n" ), GetChars( plotFileName.GetFullPath() ) );
126  reporter.Report( msg, REPORTER::RPT_ACTION );
127 
128  restoreEnvironment( plotter, oldsheetpath );
129 }
130 
131 
133  SCH_SHEET_PATH& aOldsheetpath )
134 {
135  aPlotter->EndPlot();
136  delete aPlotter;
137 
138  // Restore the previous sheet
139  m_parent->SetCurrentSheet( aOldsheetpath );
142 }
143 
144 
146  SCH_SCREEN* aScreen,
147  bool aPlotFrameRef )
148 {
149  if( aPlotFrameRef )
150  {
151  aPlotter->SetColor( BLACK );
152  PlotWorkSheet( aPlotter, m_parent->GetTitleBlock(),
154  aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
156  aScreen->GetFileName() );
157  }
158 
159  aScreen->Plot( aPlotter );
160 }
161 
162 
164 {
165  PAGE_INFO plotPage; // page size selected to plot
166  // Considerations on page size and scaling requests
167  const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
168 
169  switch( m_pageSizeSelect )
170  {
171  case PAGE_SIZE_A:
172  plotPage.SetType( wxT( "A" ) );
173  plotPage.SetPortrait( actualPage.IsPortrait() );
174  break;
175 
176  case PAGE_SIZE_A4:
177  plotPage.SetType( wxT( "A4" ) );
178  plotPage.SetPortrait( actualPage.IsPortrait() );
179  break;
180 
181  case PAGE_SIZE_AUTO:
182  default:
183  plotPage = actualPage;
184  break;
185  }
186 
187  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
188  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
189  double scale = std::min( scalex, scaley );
190  aPlotter->SetPageSettings( plotPage );
191  // Currently, plot units are in decimil
192  aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
193 }
194 
static wxString GetDefaultFileExtension()
Definition: plotter.h:826
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
Class SCH_SHEET_LIST.
int m_ScreenNumber
Definition: base_screen.h:216
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:160
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
virtual bool EndPlot() override
Implementation of conversion functions that require both schematic and board internal units...
virtual void SetColor(COLOR4D color)=0
virtual void StartPage()
Starts a new page in the PDF document.
virtual void SetTitle(const wxString &aTitle)
Definition: plotter.h:165
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
const wxString & GetFileName() const
Definition: sch_screen.h:120
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:117
SCH_SCREEN * LastScreen() const
Function LastScreen.
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.cpp:538
void restoreEnvironment(PDF_PLOTTER *aPlotter, SCH_SHEET_PATH &aOldsheetpath)
Everything done, close the plot and restore the environment.
void PlotWorkSheet(PLOTTER *plotter, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, int aSheetNumber, int aNumberOfSheets, const wxString &aSheetDesc, const wxString &aFilename)
void setupPlotPagePDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen)
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
virtual wxString GetScreenDesc() const override
virtual void ClosePage()
Close the current page in the PDF document (and emit its compressed stream)
const wxString & GetTitle() const
Definition: title_block.h:64
bool IsPortrait() const
Definition: page_info.h:116
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definition: PS_plotter.cpp:52
wxFileName createPlotFileName(wxTextCtrl *aOutputDirectoryName, wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:49
REPORTER & Reporter()
returns the reporter object that reports to this panel
SCH_SHEET_PATH & GetCurrentSheet()
Class SCH_SHEET_PATH.
void BuildSheetList(SCH_SHEET *aSheet)
Function BuildSheetList builds the list of sheets and their sheet path from aSheet.
int GetWidthMils() const
Definition: page_info.h:132
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:556
const PAGE_INFO & GetPageSettings() const override
void createPDFFile(bool aPlotAll, bool aPlotFrameRef)
Base plotter engine class.
Definition: plotter.h:96
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const int scale
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
const TITLE_BLOCK & GetTitleBlock() const override
wxString GetUniqueFilenameForCurrentSheet()
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
Definition: PDF_plotter.cpp:49
int m_NumberOfScreens
Definition: base_screen.h:217
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:115
Definition: colors.h:45
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 &#39;for free&#39; the following are to be cl...
void SetPortrait(bool aIsPortrait)
Function SetPortrait will rotate the paper page 90 degrees.
Definition: page_info.cpp:182
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
int GetHeightMils() const
Definition: page_info.h:135
virtual void SetColorMode(bool _color_mode)
Definition: plotter.h:125
void plotOneSheetPDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen, bool aPlotFrameRef)
#define min(a, b)
Definition: auxiliary.h:85