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