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 <dialog_gendrill.h>
39 #include <reporter.h>
40 #include <wx/stdpaths.h>
41 
42 
43 // list of allowed precision for EXCELLON files, for integer format:
44 // Due to difference between inches and mm,
45 // there are 2 precision values, one for inches and one for metric
46 // Note: for decimla format, the precision is not used
49 
50 
51 /* This function displays the dialog frame for drill tools
52  */
54 {
55  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
56  DIALOG_GENDRILL dlg( editFrame, editFrame );
57 
58  dlg.ShowModal();
59  return 0;
60 }
61 
62 
63 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
64  DIALOG_GENDRILL_BASE( aParent )
65 {
66  m_pcbEditFrame = aPcbEditFrame;
69 
70  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
71  // that requires us to correct the button labels here.
72  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
73  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
74  m_sdbSizerCancel->SetLabel( _( "Close" ) );
75  m_buttonsSizer->Layout();
76 
77  m_sdbSizerOK->SetDefault();
78  SetReturnCode( 1 );
79  initDialog();
80  GetSizer()->SetSizeHints( this );
81 }
82 
83 
84 // Static members of DIALOG_GENDRILL
85 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
87 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
88 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
89 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
92 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
93 
95 {
96 }
97 
98 
100 {
101  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
102 
103  m_Merge_PTH_NPTH = cfg->m_GenDrill.merge_pth_npth;
104  m_MinimalHeader = cfg->m_GenDrill.minimal_header;
105  m_Mirror = cfg->m_GenDrill.mirror;
106  m_UnitDrillIsInch = cfg->m_GenDrill.unit_drill_is_inch;
107  m_UseRouteModeForOvalHoles = cfg->m_GenDrill.use_route_for_oval_holes;
108  m_drillFileType = cfg->m_GenDrill.drill_file_type;
109  m_mapFileType = cfg->m_GenDrill.map_file_type;
110  m_ZerosFormat = cfg->m_GenDrill.zeros_format;
111 
113 
115 }
116 
117 
119 {
120  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
121 
122  m_rbExcellon->SetValue( m_drillFileType == 0 );
123  m_rbGerberX2->SetValue( m_drillFileType == 1 );
124  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
125  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
127  m_Check_Minimal->SetValue( m_MinimalHeader );
128 
130  m_Choice_Drill_Offset->SetSelection( 1 );
131 
132  m_Check_Mirror->SetValue( m_Mirror );
134  m_Choice_Drill_Map->SetSelection( m_mapFileType );
135  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
136 
139  m_throughViasCount = 0;
140  m_microViasCount = 0;
142 
143  for( FOOTPRINT* footprint : m_board->Footprints() )
144  {
145  for( PAD* pad : footprint->Pads() )
146  {
147  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
148  {
149  if( pad->GetDrillSize().x != 0 )
150  {
151  if( pad->GetAttribute() == PAD_ATTRIB_NPTH )
153  else
155  }
156  }
157  else
158  {
159  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
160  {
161  if( pad->GetAttribute() == PAD_ATTRIB_NPTH )
163  else
165  }
166  }
167  }
168  }
169 
170  for( TRACK* track : m_board->Tracks() )
171  {
172  const VIA *via = dynamic_cast<const VIA*>( track );
173 
174  if( via )
175  {
176  switch( via->GetViaType() )
177  {
178  case VIATYPE::THROUGH: m_throughViasCount++; break;
179  case VIATYPE::MICROVIA: m_microViasCount++; break;
181  default: break;
182  }
183  }
184  }
185 
186  // Display hole counts:
187  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
188  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
189  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
190  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
191  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
192 
193  // Output directory
195 
196  wxCommandEvent dummy;
198 }
199 
200 
201 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
202 {
203  bool enbl_Excellon = m_rbExcellon->GetValue();
204 
205  m_drillFileType = enbl_Excellon ? 0 : 1;
206 
207  m_Choice_Unit->Enable( enbl_Excellon );
208  m_Choice_Zeros_Format->Enable( enbl_Excellon );
209  m_Check_Mirror->Enable( enbl_Excellon );
210  m_Check_Minimal->Enable( enbl_Excellon );
211  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
212  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
213 
214  if( enbl_Excellon )
216  else
217  {
218  m_staticTextPrecision->Enable( true );
219  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
220  }
221 }
222 
223 
225 {
227 
228  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
229 
231  cfg->m_GenDrill.minimal_header = m_MinimalHeader;
232  cfg->m_GenDrill.mirror = m_Mirror;
233  cfg->m_GenDrill.unit_drill_is_inch = m_UnitDrillIsInch;
234  cfg->m_GenDrill.use_route_for_oval_holes = m_UseRouteModeForOvalHoles;
235  cfg->m_GenDrill.drill_file_type = m_drillFileType;
236  cfg->m_GenDrill.map_file_type = m_mapFileType;
237  cfg->m_GenDrill.zeros_format = m_ZerosFormat;
238 }
239 
240 
241 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
242 {
244 }
245 
246 
247 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
248 {
249  GenDrillAndMapFiles( false, true );
250 }
251 
252 
253 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
254 {
255  GenDrillAndMapFiles( true, false );
256 }
257 
258 
259 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
260 {
262 }
263 
264 
266 {
267  if( m_Choice_Unit->GetSelection()== 1 )
268  {
269  // Units = inches
271  }
272  else
273  {
274  // metric options
276  }
277 
279  m_staticTextPrecision->Enable( false );
280  else
281  m_staticTextPrecision->Enable( true );
282 }
283 
284 
286 {
287  // Build the absolute path of current output directory to preselect it in the file browser.
288  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
289  path = Prj().AbsolutePath( path );
290 
291  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
292 
293  if( dirDialog.ShowModal() == wxID_CANCEL )
294  return;
295 
296  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
297  wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) );
298  wxString defaultPath = fn.GetPathWithSep();
299  wxString msg;
300  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), defaultPath );
301 
302  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
303  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
304 
305  if( dialog.ShowModal() == wxID_YES )
306  {
307  if( !dirName.MakeRelativeTo( defaultPath ) )
308  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
309  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
310  }
311 
312  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
313 }
314 
315 
317 {
318  wxString msg;
319 
320  // Set output directory and replace backslashes with forward ones
321  wxString dirStr;
322  dirStr = m_outputDirectoryName->GetValue();
323  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
324  m_plotOpts.SetOutputDirectory( dirStr );
327 
328  m_mapFileType = m_Choice_Drill_Map->GetSelection();
329 
330  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
331  m_MinimalHeader = m_Check_Minimal->IsChecked();
332  m_Mirror = m_Check_Mirror->IsChecked();
334  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
335  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
336 
337  if( m_Choice_Drill_Offset->GetSelection() == 0 )
338  m_FileDrillOffset = wxPoint( 0, 0 );
339  else
341 
342  if( m_UnitDrillIsInch )
344  else
346 
348 }
349 
350 
351 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
352 {
353  UpdateConfig(); // set params and Save drill options
354 
357 
358  const PLOT_FORMAT filefmt[6] = {
359  // Keep these format ids in the same order than m_Choice_Drill_Map choices
362  };
363 
364  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
365 
366  if( choice >= arrayDim( filefmt ) )
367  choice = 1;
368 
369  // Create output directory if it does not exist (also transform it in
370  // absolute form). Bail if it fails
372  wxFileName outputDir = wxFileName::DirName( path );
373  wxString boardFilename = m_board->GetFileName();
374 
375  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
376  {
377  wxString msg;
378  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
379  outputDir.GetPath() );
380  DisplayError( this, msg );
381  return;
382  }
383 
384  if( m_drillFileType == 0 )
385  {
386  EXCELLON_WRITER excellonWriter( m_board );
391  excellonWriter.SetMapFileFormat( filefmt[choice] );
392 
393  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
394  &reporter );
395  }
396  else
397  {
398  GERBER_WRITER gerberWriter( m_board );
399  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
400  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
401  // the integer part precision is always 4, and units always mm
402  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
403  gerberWriter.SetOptions( m_FileDrillOffset );
404  gerberWriter.SetMapFileFormat( filefmt[choice] );
405 
406  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
407  aGenDrill, aGenMap, &reporter );
408  }
409 }
410 
411 
412 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
413 {
414  UpdateConfig(); // set params and Save drill options
415 
416  wxFileName fn = m_board->GetFileName();
417 
418  fn.SetName( fn.GetName() + wxT( "-drl" ) );
419  fn.SetExt( ReportFileExtension );
420 
421  wxString defaultPath = ExpandEnvVarSubstitutions( m_plotOpts.GetOutputDirectory(), &Prj() );
422  defaultPath = Prj().AbsolutePath( defaultPath );
423 
424  if( defaultPath.IsEmpty() )
425  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
426 
427  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
428  ReportFileWildcard(), wxFD_SAVE );
429 
430  if( dlg.ShowModal() == wxID_CANCEL )
431  return;
432 
433  bool success;
434 
435  // Info is slightly different between Excellon and Gerber
436  // (file ext, Merge PTH/NPTH option)
437  if( m_drillFileType == 0 )
438  {
439  EXCELLON_WRITER excellonWriter( m_board );
440  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
441  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
442  }
443  else
444  {
445  GERBER_WRITER gerberWriter( m_board );
446  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
447  }
448 
449  wxString msg;
450 
451  if( ! success )
452  {
453  msg.Printf( _( "** Unable to create %s **\n" ), dlg.GetPath() );
454  m_messagesBox->AppendText( msg );
455  }
456  else
457  {
458  msg.Printf( _( "Report file %s created\n" ), dlg.GetPath() );
459  m_messagesBox->AppendText( msg );
460  }
461 }
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:240
wxStaticText * m_PlatedPadsCountInfoMsg
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
void SetRouteModeForOvalHoles(bool aUseRouteModeForOvalHoles)
Definition: track.h:354
void OnGenDrillFile(wxCommandEvent &event) override
void SetUseAuxOrigin(bool aAux)
This file is part of the common library.
wxRadioBox * m_Choice_Drill_Offset
void OnSelZerosFmtSelected(wxCommandEvent &event) override
int GenerateDrillFiles(const TOOL_EVENT &aEvent)
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 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:559
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
static int m_mapFileType
PCBNEW_SETTINGS * GetPcbNewSettings()
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:255
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
Function GetPlotSettings returns the PCB_PLOT_PARAMS for the BOARD owned by this frame.
const wxString & GetFileName() const
Definition: board.h:278
wxStaticText * m_NotPlatedPadsCountInfoMsg
WX_TEXT_CTRL_REPORTER is wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:136
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:270
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
Function Prj returns 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:283
FOOTPRINTS & Footprints()
Definition: board.h:283
TOOL_EVENT.
Definition: tool_event.h:171
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:578
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...
DRILL_PRECISION m_Precision
void onFileFormatSelection(wxCommandEvent &event) override
static int m_drillFileType
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
const std::string ReportFileExtension
#define _(s)
Definition: 3d_actions.cpp:33
PCB_EDIT_FRAME is the main frame for Pcbnew.
VIATYPE GetViaType() const
Definition: track.h:384
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
BOARD * GetBoard() const
void UpdateDrillParams()
Update board drill/plot parameters.
bool GetUseAuxOrigin() const
Definition: pad.h:59
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:280
Definition: track.h:83
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame