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 <jean-pierre.charras@gipsa-lab.inpg.fr
8  * Copyright (C) 1992-2016 KiCad Developers, see change_log.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 <plot_common.h>
30 #include <class_sch_screen.h>
31 #include <schframe.h>
32 #include <base_units.h>
33 #include <sch_sheet_path.h>
34 #include <project.h>
35 
36 #include <reporter.h>
37 
38 #include <dialog_plot_schematic.h>
39 #include <wx_html_report_panel.h>
40 
41 void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotFrameRef )
42 {
43  SCH_SCREEN* screen = m_parent->GetScreen();
44  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
45 
46  /* When printing all pages, the printed page is not the current page. In
47  * complex hierarchies, we must update component references and others
48  * parameters in the given printed SCH_SCREEN, accordint to the sheet path
49  * because in complex hierarchies a SCH_SCREEN (a drawing ) is shared
50  * between many sheets and component references depend on the actual sheet
51  * path used
52  */
53  SCH_SHEET_LIST sheetList;
54 
55  if( aPlotAll )
56  sheetList.BuildSheetList( g_RootSheet );
57  else
58  sheetList.push_back( m_parent->GetCurrentSheet() );
59 
60  // Allocate the plotter and set the job level parameter
61  PDF_PLOTTER* plotter = new PDF_PLOTTER();
63  plotter->SetColorMode( getModeColor() );
64  plotter->SetCreator( wxT( "Eeschema-PDF" ) );
65  plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
66 
67  wxString msg;
68  wxFileName plotFileName;
69  REPORTER& reporter = m_MessagesBox->Reporter();
70  LOCALE_IO toggle; // Switch the locale to standard C
71 
72  for( unsigned i = 0; i < sheetList.size(); i++ )
73  {
74  m_parent->SetCurrentSheet( sheetList[i] );
77  screen = m_parent->GetCurrentSheet().LastScreen();
78 
79  if( i == 0 )
80  {
81 
82  try
83  {
84  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
85  wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
87  fname, ext, &reporter );
88 
89  if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
90  {
91  msg.Printf( _( "Unable to create file '%s'.\n" ),
92  GetChars( plotFileName.GetFullPath() ) );
93  reporter.Report( msg, REPORTER::RPT_ERROR );
94  delete plotter;
95  return;
96  }
97 
98  // Open the plotter and do the first page
99  setupPlotPagePDF( plotter, screen );
100  plotter->StartPlot();
101  }
102  catch( const IO_ERROR& e )
103  {
104  // Cannot plot PDF file
105  msg.Printf( wxT( "PDF Plotter exception: %s" ), GetChars( e.What() ) );
106  reporter.Report( msg, REPORTER::RPT_ERROR );
107 
108  restoreEnvironment( plotter, oldsheetpath );
109  return;
110  }
111 
112  }
113  else
114  {
115  /* For the following pages you need to close the (finished) page,
116  * reconfigure, and then start a new one */
117  plotter->ClosePage();
118  setupPlotPagePDF( plotter, screen );
119  plotter->StartPage();
120  }
121 
122  plotOneSheetPDF( plotter, screen, aPlotFrameRef );
123  }
124 
125  // Everything done, close the plot and restore the environment
126  msg.Printf( _( "Plot: '%s' OK.\n" ), GetChars( plotFileName.GetFullPath() ) );
127  reporter.Report( msg, REPORTER::RPT_ACTION );
128 
129  restoreEnvironment( plotter, oldsheetpath );
130 }
131 
132 
134  SCH_SHEET_PATH& aOldsheetpath )
135 {
136  aPlotter->EndPlot();
137  delete aPlotter;
138 
139  // Restore the previous sheet
140  m_parent->SetCurrentSheet( aOldsheetpath );
143 }
144 
145 
147  SCH_SCREEN* aScreen,
148  bool aPlotFrameRef )
149 {
150  if( aPlotFrameRef )
151  {
152  aPlotter->SetColor( BLACK );
153  PlotWorkSheet( aPlotter, m_parent->GetTitleBlock(),
155  aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
157  aScreen->GetFileName() );
158  }
159 
160  aScreen->Plot( aPlotter );
161 }
162 
163 
165 {
166  PAGE_INFO plotPage; // page size selected to plot
167  // Considerations on page size and scaling requests
168  PAGE_INFO actualPage = aScreen->GetPageSettings(); // page size selected in schematic
169 
170  switch( m_pageSizeSelect )
171  {
172  case PAGE_SIZE_A:
173  plotPage.SetType( wxT( "A" ) );
174  plotPage.SetPortrait( actualPage.IsPortrait() );
175  break;
176 
177  case PAGE_SIZE_A4:
178  plotPage.SetType( wxT( "A4" ) );
179  plotPage.SetPortrait( actualPage.IsPortrait() );
180  break;
181 
182  case PAGE_SIZE_AUTO:
183  default:
184  plotPage = actualPage;
185  break;
186  }
187 
188  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
189  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
190  double scale = std::min( scalex, scaley );
191  aPlotter->SetPageSettings( plotPage );
192  // Currently, plot units are in decimil
193  aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
194 }
195 
static wxString GetDefaultFileExtension()
Definition: plot_common.h:804
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
Definition: schframe.cpp:581
Class SCH_SHEET_LIST.
virtual void SetCreator(const wxString &aCreator)
Definition: plot_common.h:151
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: plot_common.h:156
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
const wxString & GetFileName() const
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)
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.
Definition: schframe.cpp:528
virtual wxString GetScreenDesc() const override
Definition: schframe.cpp:534
virtual void ClosePage()
Close the current page in the PDF document (and emit its compressed stream)
const wxString & GetTitle() const
bool IsPortrait() const
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definitions for the Eeschema program SCH_SCREEN class.
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...
REPORTER & Reporter()
returns the reporter object that reports to this panel
SCH_SHEET_PATH & GetCurrentSheet()
Definition: schframe.cpp:573
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
bool SetType(const wxString &aStandardPageDescriptionName, bool IsPortrait=false)
Function SetType sets the name of the page type and also the sizes and margins commonly associated wi...
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
void SetPortrait(bool isPortrait)
Function SetPortrait will rotate the paper page 90 degrees.
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)
Function Plot plots all the schematic objects to aPlotter.
Definition: sch_screen.cpp:584
const PAGE_INFO & GetPageSettings() const override
void createPDFFile(bool aPlotAll, bool aPlotFrameRef)
Base plotter engine class.
Definition: plot_common.h:86
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()
Function SetSheetNumberAndCount Set the m_ScreenNumber and m_NumberOfScreens members for screens must...
Definition: schframe.cpp:493
const TITLE_BLOCK & GetTitleBlock() const override
wxString GetUniqueFilenameForCurrentSheet()
Function GetUniqueFilenameForCurrentSheet.
Definition: schframe.cpp:738
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.
const PAGE_INFO & GetPageSettings() const
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 'for free' the following are to be cl...
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
virtual void SetColorMode(bool _color_mode)
Definition: plot_common.h:116
void plotOneSheetPDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen, bool aPlotFrameRef)
#define min(a, b)
Definition: auxiliary.h:85