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 <worksheet.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 
53 void SCH_EDIT_FRAME::PlotSchematic( wxCommandEvent& event )
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 
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  long plotfmt;
116  m_config->Read( PLOT_FORMAT_KEY, &plotfmt, 0 );
117 
118  switch( plotfmt )
119  {
120  default:
121  case PLOT_FORMAT_POST: m_plotFormatOpt->SetSelection( 0 ); break;
122  case PLOT_FORMAT_PDF: m_plotFormatOpt->SetSelection( 1 ); break;
123  case PLOT_FORMAT_SVG: m_plotFormatOpt->SetSelection( 2 ); break;
124  case PLOT_FORMAT_DXF: m_plotFormatOpt->SetSelection( 3 ); break;
125  case PLOT_FORMAT_HPGL: m_plotFormatOpt->SetSelection( 4 ); break;
126  }
127 
128  // Set the default line width (pen width which should be used for
129  // items that do not have a pen size defined (like frame ref)
131 
132  // Initialize HPGL specific widgets
134 
135  // Plot directory
136  wxString path = m_parent->GetPlotDirectoryName();
137 #ifdef __WINDOWS__
138  path.Replace( '/', '\\' );
139 #endif
140  m_outputDirectoryName->SetValue( path );
141 }
142 
143 
149 {
150  // Build the absolute path of current output plot directory
151  // to preselect it when opening the dialog.
152  wxFileName fn( m_outputDirectoryName->GetValue() );
153  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
154 
155  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
156 
157  if( dirDialog.ShowModal() == wxID_CANCEL )
158  return;
159 
160  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
161 
163  wxString defaultPath = fn.GetPathWithSep();
164  wxString msg;
165  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
166 
167  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
168  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
169 
170  // relative directory selected
171  if( dialog.ShowModal() == wxID_YES )
172  {
173  if( !dirName.MakeRelativeTo( defaultPath ) )
174  wxMessageBox( _( "Cannot make path relative (target volume different from file "
175  "volume)!" ), _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
176  }
177 
178  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
179 }
180 
181 
183 {
184  switch( m_plotFormatOpt->GetSelection() )
185  {
186  default:
187  case 0: return PLOT_FORMAT_POST;
188  case 1: return PLOT_FORMAT_PDF;
189  case 2: return PLOT_FORMAT_SVG;
190  case 3: return PLOT_FORMAT_DXF;
191  case 4: return PLOT_FORMAT_HPGL;
192  }
193 }
194 
195 
196 void DIALOG_PLOT_SCHEMATIC::OnPageSizeSelected( wxCommandEvent& event )
197 {
199  m_HPGLPaperSizeSelect = m_paperSizeOption->GetSelection();
200  else
201  m_pageSizeSelect = m_paperSizeOption->GetSelection();
202 }
203 
204 
205 void DIALOG_PLOT_SCHEMATIC::OnUpdateUI( wxUpdateUIEvent& event )
206 {
208 
209  if( fmt != m_plotFormat )
210  {
211  m_plotFormat = fmt;
212 
213  wxArrayString paperSizes;
214  paperSizes.push_back( _( "Schematic size" ) );
215 
216  int selection;
217 
218  if( fmt == PLOT_FORMAT_HPGL )
219  {
220  paperSizes.push_back( _( "A4" ) );
221  paperSizes.push_back( _( "A3" ) );
222  paperSizes.push_back( _( "A2" ) );
223  paperSizes.push_back( _( "A1" ) );
224  paperSizes.push_back( _( "A0" ) );
225  paperSizes.push_back( _( "A" ) );
226  paperSizes.push_back( _( "B" ) );
227  paperSizes.push_back( _( "C" ) );
228  paperSizes.push_back( _( "D" ) );
229  paperSizes.push_back( _( "E" ) );
230 
231  selection = m_HPGLPaperSizeSelect;
232  }
233  else
234  {
235  paperSizes.push_back( _( "A4" ) );
236  paperSizes.push_back( _( "A" ) );
237 
238  selection = m_pageSizeSelect;
239  }
240 
241  m_paperSizeOption->Set( paperSizes );
242  m_paperSizeOption->SetSelection( selection );
243 
245  || fmt == PLOT_FORMAT_SVG );
246 
247  m_plotOriginTitle->Enable( fmt == PLOT_FORMAT_HPGL );
248  m_plotOriginOpt->Enable( fmt == PLOT_FORMAT_HPGL );
250  }
251 }
252 
253 
255 {
257 
260  m_config->Write( PLOT_FORMAT_KEY, (long) GetPlotFileFormat() );
263 
264  // HPGL Pen Size is stored in mm in config
265  m_config->Write( PLOT_HPGL_PEN_SIZE_KEY, m_HPGLPenSize/IU_PER_MM );
266 
268 
269  // Plot directory
270  wxString path = m_outputDirectoryName->GetValue();
271  path.Replace( '\\', '/' );
272 
273  if( m_parent->GetPlotDirectoryName() != path )
274  m_configChanged = true;
275 
277 }
278 
279 
280 void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
281 {
282  PlotSchematic( false );
283 }
284 
285 
286 void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
287 {
288  PlotSchematic( true );
289 }
290 
291 
293 {
294  getPlotOptions();
295 
296  switch( GetPlotFileFormat() )
297  {
298  default:
299  case PLOT_FORMAT_POST: createPSFile( aPlotAll, getPlotFrameRef() ); break;
300  case PLOT_FORMAT_DXF: CreateDXFFile( aPlotAll, getPlotFrameRef() ); break;
301  case PLOT_FORMAT_PDF: createPDFFile( aPlotAll, getPlotFrameRef() ); break;
302  case PLOT_FORMAT_SVG: createSVGFile( aPlotAll, getPlotFrameRef() ); break;
303  case PLOT_FORMAT_HPGL: createHPGLFile( aPlotAll, getPlotFrameRef() ); break;
304  }
305 }
306 
307 
308 wxFileName DIALOG_PLOT_SCHEMATIC::createPlotFileName( wxTextCtrl* aOutputDirectoryName,
309  wxString& aPlotFileName,
310  wxString& aExtension,
311  REPORTER* aReporter )
312 {
313  wxString outputDirName = aOutputDirectoryName->GetValue();
314  wxFileName outputDir = wxFileName::DirName( outputDirName );
315 
316  wxString plotFileName = Prj().AbsolutePath( aPlotFileName + wxT( "." ) + aExtension);
317 
318  if( !EnsureFileDirectoryExists( &outputDir, plotFileName, aReporter ) )
319  {
320  wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
321  outputDir.GetPath() );
322  aReporter->Report( msg, REPORTER::RPT_ERROR );
323  }
324 
325  wxFileName fn( plotFileName );
326  fn.SetPath( outputDir.GetFullPath() );
327  return fn;
328 }
Definition of the SCH_SHEET class for Eeschema.
void PlotSchematic(bool aPlotAll)
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...
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
PlotFormat
Enum PlotFormat is the set of supported output plot formats.
Definition: plotter.h:50
void OnPlotCurrent(wxCommandEvent &event) override
Class DIALOG_PLOT_SCHEMATIC_BASE.
Schematic editor (Eeschema) main window.
Class 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
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
void SaveProjectSettings(bool aAskForSave) override
Save changes to the project settings to the project (.pro) file.
void SetDefaultLineThickness(int aThickness)
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
#define PLOT_MODECOLOR_KEY
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 createHPGLFile(bool aPlotAll, bool aPlotFrameRef)
const wxString & GetPlotDirectoryName() const
accessor to the destination directory to use when generating plot files.
void OnPlotAll(wxCommandEvent &event) override
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:56
void setPlotFrameRef(bool aPlot)
wxFileName createPlotFileName(wxTextCtrl *aOutputDirectoryName, wxString &aPlotFileName, wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
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
#define PLOT_FRAME_REFERENCE_KEY
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 PlotSchematic(wxCommandEvent &event)
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
wxString GetFileName(void) const
Return the filename corresponding to this sheet.
Definition: sch_sheet.cpp:661
#define PLOT_HPGL_PEN_SIZE_KEY
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:92
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
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.
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)