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-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 <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 <schematic.h>
38 #include <project.h>
39 #include <reporter.h>
41 
42 #include <dialog_plot_schematic.h>
43 #include <wx_html_report_panel.h>
44 #include "sch_painter.h"
45 
46 void DIALOG_PLOT_SCHEMATIC::createSVGFile( bool aPrintAll, bool aPrintFrameRef,
47  RENDER_SETTINGS* aRenderSettings )
48 {
49  wxString msg;
50  REPORTER& reporter = m_MessagesBox->Reporter();
51  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
52  SCH_SHEET_LIST sheetList;
53 
54  if( aPrintAll )
55  sheetList.BuildSheetList( &m_parent->Schematic().Root() );
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( fname, ext, &reporter );
72 
73  bool success = plotOneSheetSVG( plotFileName.GetFullPath(), screen, aRenderSettings,
74  getModeColor() ? false : true, aPrintFrameRef );
75 
76  if( !success )
77  {
78  msg.Printf( _( "Cannot create file \"%s\".\n" ), plotFileName.GetFullPath() );
79  reporter.Report( msg, RPT_SEVERITY_ERROR );
80  }
81  else
82  {
83  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
84  reporter.Report( msg, RPT_SEVERITY_ACTION );
85  }
86  }
87  catch( const IO_ERROR& e )
88  {
89  // Cannot plot SVG file
90  msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
91  reporter.Report( msg, RPT_SEVERITY_ERROR );
92  break;
93  }
94  }
95 
96  m_parent->SetCurrentSheet( oldsheetpath );
99 }
100 
101 
102 bool DIALOG_PLOT_SCHEMATIC::plotOneSheetSVG( const wxString& aFileName,
103  SCH_SCREEN* aScreen,
104  RENDER_SETTINGS* aRenderSettings,
105  bool aPlotBlackAndWhite,
106  bool aPlotFrameRef )
107 {
108  const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
109 
110  SVG_PLOTTER* plotter = new SVG_PLOTTER();
111  plotter->SetRenderSettings( aRenderSettings );
112  plotter->SetPageSettings( pageInfo );
113  plotter->SetColorMode( aPlotBlackAndWhite ? false : true );
114  wxPoint plot_offset;
115  double scale = 1.0;
116  // Currently, plot units are in decimil
117  plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
118 
119  // Init :
120  plotter->SetCreator( wxT( "Eeschema-SVG" ) );
121 
122  if( ! plotter->OpenFile( aFileName ) )
123  {
124  delete plotter;
125  return false;
126  }
127 
128  LOCALE_IO toggle;
129 
130  plotter->StartPlot();
131 
132  if( m_plotBackgroundColor->GetValue() )
133  {
135  wxPoint end( plotter->PageSettings().GetWidthIU(),
136  plotter->PageSettings().GetHeightIU() );
137  plotter->Rect( wxPoint( 0, 0 ), end, FILLED_SHAPE, 1.0 );
138  }
139 
140  if( aPlotFrameRef )
141  {
142  PlotWorkSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(), pageInfo,
143  aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
144  m_parent->GetScreenDesc(), aScreen->GetFileName(),
145  plotter->GetColorMode() ?
147  COLOR4D::BLACK );
148  }
149 
150  aScreen->Plot( plotter );
151 
152  plotter->EndPlot();
153  delete plotter;
154 
155  return true;
156 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
SCH_SHEET_LIST.
const wxString & GetFileName() const
Definition: sch_screen.h:185
int m_ScreenNumber
Definition: base_screen.h:79
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:164
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:216
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
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:77
static wxString GetDefaultFileExtension()
Definition: plotter.h:948
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:143
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition: plotter.h:146
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
bool plotOneSheetSVG(const wxString &aFileName, SCH_SCREEN *aScreen, RENDER_SETTINGS *aRenderSettings, bool aPlotBlackAndWhite, bool aPlotFrameRef)
Definition: color4d.h:44
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
void createSVGFile(bool aPlotAll, bool aPlotFrameRef, RENDER_SETTINGS *aSettings)
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:180
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
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,...
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
void PlotWorkSheet(PLOTTER *plotter, const PROJECT *aProject, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, int aSheetNumber, int aNumberOfSheets, const wxString &aSheetDesc, const wxString &aFilename, COLOR4D aColor)
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
SCHEMATIC & Schematic() const
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:781
SCH_SHEET & Root() const
Definition: schematic.h:94
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:147
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
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:91
const TITLE_BLOCK & GetTitleBlock() const override
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:77
#define IU_PER_MILS
Definition: plotter.cpp:138
wxString GetUniqueFilenameForCurrentSheet()
int m_NumberOfScreens
Definition: base_screen.h:80
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:150
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
wxFileName createPlotFileName(wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:149
bool GetColorMode() const
Definition: plotter.h:144