KiCad PCB EDA Suite
dialog_export_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) 2012 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2012 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <pcb_edit_frame.h>
27 #include <pcbnew_settings.h>
29 #include <reporter.h>
30 #include <confirm.h>
31 #include <core/arraydim.h>
32 #include <core/kicad_algo.h>
33 #include <pcbplot.h>
34 #include <locale_io.h>
35 #include <board.h>
36 #include <dialog_export_svg_base.h>
37 #include <wx_html_report_panel.h>
38 #include <bitmaps.h>
39 #include <widgets/unit_binder.h>
40 
42 {
43 public:
44  DIALOG_EXPORT_SVG( PCB_EDIT_FRAME* aParent, BOARD* aBoard );
45  ~DIALOG_EXPORT_SVG() override;
46 
47 private:
51  // the list of existing board layers in wxCheckListBox, with the
52  // board layers id:
53  std::pair<wxCheckListBox*, int> m_boxSelectLayer[PCB_LAYER_ID_COUNT];
54  bool m_printBW;
59 
60  void initDialog();
61 
62  void OnButtonPlot( wxCommandEvent& event ) override;
63 
64  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
65  void ExportSVGFile( bool aOnlyOneFile );
66 
67  bool CreateSVGFile( const wxString& FullFileName );
68 
70 };
71 
72 
73 /*
74  * DIALOG_EXPORT_SVG functions
75  */
77  DIALOG_EXPORT_SVG_BASE( aParent ),
78  m_board( aBoard ),
79  m_parent( aParent ),
80  m_printBW( false ),
81  m_printMirror( false ),
82  m_oneFileOnly( false ),
83  m_lineWidth( aParent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits, true )
84 {
85  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
86 
87  initDialog();
88 
89  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
90  // that requires us to correct the button labels here.
91  m_sdbSizer1OK->SetLabel( _( "Export" ) );
92  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
93  m_sdbSizer1->Layout();
94 
96 }
97 
98 
100 {
102  m_printBW = m_ModeColorOption->GetSelection();
103  m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
105  m_outputDirectory.Replace( wxT( "\\" ), wxT( "/" ) );
106 
107  auto cfg = m_parent->GetPcbNewSettings();
108 
110  cfg->m_ExportSvg.mirror = m_printMirror;
111  cfg->m_ExportSvg.one_file = m_oneFileOnly;
112  cfg->m_ExportSvg.plot_board_edges = m_PrintBoardEdgesCtrl->GetValue();
113  cfg->m_ExportSvg.page_size = m_rbSvgPageSizeOpt->GetSelection();
114  cfg->m_ExportSvg.output_dir = m_outputDirectory.ToStdString();
115 
116  cfg->m_ExportSvg.layers.clear();
117 
118  for( unsigned layer = 0; layer < arrayDim( m_boxSelectLayer ); ++layer )
119  {
120  if( !m_boxSelectLayer[layer].first )
121  continue;
122 
123  if( m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) )
124  cfg->m_ExportSvg.layers.push_back( m_boxSelectLayer[layer].second );
125  }
126 }
127 
128 
130 {
131  auto cfg = m_parent->GetPcbNewSettings();
132 
133  m_printBW = cfg->m_ExportSvg.black_and_white;
134  m_printMirror = cfg->m_ExportSvg.mirror;
135  m_oneFileOnly = cfg->m_ExportSvg.one_file;
136  m_outputDirectory = cfg->m_ExportSvg.output_dir;
137 
138  m_rbSvgPageSizeOpt->SetSelection( cfg->m_ExportSvg.page_size );
139  m_PrintBoardEdgesCtrl->SetValue( cfg->m_ExportSvg.plot_board_edges );
140 
142 
143  m_ModeColorOption->SetSelection( m_printBW ? 1 : 0 );
144  m_printMirrorOpt->SetValue( m_printMirror );
145  m_rbFileOpt->SetSelection( m_oneFileOnly ? 1 : 0 );
146 
148 
149  for( LSEQ seq = m_board->GetEnabledLayers().UIOrder(); seq; ++seq )
150  {
151  PCB_LAYER_ID layer = *seq;
152  int checkIndex;
153 
154  if( IsCopperLayer( layer ) )
155  {
156  checkIndex = m_CopperLayersList->Append( m_board->GetLayerName( layer ) );
157  m_boxSelectLayer[layer] = std::make_pair( m_CopperLayersList, checkIndex );
158  }
159  else
160  {
161  checkIndex = m_TechnicalLayersList->Append( m_board->GetLayerName( layer ) );
162  m_boxSelectLayer[layer] = std::make_pair( m_TechnicalLayersList, checkIndex );
163  }
164 
165  if( alg::contains( cfg->m_ExportSvg.layers, layer ) )
166  m_boxSelectLayer[layer].first->Check( checkIndex, true );
167  }
168 }
169 
170 
172 {
173  LSET ret;
174 
175  for( unsigned layer = 0; layer < arrayDim(m_boxSelectLayer); ++layer )
176  {
177  if( !m_boxSelectLayer[layer].first )
178  continue;
179 
180  if( m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) )
181  ret.set( layer );
182  }
183 
184  return ret;
185 }
186 
187 
189 {
190  // Build the absolute path of current output directory to preselect it in the file browser.
191  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
192  path = Prj().AbsolutePath( path );
193 
194  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
195 
196  if( dirDialog.ShowModal() == wxID_CANCEL )
197  return;
198 
199  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
200 
201  wxMessageDialog dialog( this, _( "Use a relative path?" ), _( "Plot Output Directory" ),
202  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
203 
204  if( dialog.ShowModal() == wxID_YES )
205  {
206  wxString boardFilePath = Prj().AbsolutePath( m_board->GetFileName() );
207 
208  boardFilePath = wxPathOnly( boardFilePath );
209 
210  if( !dirName.MakeRelativeTo( boardFilePath ) )
211  wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
212  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
213  }
214 
215  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
217 }
218 
219 
220 void DIALOG_EXPORT_SVG::ExportSVGFile( bool aOnlyOneFile )
221 {
223 
224  // Create output directory if it does not exist (also transform it in
225  // absolute form). Bail if it fails
226  wxString path = ExpandEnvVarSubstitutions( m_outputDirectory, &Prj() );
227  wxFileName outputDir = wxFileName::DirName( path );
228  wxString boardFilename = m_board->GetFileName();
229 
230  REPORTER& reporter = m_messagesPanel->Reporter();
231 
232  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
233  {
234  wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
235  outputDir.GetPath() );
236  DisplayError( this, msg );
237  return;
238  }
239 
240  m_printMirror = m_printMirrorOpt->GetValue();
241  m_printBW = m_ModeColorOption->GetSelection();
243 
244  LSET all_selected = getCheckBoxSelectedLayers();
245 
246  for( LSEQ seq = all_selected.Seq(); seq; ++seq )
247  {
248  PCB_LAYER_ID layer = *seq;
249  wxFileName fn( boardFilename );
250  wxString suffix = aOnlyOneFile ? wxT( "brd" ) : m_board->GetStandardLayerName( layer );
251 
252  BuildPlotFileName( &fn, outputDir.GetPath(), suffix, SVGFileExtension );
253  wxString svgPath = fn.GetFullPath();
254 
255  m_printMaskLayer = aOnlyOneFile ? all_selected : LSET( layer );
256 
257  if( m_PrintBoardEdgesCtrl->IsChecked() )
259 
260  if( CreateSVGFile( svgPath ) )
261  {
262  reporter.Report( wxString::Format( _( "Exported \"%s\"." ), svgPath ), RPT_SEVERITY_ACTION );
263  }
264  else // Error
265  {
266  reporter.Report( wxString::Format( _( "Unable to create file \"%s\"." ), svgPath ),
268  }
269 
270  if( aOnlyOneFile )
271  break;
272  }
273 }
274 
275 
276 // Actual SVG file export function.
277 bool DIALOG_EXPORT_SVG::CreateSVGFile( const wxString& aFullFileName )
278 {
279  PCB_PLOT_PARAMS plot_opts;
280 
281  plot_opts.SetPlotFrameRef( m_rbSvgPageSizeOpt->GetSelection() == 0 );
282 
283  // Adding drill marks, for copper layers
284  if( ( m_printMaskLayer & LSET::AllCuMask() ).any() )
286  else
288 
289  plot_opts.SetSkipPlotNPTH_Pads( false );
290 
291  plot_opts.SetMirror( m_printMirror );
292  plot_opts.SetFormat( PLOT_FORMAT::SVG );
293 
294  PAGE_INFO savedPageInfo = m_board->GetPageSettings();
295  wxPoint savedAuxOrigin = m_board->GetDesignSettings().m_AuxOrigin;
296 
297  if( m_rbSvgPageSizeOpt->GetSelection() == 2 ) // Page is board boundary size
298  {
300  PAGE_INFO currpageInfo = m_board->GetPageSettings();
301 
302  currpageInfo.SetWidthMils( bbox.GetWidth() / IU_PER_MILS );
303  currpageInfo.SetHeightMils( bbox.GetHeight() / IU_PER_MILS );
304  m_board->SetPageSettings( currpageInfo );
305  plot_opts.SetUseAuxOrigin( true );
306  wxPoint origin = bbox.GetOrigin();
308  }
309 
310  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
312 
313  plot_opts.SetColorSettings( mgr.GetColorSettings( cfg->m_ColorTheme ) );
314 
315  LOCALE_IO toggle;
316 
317  SVG_PLOTTER* plotter = (SVG_PLOTTER*) StartPlotBoard( m_board, &plot_opts, UNDEFINED_LAYER,
318  aFullFileName, wxEmptyString );
319 
320  if( plotter )
321  {
322  plotter->SetColorMode( !m_printBW );
323 
324  for( LSEQ seq = m_printMaskLayer.SeqStackupBottom2Top(); seq; ++seq )
325  PlotOneBoardLayer( m_board, plotter, *seq, plot_opts );
326 
327  plotter->EndPlot();
328  }
329 
330  delete plotter;
331 
332  // reset to the values saved earlier
333  m_board->GetDesignSettings().m_AuxOrigin = savedAuxOrigin;
334  m_board->SetPageSettings( savedPageInfo );
335 
336  return true;
337 }
338 
339 
340 void DIALOG_EXPORT_SVG::OnButtonPlot( wxCommandEvent& event )
341 {
342  m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
344 }
345 
346 
347 bool InvokeExportSVG( PCB_EDIT_FRAME* aCaller, BOARD* aBoard )
348 {
349  DIALOG_EXPORT_SVG dlg( aCaller, aBoard );
350 
351  dlg.ShowModal();
352 
353  return true;
354 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:240
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
const PAGE_INFO & GetPageSettings() const
Definition: board.h:574
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:332
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void SetUseAuxOrigin(bool aAux)
wxCheckListBox * m_CopperLayersList
This file is part of the common library.
void PlotOneBoardLayer(BOARD *aBoard, PLOTTER *aPlotter, PCB_LAYER_ID aLayer, const PCB_PLOT_PARAMS &aPlotOpt)
Function PlotOneBoardLayer main function to plot one copper or technical layer.
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:161
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: board.h:575
bool CreateSVGFile(const wxString &FullFileName)
int GetWidth() const
Definition: eda_rect.h:119
void SetMirror(bool aFlag)
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:559
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings Returns a bit-mask of all t...
Definition: board.cpp:437
LSEQ SeqStackupBottom2Top() const
Return the sequence that is typical for a bottom-to-top stack-up.
Definition: lset.cpp:461
void SetDrillMarksType(DrillMarksType aVal)
PCBNEW_SETTINGS * GetPcbNewSettings()
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:255
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
const wxString & GetFileName() const
Definition: board.h:278
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.
VTBL_ENTRY const wxString AbsolutePath(const wxString &aFileName) const
Function AbsolutePath fixes up aFileName if it is relative to the project's directory to be an absolu...
Definition: project.cpp:270
Board plot function definition file.
virtual bool EndPlot() override
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
void SetPlotFrameRef(bool aFlag)
PCB_LAYER_ID
A quick note on layer IDs:
DIALOG_EXPORT_SVG m_ExportSvg
LSET is a set of PCB_LAYER_IDs.
wxCheckListBox * m_TechnicalLayersList
DIALOG_EXPORT_SVG(PCB_EDIT_FRAME *aParent, BOARD *aBoard)
void OnButtonPlot(wxCommandEvent &event) override
void SetColorSettings(COLOR_SETTINGS *aSettings)
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition: common.cpp:283
REPORTER & Reporter()
returns the reporter object that reports to this panel
int g_DrawDefaultLineThickness
Default line thickness in internal units used to draw or plot items using a default thickness line va...
void SetSkipPlotNPTH_Pads(bool aSkip)
Definition of file extensions used in Kicad.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
AppSettings * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
LSET getCheckBoxSelectedLayers() const
void SetFormat(PLOT_FORMAT aFormat)
PCB_PLOT_PARAMS handles plot parameters and options when plotting/printing a board.
void SetHeightMils(int aHeightInMils)
Definition: page_info.cpp:257
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:81
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
int GetHeight() const
Definition: eda_rect.h:120
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void BuildPlotFileName(wxFileName *aFilename, const wxString &aOutputDir, const wxString &aSuffix, const wxString &aExtension)
Function BuildPlotFileName (helper function) Complete a plot filename: forces the output directory,...
Definition: pcbplot.cpp:370
Class DIALOG_EXPORT_SVG_BASE.
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
~DIALOG_EXPORT_SVG() override
wxStdDialogButtonSizer * m_sdbSizer1
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
#define _(s)
Definition: 3d_actions.cpp:33
WX_HTML_REPORT_PANEL * m_messagesPanel
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
void SetWidthMils(int aWidthInMils)
Definition: page_info.cpp:243
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
PCB_EDIT_FRAME is the main frame for Pcbnew.
const std::string SVGFileExtension
#define IU_PER_MILS
Definition: plotter.cpp:137
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
bool InvokeExportSVG(PCB_EDIT_FRAME *aCaller, BOARD *aBoard)
Function InvokeExportSVG shows the Export SVG dialog.
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:998
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
PCB_EDIT_FRAME * m_parent
void ExportSVGFile(bool aOnlyOneFile)
PLOTTER * StartPlotBoard(BOARD *aBoard, PCB_PLOT_PARAMS *aPlotOpts, int aLayer, const wxString &aFullFileName, const wxString &aSheetDesc)
Open a new plotfile using the options (and especially the format) specified in the options and prepar...
LSEQ UIOrder() const
Definition: lset.cpp:888
wxPoint m_AuxOrigin
origin for plot exports
std::pair< wxCheckListBox *, int > m_boxSelectLayer[PCB_LAYER_ID_COUNT]
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:641
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:176