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  {
60  m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
62  initDialog();
63 
64  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
65  // that requires us to correct the button labels here.
66  m_sdbSizerOK->SetLabel( _( "Generate Position File" ) );
67  m_sdbSizerCancel->SetLabel( _( "Close" ) );
68  m_sdbSizer->Layout();
69 
70  m_sdbSizerOK->SetDefault();
71 
72  GetSizer()->SetSizeHints(this);
73  Centre();
74  }
75 
76 private:
80 
81  static int m_unitsOpt;
82  static int m_fileOpt;
83  static int m_fileFormat;
84  static bool m_includeBoardEdge;
85 
86  void initDialog();
87  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
88  void OnGenerate( wxCommandEvent& event ) override;
89 
90  void onUpdateUIUnits( wxUpdateUIEvent& event ) override
91  {
92  m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
93  }
94 
95  void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
96  {
97  m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
98  }
99 
100  void onUpdateUIExcludeTH( wxUpdateUIEvent& event ) override
101  {
102  if( m_rbFormat->GetSelection() == 2 )
103  {
104  m_excludeTH->SetValue( false );
105  m_excludeTH->Enable( false );
106  }
107  else
108  {
109  m_excludeTH->Enable( true );
110  }
111  }
112 
113  void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
114  {
115  m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
116  }
117 
120  bool CreateAsciiFiles();
121 
124  bool CreateGerberFiles();
125 
126  // accessors to options:
127  bool UnitsMM()
128  {
129  return m_radioBoxUnits->GetSelection() == 1;
130  }
131 
132  bool OneFileOnly()
133  {
134  return m_radioBoxFilesCount->GetSelection() == 1;
135  }
136 
138  {
139  return m_excludeTH->GetValue();
140  }
141 };
142 
143 
144 // Static members to remember choices
148 
149 
150 
152 {
153  m_browseButton->SetBitmap( KiBitmap( small_folder_xpm ) );
154 
155  auto cfg = m_parent->GetPcbNewSettings();
156 
157  m_units = cfg->m_PlaceFile.units == 0 ? EDA_UNITS::INCHES : EDA_UNITS::MILLIMETRES;
158  m_fileOpt = cfg->m_PlaceFile.file_options;
159  m_fileFormat = cfg->m_PlaceFile.file_format;
160  m_includeBoardEdge = cfg->m_PlaceFile.include_board_edge;
161 
162  // Output directory
164 
165  // Update Options
166  m_radioBoxUnits->SetSelection( cfg->m_PlaceFile.units );
167  m_radioBoxFilesCount->SetSelection( m_fileOpt );
168  m_rbFormat->SetSelection( m_fileFormat );
170 
171 
172  // Update sizes and sizers:
173  m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
174  GetSizer()->SetSizeHints( this );
175 }
176 
178 {
179  // Build the absolute path of current output directory to preselect it in the file browser.
180  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
181  path = Prj().AbsolutePath( path );
182 
183  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
184 
185  if( dirDialog.ShowModal() == wxID_CANCEL )
186  return;
187 
188  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
189 
190  wxMessageDialog dialog( this, _( "Use a relative path?"),
191  _( "Plot Output Directory" ),
192  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
193 
194  if( dialog.ShowModal() == wxID_YES )
195  {
196  wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName() ).GetPath();
197 
198  if( !dirName.MakeRelativeTo( boardFilePath ) )
199  wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
200  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
201  }
202 
203  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
204 }
205 
206 void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
207 {
208  m_fileOpt = m_radioBoxFilesCount->GetSelection();
209  m_fileFormat = m_rbFormat->GetSelection();
211 
212  auto cfg = m_parent->GetPcbNewSettings();
214 
215  cfg->m_PlaceFile.units = m_units == EDA_UNITS::INCHES ? 0 : 1;
216  cfg->m_PlaceFile.file_options = m_fileOpt;
217  cfg->m_PlaceFile.file_format = m_fileFormat;
218  cfg->m_PlaceFile.include_board_edge = m_includeBoardEdge;
219 
220  // Set output directory and replace backslashes with forward ones
221  // (Keep unix convention in cfg files)
222  wxString dirStr;
223  dirStr = m_outputDirectoryName->GetValue();
224  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
225 
226  m_plotOpts.SetOutputDirectory( dirStr );
228 
229  if( m_fileFormat == 2 )
231  else
233 }
234 
236 {
237  BOARD* brd = m_parent->GetBoard();
238  wxFileName fn;
239  wxString msg;
240  int fullcount = 0;
241 
242  // Create output directory if it does not exist. Also transform it in absolute path.
243  // Bail if it fails
245  wxFileName outputDir = wxFileName::DirName( path );
246  wxString boardFilename = m_parent->GetBoard()->GetFileName();
247 
249 
250  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
251  {
252  msg.Printf( _( "Could not write plot files to folder \"%s\"." ),
253  outputDir.GetPath() );
254  DisplayError( this, msg );
255  return false;
256  }
257 
258  fn = m_parent->GetBoard()->GetFileName();
259  fn.SetPath( outputDir.GetPath() );
260 
261  // Create the the Front and Top side placement files. Gerber P&P files are always separated.
262  // Not also they include all footprints
263  PLACEFILE_GERBER_WRITER exporter( brd );
264  wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
265 
266  int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_includeBoardEdge );
267 
268  if( fpcount < 0 )
269  {
270  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
271  wxMessageBox( msg );
273  return false;
274  }
275 
276  msg.Printf( _( "Front (top side) placement file: \"%s\"." ), filename );
278 
279  msg.Printf( _( "Component count: %d." ), fpcount );
281 
282  // Create the Back or Bottom side placement file
283  fullcount = fpcount;
284 
285  filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
286 
287  fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_includeBoardEdge );
288 
289  if( fpcount < 0 )
290  {
291  msg.Printf( _( "Unable to create file \"%s\"." ), filename );
293  wxMessageBox( msg );
294  return false;
295  }
296 
297  // Display results
298  msg.Printf( _( "Back (bottom side) placement file: \"%s\"." ), filename );
300 
301  msg.Printf( _( "Component count: %d." ), fpcount );
302 
304 
305  fullcount += fpcount;
306  msg.Printf( _( "Full component count: %d\n" ), fullcount );
308 
309  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_ACTION );
310 
311  return true;
312 }
313 
314 
316 {
317  BOARD * brd = m_parent->GetBoard();
318  wxFileName fn;
319  wxString msg;
320  bool singleFile = OneFileOnly();
321  bool useCSVfmt = m_fileFormat == 1;
322  int fullcount = 0;
323  int top_side = true;
324  int bottom_side = true;
325 
326  // Test for any footprint candidate in list.
327  {
328  PLACE_FILE_EXPORTER exporter( brd, UnitsMM(), ExcludeAllTH(), top_side, bottom_side,
329  useCSVfmt );
330  exporter.GenPositionData();
331 
332  if( exporter.GetFootprintCount() == 0 )
333  {
334  wxMessageBox( _( "No footprint for automated placement." ) );
335  return false;
336  }
337  }
338 
339  // Create output directory if it does not exist.
340  // Also transform it in absolute path.
341  // Bail if it fails
342  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
343  wxString boardFilename = m_parent->GetBoard()->GetFileName();
344 
346 
347  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
348  {
349  msg.Printf( _( "Could not write plot files to folder \"%s\"." ), outputDir.GetPath() );
350  DisplayError( this, msg );
351  return false;
352  }
353 
354  fn = m_parent->GetBoard()->GetFileName();
355  fn.SetPath( outputDir.GetPath() );
356 
357  // Create the the Front or Top side placement file, or a single file
358  top_side = true;
359  bottom_side = false;
360 
361  if( singleFile )
362  {
363  bottom_side = true;
364  fn.SetName( fn.GetName() + wxT( "-" ) + wxT("all") );
365  }
366  else
367  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
368 
369 
370  if( useCSVfmt )
371  {
372  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
373  fn.SetExt( wxT( "csv" ) );
374  }
375  else
376  fn.SetExt( FootprintPlaceFileExtension );
377 
378  int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(),
379  ExcludeAllTH(), top_side, bottom_side,
380  useCSVfmt );
381  if( fpcount < 0 )
382  {
383  msg.Printf( _( "Unable to create \"%s\"." ), fn.GetFullPath() );
384  wxMessageBox( msg );
386  return false;
387  }
388 
389  if( singleFile )
390  msg.Printf( _( "Placement file: \"%s\"." ), fn.GetFullPath() );
391  else
392  msg.Printf( _( "Front (top side) placement file: \"%s\"." ),
393  fn.GetFullPath() );
395 
396  msg.Printf( _( "Component count: %d." ), fpcount );
398 
399  if( singleFile )
400  {
401  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_ACTION );
402  return true;
403  }
404 
405  // Create the Back or Bottom side placement file
406  fullcount = fpcount;
407  top_side = false;
408  bottom_side = true;
409  fn = brd->GetFileName();
410  fn.SetPath( outputDir.GetPath() );
411  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
412 
413  if( useCSVfmt )
414  {
415  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
416  fn.SetExt( wxT( "csv" ) );
417  }
418  else
419  fn.SetExt( FootprintPlaceFileExtension );
420 
421  fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), ExcludeAllTH(),
422  top_side, bottom_side, useCSVfmt );
423 
424  if( fpcount < 0 )
425  {
426  msg.Printf( _( "Unable to create file \"%s\"." ), fn.GetFullPath() );
428  wxMessageBox( msg );
429  return false;
430  }
431 
432  // Display results
433  if( !singleFile )
434  {
435  msg.Printf( _( "Back (bottom side) placement file: \"%s\"." ), fn.GetFullPath() );
437 
438  msg.Printf( _( "Component count: %d." ), fpcount );
439 
441  }
442 
443  if( !singleFile )
444  {
445  fullcount += fpcount;
446  msg.Printf( _( "Full component count: %d\n" ), fullcount );
448  }
449 
450  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_ACTION );
451 
452  return true;
453 }
454 
455 
457 {
458  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
459  DIALOG_GEN_FOOTPRINT_POSITION dlg( editFrame );
460 
461  dlg.ShowModal();
462  return 0;
463 }
464 
465 
466 int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
467  bool aForceSmdItems, bool aTopSide,
468  bool aBottomSide, bool aFormatCSV )
469 {
470  FILE * file = NULL;
471 
472  if( !aFullFileName.IsEmpty() )
473  {
474  file = wxFopen( aFullFileName, wxT( "wt" ) );
475 
476  if( file == NULL )
477  return -1;
478  }
479 
480  std::string data;
481  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aForceSmdItems, aTopSide, aBottomSide,
482  aFormatCSV );
483  data = exporter.GenPositionData();
484 
485  // if aFullFileName is empty, the file is not created, only the
486  // count of footprints to place is returned
487  if( file )
488  {
489  // Creates a footprint position file
490  // aSide = 0 -> Back (bottom) side)
491  // aSide = 1 -> Front (top) side)
492  // aSide = 2 -> both sides
493  fputs( data.c_str(), file );
494  fclose( file );
495  }
496 
497  return exporter.GetFootprintCount();
498 }
499 
500 
501 void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
502 {
503  wxFileName fn;
504 
505  wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
506  wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
507 
508  if( dirDialog.ShowModal() == wxID_CANCEL )
509  return;
510 
511  fn = GetBoard()->GetFileName();
512  fn.SetPath( dirDialog.GetPath() );
513  fn.SetExt( wxT( "rpt" ) );
514 
515  bool unitMM = GetUserUnits() == EDA_UNITS::MILLIMETRES;
516  bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
517 
518  wxString msg;
519  if( success )
520  {
521  msg.Printf( _( "Footprint report file created:\n\"%s\"" ), fn.GetFullPath() );
522  wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
523  }
524 
525  else
526  {
527  msg.Printf( _( "Unable to create \"%s\"" ), fn.GetFullPath() );
528  DisplayError( this, msg );
529  }
530 }
531 
532 /* Print a footprint report.
533  */
534 bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
535 {
536  FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
537 
538  if( rptfile == NULL )
539  return false;
540 
541  std::string data;
542  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, false, true, true, false );
543  data = exporter.GenReportData();
544 
545  fputs( data.c_str(), rptfile );
546  fclose( rptfile );
547 
548  return true;
549 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
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.
const BITMAP_OPAQUE small_folder_xpm[1]
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void onUpdateUIUnits(wxUpdateUIEvent &event) override
int GeneratePosFile(const TOOL_EVENT &aEvent)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:269
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:298
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
bool CreateAsciiFiles()
Creates files in text or csv format.
void onUpdateUIincludeBoardEdge(wxUpdateUIEvent &event) override
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
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)
Create an ASCII footprint report file giving some infos on footprints and board outlines.
void GenFootprintsReport(wxCommandEvent &event)
Call DoGenFootprintsReport to create a footprint report file.
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
Return 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:297
Generic, UI-independent tool event.
Definition: tool_event.h:173
Definition of file extensions used in Kicad.
virtual void SetPlotSettings(const PCB_PLOT_PARAMS &aSettings)
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:199
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:190
#define _(s)
Definition: 3d_actions.cpp:33
The main frame for Pcbnew.
PCBNEW_SETTINGS * GetPcbNewSettings() const
void MsgPanelSetMinSize(const wxSize &aMinSize)
returns the reporter object that reports to this panel
const std::string FootprintPlaceFileExtension
BOARD * GetBoard() const
int DoGenFootprintsPositionFile(const wxString &aFullFileName, bool aUnitsMM, bool aForceSmdItems, bool aTopSide, bool aBottomSide, bool aFormatCSV=false)
Create an ASCII footprint position file.
void SetFileName(const wxString &aReportFileName)
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