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 <fctsys.h>
27 #include <kiface_i.h>
28 #include <common.h>
29 #include <pcb_base_frame.h>
31 #include <reporter.h>
32 #include <confirm.h>
33 
34 #include <pcbnew.h>
35 #include <pcbplot.h>
36 #include <class_board.h>
37 #include <dialog_export_svg_base.h>
38 #include <invoke_pcb_dialog.h>
39 #include <wx_html_report_panel.h>
40 #include <bitmaps.h>
41 #include <widgets/unit_binder.h>
42 
44 {
45 public:
46  DIALOG_EXPORT_SVG( PCB_BASE_FRAME* aParent, BOARD* aBoard );
47  ~DIALOG_EXPORT_SVG() override;
48 
49 private:
51  wxConfigBase* m_config;
53  // the list of existing board layers in wxCheckListBox, with the
54  // board layers id:
55  std::pair<wxCheckListBox*, int> m_boxSelectLayer[PCB_LAYER_ID_COUNT];
56  bool m_printBW;
61 
62  void initDialog();
63 
64  void OnButtonPlot( wxCommandEvent& event ) override;
65 
66  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
67  void ExportSVGFile( bool aOnlyOneFile );
68 
69  bool CreateSVGFile( const wxString& FullFileName );
70 
72 };
73 
74 
75 // Keys for configuration
76 #define PLOTSVGMODECOLOR_KEY wxT( "PlotSVGModeColor" )
77 #define PLOTSVGMODEMIRROR_KEY wxT( "PlotSVGModeMirror" )
78 #define PLOTSVGMODEONEFILE_KEY wxT( "PlotSVGModeOneFile" )
79 #define PLOTSVGPAGESIZEOPT_KEY wxT( "PlotSVGPageOpt" )
80 #define PLOTSVGPLOT_BRD_EDGE_KEY wxT( "PlotSVGBrdEdge" )
81 #define PLOTSVG_LAYERBASE wxT( "PlotSVGLayer_%d" )
82 #define PLOTSVG_DIR_KEY wxT( "PlotSVGDirectory" )
83 
84 // reasonable values for default pen width
85 #define WIDTH_MAX_VALUE (2 * IU_PER_MM)
86 #define WIDTH_MIN_VALUE (0.05 * IU_PER_MM)
87 
88 
89 /*
90  * DIALOG_EXPORT_SVG functions
91  */
93  DIALOG_EXPORT_SVG_BASE( aParent ),
96 {
97  m_board = aBoard;
99 
100  memset( m_boxSelectLayer, 0, sizeof( m_boxSelectLayer ) );
101 
102  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
103 
104  initDialog();
105 
106  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
107  // that requires us to correct the button labels here.
108  m_sdbSizer1OK->SetLabel( _( "Export" ) );
109  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
110  m_sdbSizer1->Layout();
111 
113 }
114 
115 
117 {
119  m_printBW = m_ModeColorOption->GetSelection();
120  m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
122  m_outputDirectory.Replace( wxT( "\\" ), wxT( "/" ) );
123 
124  if( m_config )
125  {
130  m_config->Write( PLOTSVGPAGESIZEOPT_KEY, m_rbSvgPageSizeOpt->GetSelection() );
132 
133  wxString layerKey;
134 
135  for( unsigned layer = 0; layer < DIM(m_boxSelectLayer); ++layer )
136  {
137  if( !m_boxSelectLayer[layer].first )
138  continue;
139 
140  layerKey.Printf( PLOTSVG_LAYERBASE, layer );
141  m_config->Write( layerKey, m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) );
142  }
143  }
144 }
145 
146 
148 {
149  if( m_config )
150  {
151  m_config->Read( PLOTSVG_DIR_KEY, &m_outputDirectory, wxEmptyString );
152  m_config->Read( PLOTSVGMODECOLOR_KEY, &m_printBW, false );
153  long ltmp;
154  m_config->Read( PLOTSVGPAGESIZEOPT_KEY, &ltmp, 0 );
155  m_rbSvgPageSizeOpt->SetSelection( ltmp );
156  m_config->Read( PLOTSVGMODEMIRROR_KEY, &m_printMirror, false );
158  m_config->Read( PLOTSVGPLOT_BRD_EDGE_KEY, &ltmp, 1 );
159  m_PrintBoardEdgesCtrl->SetValue( ltmp );
160  }
161 
163 
164  m_ModeColorOption->SetSelection( m_printBW ? 1 : 0 );
165  m_printMirrorOpt->SetValue( m_printMirror );
166  m_rbFileOpt->SetSelection( m_oneFileOnly ? 1 : 0 );
167 
169 
170  for( LSEQ seq = m_board->GetEnabledLayers().UIOrder(); seq; ++seq )
171  {
172  PCB_LAYER_ID layer = *seq;
173  int checkIndex;
174 
175  if( IsCopperLayer( layer ) )
176  {
177  checkIndex = m_CopperLayersList->Append( m_board->GetLayerName( layer ) );
178  m_boxSelectLayer[layer] = std::make_pair( m_CopperLayersList, checkIndex );
179  }
180  else
181  {
182  checkIndex = m_TechnicalLayersList->Append( m_board->GetLayerName( layer ) );
183  m_boxSelectLayer[layer] = std::make_pair( m_TechnicalLayersList, checkIndex );
184  }
185 
186  if( m_config )
187  {
188  wxString layerKey;
189  layerKey.Printf( PLOTSVG_LAYERBASE, layer );
190  bool option;
191 
192  if( m_config && m_config->Read( layerKey, &option ) )
193  m_boxSelectLayer[layer].first->Check( checkIndex, option );
194  }
195  }
196 }
197 
198 
200 {
201  LSET ret;
202 
203  for( unsigned layer = 0; layer < DIM(m_boxSelectLayer); ++layer )
204  {
205  if( !m_boxSelectLayer[layer].first )
206  continue;
207 
208  if( m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) )
209  ret.set( layer );
210  }
211 
212  return ret;
213 }
214 
215 
217 {
218  // Build the absolute path of current output plot directory
219  // to preselect it when opening the dialog.
220  wxFileName fn( m_outputDirectoryName->GetValue() );
221  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
222 
223  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
224 
225  if( dirDialog.ShowModal() == wxID_CANCEL )
226  return;
227 
228  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
229 
230  wxMessageDialog dialog( this, _( "Use a relative path?" ), _( "Plot Output Directory" ),
231  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
232 
233  if( dialog.ShowModal() == wxID_YES )
234  {
235  wxString boardFilePath = Prj().AbsolutePath( m_board->GetFileName() );
236 
237  boardFilePath = wxPathOnly( boardFilePath );
238 
239  if( !dirName.MakeRelativeTo( boardFilePath ) )
240  wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
241  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
242  }
243 
244  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
246 }
247 
248 
249 void DIALOG_EXPORT_SVG::ExportSVGFile( bool aOnlyOneFile )
250 {
252 
253  // Create output directory if it does not exist (also transform it in
254  // absolute form). Bail if it fails
255  wxFileName outputDir = wxFileName::DirName( m_outputDirectory );
256  wxString boardFilename = m_board->GetFileName();
257 
258  REPORTER& reporter = m_messagesPanel->Reporter();
259 
260  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
261  {
262  wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
263  outputDir.GetPath() );
264  DisplayError( this, msg );
265  return;
266  }
267 
268  m_printMirror = m_printMirrorOpt->GetValue();
269  m_printBW = m_ModeColorOption->GetSelection();
271 
272  LSET all_selected = getCheckBoxSelectedLayers();
273 
274  for( LSEQ seq = all_selected.Seq(); seq; ++seq )
275  {
276  PCB_LAYER_ID layer = *seq;
277  wxFileName fn( boardFilename );
278  wxString suffix = aOnlyOneFile ? wxT( "brd" ) : m_board->GetStandardLayerName( layer );
279 
280  BuildPlotFileName( &fn, outputDir.GetPath(), suffix, SVGFileExtension );
281  wxString path = fn.GetFullPath();
282 
283  m_printMaskLayer = aOnlyOneFile ? all_selected : LSET( layer );
284 
285  if( m_PrintBoardEdgesCtrl->IsChecked() )
287 
288  if( CreateSVGFile( path ) )
289  {
290  reporter.Report( wxString::Format( _( "Exported \"%s\"." ), path ),
292  }
293  else // Error
294  {
295  reporter.Report( wxString::Format( _( "Unable to create file \"%s\"." ), path ),
297  }
298 
299  if( aOnlyOneFile )
300  break;
301  }
302 }
303 
304 
305 // Actual SVG file export function.
306 bool DIALOG_EXPORT_SVG::CreateSVGFile( const wxString& aFullFileName )
307 {
308  PCB_PLOT_PARAMS plot_opts;
309 
310  plot_opts.SetPlotFrameRef( m_rbSvgPageSizeOpt->GetSelection() == 0 );
311 
312  // Adding drill marks, for copper layers
313  if( ( m_printMaskLayer & LSET::AllCuMask() ).any() )
315  else
317 
318  plot_opts.SetSkipPlotNPTH_Pads( false );
319 
320  plot_opts.SetMirror( m_printMirror );
321  plot_opts.SetFormat( PLOT_FORMAT_SVG );
322 
323  PAGE_INFO pageInfo = m_board->GetPageSettings();
324  wxPoint axisorigin = m_board->GetAuxOrigin();
325 
326  if( m_rbSvgPageSizeOpt->GetSelection() == 2 ) // Page is board boundary size
327  {
329  PAGE_INFO currpageInfo = m_board->GetPageSettings();
330 
331  currpageInfo.SetWidthMils( bbox.GetWidth() / IU_PER_MILS );
332  currpageInfo.SetHeightMils( bbox.GetHeight() / IU_PER_MILS );
333  m_board->SetPageSettings( currpageInfo );
334  plot_opts.SetUseAuxOrigin( true );
335  wxPoint origin = bbox.GetOrigin();
336  m_board->SetAuxOrigin( origin );
337  }
338 
339  LOCALE_IO toggle;
340 
341  SVG_PLOTTER* plotter = (SVG_PLOTTER*) StartPlotBoard( m_board, &plot_opts, UNDEFINED_LAYER,
342  aFullFileName, wxEmptyString );
343 
344  if( plotter )
345  {
346  plotter->SetColorMode( !m_printBW );
347 
348  for( LSEQ seq = m_printMaskLayer.SeqStackupBottom2Top(); seq; ++seq )
349  PlotOneBoardLayer( m_board, plotter, *seq, plot_opts );
350 
351  plotter->EndPlot();
352  }
353 
354  delete plotter;
355 
356  m_board->SetAuxOrigin( axisorigin ); // reset to the values saved earlier
357  m_board->SetPageSettings( pageInfo );
358 
359  return true;
360 }
361 
362 
363 void DIALOG_EXPORT_SVG::OnButtonPlot( wxCommandEvent& event )
364 {
365  m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
367 }
368 
369 
370 bool InvokeExportSVG( PCB_BASE_FRAME* aCaller, BOARD* aBoard )
371 {
372  DIALOG_EXPORT_SVG dlg( aCaller, aBoard );
373 
374  dlg.ShowModal();
375 
376  return true;
377 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:673
#define DIM(x)
of elements in an array
Definition: macros.h:98
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
#define PLOTSVGMODEONEFILE_KEY
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:179
DIALOG_EXPORT_SVG(PCB_BASE_FRAME *aParent, BOARD *aBoard)
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void SetUseAuxOrigin(bool aAux)
wxCheckListBox * m_CopperLayersList
This file is part of the common library.
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
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.
Class BOARD to handle a board.
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:126
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: class_board.h:554
wxConfigBase * m_config
void SetFormat(PlotFormat aFormat)
bool CreateSVGFile(const wxString &FullFileName)
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
#define PLOTSVG_DIR_KEY
int GetHeight() const
Definition: eda_rect.h:118
void SetMirror(bool aFlag)
void SetDrillMarksType(DrillMarksType aVal)
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
#define PLOTSVGPLOT_BRD_EDGE_KEY
LSET GetEnabledLayers() const
Function GetEnabledLayers is a proxy function that calls the corresponding function in m_BoardSetting...
void SetAuxOrigin(const wxPoint &aPoint)
Function SetAuxOrigin sets the origin point used for plotting.
Definition: class_board.h:348
LSEQ SeqStackupBottom2Top() const
Function SeqStackBottom2Top returns the sequence that is typical for a bottom-to-top stack-up...
Definition: lset.cpp:412
#define WIDTH_MAX_VALUE
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
Classes used in Pcbnew, CvPcb and GerbView.
Board plot function definition file.
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Function Seq returns an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:364
virtual bool EndPlot() override
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void SetPlotFrameRef(bool aFlag)
PCB_LAYER_ID
A quick note on layer IDs:
Class LSET is a set of PCB_LAYER_IDs.
wxCheckListBox * m_TechnicalLayersList
void OnButtonPlot(wxCommandEvent &event) override
const wxString & GetFileName() const
Definition: class_board.h:237
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:54
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter)
Make aTargetFullFileName absolute and create the path of this file if it doesn&#39;t yet exist...
Definition: common.cpp:469
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...
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
VTBL_ENTRY const wxString AbsolutePath(const wxString &aFileName) const
Function AbsolutePath fixes up aFileName if it is relative to the project&#39;s directory to be an absolu...
Definition: project.cpp:380
void SetSkipPlotNPTH_Pads(bool aSkip)
The common library.
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
Class PCB_PLOT_PARAMS handles plot parameters and options when plotting/printing a board...
void SetHeightMils(int aHeightInMils)
Definition: page_info.cpp:253
Class LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
const PAGE_INFO & GetPageSettings() const
Definition: class_board.h:553
LSET getCheckBoxSelectedLayers() const
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:349
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:380
Class DIALOG_EXPORT_SVG_BASE.
#define PLOTSVGMODEMIRROR_KEY
#define PLOTSVG_LAYERBASE
wxStdDialogButtonSizer * m_sdbSizer1
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool InvokeExportSVG(PCB_BASE_FRAME *aCaller, BOARD *aBoard)
Function InvokeExportSVG shows the Export SVG dialog.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
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:239
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
#define WIDTH_MIN_VALUE
const std::string SVGFileExtension
#define IU_PER_MILS
Definition: plotter.cpp:134
The common library.
int GetWidth() const
Definition: eda_rect.h:117
#define PLOTSVGPAGESIZEOPT_KEY
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
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...
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
std::pair< wxCheckListBox *, int > m_boxSelectLayer[PCB_LAYER_ID_COUNT]
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Function GetStandardLayerName returns an "English Standard" name of a PCB layer when given aLayerNumb...
Definition: class_board.h:653
LSEQ UIOrder() const
Definition: lset.cpp:800
#define PLOTSVGMODECOLOR_KEY