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 #include <general.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(),
159  }
160 
161  aScreen->Plot( aPlotter );
162 }
163 
164 
166 {
167  PAGE_INFO plotPage; // page size selected to plot
168  // Considerations on page size and scaling requests
169  const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
170 
171  switch( m_pageSizeSelect )
172  {
173  case PAGE_SIZE_A:
174  plotPage.SetType( wxT( "A" ) );
175  plotPage.SetPortrait( actualPage.IsPortrait() );
176  break;
177 
178  case PAGE_SIZE_A4:
179  plotPage.SetType( wxT( "A4" ) );
180  plotPage.SetPortrait( actualPage.IsPortrait() );
181  break;
182 
183  case PAGE_SIZE_AUTO:
184  default:
185  plotPage = actualPage;
186  break;
187  }
188 
189  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
190  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
191  double scale = std::min( scalex, scaley );
192  aPlotter->SetPageSettings( plotPage );
193  // Currently, plot units are in decimil
194  aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
195 }
196 
static wxString GetDefaultFileExtension()
Definition: plotter.h:855
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
Class SCH_SHEET_LIST.
const wxString & GetFileName() const
Definition: sch_screen.h:123
int m_ScreenNumber
Definition: base_screen.h:135
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:161
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
virtual bool EndPlot() override
Implementation of conversion functions that require both schematic and board internal units.
virtual void SetColor(COLOR4D color)=0
int GetHeightMils() const
Definition: page_info.h:140
virtual void StartPage()
Starts a new page in the PDF document.
virtual void SetTitle(const wxString &aTitle)
Definition: plotter.h:166
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:126
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
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
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...
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.cpp:590
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:118
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, const COLOR4D aColor)
void setupPlotPagePDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen)
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
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)
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:47
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definition: PS_plotter.cpp:52
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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:54
REPORTER & Reporter()
returns the reporter object that reports to this panel
SCH_SHEET_PATH & GetCurrentSheet()
SCH_LAYER_ID
Eeschema drawing layers.
Class SCH_SHEET_PATH.
void BuildSheetList(SCH_SHEET *aSheet)
Function BuildSheetList builds the list of sheets and their sheet path from aSheet.
#define _(s)
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:549
const PAGE_INFO & GetPageSettings() const override
void createPDFFile(bool aPlotAll, bool aPlotFrameRef)
Base plotter engine class.
Definition: plotter.h:97
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:101
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
bool IsPortrait() const
Definition: page_info.h:121
const TITLE_BLOCK & GetTitleBlock() const override
size_t i
Definition: json11.cpp:649
#define IU_PER_MILS
Definition: plotter.cpp:136
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:136
const wxString & GetTitle() const
Definition: title_block.h:65
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...
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:76
void plotOneSheetPDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen, bool aPlotFrameRef)
SCH_SCREEN * LastScreen() const
Function LastScreen.
int GetWidthMils() const
Definition: page_info.h:137
#define min(a, b)
Definition: auxiliary.h:85