KiCad PCB EDA Suite
plot_schematic_PS.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-2020 KiCad Developers, see change_log.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <plotters_specific.h>
28 #include <sch_edit_frame.h>
29 #include <base_units.h>
30 #include <sch_sheet_path.h>
31 #include <locale_io.h>
32 #include <pgm_base.h>
33 #include <project.h>
34 #include <reporter.h>
36 #include <sch_painter.h>
37 #include <schematic.h>
38 #include <dialog_plot_schematic.h>
39 #include <wx_html_report_panel.h>
40 
41 
42 void DIALOG_PLOT_SCHEMATIC::createPSFile( bool aPlotAll, bool aPlotFrameRef,
43  RENDER_SETTINGS* aRenderSettings )
44 {
45  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
46  PAGE_INFO plotPage; // page size selected to plot
47 
48  /* When printing all pages, the printed page is not the current page.
49  * In complex hierarchies, we must update component references
50  * and others parameters in the given printed SCH_SCREEN, accordant to the sheet path
51  * because in complex hierarchies a SCH_SCREEN (a drawing )
52  * is shared between many sheets and component references depend on the actual sheet path used
53  */
54  SCH_SHEET_LIST sheetList;
55 
56  if( aPlotAll )
57  {
58  sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
59  sheetList.SortByPageNumbers();
60  }
61  else
62  {
63  sheetList.push_back( m_parent->GetCurrentSheet() );
64  }
65 
66  for( unsigned i = 0; i < sheetList.size(); i++ )
67  {
68  m_parent->SetCurrentSheet( sheetList[i] );
71 
73  PAGE_INFO actualPage = screen->GetPageSettings();
74 
75  switch( m_pageSizeSelect )
76  {
77  case PAGE_SIZE_A:
78  plotPage.SetType( wxT( "A" ) );
79  plotPage.SetPortrait( actualPage.IsPortrait() );
80  break;
81 
82  case PAGE_SIZE_A4:
83  plotPage.SetType( wxT( "A4" ) );
84  plotPage.SetPortrait( actualPage.IsPortrait() );
85  break;
86 
87  case PAGE_SIZE_AUTO:
88  default:
89  plotPage = actualPage;
90  break;
91  }
92 
93  double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
94  double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
95 
96  double scale = std::min( scalex, scaley );
97 
98  wxPoint plot_offset;
99 
100  wxString msg;
101  REPORTER& reporter = m_MessagesBox->Reporter();
102 
103  try
104  {
105  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
106  // The sub sheet can be in a sub_hierarchy, but we plot the file in the
107  // main project folder (or the folder specified by the caller),
108  // so replace separators to create a unique filename:
109  fname.Replace("/", "_" );
110  fname.Replace("\\", "_" );
111  wxString ext = PS_PLOTTER::GetDefaultFileExtension();
112  wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
113 
114  if( plotOneSheetPS( plotFileName.GetFullPath(), screen, aRenderSettings, plotPage,
115  plot_offset, scale, aPlotFrameRef ) )
116  {
117  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
118  reporter.Report( msg, RPT_SEVERITY_ACTION );
119  }
120  else
121  {
122  // Error
123  msg.Printf( _( "Unable to create file \"%s\".\n" ), plotFileName.GetFullPath() );
124  reporter.Report( msg, RPT_SEVERITY_ERROR );
125  }
126 
127  }
128  catch( IO_ERROR& e )
129  {
130  msg.Printf( wxT( "PS Plotter exception: %s"), e.What() );
131  reporter.Report( msg, RPT_SEVERITY_ERROR );
132  }
133  }
134 
135  m_parent->SetCurrentSheet( oldsheetpath );
138 }
139 
140 
141 bool DIALOG_PLOT_SCHEMATIC::plotOneSheetPS( const wxString& aFileName,
142  SCH_SCREEN* aScreen,
143  RENDER_SETTINGS* aRenderSettings,
144  const PAGE_INFO& aPageInfo,
145  wxPoint aPlot0ffset,
146  double aScale,
147  bool aPlotFrameRef )
148 {
149  PS_PLOTTER* plotter = new PS_PLOTTER();
150  plotter->SetRenderSettings( aRenderSettings );
151  plotter->SetPageSettings( aPageInfo );
152  plotter->SetColorMode( getModeColor() );
153  // Currently, plot units are in decimil
154  plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
155 
156  // Init :
157  plotter->SetCreator( wxT( "Eeschema-PS" ) );
158 
159  if( ! plotter->OpenFile( aFileName ) )
160  {
161  delete plotter;
162  return false;
163  }
164 
165  LOCALE_IO toggle; // Switch the locale to standard C
166 
167  plotter->StartPlot();
168 
169  if( m_plotBackgroundColor->GetValue() )
170  {
172  wxPoint end( plotter->PageSettings().GetWidthIU(),
173  plotter->PageSettings().GetHeightIU() );
174  plotter->Rect( wxPoint( 0, 0 ), end, FILL_TYPE::FILLED_SHAPE, 1.0 );
175  }
176 
177  if( aPlotFrameRef )
178  {
179  PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
180  aPageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
181  m_parent->GetScreenDesc(), aScreen->GetFileName(),
182  plotter->GetColorMode() ?
184  COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
185  }
186 
187  aScreen->Plot( plotter );
188 
189  plotter->EndPlot();
190  delete plotter;
191 
192  return true;
193 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
Definition: PS_plotter.cpp:420
const wxString & GetFileName() const
Definition: sch_screen.h:192
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:172
static wxString GetDefaultFileExtension()
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Implementation of conversion functions that require both schematic and board internal units.
Plotting engines (PostScript, Gerber, HPGL and DXF)
int GetHeightMils() const
Definition: page_info.h:133
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:76
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:151
int GetVirtualPageNumber() const
Definition: base_screen.h:79
virtual bool EndPlot() override
Definition: PS_plotter.cpp:983
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition: plotter.h:154
bool SetType(const wxString &aStandardPageDescriptionName, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
Definition: page_info.cpp:119
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
int GetPageCount() const
Definition: base_screen.h:76
Definition: color4d.h:44
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void UpdateAllScreenReferences()
Update all the symbol references for this sheet path.
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:187
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
const wxString & GetPageNumber() const
Definition: base_screen.cpp:78
wxString GetScreenDesc() const override
Return a human-readable description of the current screen.
virtual bool StartPlot() override
The code within this function (and the CloseFilePS function) creates postscript files whose contents ...
Definition: PS_plotter.cpp:826
wxFileName createPlotFileName(const wxString &aPlotFileName, const wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
bool plotOneSheetPS(const wxString &aFileName, SCH_SCREEN *aScreen, RENDER_SETTINGS *aRenderSettings, const PAGE_INFO &aPageInfo, wxPoint aPlot0ffset, double aScale, bool aPlotFrameRef)
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
Definition: PS_plotter.cpp:65
SCHEMATIC & Schematic() const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void createPSFile(bool aPlotAll, bool aPlotFrameRef, RENDER_SETTINGS *aSettings)
void Plot(PLOTTER *aPlotter)
Plot all the schematic objects to aPlotter.
Definition: sch_screen.cpp:793
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:99
SCH_SHEET & Root() const
Definition: schematic.h:116
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:155
const int scale
see class PGM_BASE
SCH_SCREEN * LastScreen()
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
bool IsPortrait() const
Definition: page_info.h:117
#define _(s)
Definition: 3d_actions.cpp:33
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:87
const TITLE_BLOCK & GetTitleBlock() const override
void PlotDrawingSheet(PLOTTER *plotter, const PROJECT *aProject, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, const wxString &aSheetNumber, int aSheetCount, const wxString &aSheetDesc, const wxString &aFilename, COLOR4D aColor, bool aIsFirstPage)
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sorts the list of sheets by page number.
#define IU_PER_MILS
Definition: plotter.cpp:137
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH) override
Definition: PS_plotter.cpp:590
wxString GetUniqueFilenameForCurrentSheet()
SCH_SHEET_PATH & GetCurrentSheet() const
WX_HTML_REPORT_PANEL * m_MessagesBox
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
PAGE_INFO & PageSettings()
Definition: plotter.h:158
void SetPortrait(bool aIsPortrait)
Rotate the paper page 90 degrees.
Definition: page_info.cpp:186
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
int GetWidthMils() const
Definition: page_info.h:130
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:157
bool GetColorMode() const
Definition: plotter.h:152