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-2016 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2016 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 <fctsys.h>
31 #include <pgm_base.h>
32 #include <sch_draw_panel.h>
33 #include <sch_edit_frame.h>
34 #include <base_units.h>
35 #include <lib_edit_frame.h>
36 #include <sch_sheet_path.h>
37 #include <project.h>
38 #include <reporter.h>
40 
41 #include <dialog_plot_schematic.h>
42 #include <wx_html_report_panel.h>
43 
44 void DIALOG_PLOT_SCHEMATIC::createSVGFile( bool aPrintAll, bool aPrintFrameRef )
45 {
46  wxString msg;
47  REPORTER& reporter = m_MessagesBox->Reporter();
48  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
49  SCH_SHEET_LIST sheetList;
50 
51  auto colors = static_cast<COLOR_SETTINGS*>(
52  m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
53 
54  if( aPrintAll )
55  sheetList.BuildSheetList( g_RootSheet );
56  else
57  sheetList.push_back( m_parent->GetCurrentSheet() );
58 
59  for( unsigned i = 0; i < sheetList.size(); i++ )
60  {
61  SCH_SCREEN* screen;
62  m_parent->SetCurrentSheet( sheetList[i] );
65  screen = m_parent->GetCurrentSheet().LastScreen();
66 
67  try
68  {
69  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
70  wxString ext = SVG_PLOTTER::GetDefaultFileExtension();
71  wxFileName plotFileName = createPlotFileName( m_outputDirectoryName,
72  fname, ext, &reporter );
73 
74  bool success = plotOneSheetSVG( m_parent, plotFileName.GetFullPath(), screen,
75  getModeColor() ? false : true,
76  aPrintFrameRef, m_plotBackgroundColor->GetValue(),
77  colors );
78 
79  if( !success )
80  {
81  msg.Printf( _( "Cannot create file \"%s\".\n" ), plotFileName.GetFullPath() );
82  reporter.Report( msg, RPT_SEVERITY_ERROR );
83  }
84  else
85  {
86  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
87  reporter.Report( msg, RPT_SEVERITY_ACTION );
88  }
89  }
90  catch( const IO_ERROR& e )
91  {
92  // Cannot plot SVG file
93  msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
94  reporter.Report( msg, RPT_SEVERITY_ERROR );
95  break;
96  }
97  }
98 
99  m_parent->SetCurrentSheet( oldsheetpath );
102 }
103 
104 
106  const wxString& aFileName,
107  SCH_SCREEN* aScreen,
108  bool aPlotBlackAndWhite,
109  bool aPlotFrameRef,
110  bool aPlotBackgroundColor,
111  COLOR_SETTINGS* aColors )
112 {
113  SVG_PLOTTER* plotter = new SVG_PLOTTER();
114 
115  if( !aColors )
116  aColors = aFrame->GetColorSettings();
117 
118  const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
119  plotter->SetPageSettings( pageInfo );
121  plotter->SetColorMode( aPlotBlackAndWhite ? false : true );
122  plotter->SetColorSettings( aColors );
123  wxPoint plot_offset;
124  double scale = 1.0;
125  // Currently, plot units are in decimil
126  plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
127 
128  // Init :
129  plotter->SetCreator( wxT( "Eeschema-SVG" ) );
130 
131  if( ! plotter->OpenFile( aFileName ) )
132  {
133  delete plotter;
134  return false;
135  }
136 
137  LOCALE_IO toggle;
138 
139  plotter->StartPlot();
140 
141  if( aPlotBackgroundColor )
142  {
143  plotter->SetColor( plotter->ColorSettings()->GetColor( LAYER_SCHEMATIC_BACKGROUND ) );
144  wxPoint end( plotter->PageSettings().GetWidthIU(),
145  plotter->PageSettings().GetHeightIU() );
146  plotter->Rect( wxPoint( 0, 0 ), end, FILLED_SHAPE, 1.0 );
147  }
148 
149  if( aPlotFrameRef )
150  {
151  COLOR4D color = plotter->GetColorMode() ?
154 
155  PlotWorkSheet( plotter, aFrame->GetTitleBlock(),
156  aFrame->GetPageSettings(),
157  aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
158  aFrame->GetScreenDesc(),
159  aScreen->GetFileName(),
160  color );
161  }
162 
163  aScreen->Plot( plotter );
164 
165  plotter->EndPlot();
166  delete plotter;
167 
168  return true;
169 }
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
virtual const TITLE_BLOCK & GetTitleBlock() const =0
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:202
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
Implementation of conversion functions that require both schematic and board internal units.
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:82
static wxString GetDefaultFileExtension()
Definition: plotter.h:954
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....
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...
The base class for create windows for drawing purpose.
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.
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
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)
virtual bool EndPlot() override
virtual const PAGE_INFO & GetPageSettings() const =0
virtual void SetColor(COLOR4D color) override
The SetColor implementation is split with the subclasses: The PSLIKE computes the rgb values,...
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()
virtual bool StartPlot() override
The code within this function creates SVG files header.
SCH_SHEET_PATH.
void BuildSheetList(SCH_SHEET *aSheet)
Function BuildSheetList builds the list of sheets and their sheet path from aSheet.
void Plot(PLOTTER *aPlotter)
Plot all the schematic objects to aPlotter.
Definition: sch_screen.cpp:573
static bool plotOneSheetSVG(EDA_DRAW_FRAME *aFrame, const wxString &aFileName, SCH_SCREEN *aScreen, bool aPlotBlackAndWhite, bool aPlotFrameRef, bool aPlotBackgroundColor=false, COLOR_SETTINGS *aColors=nullptr)
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.
#define _(s)
Definition: 3d_actions.cpp:33
#define IU_PER_MILS
Definition: plotter.cpp:137
void SetColorSettings(COLOR_SETTINGS *aSettings)
Definition: plotter.h:143
virtual COLOR_SETTINGS * GetColorSettings()
Returns a pointer to the active color theme settings.
wxString GetUniqueFilenameForCurrentSheet()
int m_NumberOfScreens
Definition: base_screen.h:136
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.
PAGE_INFO & PageSettings()
Definition: plotter.h:152
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 createSVGFile(bool aPlotAll, bool aPlotFrameRef)
static VRML_COLOR colors[VRML_COLOR_LAST]
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:147
virtual wxString GetScreenDesc() const
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
bool GetColorMode() const
Definition: plotter.h:138