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 module report (pos and module descr) (ascii file)
27  */
28 
29 #include <fctsys.h>
30 #include <confirm.h>
31 #include <kicad_string.h>
32 #include <gestfich.h>
33 #include <pcb_edit_frame.h>
34 #include <pcbnew_settings.h>
35 #include <pgm_base.h>
36 #include <bitmaps.h>
37 #include <macros.h>
38 #include <reporter.h>
40 #include <class_board.h>
41 #include <class_module.h>
43 #include <kiface_i.h>
44 #include <wx_html_report_panel.h>
48 
49 
55 {
56 public:
59  m_parent( aParent ),
60  m_plotOpts( aParent->GetPlotSettings() )
61  {
63  initDialog();
64 
65  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
66  // that requires us to correct the button labels here.
67  m_sdbSizerOK->SetLabel( _( "Generate Position File" ) );
68  m_sdbSizerCancel->SetLabel( _( "Close" ) );
69  m_sdbSizer->Layout();
70 
71  m_sdbSizerOK->SetDefault();
72 
73  GetSizer()->SetSizeHints(this);
74  Centre();
75  }
76 
77 private:
81 
82  static int m_unitsOpt;
83  static int m_fileOpt;
84  static int m_fileFormat;
85  static bool m_includeBoardEdge;
86 
87  void initDialog();
88  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
89  void OnGenerate( wxCommandEvent& event ) override;
90 
91  void onUpdateUIUnits( wxUpdateUIEvent& event ) override
92  {
93  m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
94  }
95 
96  void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
97  {
98  m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
99  }
100 
101  void onUpdateUIforceSMDOpt( wxUpdateUIEvent& event ) override
102  {
103  m_forceSMDOpt->Enable( m_rbFormat->GetSelection() != 2 );
104  }
105 
106  void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
107  {
108  m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
109  }
110 
113  bool CreateAsciiFiles();
114 
117  bool CreateGerberFiles();
118 
119  // accessors to options:
120  bool UnitsMM()
121  {
122  return m_radioBoxUnits->GetSelection() == 1;
123  }
124 
125  bool OneFileOnly()
126  {
127  return m_radioBoxFilesCount->GetSelection() == 1;
128  }
129 
130  bool ForceAllSmd()
131  {
132  return m_forceSMDOpt->GetValue();
133  }
134 };
135 
136 
137 // Static members to remember choices
142 
143 
144 
146 {
147  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
148 
149  auto cfg = m_parent->GetPcbNewSettings();
150 
151  m_units = static_cast<EDA_UNITS>( cfg->m_PlaceFile.units );
152  m_fileOpt = cfg->m_PlaceFile.file_options;
153  m_fileFormat = cfg->m_PlaceFile.file_format;
154  m_includeBoardEdge = cfg->m_PlaceFile.include_board_edge;
155 
156  // Output directory
158 
159  // Update Options
160  m_radioBoxUnits->SetSelection( m_unitsOpt );
161  m_radioBoxFilesCount->SetSelection( m_fileOpt );
162  m_rbFormat->SetSelection( m_fileFormat );
164 
165 
166  // Update sizes and sizers:
167  m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
168  GetSizer()->SetSizeHints( this );
169 }
170 
172 {
173  // Build the absolute path of current output directory to preselect it in the file browser.
174  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
175  path = Prj().AbsolutePath( path );
176 
177  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
178 
179  if( dirDialog.ShowModal() == wxID_CANCEL )
180  return;
181 
182  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
183 
184  wxMessageDialog dialog( this, _( "Use a relative path?"),
185  _( "Plot Output Directory" ),
186  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
187 
188  if( dialog.ShowModal() == wxID_YES )
189  {
190  wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName()).GetPath();
191 
192  if( !dirName.MakeRelativeTo( boardFilePath ) )
193  wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
194  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
195  }
196 
197  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
198 }
199 
200 void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
201 {
202  m_unitsOpt = m_radioBoxUnits->GetSelection();
203  m_fileOpt = m_radioBoxFilesCount->GetSelection();
204  m_fileFormat = m_rbFormat->GetSelection();
206 
207  auto cfg = m_parent->GetPcbNewSettings();
208 
209  cfg->m_PlaceFile.units = static_cast<int>( m_units );
210  cfg->m_PlaceFile.file_options = m_fileOpt;
211  cfg->m_PlaceFile.file_format = m_fileFormat;
212  cfg->m_PlaceFile.include_board_edge = m_includeBoardEdge;
213 
214  // Set output directory and replace backslashes with forward ones
215  // (Keep unix convention in cfg files)
216  wxString dirStr;
217  dirStr = m_outputDirectoryName->GetValue();
218  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
219 
220  m_plotOpts.SetOutputDirectory( dirStr );
222 
223  if( m_fileFormat == 2 )
225  else
227 }
228 
230 {
231  BOARD* brd = m_parent->GetBoard();
232  wxFileName fn;
233  wxString msg;
234  int fullcount = 0;
235 
236  // Create output directory if it does not exist. Also transform it in absolute path.
237  // Bail if it fails
239  wxFileName outputDir = wxFileName::DirName( path );
240  wxString boardFilename = m_parent->GetBoard()->GetFileName();
241 
243 
244  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
245  {
246  msg.Printf( _( "Could not write plot files to folder \"%s\"." ),
247  outputDir.GetPath() );
248  DisplayError( this, msg );
249  return false;
250  }
251 
252  fn = m_parent->GetBoard()->GetFileName();
253  fn.SetPath( outputDir.GetPath() );
254 
255  // Create the the Front and Top side placement files. Gerber P&P files are always separated.
256  // Not also they include all footprints
257  PLACEFILE_GERBER_WRITER exporter( brd );
258  wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
259 
260  int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_includeBoardEdge );
261 
262  if( fpcount < 0 )
263  {
264  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
265  wxMessageBox( msg );
267  return false;
268  }
269 
270  msg.Printf( _( "Front side (top side) place file: \"%s\"." ), filename );
272 
273  msg.Printf( _( "Component count: %d." ), fpcount );
275 
276  // Create the Back or Bottom side placement file
277  fullcount = fpcount;
278 
279  filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
280 
281  fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_includeBoardEdge );
282 
283  if( fpcount < 0 )
284  {
285  msg.Printf( _( "Unable to create file \"%s\"." ), filename );
287  wxMessageBox( msg );
288  return false;
289  }
290 
291  // Display results
292  msg.Printf( _( "Back side (bottom side) place file: \"%s\"." ), filename );
294 
295  msg.Printf( _( "Component count: %d." ), fpcount );
296 
298 
299  fullcount += fpcount;
300  msg.Printf( _( "Full component count: %d\n" ), fullcount );
302 
303  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
304 
305  return true;
306 }
307 
308 
310 {
311  BOARD * brd = m_parent->GetBoard();
312  wxFileName fn;
313  wxString msg;
314  bool singleFile = OneFileOnly();
315  bool useCSVfmt = m_fileFormat == 1;
316  int fullcount = 0;
317  int top_side = true;
318  int bottom_side = true;
319 
320  // Test for any footprint candidate in list, and display the list of forced footprints
321  // if ForceAllSmd() is true
322  {
323  PLACE_FILE_EXPORTER exporter( brd, UnitsMM(), ForceAllSmd(), top_side, bottom_side,
324  useCSVfmt );
325  exporter.GenPositionData();
326 
327  if( exporter.GetFootprintCount() == 0)
328  {
329  wxMessageBox( _( "No footprint for automated placement." ) );
330  return false;
331  }
332 
333  if( ForceAllSmd() )
334  {
335  std::vector<MODULE*>& fp_no_smd_list = exporter.GetSmdFootprintsNotLabeledSMD();
336 
337  for( MODULE* item : fp_no_smd_list )
338  {
339  msg.Printf( _( "footprint %s (not set as SMD) forced in list" ),
340  item->GetReference() );
342  }
343  }
344  }
345 
346  // Create output directory if it does not exist.
347  // Also transform it in absolute path.
348  // Bail if it fails
349  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
350  wxString boardFilename = m_parent->GetBoard()->GetFileName();
351 
353 
354  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
355  {
356  msg.Printf( _( "Could not write plot files to folder \"%s\"." ),
357  GetChars( outputDir.GetPath() ) );
358  DisplayError( this, msg );
359  return false;
360  }
361 
362  fn = m_parent->GetBoard()->GetFileName();
363  fn.SetPath( outputDir.GetPath() );
364 
365  // Create the the Front or Top side placement file, or a single file
366  top_side = true;
367  bottom_side = false;
368 
369  if( singleFile )
370  {
371  bottom_side = true;
372  fn.SetName( fn.GetName() + wxT( "-" ) + wxT("all") );
373  }
374  else
375  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
376 
377 
378  if( useCSVfmt )
379  {
380  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
381  fn.SetExt( wxT( "csv" ) );
382  }
383  else
384  fn.SetExt( FootprintPlaceFileExtension );
385 
386  int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(),
387  ForceAllSmd(),
388  top_side, bottom_side, useCSVfmt );
389  if( fpcount < 0 )
390  {
391  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
392  wxMessageBox( msg );
394  return false;
395  }
396 
397  if( singleFile )
398  msg.Printf( _( "Place file: \"%s\"." ), fn.GetFullPath() );
399  else
400  msg.Printf( _( "Front side (top side) place file: \"%s\"." ),
401  fn.GetFullPath() );
403 
404  msg.Printf( _( "Component count: %d." ), fpcount );
406 
407  if( singleFile )
408  {
409  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
410  return true;
411  }
412 
413  // Create the Back or Bottom side placement file
414  fullcount = fpcount;
415  top_side = false;
416  bottom_side = true;
417  fn = brd->GetFileName();
418  fn.SetPath( outputDir.GetPath() );
419  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
420 
421  if( useCSVfmt )
422  {
423  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
424  fn.SetExt( wxT( "csv" ) );
425  }
426  else
427  fn.SetExt( FootprintPlaceFileExtension );
428 
429  fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(),
430  ForceAllSmd(), top_side, bottom_side, useCSVfmt );
431 
432  if( fpcount < 0 )
433  {
434  msg.Printf( _( "Unable to create file \"%s\"." ), fn.GetFullPath() );
436  wxMessageBox( msg );
437  return false;
438  }
439 
440  // Display results
441  if( !singleFile )
442  {
443  msg.Printf( _( "Back side (bottom side) place file: \"%s\"." ), fn.GetFullPath() );
445 
446  msg.Printf( _( "Component count: %d." ), fpcount );
447 
449  }
450 
451  if( !singleFile )
452  {
453  fullcount += fpcount;
454  msg.Printf( _( "Full component count: %d\n" ), fullcount );
456  }
457 
458  m_reporter->Report( _( "Component Placement File generation OK." ), RPT_SEVERITY_ACTION );
459 
460  return true;
461 }
462 
463 
465 {
466  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
467  DIALOG_GEN_FOOTPRINT_POSITION dlg( editFrame );
468 
469  dlg.ShowModal();
470  return 0;
471 }
472 
473 
474 int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
475  bool aForceSmdItems, bool aTopSide,
476  bool aBottomSide, bool aFormatCSV )
477 {
478  FILE * file = NULL;
479 
480  if( !aFullFileName.IsEmpty() )
481  {
482  file = wxFopen( aFullFileName, wxT( "wt" ) );
483 
484  if( file == NULL )
485  return -1;
486  }
487 
488  std::string data;
489  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aForceSmdItems, aTopSide, aBottomSide,
490  aFormatCSV );
491  data = exporter.GenPositionData();
492 
493  // if aFullFileName is empty, the file is not created, only the
494  // count of footprints to place is returned
495  if( file )
496  {
497  // Creates a footprint position file
498  // aSide = 0 -> Back (bottom) side)
499  // aSide = 1 -> Front (top) side)
500  // aSide = 2 -> both sides
501  fputs( data.c_str(), file );
502  fclose( file );
503  }
504 
505  return exporter.GetFootprintCount();
506 }
507 
508 
509 void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
510 {
511  wxFileName fn;
512 
513  wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
514  wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
515 
516  if( dirDialog.ShowModal() == wxID_CANCEL )
517  return;
518 
519  fn = GetBoard()->GetFileName();
520  fn.SetPath( dirDialog.GetPath() );
521  fn.SetExt( wxT( "rpt" ) );
522 
523  bool unitMM = GetUserUnits() != EDA_UNITS::INCHES;
524  bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
525 
526  wxString msg;
527  if( success )
528  {
529  msg.Printf( _( "Footprint report file created:\n\"%s\"" ), fn.GetFullPath() );
530  wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
531  }
532 
533  else
534  {
535  msg.Printf( _( "Unable to create \"%s\"" ), fn.GetFullPath() );
536  DisplayError( this, msg );
537  }
538 }
539 
540 /* Print a module report.
541  */
542 bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
543 {
544  FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
545 
546  if( rptfile == NULL )
547  return false;
548 
549  std::string data;
550  PLACE_FILE_EXPORTER exporter ( GetBoard(), aUnitsMM, false, true, true, false );
551  data = exporter.GenReportData();
552 
553  fputs( data.c_str(), rptfile );
554  fclose( rptfile );
555 
556  return true;
557 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
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:574
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: class_board.h:255
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
This file contains miscellaneous commonly used macros and functions.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
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:602
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:
void onUpdateUIforceSMDOpt(wxUpdateUIEvent &event) override
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.
std::vector< MODULE * > & GetSmdFootprintsNotLabeledSMD()
wxString GetOutputDirectory() const
EDA_UNITS m_units
Definition: dialog_shim.h:197
DIALOG_PLACE_FILE m_PlaceFile
const wxString GetPlaceFileName(const wxString &aFullBaseFilename, PCB_LAYER_ID aLayer) const
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:153
see class PGM_BASE
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#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