KiCad PCB EDA Suite
plot_schematic_SVG.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
30 #include <pgm_base.h>
31 #include <sch_draw_panel.h>
32 #include <sch_edit_frame.h>
33 #include <base_units.h>
34 #include <locale_io.h>
35 #include <sch_sheet_path.h>
36 #include <schematic.h>
37 #include <project.h>
38 #include <reporter.h>
40 
41 #include <dialog_plot_schematic.h>
42 #include <wx_html_report_panel.h>
43 #include "sch_painter.h"
44 #include <plotters_specific.h>
45 
46 
47 void DIALOG_PLOT_SCHEMATIC::createSVGFile( bool aPrintAll, bool aPrintFrameRef,
48  RENDER_SETTINGS* aRenderSettings )
49 {
50  wxString msg;
51  REPORTER& reporter = m_MessagesBox->Reporter();
52  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
53  SCH_SHEET_LIST sheetList;
54 
55  if( aPrintAll )
56  {
57  sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
58  sheetList.SortByPageNumbers();
59  }
60  else
61  {
62  sheetList.push_back( m_parent->GetCurrentSheet() );
63  }
64 
65  for( unsigned i = 0; i < sheetList.size(); i++ )
66  {
67  SCH_SCREEN* screen;
68  m_parent->SetCurrentSheet( sheetList[i] );
71  screen = m_parent->GetCurrentSheet().LastScreen();
72 
73  try
74  {
75  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
76  // The sub sheet can be in a sub_hierarchy, but we plot the file in the
77  // main project folder (or the folder specified by the caller),
78  // so replace separators to create a unique filename:
79  fname.Replace("/", "_" );
80  fname.Replace("\\", "_" );
81  wxString ext = SVG_PLOTTER::GetDefaultFileExtension();
82  wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
83 
84  bool success = plotOneSheetSVG( plotFileName.GetFullPath(), screen, aRenderSettings,
85  getModeColor() ? false : true, aPrintFrameRef );
86 
87  if( !success )
88  {
89  msg.Printf( _( "Cannot create file \"%s\".\n" ), plotFileName.GetFullPath() );
90  reporter.Report( msg, RPT_SEVERITY_ERROR );
91  }
92  else
93  {
94  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
95  reporter.Report( msg, RPT_SEVERITY_ACTION );
96  }
97  }
98  catch( const IO_ERROR& e )
99  {
100  // Cannot plot SVG file
101  msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
102  reporter.Report( msg, RPT_SEVERITY_ERROR );
103  break;
104  }
105  }
106 
107  m_parent->SetCurrentSheet( oldsheetpath );
110 }
111 
112 
113 bool DIALOG_PLOT_SCHEMATIC::plotOneSheetSVG( const wxString& aFileName,
114  SCH_SCREEN* aScreen,
115  RENDER_SETTINGS* aRenderSettings,
116  bool aPlotBlackAndWhite,
117  bool aPlotFrameRef )
118 {
119  const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
120 
121  SVG_PLOTTER* plotter = new SVG_PLOTTER();
122  plotter->SetRenderSettings( aRenderSettings );
123  plotter->SetPageSettings( pageInfo );
124  plotter->SetColorMode( aPlotBlackAndWhite ? false : true );
125  wxPoint plot_offset;
126  double scale = 1.0;
127  // Currently, plot units are in decimil
128  plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
129 
130  // Init :
131  plotter->SetCreator( wxT( "Eeschema-SVG" ) );
132 
133  if( ! plotter->OpenFile( aFileName ) )
134  {
135  delete plotter;
136  return false;
137  }
138 
139  LOCALE_IO toggle;
140 
141  plotter->StartPlot();
142 
143  if( m_plotBackgroundColor->GetValue() )
144  {
146  wxPoint end( plotter->PageSettings().GetWidthIU(),
147  plotter->PageSettings().GetHeightIU() );
148  plotter->Rect( wxPoint( 0, 0 ), end, FILL_TYPE::FILLED_SHAPE, 1.0 );
149  }
150 
151  if( aPlotFrameRef )
152  {
153  PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
154  pageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
155  m_parent->GetScreenDesc(), aScreen->GetFileName(),
156  plotter->GetColorMode() ?
158  COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
159  }
160 
161  aScreen->Plot( plotter );
162 
163  plotter->EndPlot();
164  delete plotter;
165 
166  return true;
167 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
const wxString & GetFileName() const
Definition: sch_screen.h:192
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:172
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
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)
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:76
static wxString GetDefaultFileExtension()
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:151
int GetVirtualPageNumber() const
Definition: base_screen.h:79
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition: plotter.h:154
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
int GetPageCount() const
Definition: base_screen.h:76
bool plotOneSheetSVG(const wxString &aFileName, SCH_SCREEN *aScreen, RENDER_SETTINGS *aRenderSettings, bool aPlotBlackAndWhite, bool aPlotFrameRef)
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.
void createSVGFile(bool aPlotAll, bool aPlotFrameRef, RENDER_SETTINGS *aSettings)
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 EndPlot() override
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
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
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.
SCHEMATIC & Schematic() const
virtual bool StartPlot() override
The code within this function creates SVG files header.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
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.
#define _(s)
Definition: 3d_actions.cpp:33
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:87
const TITLE_BLOCK & GetTitleBlock() const override
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH) 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
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
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:157
bool GetColorMode() const
Definition: plotter.h:152