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