KiCad PCB EDA Suite
gen_footprints_placefile.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) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 /*
25  * 1 - create ascii files for automatic placement of smd components
26  * 2 - create a footprint report (pos and footprint descr) (ascii file)
27  */
28 
29 #include <confirm.h>
30 #include <kicad_string.h>
31 #include <gestfich.h>
32 #include <pcb_edit_frame.h>
33 #include <pcbnew_settings.h>
34 #include <pgm_base.h>
35 #include <bitmaps.h>
36 #include <reporter.h>
38 #include <board.h>
39 #include <footprint.h>
41 #include <kiface_i.h>
42 #include <wx_html_report_panel.h>
46 
47 
53 {
54 public:
57  m_parent( aParent ),
58  m_plotOpts( aParent->GetPlotSettings() )
59  {
61  initDialog();
62 
63  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
64  // that requires us to correct the button labels here.
65  m_sdbSizerOK->SetLabel( _( "Generate Position File" ) );
66  m_sdbSizerCancel->SetLabel( _( "Close" ) );
67  m_sdbSizer->Layout();
68 
69  m_sdbSizerOK->SetDefault();
70 
71  GetSizer()->SetSizeHints(this);
72  Centre();
73  }
74 
75 private:
79 
80  static int m_unitsOpt;
81  static int m_fileOpt;
82  static int m_fileFormat;
83  static bool m_includeBoardEdge;
84 
85  void initDialog();
86  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
87  void OnGenerate( wxCommandEvent& event ) override;
88 
89  void onUpdateUIUnits( wxUpdateUIEvent& event ) override
90  {
91  m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
92  }
93 
94  void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
95  {
96  m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
97  }
98 
99  void onUpdateUIExcludeTH( wxUpdateUIEvent& event ) override
100  {
101  if( m_rbFormat->GetSelection() == 2 )
102  {
103  m_excludeTH->SetValue( false );
104  m_excludeTH->Enable( false );
105  }
106  else
107  {
108  m_excludeTH->Enable( true );
109  }
110  }
111 
112  void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
113  {
114  m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
115  }
116 
119  bool CreateAsciiFiles();
120 
123  bool CreateGerberFiles();
124 
125  // accessors to options:
126  bool UnitsMM()
127  {
128  return m_radioBoxUnits->GetSelection() == 1;
129  }
130 
131  bool OneFileOnly()
132  {
133  return m_radioBoxFilesCount->GetSelection() == 1;
134  }
135 
137  {
138  return m_excludeTH->GetValue();
139  }
140 };
141 
142 
143 // Static members to remember choices
147 
148 
149 
151 {
152  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
153 
154  auto cfg = m_parent->GetPcbNewSettings();
155 
156  m_units = cfg->m_PlaceFile.units == 0 ? EDA_UNITS::INCHES : EDA_UNITS::MILLIMETRES;
157  m_fileOpt = cfg->m_PlaceFile.file_options;
158  m_fileFormat = cfg->m_PlaceFile.file_format;
159  m_includeBoardEdge = cfg->m_PlaceFile.include_board_edge;
160 
161  // Output directory
163 
164  // Update Options
165  m_radioBoxUnits->SetSelection( cfg->m_PlaceFile.units );
166  m_radioBoxFilesCount->SetSelection( m_fileOpt );
167  m_rbFormat->SetSelection( m_fileFormat );
169 
170 
171  // Update sizes and sizers:
172  m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
173  GetSizer()->SetSizeHints( this );
174 }
175 
177 {
178  // Build the absolute path of current output directory to preselect it in the file browser.
179  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
180  path = Prj().AbsolutePath( path );
181 
182  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
183 
184  if( dirDialog.ShowModal() == wxID_CANCEL )
185  return;
186 
187  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
188 
189  wxMessageDialog dialog( this, _( "Use a relative path?"),
190  _( "Plot Output Directory" ),
191  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
192 
193  if( dialog.ShowModal() == wxID_YES )
194  {
195  wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName() ).GetPath();
196 
197  if( !dirName.MakeRelativeTo( boardFilePath ) )
198  wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
199  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
200  }
201 
202  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
203 }
204 
205 void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
206 {
207  m_fileOpt = m_radioBoxFilesCount->GetSelection();
208  m_fileFormat = m_rbFormat->GetSelection();
210 
211  auto cfg = m_parent->GetPcbNewSettings();
213 
214  cfg->m_PlaceFile.units = m_units == EDA_UNITS::INCHES ? 0 : 1;
215  cfg->m_PlaceFile.file_options = m_fileOpt;
216  cfg->m_PlaceFile.file_format = m_fileFormat;
217  cfg->m_PlaceFile.include_board_edge = m_includeBoardEdge;
218 
219  // Set output directory and replace backslashes with forward ones
220  // (Keep unix convention in cfg files)
221  wxString dirStr;
222  dirStr = m_outputDirectoryName->GetValue();
223  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
224 
225  m_plotOpts.SetOutputDirectory( dirStr );
227 
228  if( m_fileFormat == 2 )
230  else
232 }
233 
235 {
236  BOARD* brd = m_parent->GetBoard();
237  wxFileName fn;
238  wxString msg;
239  int fullcount = 0;
240 
241  // Create output directory if it does not exist. Also transform it in absolute path.
242  // Bail if it fails
244  wxFileName outputDir = wxFileName::DirName( path );
245  wxString boardFilename = m_parent->GetBoard()->GetFileName();
246 
248 
249  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
250  {
251  msg.Printf( _( "Could not write plot files to folder \"%s\"." ),
252  outputDir.GetPath() );
253  DisplayError( this, msg );
254  return false;
255  }
256 
257  fn = m_parent->GetBoard()->GetFileName();
258  fn.SetPath( outputDir.GetPath() );
259 
260  // Create the the Front and Top side placement files. Gerber P&P files are always separated.
261  // Not also they include all footprints
262  PLACEFILE_GERBER_WRITER exporter( brd );
263  wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
264 
265  int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_includeBoardEdge );
266 
267  if( fpcount < 0 )
268  {
269  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
270  wxMessageBox( msg );
272  return false;
273  }
274 
275  msg.Printf( _( "Front side (top side) place file: \"%s\"." ), filename );
277 
278  msg.Printf( _( "Component count: %d." ), fpcount );
280 
281  // Create the Back or Bottom side placement file
282  fullcount = fpcount;
283 
284  filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
285 
286  fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_includeBoardEdge );
287 
288  if( fpcount < 0 )
289  {
290  msg.Printf( _( "Unable to create file \"%s\"." ), filename );
292  wxMessageBox( msg );
293  return false;
294  }
295 
296  // Display results
297  msg.Printf( _( "Back side (bottom side) place file: \"%s\"." ), filename );
299 
300  msg.Printf( _( "Component count: %d." ), fpcount );
301 
303 
304  fullcount += fpcount;
305  msg.Printf( _( "Full component count: %d\n" ), fullcount );
307 
308  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
309 
310  return true;
311 }
312 
313 
315 {
316  BOARD * brd = m_parent->GetBoard();
317  wxFileName fn;
318  wxString msg;
319  bool singleFile = OneFileOnly();
320  bool useCSVfmt = m_fileFormat == 1;
321  int fullcount = 0;
322  int top_side = true;
323  int bottom_side = true;
324 
325  // Test for any footprint candidate in list.
326  {
327  PLACE_FILE_EXPORTER exporter( brd, UnitsMM(), ExcludeAllTH(), top_side, bottom_side,
328  useCSVfmt );
329  exporter.GenPositionData();
330 
331  if( exporter.GetFootprintCount() == 0 )
332  {
333  wxMessageBox( _( "No footprint for automated placement." ) );
334  return false;
335  }
336  }
337 
338  // Create output directory if it does not exist.
339  // Also transform it in absolute path.
340  // Bail if it fails
341  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
342  wxString boardFilename = m_parent->GetBoard()->GetFileName();
343 
345 
346  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
347  {
348  msg.Printf( _( "Could not write plot files to folder \"%s\"." ), outputDir.GetPath() );
349  DisplayError( this, msg );
350  return false;
351  }
352 
353  fn = m_parent->GetBoard()->GetFileName();
354  fn.SetPath( outputDir.GetPath() );
355 
356  // Create the the Front or Top side placement file, or a single file
357  top_side = true;
358  bottom_side = false;
359 
360  if( singleFile )
361  {
362  bottom_side = true;
363  fn.SetName( fn.GetName() + wxT( "-" ) + wxT("all") );
364  }
365  else
366  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
367 
368 
369  if( useCSVfmt )
370  {
371  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
372  fn.SetExt( wxT( "csv" ) );
373  }
374  else
375  fn.SetExt( FootprintPlaceFileExtension );
376 
377  int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(),
378  ExcludeAllTH(), top_side, bottom_side,
379  useCSVfmt );
380  if( fpcount < 0 )
381  {
382  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
383  wxMessageBox( msg );
385  return false;
386  }
387 
388  if( singleFile )
389  msg.Printf( _( "Place file: \"%s\"." ), fn.GetFullPath() );
390  else
391  msg.Printf( _( "Front side (top side) place file: \"%s\"." ),
392  fn.GetFullPath() );
394 
395  msg.Printf( _( "Component count: %d." ), fpcount );
397 
398  if( singleFile )
399  {
400  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
401  return true;
402  }
403 
404  // Create the Back or Bottom side placement file
405  fullcount = fpcount;
406  top_side = false;
407  bottom_side = true;
408  fn = brd->GetFileName();
409  fn.SetPath( outputDir.GetPath() );
410  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
411 
412  if( useCSVfmt )
413  {
414  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
415  fn.SetExt( wxT( "csv" ) );
416  }
417  else
418  fn.SetExt( FootprintPlaceFileExtension );
419 
420  fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), ExcludeAllTH(),
421  top_side, bottom_side, useCSVfmt );
422 
423  if( fpcount < 0 )
424  {
425  msg.Printf( _( "Unable to create file \"%s\"." ), fn.GetFullPath() );
427  wxMessageBox( msg );
428  return false;
429  }
430 
431  // Display results
432  if( !singleFile )
433  {
434  msg.Printf( _( "Back side (bottom side) place file: \"%s\"." ), fn.GetFullPath() );
436 
437  msg.Printf( _( "Component count: %d." ), fpcount );
438 
440  }
441 
442  if( !singleFile )
443  {
444  fullcount += fpcount;
445  msg.Printf( _( "Full component count: %d\n" ), fullcount );
447  }
448 
449  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
450 
451  return true;
452 }
453 
454 
456 {
457  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
458  DIALOG_GEN_FOOTPRINT_POSITION dlg( editFrame );
459 
460  dlg.ShowModal();
461  return 0;
462 }
463 
464 
465 int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
466  bool aForceSmdItems, bool aTopSide,
467  bool aBottomSide, bool aFormatCSV )
468 {
469  FILE * file = NULL;
470 
471  if( !aFullFileName.IsEmpty() )
472  {
473  file = wxFopen( aFullFileName, wxT( "wt" ) );
474 
475  if( file == NULL )
476  return -1;
477  }
478 
479  std::string data;
480  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aForceSmdItems, aTopSide, aBottomSide,
481  aFormatCSV );
482  data = exporter.GenPositionData();
483 
484  // if aFullFileName is empty, the file is not created, only the
485  // count of footprints to place is returned
486  if( file )
487  {
488  // Creates a footprint position file
489  // aSide = 0 -> Back (bottom) side)
490  // aSide = 1 -> Front (top) side)
491  // aSide = 2 -> both sides
492  fputs( data.c_str(), file );
493  fclose( file );
494  }
495 
496  return exporter.GetFootprintCount();
497 }
498 
499 
500 void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
501 {
502  wxFileName fn;
503 
504  wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
505  wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
506 
507  if( dirDialog.ShowModal() == wxID_CANCEL )
508  return;
509 
510  fn = GetBoard()->GetFileName();
511  fn.SetPath( dirDialog.GetPath() );
512  fn.SetExt( wxT( "rpt" ) );
513 
514  bool unitMM = GetUserUnits() == EDA_UNITS::MILLIMETRES;
515  bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
516 
517  wxString msg;
518  if( success )
519  {
520  msg.Printf( _( "Footprint report file created:\n\"%s\"" ), fn.GetFullPath() );
521  wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
522  }
523 
524  else
525  {
526  msg.Printf( _( "Unable to create \"%s\"" ), fn.GetFullPath() );
527  DisplayError( this, msg );
528  }
529 }
530 
531 /* Print a footprint report.
532  */
533 bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
534 {
535  FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
536 
537  if( rptfile == NULL )
538  return false;
539 
540  std::string data;
541  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, false, true, true, false );
542  data = exporter.GenReportData();
543 
544  fputs( data.c_str(), rptfile );
545  fclose( rptfile );
546 
547  return true;
548 }
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
std::string GenPositionData()
build a string filled with the position data
Classes used in place file generation.
int CreatePlaceFile(wxString &aFullFilename, PCB_LAYER_ID aLayer, bool aIncludeBrdEdges)
Creates an pnp gerber file.
This file is part of the common library TODO brief description.
This file is part of the common library.
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void onUpdateUIUnits(wxUpdateUIEvent &event) override
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
Class DIALOG_GEN_FOOTPRINT_POSITION_BASE.
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.
bool CreateAsciiFiles()
Creates files in text or csv format.
void onUpdateUIincludeBoardEdge(wxUpdateUIEvent &event) override
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
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
bool DoGenFootprintsReport(const wxString &aFullFilename, bool aUnitsMM)
Function DoGenFootprintsReport Creates an ascii footprint report file giving some infos on footprints...
void GenFootprintsReport(wxCommandEvent &event)
Function GenFootprintsReport Calls DoGenFootprintsReport to create a footprint reprot file See DoGenF...
DIALOG_GEN_FOOTPRINT_POSITION(PCB_EDIT_FRAME *aParent)
bool CreateGerberFiles()
Creates placement files in gerber format.
#define NULL
void onUpdateUIFileOpt(wxUpdateUIEvent &event) override
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
TOOL_EVENT.
Definition: tool_event.h:171
Definition of file extensions used in Kicad.
virtual void SetPlotSettings(const PCB_PLOT_PARAMS &aSettings)
int GeneratePosFile(const TOOL_EVENT &aEvent)
void SetOutputDirectory(wxString aDir)
The ASCII format of the kicad place file is:
std::string GenReportData()
build a string filled with the pad report data This report does not used options aForceSmdItems,...
PCB_PLOT_PARAMS handles plot parameters and options when plotting/printing a board.
void onUpdateUIExcludeTH(wxUpdateUIEvent &event) override
wxString GetOutputDirectory() const
EDA_UNITS m_units
Definition: dialog_shim.h:190
const wxString GetPlaceFileName(const wxString &aFullBaseFilename, PCB_LAYER_ID aLayer) const
see class PGM_BASE
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
#define _(s)
Definition: 3d_actions.cpp:33
PCB_EDIT_FRAME is the main frame for Pcbnew.
void MsgPanelSetMinSize(const wxSize &aMinSize)
Set the min size of the area which displays html messages:
const std::string FootprintPlaceFileExtension
BOARD * GetBoard() const
int DoGenFootprintsPositionFile(const wxString &aFullFileName, bool aUnitsMM, bool aForceSmdItems, bool aTopSide, bool aBottomSide, bool aFormatCSV=false)
Function DoGenFootprintsPositionFile Creates an ascii footprint position file.
PLACEFILE_GERBER_WRITER is a class mainly used to create Gerber drill files.
The dialog to create footprint position files and choose options (one or 2 files, units and force all...
static std::string GetBackSideName()
static std::string GetFrontSideName()
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void OnGenerate(wxCommandEvent &event) override