KiCad PCB EDA Suite
dialog_gendrill.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) 1992-2019 Jean_Pierre Charras <jp.charras at wanadoo.fr>
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <confirm.h>
26 #include <core/arraydim.h>
27 #include <pcb_edit_frame.h>
28 #include <pcbnew_settings.h>
29 #include <pcbplot.h>
31 #include <gendrill_gerber_writer.h>
32 #include <bitmaps.h>
34 #include <board.h>
35 #include <track.h>
36 #include <footprint.h>
37 #include <paths.h>
38 #include <dialog_gendrill.h>
40 #include <reporter.h>
41 #include <wx/stdpaths.h>
42 
43 
44 // list of allowed precision for EXCELLON files, for integer format:
45 // Due to difference between inches and mm,
46 // there are 2 precision values, one for inches and one for metric
47 // Note: for decimla format, the precision is not used
50 
51 
52 /* This function displays the dialog frame for drill tools
53  */
55 {
56  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
57  DIALOG_GENDRILL dlg( editFrame, editFrame );
58 
59  dlg.ShowModal();
60  return 0;
61 }
62 
63 
64 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
65  DIALOG_GENDRILL_BASE( aParent )
66 {
67  m_pcbEditFrame = aPcbEditFrame;
70 
71  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
72  // that requires us to correct the button labels here.
73  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
74  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
75  m_sdbSizerCancel->SetLabel( _( "Close" ) );
76  m_buttonsSizer->Layout();
77 
78  m_sdbSizerOK->SetDefault();
79  SetReturnCode( 1 );
80  initDialog();
81  GetSizer()->SetSizeHints( this );
82 }
83 
84 
85 // Static members of DIALOG_GENDRILL
86 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
88 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
89 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
90 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
93 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
94 
96 {
97 }
98 
99 
101 {
102  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
103 
104  m_Merge_PTH_NPTH = cfg->m_GenDrill.merge_pth_npth;
105  m_MinimalHeader = cfg->m_GenDrill.minimal_header;
106  m_Mirror = cfg->m_GenDrill.mirror;
107  m_UnitDrillIsInch = cfg->m_GenDrill.unit_drill_is_inch;
108  m_UseRouteModeForOvalHoles = cfg->m_GenDrill.use_route_for_oval_holes;
109  m_drillFileType = cfg->m_GenDrill.drill_file_type;
110  m_mapFileType = cfg->m_GenDrill.map_file_type;
111  m_ZerosFormat = cfg->m_GenDrill.zeros_format;
112 
114 
116 }
117 
118 
120 {
121  m_browseButton->SetBitmap( KiBitmap( small_folder_xpm ) );
122 
123  m_rbExcellon->SetValue( m_drillFileType == 0 );
124  m_rbGerberX2->SetValue( m_drillFileType == 1 );
125  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
126  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
128  m_Check_Minimal->SetValue( m_MinimalHeader );
129 
131  m_Choice_Drill_Offset->SetSelection( 1 );
132 
133  m_Check_Mirror->SetValue( m_Mirror );
135  m_Choice_Drill_Map->SetSelection( m_mapFileType );
136  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
137 
140  m_throughViasCount = 0;
141  m_microViasCount = 0;
143 
144  for( FOOTPRINT* footprint : m_board->Footprints() )
145  {
146  for( PAD* pad : footprint->Pads() )
147  {
148  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
149  {
150  if( pad->GetDrillSize().x != 0 )
151  {
152  if( pad->GetAttribute() == PAD_ATTRIB_NPTH )
154  else
156  }
157  }
158  else
159  {
160  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
161  {
162  if( pad->GetAttribute() == PAD_ATTRIB_NPTH )
164  else
166  }
167  }
168  }
169  }
170 
171  for( TRACK* track : m_board->Tracks() )
172  {
173  const VIA *via = dynamic_cast<const VIA*>( track );
174 
175  if( via )
176  {
177  switch( via->GetViaType() )
178  {
179  case VIATYPE::THROUGH: m_throughViasCount++; break;
180  case VIATYPE::MICROVIA: m_microViasCount++; break;
182  default: break;
183  }
184  }
185  }
186 
187  // Display hole counts:
188  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
189  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
190  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
191  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
192  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
193 
194  // Output directory
196 
197  wxCommandEvent dummy;
199 }
200 
201 
202 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
203 {
204  bool enbl_Excellon = m_rbExcellon->GetValue();
205 
206  m_drillFileType = enbl_Excellon ? 0 : 1;
207 
208  m_Choice_Unit->Enable( enbl_Excellon );
209  m_Choice_Zeros_Format->Enable( enbl_Excellon );
210  m_Check_Mirror->Enable( enbl_Excellon );
211  m_Check_Minimal->Enable( enbl_Excellon );
212  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
213  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
214 
215  if( enbl_Excellon )
217  else
218  {
219  m_staticTextPrecision->Enable( true );
220  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
221  }
222 }
223 
224 
226 {
228 
229  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
230 
232  cfg->m_GenDrill.minimal_header = m_MinimalHeader;
233  cfg->m_GenDrill.mirror = m_Mirror;
234  cfg->m_GenDrill.unit_drill_is_inch = m_UnitDrillIsInch;
235  cfg->m_GenDrill.use_route_for_oval_holes = m_UseRouteModeForOvalHoles;
236  cfg->m_GenDrill.drill_file_type = m_drillFileType;
237  cfg->m_GenDrill.map_file_type = m_mapFileType;
238  cfg->m_GenDrill.zeros_format = m_ZerosFormat;
239 }
240 
241 
242 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
243 {
245 }
246 
247 
248 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
249 {
250  GenDrillAndMapFiles( false, true );
251 }
252 
253 
254 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
255 {
256  GenDrillAndMapFiles( true, false );
257 }
258 
259 
260 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
261 {
263 }
264 
265 
267 {
268  if( m_Choice_Unit->GetSelection()== 1 )
269  {
270  // Units = inches
272  }
273  else
274  {
275  // metric options
277  }
278 
280  m_staticTextPrecision->Enable( false );
281  else
282  m_staticTextPrecision->Enable( true );
283 }
284 
285 
287 {
288  // Build the absolute path of current output directory to preselect it in the file browser.
289  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
290  path = Prj().AbsolutePath( path );
291 
292  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
293 
294  if( dirDialog.ShowModal() == wxID_CANCEL )
295  return;
296 
297  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
298  wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) );
299  wxString defaultPath = fn.GetPathWithSep();
300  wxString msg;
301  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), defaultPath );
302 
303  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
304  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
305 
306  if( dialog.ShowModal() == wxID_YES )
307  {
308  if( !dirName.MakeRelativeTo( defaultPath ) )
309  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
310  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
311  }
312 
313  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
314 }
315 
316 
318 {
319  wxString msg;
320 
321  // Set output directory and replace backslashes with forward ones
322  wxString dirStr;
323  dirStr = m_outputDirectoryName->GetValue();
324  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
325  m_plotOpts.SetOutputDirectory( dirStr );
328 
329  m_mapFileType = m_Choice_Drill_Map->GetSelection();
330 
331  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
332  m_MinimalHeader = m_Check_Minimal->IsChecked();
333  m_Mirror = m_Check_Mirror->IsChecked();
335  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
336  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
337 
338  if( m_Choice_Drill_Offset->GetSelection() == 0 )
339  m_FileDrillOffset = wxPoint( 0, 0 );
340  else
342 
343  if( m_UnitDrillIsInch )
345  else
347 
349 }
350 
351 
352 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
353 {
354  UpdateConfig(); // set params and Save drill options
355 
358 
359  const PLOT_FORMAT filefmt[6] = {
360  // Keep these format ids in the same order than m_Choice_Drill_Map choices
363  };
364 
365  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
366 
367  if( choice >= arrayDim( filefmt ) )
368  choice = 1;
369 
370  // Create output directory if it does not exist (also transform it in
371  // absolute form). Bail if it fails
373  wxFileName outputDir = wxFileName::DirName( path );
374  wxString boardFilename = m_board->GetFileName();
375 
376  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
377  {
378  wxString msg;
379  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
380  outputDir.GetPath() );
381  DisplayError( this, msg );
382  return;
383  }
384 
385  if( m_drillFileType == 0 )
386  {
387  EXCELLON_WRITER excellonWriter( m_board );
392  excellonWriter.SetMapFileFormat( filefmt[choice] );
393 
394  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
395  &reporter );
396  }
397  else
398  {
399  GERBER_WRITER gerberWriter( m_board );
400  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
401  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
402  // the integer part precision is always 4, and units always mm
403  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
404  gerberWriter.SetOptions( m_FileDrillOffset );
405  gerberWriter.SetMapFileFormat( filefmt[choice] );
406 
407  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
408  aGenDrill, aGenMap, &reporter );
409  }
410 }
411 
412 
413 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
414 {
415  UpdateConfig(); // set params and Save drill options
416 
417  wxFileName fn = m_board->GetFileName();
418 
419  fn.SetName( fn.GetName() + wxT( "-drl" ) );
420  fn.SetExt( ReportFileExtension );
421 
422  wxString defaultPath = ExpandEnvVarSubstitutions( m_plotOpts.GetOutputDirectory(), &Prj() );
423  defaultPath = Prj().AbsolutePath( defaultPath );
424 
425  if( defaultPath.IsEmpty() )
426  defaultPath = PATHS::GetDefaultUserProjectsPath();
427 
428  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
429  ReportFileWildcard(), wxFD_SAVE );
430 
431  if( dlg.ShowModal() == wxID_CANCEL )
432  return;
433 
434  bool success;
435 
436  // Info is slightly different between Excellon and Gerber
437  // (file ext, Merge PTH/NPTH option)
438  if( m_drillFileType == 0 )
439  {
440  EXCELLON_WRITER excellonWriter( m_board );
441  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
442  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
443  }
444  else
445  {
446  GERBER_WRITER gerberWriter( m_board );
447  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
448  }
449 
450  wxString msg;
451 
452  if( ! success )
453  {
454  msg.Printf( _( "** Unable to create %s **\n" ), dlg.GetPath() );
455  m_messagesBox->AppendText( msg );
456  }
457  else
458  {
459  msg.Printf( _( "Report file %s created\n" ), dlg.GetPath() );
460  m_messagesBox->AppendText( msg );
461  }
462 }
static int m_UnitDrillIsInch
DIALOG_GENERATE_DRILL m_GenDrill
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:253
wxStaticText * m_PlatedPadsCountInfoMsg
void SetRouteModeForOvalHoles(bool aUseRouteModeForOvalHoles)
Definition: track.h:343
void OnGenDrillFile(wxCommandEvent &event) override
void SetUseAuxOrigin(bool aAux)
This file is part of the common library.
const BITMAP_OPAQUE small_folder_xpm[1]
wxRadioBox * m_Choice_Drill_Offset
void OnSelZerosFmtSelected(wxCommandEvent &event) override
EXCELLON_WRITER is a class mainly used to create Excellon drill files However, this class is also use...
wxCheckBox * m_Check_Merge_PTH_NPTH
void OnSelDrillUnitsSelected(wxCommandEvent &event) override
Class DIALOG_GENDRILL_BASE.
static bool m_Merge_PTH_NPTH
static int m_ZerosFormat
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
static bool m_UseRouteModeForOvalHoles
wxPoint m_FileDrillOffset
void SetMergeOption(bool aMerge)
set the option to make separate drill files for PTH and NPTH
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:591
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
static int m_mapFileType
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:269
GERBER_WRITER is a class mainly used to create Gerber drill files.
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:85
virtual const PCB_PLOT_PARAMS & GetPlotSettings() const
Return the PCB_PLOT_PARAMS for the BOARD owned by this frame.
const wxString & GetFileName() const
Definition: board.h:298
wxStaticText * m_NotPlatedPadsCountInfoMsg
A wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:134
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.
void SetFormat(bool aMetric, ZEROS_FMT aZerosFmt=DECIMAL_FORMAT, int aLeftDigits=0, int aRightDigits=0)
Function SetFormat Initialize internal parameters to match the given format.
Board plot function definition file.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
wxRadioBox * m_radioBoxOvalHoleMode
static bool m_MinimalHeader
wxStaticText * m_MicroViasInfoMsg
int GetGerberPrecision() const
wxRadioButton * m_rbExcellon
void SetMapFileFormat(PLOT_FORMAT aMapFmt)
Initialize the format for the drill map file.
bool GenDrillReportFile(const wxString &aFullFileName)
Function GenDrillReportFile Create a plain text report file giving a list of drill values and drill c...
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
wxStaticText * m_ThroughViasInfoMsg
PLOT_FORMAT
The set of supported output plot formats.
Definition: plotter.h:67
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
wxTextCtrl * m_outputDirectoryName
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
FOOTPRINTS & Footprints()
Definition: board.h:303
Generic, UI-independent tool event.
Definition: tool_event.h:173
wxRadioBox * m_Choice_Zeros_Format
Definition of file extensions used in Kicad.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: board.h:610
void SetOutputDirectory(wxString aDir)
static DRILL_PRECISION precisionListForMetric(3, 3)
virtual void ClearMsgPanel()
Clear all messages from the message panel.
DIALOG_GENDRILL(PCB_EDIT_FRAME *aPcbEditFrame, wxWindow *aParent)
Ctor.
wxBitmapButton * m_browseButton
wxString GetOutputDirectory() const
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=NULL)
Function CreateDrillandMapFilesSet Creates the full set of Excellon drill file for the board filename...
static bool m_Mirror
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=NULL)
Function CreateDrillandMapFilesSet Creates the full set of Excellon drill file for the board filename...
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:129
DRILL_PRECISION m_Precision
void onFileFormatSelection(wxCommandEvent &event) override
static int m_drillFileType
const std::string ReportFileExtension
#define _(s)
Definition: 3d_actions.cpp:33
The main frame for Pcbnew.
VIATYPE GetViaType() const
Definition: track.h:373
PCBNEW_SETTINGS * GetPcbNewSettings() const
Classes used in drill files, map files and report files generation.
wxStaticText * m_staticTextPrecision
Classes used in drill files, map files and report files generation.
void OnGenReportFile(wxCommandEvent &event) override
int GenerateDrillFiles(const TOOL_EVENT &aEvent)
BOARD * GetBoard() const
void UpdateDrillParams()
Update board drill/plot parameters.
bool GetUseAuxOrigin() const
Definition: pad.h:60
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void SetOptions(bool aMirror, bool aMinimalHeader, wxPoint aOffset, bool aMerge_PTH_NPTH)
Function SetOptions Initialize internal parameters to match drill options.
void OnGenMapFile(wxCommandEvent &event) override
void GenDrillAndMapFiles(bool aGenDrill, bool aGenMap)
Function GenDrillAndMapFiles Calls the functions to create EXCELLON drill files and/or drill map file...
void SetOptions(wxPoint aOffset)
Function SetOptions Initialize internal parameters to match drill options note: PTH and NPTH are alwa...
wxPoint m_AuxOrigin
origin for plot exports
wxRadioButton * m_rbGerberX2
TRACKS & Tracks()
Definition: board.h:300
Definition: track.h:83
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame