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-2021 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 <string_utils.h>
31 #include <gestfich.h>
32 #include <pcb_edit_frame.h>
33 #include <pcbnew_settings.h>
34 #include <bitmaps.h>
35 #include <reporter.h>
37 #include <board.h>
38 #include <footprint.h>
40 #include <kiface_base.h>
41 #include <wx_html_report_panel.h>
45 
46 #include <wx/dirdlg.h>
47 
48 
54 {
55 public:
58  m_parent( aParent ),
59  m_plotOpts( aParent->GetPlotSettings() )
60  {
61  m_messagesPanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
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:
78  void initDialog();
79  void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
80  void OnGenerate( wxCommandEvent& event ) override;
81 
82  void onUpdateUIUnits( wxUpdateUIEvent& event ) override
83  {
84  m_radioBoxUnits->Enable( m_rbFormat->GetSelection() != 2 );
85  }
86 
87  void onUpdateUIFileOpt( wxUpdateUIEvent& event ) override
88  {
89  m_radioBoxFilesCount->Enable( m_rbFormat->GetSelection() != 2 );
90  }
91 
92  void onUpdateUIOnlySMD( wxUpdateUIEvent& event ) override
93  {
94  if( m_rbFormat->GetSelection() == 2 )
95  {
96  m_onlySMD->SetValue( false );
97  m_onlySMD->Enable( false );
98  }
99  else
100  {
101  m_onlySMD->Enable( true );
102  }
103  }
104 
105  void onUpdateUIExcludeTH( wxUpdateUIEvent& event ) override
106  {
107  if( m_rbFormat->GetSelection() == 2 )
108  {
109  m_excludeTH->SetValue( false );
110  m_excludeTH->Enable( false );
111  }
112  else
113  {
114  m_excludeTH->Enable( true );
115  }
116  }
117 
118  void onUpdateUIincludeBoardEdge( wxUpdateUIEvent& event ) override
119  {
120  m_cbIncludeBoardEdge->Enable( m_rbFormat->GetSelection() == 2 );
121  }
122 
126  bool CreateAsciiFiles();
127 
131  bool CreateGerberFiles();
132 
133  // accessors to options:
134  bool UnitsMM()
135  {
136  return m_radioBoxUnits->GetSelection() == 1;
137  }
138 
139  bool OneFileOnly()
140  {
141  return m_radioBoxFilesCount->GetSelection() == 1;
142  }
143 
144  bool OnlySMD()
145  {
146  return m_onlySMD->GetValue();
147  }
148 
150  {
151  return m_excludeTH->GetValue();
152  }
153 
157 
158  static int m_unitsOpt;
159  static int m_fileOpt;
160  static int m_fileFormat;
161  static bool m_includeBoardEdge;
162  static bool m_excludeTHOpt;
163  static bool m_onlySMDOpt;
164 };
165 
166 
167 // Static members to remember choices
173 
174 
176 {
178 
180 
185 
186  // Output directory
188 
189  // Update Options
190  m_radioBoxUnits->SetSelection( cfg->m_PlaceFile.units );
191  m_radioBoxFilesCount->SetSelection( m_fileOpt );
192  m_rbFormat->SetSelection( m_fileFormat );
195  m_onlySMD->SetValue( m_onlySMDOpt );
196  m_excludeTH->SetValue( m_excludeTHOpt );
197 
198  // Update sizes and sizers:
199  m_messagesPanel->MsgPanelSetMinSize( wxSize( -1, 160 ) );
200  GetSizer()->SetSizeHints( this );
201 }
202 
204 {
205  // Build the absolute path of current output directory to preselect it in the file browser.
206  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
207  path = Prj().AbsolutePath( path );
208 
209  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
210 
211  if( dirDialog.ShowModal() == wxID_CANCEL )
212  return;
213 
214  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
215 
216  wxMessageDialog dialog( this, _( "Use a relative path?"),
217  _( "Plot Output Directory" ),
218  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
219 
220  if( dialog.ShowModal() == wxID_YES )
221  {
222  wxString boardFilePath = ( (wxFileName) m_parent->GetBoard()->GetFileName() ).GetPath();
223 
224  if( !dirName.MakeRelativeTo( boardFilePath ) )
225  wxMessageBox( _( "Cannot make path relative (target volume different from board "
226  "file volume)!" ),
227  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
228  }
229 
230  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
231 }
232 
233 
234 void DIALOG_GEN_FOOTPRINT_POSITION::OnGenerate( wxCommandEvent& event )
235 {
236  m_fileOpt = m_radioBoxFilesCount->GetSelection();
237  m_fileFormat = m_rbFormat->GetSelection();
239  m_onlySMDOpt = m_onlySMD->GetValue();
240  m_excludeTHOpt = m_excludeTH->GetValue();
241 
242  auto cfg = m_parent->GetPcbNewSettings();
244 
245  cfg->m_PlaceFile.units = m_units == EDA_UNITS::INCHES ? 0 : 1;
246  cfg->m_PlaceFile.file_options = m_fileOpt;
247  cfg->m_PlaceFile.file_format = m_fileFormat;
248  cfg->m_PlaceFile.include_board_edge = m_includeBoardEdge;
249  cfg->m_PlaceFile.use_aux_origin = m_useDrillPlaceOrigin->GetValue();
250 
251  // Set output directory and replace backslashes with forward ones
252  // (Keep unix convention in cfg files)
253  wxString dirStr;
254  dirStr = m_outputDirectoryName->GetValue();
255  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
256 
257  m_plotOpts.SetOutputDirectory( dirStr );
259 
260  if( m_fileFormat == 2 )
262  else
264 }
265 
266 
268 {
269  BOARD* brd = m_parent->GetBoard();
270  wxFileName fn;
271  wxString msg;
272  int fullcount = 0;
273 
274  // Create output directory if it does not exist (also transform it in absolute form).
275  // Bail if it fails.
276 
277  std::function<bool( wxString* )> textResolver =
278  [&]( wxString* token ) -> bool
279  {
280  // Handles board->GetTitleBlock() *and* board->GetProject()
281  return m_parent->GetBoard()->ResolveTextVar( token, 0 );
282  };
283 
284  wxString path = m_plotOpts.GetOutputDirectory();
285  path = ExpandTextVars( path, &textResolver, nullptr, nullptr );
286  path = ExpandEnvVarSubstitutions( path, nullptr );
287 
288  wxFileName outputDir = wxFileName::DirName( path );
289  wxString boardFilename = m_parent->GetBoard()->GetFileName();
290 
292 
293  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
294  {
295  msg.Printf( _( "Could not write plot files to folder '%s'." ),
296  outputDir.GetPath() );
297  DisplayError( this, msg );
298  return false;
299  }
300 
301  fn = m_parent->GetBoard()->GetFileName();
302  fn.SetPath( outputDir.GetPath() );
303 
304  // Create the Front and Top side placement files. Gerber P&P files are always separated.
305  // Not also they include all footprints
306  PLACEFILE_GERBER_WRITER exporter( brd );
307  wxString filename = exporter.GetPlaceFileName( fn.GetFullPath(), F_Cu );
308 
309  int fpcount = exporter.CreatePlaceFile( filename, F_Cu, m_includeBoardEdge );
310 
311  if( fpcount < 0 )
312  {
313  msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
314  wxMessageBox( msg );
316  return false;
317  }
318 
319  msg.Printf( _( "Front (top side) placement file: '%s'." ), filename );
321 
322  msg.Printf( _( "Component count: %d." ), fpcount );
324 
325  // Create the Back or Bottom side placement file
326  fullcount = fpcount;
327 
328  filename = exporter.GetPlaceFileName( fn.GetFullPath(), B_Cu );
329 
330  fpcount = exporter.CreatePlaceFile( filename, B_Cu, m_includeBoardEdge );
331 
332  if( fpcount < 0 )
333  {
334  msg.Printf( _( "Failed to create file '%s'." ), filename );
336  wxMessageBox( msg );
337  return false;
338  }
339 
340  // Display results
341  msg.Printf( _( "Back (bottom side) placement file: '%s'." ), filename );
343 
344  msg.Printf( _( "Component count: %d." ), fpcount );
346 
347  fullcount += fpcount;
348  msg.Printf( _( "Full component count: %d." ), fullcount );
350 
351  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
352 
353  return true;
354 }
355 
356 
358 {
359  BOARD * brd = m_parent->GetBoard();
360  wxFileName fn;
361  wxString msg;
362  bool singleFile = OneFileOnly();
363  bool useCSVfmt = m_fileFormat == 1;
364  bool useAuxOrigin = m_useDrillPlaceOrigin->GetValue();
365  int fullcount = 0;
366  int topSide = true;
367  int bottomSide = true;
368 
369  // Test for any footprint candidate in list.
370  {
371  PLACE_FILE_EXPORTER exporter( brd, UnitsMM(), OnlySMD(), ExcludeAllTH(), topSide,
372  bottomSide, useCSVfmt, useAuxOrigin );
373  exporter.GenPositionData();
374 
375  if( exporter.GetFootprintCount() == 0 )
376  {
377  wxMessageBox( _( "No footprint for automated placement." ) );
378  return false;
379  }
380  }
381 
382  // Create output directory if it does not exist (also transform it in absolute form).
383  // Bail if it fails.
384 
385  std::function<bool( wxString* )> textResolver =
386  [&]( wxString* token ) -> bool
387  {
388  // Handles board->GetTitleBlock() *and* board->GetProject()
389  return m_parent->GetBoard()->ResolveTextVar( token, 0 );
390  };
391 
392  wxString path = m_plotOpts.GetOutputDirectory();
393  path = ExpandTextVars( path, &textResolver, nullptr, nullptr );
394  path = ExpandEnvVarSubstitutions( path, nullptr );
395 
396  wxFileName outputDir = wxFileName::DirName( path );
397  wxString boardFilename = m_parent->GetBoard()->GetFileName();
398 
400 
401  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, m_reporter ) )
402  {
403  msg.Printf( _( "Could not write plot files to folder '%s'." ), outputDir.GetPath() );
404  DisplayError( this, msg );
405  return false;
406  }
407 
408  fn = m_parent->GetBoard()->GetFileName();
409  fn.SetPath( outputDir.GetPath() );
410 
411  // Create the Front or Top side placement file, or a single file
412  topSide = true;
413  bottomSide = false;
414 
415  if( singleFile )
416  {
417  bottomSide = true;
418  fn.SetName( fn.GetName() + wxT( "-" ) + wxT( "all" ) );
419  }
420  else
421  {
422  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetFrontSideName().c_str() );
423  }
424 
425 
426  if( useCSVfmt )
427  {
428  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
429  fn.SetExt( wxT( "csv" ) );
430  }
431  else
432  {
433  fn.SetExt( FootprintPlaceFileExtension );
434  }
435 
436  int fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
437  ExcludeAllTH(), topSide, bottomSide,
438  useCSVfmt, useAuxOrigin );
439  if( fpcount < 0 )
440  {
441  msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
442  wxMessageBox( msg );
444  return false;
445  }
446 
447  if( singleFile )
448  msg.Printf( _( "Placement file: '%s'." ), fn.GetFullPath() );
449  else
450  msg.Printf( _( "Front (top side) placement file: '%s'." ), fn.GetFullPath() );
451 
453 
454  msg.Printf( _( "Component count: %d." ), fpcount );
456 
457  if( singleFile )
458  {
459  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
460  return true;
461  }
462 
463  // Create the Back or Bottom side placement file
464  fullcount = fpcount;
465  topSide = false;
466  bottomSide = true;
467  fn = brd->GetFileName();
468  fn.SetPath( outputDir.GetPath() );
469  fn.SetName( fn.GetName() + wxT( "-" ) + PLACE_FILE_EXPORTER::GetBackSideName().c_str() );
470 
471  if( useCSVfmt )
472  {
473  fn.SetName( fn.GetName() + wxT( "-" ) + FootprintPlaceFileExtension );
474  fn.SetExt( wxT( "csv" ) );
475  }
476  else
477  {
478  fn.SetExt( FootprintPlaceFileExtension );
479  }
480 
481  fpcount = m_parent->DoGenFootprintsPositionFile( fn.GetFullPath(), UnitsMM(), OnlySMD(),
482  ExcludeAllTH(), topSide, bottomSide, useCSVfmt,
483  useAuxOrigin );
484 
485  if( fpcount < 0 )
486  {
487  msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
489  wxMessageBox( msg );
490  return false;
491  }
492 
493  // Display results
494  if( !singleFile )
495  {
496  msg.Printf( _( "Back (bottom side) placement file: '%s'." ), fn.GetFullPath() );
498 
499  msg.Printf( _( "Component count: %d." ), fpcount );
501  }
502 
503  if( !singleFile )
504  {
505  fullcount += fpcount;
506  msg.Printf( _( "Full component count: %d." ), fullcount );
508  }
509 
510  m_reporter->Report( _( "File generation successful." ), RPT_SEVERITY_INFO );
511 
512  return true;
513 }
514 
515 
517 {
518  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
519  DIALOG_GEN_FOOTPRINT_POSITION dlg( editFrame );
520 
521  dlg.ShowModal();
522  return 0;
523 }
524 
525 
526 int PCB_EDIT_FRAME::DoGenFootprintsPositionFile( const wxString& aFullFileName, bool aUnitsMM,
527  bool aOnlySMD, bool aNoTHItems, bool aTopSide,
528  bool aBottomSide, bool aFormatCSV,
529  bool aUseAuxOrigin )
530 {
531  FILE * file = nullptr;
532 
533  if( !aFullFileName.IsEmpty() )
534  {
535  file = wxFopen( aFullFileName, wxT( "wt" ) );
536 
537  if( file == nullptr )
538  return -1;
539  }
540 
541  std::string data;
542  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, aOnlySMD, aNoTHItems, aTopSide, aBottomSide,
543  aFormatCSV, aUseAuxOrigin );
544  data = exporter.GenPositionData();
545 
546  // if aFullFileName is empty, the file is not created, only the
547  // count of footprints to place is returned
548  if( file )
549  {
550  // Creates a footprint position file
551  // aSide = 0 -> Back (bottom) side)
552  // aSide = 1 -> Front (top) side)
553  // aSide = 2 -> both sides
554  fputs( data.c_str(), file );
555  fclose( file );
556  }
557 
558  return exporter.GetFootprintCount();
559 }
560 
561 
562 void PCB_EDIT_FRAME::GenFootprintsReport( wxCommandEvent& event )
563 {
564  wxFileName fn;
565 
566  wxString boardFilePath = ( (wxFileName) GetBoard()->GetFileName() ).GetPath();
567  wxDirDialog dirDialog( this, _( "Select Output Directory" ), boardFilePath );
568 
569  if( dirDialog.ShowModal() == wxID_CANCEL )
570  return;
571 
572  fn = GetBoard()->GetFileName();
573  fn.SetPath( dirDialog.GetPath() );
574  fn.SetExt( wxT( "rpt" ) );
575 
576  bool unitMM = GetUserUnits() == EDA_UNITS::MILLIMETRES;
577  bool success = DoGenFootprintsReport( fn.GetFullPath(), unitMM );
578 
579  wxString msg;
580 
581  if( success )
582  {
583  msg.Printf( _( "Footprint report file created:\n'%s'." ), fn.GetFullPath() );
584  wxMessageBox( msg, _( "Footprint Report" ), wxICON_INFORMATION );
585  }
586 
587  else
588  {
589  msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
590  DisplayError( this, msg );
591  }
592 }
593 
594 
595 bool PCB_EDIT_FRAME::DoGenFootprintsReport( const wxString& aFullFilename, bool aUnitsMM )
596 {
597  FILE* rptfile = wxFopen( aFullFilename, wxT( "wt" ) );
598 
599  if( rptfile == nullptr )
600  return false;
601 
602  std::string data;
603  PLACE_FILE_EXPORTER exporter( GetBoard(), aUnitsMM, false, false, true, true, false, true );
604  data = exporter.GenReportData();
605 
606  fputs( data.c_str(), rptfile );
607  fclose( rptfile );
608 
609  return true;
610 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
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)
Create an pnp gerber file.
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:57
This file is part of the common library.
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void onUpdateUIUnits(wxUpdateUIEvent &event) override
int DoGenFootprintsPositionFile(const wxString &aFullFileName, bool aUnitsMM, bool aOnlySMD, bool aNoTHItems, bool aTopSide, bool aBottomSide, bool aFormatCSV, bool aUseAuxOrigin)
Create an ASCII footprint position file.
void SetOutputDirectory(const wxString &aDir)
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:267
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
Class DIALOG_GEN_FOOTPRINT_POSITION_BASE.
const wxString & GetFileName() const
Definition: board.h:229
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.
Definition: project.cpp:269
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.
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:295
Generic, UI-independent tool event.
Definition: tool_event.h:152
Definition of file extensions used in Kicad.
#define _(s)
virtual void SetPlotSettings(const PCB_PLOT_PARAMS &aSettings)
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,...
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
Parameters and options when plotting/printing a board.
void onUpdateUIExcludeTH(wxUpdateUIEvent &event) override
wxString GetOutputDirectory() const
EDA_UNITS m_units
Definition: dialog_shim.h:200
DIALOG_PLACE_FILE m_PlaceFile
const wxString GetPlaceFileName(const wxString &aFullBaseFilename, PCB_LAYER_ID aLayer) const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
Definition: layer_ids.h:71
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
void SetFileName(const wxString &aReportFileName)
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:240
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()
void onUpdateUIOnlySMD(wxUpdateUIEvent &event) override
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void OnGenerate(wxCommandEvent &event) override