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 <bitmaps.h>
32 #include <dialog_plot_schematic.h>
33 #include <eeschema_settings.h>
34 #include <kiface_i.h>
35 #include <pgm_base.h>
36 #include <sch_sheet.h>
38 #include <ws_painter.h>
39 
40 
41 // static members (static to remember last state):
44 
45 
47 {
48  DIALOG_PLOT_SCHEMATIC dlg( this );
49 
50  dlg.ShowModal();
51 
52  // save project config if the prj config has changed:
53  if( dlg.PrjConfigChanged() )
55 }
56 
57 
59  : DIALOG_PLOT_SCHEMATIC_BASE( parent ),
60  m_parent( parent ),
61  m_plotFormat( PLOT_FORMAT::UNDEFINED ),
62  m_defaultLineWidth( parent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits, true ),
63  m_penWidth( parent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits, true )
64 {
65  m_configChanged = false;
66 
67  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
68 
69  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
70  // that requires us to correct the button labels here.
71  m_sdbSizer1OK->SetLabel( _( "Plot All Pages" ) );
72  m_sdbSizer1Apply->SetLabel( _( "Plot Current Page" ) );
73  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
74  m_sdbSizer1->Layout();
75 
76  m_sdbSizer1OK->SetDefault();
77  initDlg();
78 
79  // Now all widgets have the size fixed, call FinishDialogSettings
81 }
82 
83 
84 // Initialize the dialog options:
86 {
87  auto cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
88 
89  for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
90  {
91  int idx = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
92 
93  if( settings->GetFilename() == cfg->m_PlotPanel.color_theme )
94  m_colorTheme->SetSelection( idx );
95  }
96 
97  m_colorTheme->Enable( cfg->m_PlotPanel.color );
98 
99  m_plotBackgroundColor->Enable( cfg->m_PlotPanel.color );
100  m_plotBackgroundColor->SetValue( cfg->m_PlotPanel.background_color );
101 
102  // Set color or B&W plot option
103  setModeColor( cfg->m_PlotPanel.color );
104 
105  // Set plot or not frame reference option
106  setPlotFrameRef( cfg->m_PlotPanel.frame_reference );
107 
108  // Set HPGL plot origin to center of paper of left bottom corner
109  SetPlotOriginCenter( cfg->m_PlotPanel.hpgl_origin );
110 
111  m_HPGLPaperSizeSelect = cfg->m_PlotPanel.hpgl_paper_size;
112 
113  // HPGL Pen Size is stored in mm in config
114  m_HPGLPenSize = cfg->m_PlotPanel.hpgl_pen_size * IU_PER_MM;
115 
116  // Switch to the last save plot format
117  switch( static_cast<PLOT_FORMAT>( cfg->m_PlotPanel.format ) )
118  {
119  default:
120  case PLOT_FORMAT::POST:
121  m_plotFormatOpt->SetSelection( 0 );
122  break;
123  case PLOT_FORMAT::PDF:
124  m_plotFormatOpt->SetSelection( 1 );
125  break;
126  case PLOT_FORMAT::SVG:
127  m_plotFormatOpt->SetSelection( 2 );
128  break;
129  case PLOT_FORMAT::DXF:
130  m_plotFormatOpt->SetSelection( 3 );
131  m_plotBackgroundColor->Disable();
132  break;
133  case PLOT_FORMAT::HPGL:
134  m_plotFormatOpt->SetSelection( 4 );
135  m_plotBackgroundColor->Disable();
136  break;
137  }
138 
139  // Set the default line width (pen width which should be used for
140  // items that do not have a pen size defined (like frame ref)
142 
143  // Initialize HPGL specific widgets
145 
146  // Plot directory
147  wxString path = m_parent->GetPlotDirectoryName();
148 #ifdef __WINDOWS__
149  path.Replace( '/', '\\' );
150 #endif
151  m_outputDirectoryName->SetValue( path );
152 }
153 
154 
160 {
161  // Build the absolute path of current output plot directory
162  // to preselect it when opening the dialog.
163  wxFileName fn( m_outputDirectoryName->GetValue() );
164  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
165 
166  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
167 
168  if( dirDialog.ShowModal() == wxID_CANCEL )
169  return;
170 
171  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
172 
174  wxString defaultPath = fn.GetPathWithSep();
175  wxString msg;
176  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
177 
178  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
179  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
180 
181  // relative directory selected
182  if( dialog.ShowModal() == wxID_YES )
183  {
184  if( !dirName.MakeRelativeTo( defaultPath ) )
185  wxMessageBox( _( "Cannot make path relative (target volume different from file "
186  "volume)!" ), _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
187  }
188 
189  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
190 }
191 
192 
194 {
195  switch( m_plotFormatOpt->GetSelection() )
196  {
197  default:
198  case 0:
199  return PLOT_FORMAT::POST;
200  case 1:
201  return PLOT_FORMAT::PDF;
202  case 2:
203  return PLOT_FORMAT::SVG;
204  case 3:
205  return PLOT_FORMAT::DXF;
206  case 4:
207  return PLOT_FORMAT::HPGL;
208  }
209 }
210 
211 
212 void DIALOG_PLOT_SCHEMATIC::OnPageSizeSelected( wxCommandEvent& event )
213 {
215  m_HPGLPaperSizeSelect = m_paperSizeOption->GetSelection();
216  else
217  m_pageSizeSelect = m_paperSizeOption->GetSelection();
218 }
219 
220 
221 void DIALOG_PLOT_SCHEMATIC::OnUpdateUI( wxUpdateUIEvent& event )
222 {
224 
225  if( fmt != m_plotFormat )
226  {
227  m_plotFormat = fmt;
228 
229  wxArrayString paperSizes;
230  paperSizes.push_back( _( "Schematic size" ) );
231 
232  int selection;
233 
234  if( fmt == PLOT_FORMAT::HPGL )
235  {
236  paperSizes.push_back( _( "A4" ) );
237  paperSizes.push_back( _( "A3" ) );
238  paperSizes.push_back( _( "A2" ) );
239  paperSizes.push_back( _( "A1" ) );
240  paperSizes.push_back( _( "A0" ) );
241  paperSizes.push_back( _( "A" ) );
242  paperSizes.push_back( _( "B" ) );
243  paperSizes.push_back( _( "C" ) );
244  paperSizes.push_back( _( "D" ) );
245  paperSizes.push_back( _( "E" ) );
246 
247  selection = m_HPGLPaperSizeSelect;
248  }
249  else
250  {
251  paperSizes.push_back( _( "A4" ) );
252  paperSizes.push_back( _( "A" ) );
253 
254  selection = m_pageSizeSelect;
255  }
256 
257  m_paperSizeOption->Set( paperSizes );
258  m_paperSizeOption->SetSelection( selection );
259 
261  fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
262 
263  m_plotOriginTitle->Enable( fmt == PLOT_FORMAT::HPGL );
264  m_plotOriginOpt->Enable( fmt == PLOT_FORMAT::HPGL );
266 
267  m_plotBackgroundColor->Enable(
268  fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
269 
270  m_colorTheme->Enable( fmt != PLOT_FORMAT::HPGL );
271  m_ModeColorOption->Enable( fmt != PLOT_FORMAT::HPGL );
272  }
273 }
274 
275 
277 {
279 
280  auto cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
281 
282  cfg->m_PlotPanel.background_color = m_plotBackgroundColor->GetValue();
283  cfg->m_PlotPanel.color = getModeColor();
284  cfg->m_PlotPanel.color_theme = static_cast<COLOR_SETTINGS*>(
285  m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) )->GetFilename();
286  cfg->m_PlotPanel.frame_reference = getPlotFrameRef();
287  cfg->m_PlotPanel.format = static_cast<int>( GetPlotFileFormat() );
288  cfg->m_PlotPanel.hpgl_origin = GetPlotOriginCenter();
289  cfg->m_PlotPanel.hpgl_paper_size = m_HPGLPaperSizeSelect;
290 
291  // HPGL Pen Size is stored in mm in config
292  cfg->m_PlotPanel.hpgl_pen_size = m_HPGLPenSize / IU_PER_MM;
293 
295 
296  // Plot directory
297  wxString path = m_outputDirectoryName->GetValue();
298  path.Replace( '\\', '/' );
299 
300  if( m_parent->GetPlotDirectoryName() != path )
301  m_configChanged = true;
302 
304 }
305 
306 
307 void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
308 {
309  PlotSchematic( false );
310 }
311 
312 
313 void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
314 {
315  PlotSchematic( true );
316 }
317 
318 
320 {
321  getPlotOptions();
322 
323  switch( GetPlotFileFormat() )
324  {
325  default:
326  case PLOT_FORMAT::POST:
327  createPSFile( aPlotAll, getPlotFrameRef() );
328  break;
329  case PLOT_FORMAT::DXF:
330  CreateDXFFile( aPlotAll, getPlotFrameRef() );
331  break;
332  case PLOT_FORMAT::PDF:
333  createPDFFile( aPlotAll, getPlotFrameRef() );
334  break;
335  case PLOT_FORMAT::SVG:
336  createSVGFile( aPlotAll, getPlotFrameRef() );
337  break;
338  case PLOT_FORMAT::HPGL:
339  createHPGLFile( aPlotAll, getPlotFrameRef() );
340  break;
341  }
342 }
343 
344 
345 wxFileName DIALOG_PLOT_SCHEMATIC::createPlotFileName( wxTextCtrl* aOutputDirectoryName,
346  wxString& aPlotFileName,
347  wxString& aExtension,
348  REPORTER* aReporter )
349 {
350  wxString outputDirName = aOutputDirectoryName->GetValue();
351  wxFileName outputDir = wxFileName::DirName( outputDirName );
352 
353  wxString plotFileName = Prj().AbsolutePath( aPlotFileName + wxT( "." ) + aExtension);
354 
355  if( !EnsureFileDirectoryExists( &outputDir, plotFileName, aReporter ) )
356  {
357  wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
358  outputDir.GetPath() );
359  aReporter->Report( msg, RPT_SEVERITY_ERROR );
360  }
361 
362  wxFileName fn( plotFileName );
363  fn.SetPath( outputDir.GetFullPath() );
364  return fn;
365 }
void PlotSchematic(bool aPlotAll)
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
void CreateDXFFile(bool aPlotAll, bool aPlotFrameRef)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
void SetPlotDirectoryName(const wxString &aDirName)
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
Set the m_outputDirectoryName variable to the selected directory from directory dialog.
void SaveProjectSettings() override
Save changes to the project settings to the project (.pro) file.
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...
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:62
void OnPageSizeSelected(wxCommandEvent &event) override
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:413
void SetDefaultLineThickness(int aThickness)
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
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:51
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:552
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
const wxString & GetPlotDirectoryName() const
accessor to the destination directory to use when generating plot files.
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:491
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:33
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
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.
void setModeColor(bool aColor)