KiCad PCB EDA Suite
dialog_plot_schematic.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2018 Jean-Pierre Charras jp.charras at wanadoo.fr
8  * Copyright (C) 1992-2010 Lorenzo Marcantonio
9  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
10  *
11  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <pgm_base.h>
32 #include <kiface_i.h>
33 #include <bitmaps.h>
34 #include <ws_painter.h>
35 #include <sch_sheet.h>
36 #include <dialog_plot_schematic.h>
37 
38 
39 // Keys for configuration
40 #define PLOT_FORMAT_KEY wxT( "PlotFormat" )
41 #define PLOT_MODECOLOR_KEY wxT( "PlotModeColor" )
42 #define PLOT_FRAME_REFERENCE_KEY wxT( "PlotFrameRef" )
43 #define PLOT_HPGL_ORIGIN_KEY wxT( "PlotHPGLOrg" )
44 #define PLOT_HPGL_PAPERSIZE_KEY wxT( "PlotHPGLPaperSize" )
45 #define PLOT_HPGL_PEN_SIZE_KEY wxT( "PlotHPGLPenSize" )
46 
47 
48 // static members (static to remember last state):
51 
52 
54 {
55  DIALOG_PLOT_SCHEMATIC dlg( this );
56 
57  dlg.ShowModal();
58 
59  // save project config if the prj config has changed:
60  if( dlg.PrjConfigChanged() )
61  SaveProjectSettings( false );
62 }
63 
64 
66  : DIALOG_PLOT_SCHEMATIC_BASE( parent ),
67  m_parent( parent ),
68  m_plotFormat( PLOT_FORMAT::UNDEFINED ),
69  m_defaultLineWidth( parent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits, true ),
70  m_penWidth( parent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits, true )
71 {
73  m_configChanged = false;
74 
75  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
76 
77  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
78  // that requires us to correct the button labels here.
79  m_sdbSizer1OK->SetLabel( _( "Plot All Pages" ) );
80  m_sdbSizer1Apply->SetLabel( _( "Plot Current Page" ) );
81  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
82  m_sdbSizer1->Layout();
83 
84  m_sdbSizer1OK->SetDefault();
85  initDlg();
86 
87  // Now all widgets have the size fixed, call FinishDialogSettings
89 }
90 
91 
92 // Initialize the dialog options:
94 {
95  // Set color or B&W plot option
96  bool tmp;
97  m_config->Read( PLOT_MODECOLOR_KEY, &tmp, true );
98  setModeColor( tmp );
99 
100  // Set plot or not frame reference option
101  m_config->Read( PLOT_FRAME_REFERENCE_KEY, &tmp, true );
102  setPlotFrameRef( tmp );
103 
104  // Set HPGL plot origin to center of paper of left bottom corner
105  m_config->Read( PLOT_HPGL_ORIGIN_KEY, &tmp, false );
106  SetPlotOriginCenter( tmp );
107 
109 
110  // HPGL Pen Size is stored in mm in config
112  m_HPGLPenSize *= IU_PER_MM;
113 
114  // Switch to the last save plot format
115  int plotfmt;
116  m_config->Read( PLOT_FORMAT_KEY, &plotfmt, 0 );
117 
118  switch( static_cast<PLOT_FORMAT>( plotfmt ) )
119  {
120  default:
121  case PLOT_FORMAT::POST:
122  m_plotFormatOpt->SetSelection( 0 );
123  break;
124  case PLOT_FORMAT::PDF:
125  m_plotFormatOpt->SetSelection( 1 );
126  break;
127  case PLOT_FORMAT::SVG:
128  m_plotFormatOpt->SetSelection( 2 );
129  break;
130  case PLOT_FORMAT::DXF:
131  m_plotFormatOpt->SetSelection( 3 );
132  break;
133  case PLOT_FORMAT::HPGL:
134  m_plotFormatOpt->SetSelection( 4 );
135  break;
136  }
137 
138  // Set the default line width (pen width which should be used for
139  // items that do not have a pen size defined (like frame ref)
141 
142  // Initialize HPGL specific widgets
144 
145  // Plot directory
146  wxString path = m_parent->GetPlotDirectoryName();
147 #ifdef __WINDOWS__
148  path.Replace( '/', '\\' );
149 #endif
150  m_outputDirectoryName->SetValue( path );
151 }
152 
153 
159 {
160  // Build the absolute path of current output plot directory
161  // to preselect it when opening the dialog.
162  wxFileName fn( m_outputDirectoryName->GetValue() );
163  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
164 
165  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
166 
167  if( dirDialog.ShowModal() == wxID_CANCEL )
168  return;
169 
170  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
171 
173  wxString defaultPath = fn.GetPathWithSep();
174  wxString msg;
175  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
176 
177  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
178  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
179 
180  // relative directory selected
181  if( dialog.ShowModal() == wxID_YES )
182  {
183  if( !dirName.MakeRelativeTo( defaultPath ) )
184  wxMessageBox( _( "Cannot make path relative (target volume different from file "
185  "volume)!" ), _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
186  }
187 
188  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
189 }
190 
191 
193 {
194  switch( m_plotFormatOpt->GetSelection() )
195  {
196  default:
197  case 0:
198  return PLOT_FORMAT::POST;
199  case 1:
200  return PLOT_FORMAT::PDF;
201  case 2:
202  return PLOT_FORMAT::SVG;
203  case 3:
204  return PLOT_FORMAT::DXF;
205  case 4:
206  return PLOT_FORMAT::HPGL;
207  }
208 }
209 
210 
211 void DIALOG_PLOT_SCHEMATIC::OnPageSizeSelected( wxCommandEvent& event )
212 {
214  m_HPGLPaperSizeSelect = m_paperSizeOption->GetSelection();
215  else
216  m_pageSizeSelect = m_paperSizeOption->GetSelection();
217 }
218 
219 
220 void DIALOG_PLOT_SCHEMATIC::OnUpdateUI( wxUpdateUIEvent& event )
221 {
223 
224  if( fmt != m_plotFormat )
225  {
226  m_plotFormat = fmt;
227 
228  wxArrayString paperSizes;
229  paperSizes.push_back( _( "Schematic size" ) );
230 
231  int selection;
232 
233  if( fmt == PLOT_FORMAT::HPGL )
234  {
235  paperSizes.push_back( _( "A4" ) );
236  paperSizes.push_back( _( "A3" ) );
237  paperSizes.push_back( _( "A2" ) );
238  paperSizes.push_back( _( "A1" ) );
239  paperSizes.push_back( _( "A0" ) );
240  paperSizes.push_back( _( "A" ) );
241  paperSizes.push_back( _( "B" ) );
242  paperSizes.push_back( _( "C" ) );
243  paperSizes.push_back( _( "D" ) );
244  paperSizes.push_back( _( "E" ) );
245 
246  selection = m_HPGLPaperSizeSelect;
247  }
248  else
249  {
250  paperSizes.push_back( _( "A4" ) );
251  paperSizes.push_back( _( "A" ) );
252 
253  selection = m_pageSizeSelect;
254  }
255 
256  m_paperSizeOption->Set( paperSizes );
257  m_paperSizeOption->SetSelection( selection );
258 
260  fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
261 
262  m_plotOriginTitle->Enable( fmt == PLOT_FORMAT::HPGL );
263  m_plotOriginOpt->Enable( fmt == PLOT_FORMAT::HPGL );
265  }
266 }
267 
268 
270 {
272 
275  m_config->Write( PLOT_FORMAT_KEY, (long) GetPlotFileFormat() );
278 
279  // HPGL Pen Size is stored in mm in config
280  m_config->Write( PLOT_HPGL_PEN_SIZE_KEY, m_HPGLPenSize/IU_PER_MM );
281 
283 
284  // Plot directory
285  wxString path = m_outputDirectoryName->GetValue();
286  path.Replace( '\\', '/' );
287 
288  if( m_parent->GetPlotDirectoryName() != path )
289  m_configChanged = true;
290 
292 }
293 
294 
295 void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
296 {
297  PlotSchematic( false );
298 }
299 
300 
301 void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
302 {
303  PlotSchematic( true );
304 }
305 
306 
308 {
309  getPlotOptions();
310 
311  switch( GetPlotFileFormat() )
312  {
313  default:
314  case PLOT_FORMAT::POST:
315  createPSFile( aPlotAll, getPlotFrameRef() );
316  break;
317  case PLOT_FORMAT::DXF:
318  CreateDXFFile( aPlotAll, getPlotFrameRef() );
319  break;
320  case PLOT_FORMAT::PDF:
321  createPDFFile( aPlotAll, getPlotFrameRef() );
322  break;
323  case PLOT_FORMAT::SVG:
324  createSVGFile( aPlotAll, getPlotFrameRef() );
325  break;
326  case PLOT_FORMAT::HPGL:
327  createHPGLFile( aPlotAll, getPlotFrameRef() );
328  break;
329  }
330 }
331 
332 
333 wxFileName DIALOG_PLOT_SCHEMATIC::createPlotFileName( wxTextCtrl* aOutputDirectoryName,
334  wxString& aPlotFileName,
335  wxString& aExtension,
336  REPORTER* aReporter )
337 {
338  wxString outputDirName = aOutputDirectoryName->GetValue();
339  wxFileName outputDir = wxFileName::DirName( outputDirName );
340 
341  wxString plotFileName = Prj().AbsolutePath( aPlotFileName + wxT( "." ) + aExtension);
342 
343  if( !EnsureFileDirectoryExists( &outputDir, plotFileName, aReporter ) )
344  {
345  wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
346  outputDir.GetPath() );
347  aReporter->Report( msg, REPORTER::RPT_ERROR );
348  }
349 
350  wxFileName fn( plotFileName );
351  fn.SetPath( outputDir.GetFullPath() );
352  return fn;
353 }
void PlotSchematic(bool aPlotAll)
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
void CreateDXFFile(bool aPlotAll, bool aPlotFrameRef)
void SetPlotDirectoryName(const wxString &aDirName)
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
Set the m_outputDirectoryName variable to the selected directory from directory dialog.
void OnUpdateUI(wxUpdateUIEvent &event) override
wxStdDialogButtonSizer * m_sdbSizer1
void SetPlotOriginCenter(bool aCenter)
DIALOG_PLOT_SCHEMATIC(SCH_EDIT_FRAME *parent)
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void OnPlotCurrent(wxCommandEvent &event) override
Class DIALOG_PLOT_SCHEMATIC_BASE.
Schematic editor (Eeschema) main window.
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
#define PLOT_HPGL_ORIGIN_KEY
void OnPageSizeSelected(wxCommandEvent &event) override
void SaveProjectSettings(bool aAskForSave) override
Save changes to the project settings to the project (.pro) file.
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:386
void SetDefaultLineThickness(int aThickness)
#define PLOT_MODECOLOR_KEY
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void createHPGLFile(bool aPlotAll, bool aPlotFrameRef)
void OnPlotAll(wxCommandEvent &event) override
void setPlotFrameRef(bool aPlot)
PLOT_FORMAT
Enum PlotFormat is the set of supported output plot formats.
Definition: plotter.h:50
wxFileName createPlotFileName(wxTextCtrl *aOutputDirectoryName, wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
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:456
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
#define PLOT_FRAME_REFERENCE_KEY
const wxString & GetPlotDirectoryName() const
accessor to the destination directory to use when generating plot files.
#define PLOT_HPGL_PEN_SIZE_KEY
wxString GetFileName(void) const
Return the filename corresponding to this sheet.
Definition: sch_sheet.cpp:616
void createPDFFile(bool aPlotAll, bool aPlotFrameRef)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
see class PGM_BASE
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
#define _(s)
Definition: 3d_actions.cpp:31
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
SCH_SHEET * g_RootSheet
void createPSFile(bool aPlotAll, bool aPlotFrameRef)
void createSVGFile(bool aPlotAll, bool aPlotFrameRef)
void Enable(bool aEnable)
Function Enable Enables/diasables the label, widget and units label.
#define PLOT_HPGL_PAPERSIZE_KEY
#define PLOT_FORMAT_KEY
void setModeColor(bool aColor)