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 <pgm_base.h>
34 #include <project.h>
35 #include <general.h>
37 
38 #include <reporter.h>
39 
40 #include <dialog_plot_schematic.h>
41 #include <wx_html_report_panel.h>
42 
43 void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotFrameRef )
44 {
45  SCH_SCREEN* screen = m_parent->GetScreen();
46  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
47 
48  /* When printing all pages, the printed page is not the current page. In
49  * complex hierarchies, we must update component references and others
50  * parameters in the given printed SCH_SCREEN, accordint to the sheet path
51  * because in complex hierarchies a SCH_SCREEN (a drawing ) is shared
52  * between many sheets and component references depend on the actual sheet
53  * path used
54  */
55  SCH_SHEET_LIST sheetList;
56 
57  if( aPlotAll )
58  sheetList.BuildSheetList( g_RootSheet );
59  else
60  sheetList.push_back( m_parent->GetCurrentSheet() );
61 
62  auto colors = static_cast<COLOR_SETTINGS*>(
63  m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
64 
65  // Allocate the plotter and set the job level parameter
66  PDF_PLOTTER* plotter = new PDF_PLOTTER();
68  plotter->SetColorMode( getModeColor() );
69  plotter->SetColorSettings( colors );
70  plotter->SetCreator( wxT( "Eeschema-PDF" ) );
71  plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
72 
73  wxString msg;
74  wxFileName plotFileName;
75  REPORTER& reporter = m_MessagesBox->Reporter();
76  LOCALE_IO toggle; // Switch the locale to standard C
77 
78  for( unsigned i = 0; i < sheetList.size(); i++ )
79  {
80  m_parent->SetCurrentSheet( sheetList[i] );
83  screen = m_parent->GetCurrentSheet().LastScreen();
84 
85  if( i == 0 )
86  {
87 
88  try
89  {
90  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
91  wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
92  plotFileName = createPlotFileName( m_outputDirectoryName, fname, ext, &reporter );
93 
94  if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
95  {
96  msg.Printf( _( "Unable to create file \"%s\".\n" ), plotFileName.GetFullPath() );
97  reporter.Report( msg, RPT_SEVERITY_ERROR );
98  delete plotter;
99  return;
100  }
101 
102  // Open the plotter and do the first page
103  setupPlotPagePDF( plotter, screen );
104  plotter->StartPlot();
105  }
106  catch( const IO_ERROR& e )
107  {
108  // Cannot plot PDF file
109  msg.Printf( wxT( "PDF Plotter exception: %s" ), e.What() );
110  reporter.Report( msg, RPT_SEVERITY_ERROR );
111 
112  restoreEnvironment( plotter, oldsheetpath );
113  return;
114  }
115 
116  }
117  else
118  {
119  /* For the following pages you need to close the (finished) page,
120  * reconfigure, and then start a new one */
121  plotter->ClosePage();
122  setupPlotPagePDF( plotter, screen );
123  plotter->StartPage();
124  }
125 
126  plotOneSheetPDF( plotter, screen, aPlotFrameRef );
127  }
128 
129  // Everything done, close the plot and restore the environment
130  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
131  reporter.Report( msg, RPT_SEVERITY_ACTION );
132 
133  restoreEnvironment( plotter, oldsheetpath );
134 }
135 
136 
138  SCH_SHEET_PATH& aOldsheetpath )
139 {
140  aPlotter->EndPlot();
141  delete aPlotter;
142 
143  // Restore the previous sheet
144  m_parent->SetCurrentSheet( aOldsheetpath );
147 }
148 
149 
151  SCH_SCREEN* aScreen,
152  bool aPlotFrameRef )
153 {
154  if( m_plotBackgroundColor->GetValue() )
155  {
156  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_SCHEMATIC_BACKGROUND ) );
157  wxPoint end( aPlotter->PageSettings().GetWidthIU(),
158  aPlotter->PageSettings().GetHeightIU() );
159  aPlotter->Rect( wxPoint( 0, 0 ), end, FILLED_SHAPE, 1.0 );
160  }
161 
162  if( aPlotFrameRef )
163  {
164  COLOR4D color = aPlotter->GetColorMode() ?
167 
168  PlotWorkSheet( aPlotter, m_parent->GetTitleBlock(),
170  aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
172  aScreen->GetFileName(),
173  color );
174  }
175 
176  aScreen->Plot( aPlotter );
177 }
178 
179 
181 {
182  PAGE_INFO plotPage; // page size selected to plot
183  // Considerations on page size and scaling requests
184  const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
185 
186  switch( m_pageSizeSelect )
187  {
188  case PAGE_SIZE_A:
189  plotPage.SetType( wxT( "A" ) );
190  plotPage.SetPortrait( actualPage.IsPortrait() );
191  break;
192 
193  case PAGE_SIZE_A4:
194  plotPage.SetType( wxT( "A4" ) );
195  plotPage.SetPortrait( actualPage.IsPortrait() );
196  break;
197 
198  case PAGE_SIZE_AUTO:
199  default:
200  plotPage = actualPage;
201  break;
202  }
203 
204  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
205  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
206  double scale = std::min( scalex, scaley );
207  aPlotter->SetPageSettings( plotPage );
208  // Currently, plot units are in decimil
209  aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
210 }
211 
static wxString GetDefaultFileExtension()
Definition: plotter.h:876
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
SCH_SHEET_LIST.
const wxString & GetFileName() const
Definition: sch_screen.h:157
int m_ScreenNumber
Definition: base_screen.h:135
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:180
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:202
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:185
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:133
int color
Definition: DXF_plotter.cpp:61
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
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:62
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:152
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 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)
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
Definition: PS_plotter.cpp:53
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
wxFileName createPlotFileName(wxTextCtrl *aOutputDirectoryName, wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
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
SCH_SHEET_PATH & GetCurrentSheet()
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:573
const PAGE_INFO & GetPageSettings() const override
void createPDFFile(bool aPlotAll, bool aPlotFrameRef)
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
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:121
#define _(s)
Definition: 3d_actions.cpp:33
const TITLE_BLOCK & GetTitleBlock() const override
#define IU_PER_MILS
Definition: plotter.cpp:137
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
void SetColorSettings(COLOR_SETTINGS *aSettings)
Definition: plotter.h:143
wxString GetUniqueFilenameForCurrentSheet()
virtual bool OpenFile(const wxString &aFullFilename) override
Open or create the plot file aFullFilename.
Definition: PDF_plotter.cpp:50
int m_NumberOfScreens
Definition: base_screen.h:136
const wxString & GetTitle() const
Definition: title_block.h:65
SCH_SHEET * g_RootSheet
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...
PAGE_INFO & PageSettings()
Definition: plotter.h:152
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
static VRML_COLOR colors[VRML_COLOR_LAST]
void plotOneSheetPDF(PLOTTER *aPlotter, SCH_SCREEN *aScreen, bool aPlotFrameRef)
int GetWidthMils() const
Definition: page_info.h:137
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:147
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
bool GetColorMode() const
Definition: plotter.h:138