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 <fctsys.h>
26 #include <kiface_i.h>
27 #include <confirm.h>
28 #include <pcbnew.h>
29 #include <pcb_edit_frame.h>
30 #include <pcbplot.h>
32 #include <gendrill_gerber_writer.h>
33 #include <bitmaps.h>
35 #include <class_board.h>
36 #include <class_track.h>
37 #include <class_module.h>
38 #include <dialog_gendrill.h>
40 #include <reporter.h>
41 #include <wx/stdpaths.h>
42 
43 
44 // Keywords for read and write parameters in project config
45 #define ZerosFormatKey wxT( "DrillZerosFormat" )
46 #define MirrorKey wxT( "DrillMirrorYOpt" )
47 #define MinimalHeaderKey wxT( "DrillMinHeader" )
48 #define MergePTHNPTHKey wxT( "DrillMergePTHNPTH" )
49 #define UnitDrillInchKey wxT( "DrillUnit" )
50 #define DrillMapFileTypeKey wxT( "DrillMapFileType" )
51 #define DrillFileFormatKey wxT( "DrillFileType" )
52 #define OvalHolesRouteModeKey wxT( "OvalHolesRouteMode" )
53 
54 // list of allowed precision for EXCELLON files, for integer format:
55 // Due to difference between inches and mm,
56 // there are 2 precision values, one for inches and one for metric
57 // Note: for decimla format, the precision is not used
60 
61 
62 /* This function displays the dialog frame for drill tools
63  */
65 {
66  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
67  DIALOG_GENDRILL dlg( editFrame, editFrame );
68 
69  dlg.ShowModal();
70  return 0;
71 }
72 
73 
74 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
75  DIALOG_GENDRILL_BASE( aParent )
76 {
77  m_pcbEditFrame = aPcbEditFrame;
81 
82  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
83  // that requires us to correct the button labels here.
84  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
85  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
86  m_sdbSizerCancel->SetLabel( _( "Close" ) );
87  m_buttonsSizer->Layout();
88 
89  m_sdbSizerOK->SetDefault();
90  SetReturnCode( 1 );
91  initDialog();
92  GetSizer()->SetSizeHints( this );
93 }
94 
95 
96 // Static members of DIALOG_GENDRILL
97 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
99 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
100 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
101 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
104 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
105 
107 {
108 }
109 
110 
112 {
114  m_config->Read( MirrorKey, &m_Mirror );
122 
124 }
125 
126 
128 {
129  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
130 
131  m_rbExcellon->SetValue( m_drillFileType == 0 );
132  m_rbGerberX2->SetValue( m_drillFileType == 1 );
133  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
134  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
136  m_Check_Minimal->SetValue( m_MinimalHeader );
137 
139  m_Choice_Drill_Offset->SetSelection( 1 );
140 
141  m_Check_Mirror->SetValue( m_Mirror );
143  m_Choice_Drill_Map->SetSelection( m_mapFileType );
144  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
145 
148  m_throughViasCount = 0;
149  m_microViasCount = 0;
151 
152  for( auto module : m_board->Modules() )
153  {
154  for( auto pad : module->Pads() )
155  {
156  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
157  {
158  if( pad->GetDrillSize().x != 0 )
159  {
160  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
162  else
164  }
165  }
166  else
167  {
168  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
169  {
170  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
172  else
174  }
175  }
176  }
177  }
178 
179  for( auto track : m_board->Tracks() )
180  {
181  const VIA *via = dynamic_cast<const VIA*>( track );
182  if( via )
183  {
184  switch( via->GetViaType() )
185  {
186  case VIA_THROUGH:
188  break;
189 
190  case VIA_MICROVIA:
192  break;
193 
194  case VIA_BLIND_BURIED:
196  break;
197 
198  default:
199  break;
200  }
201  }
202  }
203 
204  // Display hole counts:
205  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
206  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
207  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
208  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
209  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
210 
211  // Output directory
213 
214  wxCommandEvent dummy;
216 }
217 
218 
219 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
220 {
221  bool enbl_Excellon = m_rbExcellon->GetValue();
222 
223  m_drillFileType = enbl_Excellon ? 0 : 1;
224 
225  m_Choice_Unit->Enable( enbl_Excellon );
226  m_Choice_Zeros_Format->Enable( enbl_Excellon );
227  m_Check_Mirror->Enable( enbl_Excellon );
228  m_Check_Minimal->Enable( enbl_Excellon );
229  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
230  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
231 
232  if( enbl_Excellon )
234  else
235  {
236  m_staticTextPrecision->Enable( true );
237  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
238  }
239 }
240 
241 
243 {
245 
247  m_config->Write( MirrorKey, m_Mirror );
254 }
255 
256 
257 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
258 {
260 }
261 
262 
263 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
264 {
265  GenDrillAndMapFiles( false, true);
266 }
267 
268 
269 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
270 {
271  GenDrillAndMapFiles(true, false);
272 }
273 
274 
275 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
276 {
278 }
279 
280 
282 {
283  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
285  else
286  {
287  // metric options
289  }
290 
292  m_staticTextPrecision->Enable( false );
293  else
294  m_staticTextPrecision->Enable( true );
295 }
296 
297 
299 {
300  // Build the absolute path of current output plot directory
301  // to preselect it when opening the dialog.
302  wxFileName fn( m_outputDirectoryName->GetValue() );
303  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
304 
305  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
306 
307  if( dirDialog.ShowModal() == wxID_CANCEL )
308  return;
309 
310  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
311 
312  fn = Prj().AbsolutePath( m_board->GetFileName() );
313  wxString defaultPath = fn.GetPathWithSep();
314  wxString msg;
315  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
316 
317  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
318  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
319 
320  if( dialog.ShowModal() == wxID_YES )
321  {
322  if( !dirName.MakeRelativeTo( defaultPath ) )
323  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
324  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
325  }
326 
327  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
328 }
329 
330 
332 {
333  wxString msg;
334 
335  // Set output directory and replace backslashes with forward ones
336  wxString dirStr;
337  dirStr = m_outputDirectoryName->GetValue();
338  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
339  m_plotOpts.SetOutputDirectory( dirStr );
342 
343  m_mapFileType = m_Choice_Drill_Map->GetSelection();
344 
345  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
346  m_MinimalHeader = m_Check_Minimal->IsChecked();
347  m_Mirror = m_Check_Mirror->IsChecked();
349  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
350  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
351 
352  if( m_Choice_Drill_Offset->GetSelection() == 0 )
353  m_FileDrillOffset = wxPoint( 0, 0 );
354  else
356 
357  if( m_UnitDrillIsInch )
359  else
361 
363 }
364 
365 
366 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
367 {
368  UpdateConfig(); // set params and Save drill options
369 
372 
373  const PlotFormat filefmt[6] =
374  { // Keep these format ids in the same order than m_Choice_Drill_Map choices
377  };
378 
379  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
380 
381  if( choice >= arrayDim( filefmt ) )
382  choice = 1;
383 
384  // Create output directory if it does not exist (also transform it in
385  // absolute form). Bail if it fails
386  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
387  wxString boardFilename = m_board->GetFileName();
388 
389  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
390  {
391  wxString msg;
392  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
393  GetChars( outputDir.GetPath() ) );
394  DisplayError( this, msg );
395  return;
396  }
397 
398  if( m_drillFileType == 0 )
399  {
400  EXCELLON_WRITER excellonWriter( m_board );
405  excellonWriter.SetMapFileFormat( filefmt[choice] );
406 
407  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
408  aGenDrill, aGenMap, &reporter );
409  }
410  else
411  {
412  GERBER_WRITER gerberWriter( m_board );
413  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
414  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
415  // the integer part precision is always 4, and units always mm
416  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
417  gerberWriter.SetOptions( m_FileDrillOffset );
418  gerberWriter.SetMapFileFormat( filefmt[choice] );
419 
420  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
421  aGenDrill, aGenMap, &reporter );
422  }
423 }
424 
425 
426 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
427 {
428  UpdateConfig(); // set params and Save drill options
429 
430  wxFileName fn = m_board->GetFileName();
431 
432  fn.SetName( fn.GetName() + wxT( "-drl" ) );
433  fn.SetExt( ReportFileExtension );
434 
435  wxString defaultPath = Prj().AbsolutePath( m_plotOpts.GetOutputDirectory() );
436 
437  if( defaultPath.IsEmpty() )
438  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
439 
440  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
441  ReportFileWildcard(), wxFD_SAVE );
442 
443  if( dlg.ShowModal() == wxID_CANCEL )
444  return;
445 
446  bool success;
447 
448  // Info is slightly different between Excellon and Gerber
449  // (file ext, Merge PTH/NPTH option)
450  if( m_drillFileType == 0 )
451  {
452  EXCELLON_WRITER excellonWriter( m_board );
453  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
454  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
455  }
456  else
457  {
458  GERBER_WRITER gerberWriter( m_board );
459  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
460  }
461 
462  wxString msg;
463 
464  if( ! success )
465  {
466  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( dlg.GetPath() ) );
467  m_messagesBox->AppendText( msg );
468  }
469  else
470  {
471  msg.Printf( _( "Report file %s created\n" ), GetChars( dlg.GetPath() ) );
472  m_messagesBox->AppendText( msg );
473  }
474 }
#define DrillMapFileTypeKey
#define UnitDrillInchKey
static int m_UnitDrillIsInch
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
wxStaticText * m_PlatedPadsCountInfoMsg
void SetRouteModeForOvalHoles(bool aUseRouteModeForOvalHoles)
void OnGenDrillFile(wxCommandEvent &event) override
wxConfigBase * m_config
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
void SetUseAuxOrigin(bool aAux)
This file is part of the common library.
#define MinimalHeaderKey
wxRadioBox * m_Choice_Drill_Offset
#define MergePTHNPTHKey
void OnSelZerosFmtSelected(wxCommandEvent &event) override
int GenerateDrillFiles(const TOOL_EVENT &aEvent)
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
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
PlotFormat
Enum PlotFormat is the set of supported output plot formats.
Definition: plotter.h:50
static bool m_UseRouteModeForOvalHoles
wxPoint m_FileDrillOffset
void SetMergeOption(bool aMerge)
set the option to make separate drill files for PTH and NPTH
#define ZerosFormatKey
void SetFormat(int aRightDigits=6)
Function SetFormat Initialize internal parameters to match the given format.
static int m_mapFileType
#define DrillFileFormatKey
GERBER_WRITER is a class mainly used to create Gerber drill files.
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: class_board.h:216
wxStaticText * m_NotPlatedPadsCountInfoMsg
const wxPoint & GetAuxOrigin() const override
Return the origin of the axis used for plotting and various exports.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
#define MirrorKey
Definitions for tracks, vias and zones.
Class WX_TEXT_CTRL_REPORTER is wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:144
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:385
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:79
wxRadioBox * m_radioBoxOvalHoleMode
static bool m_MinimalHeader
wxStaticText * m_MicroViasInfoMsg
int GetGerberPrecision() const
wxRadioButton * m_rbExcellon
bool GenDrillReportFile(const wxString &aFullFileName)
Function GenDrillReportFile Create a plain text report file giving a list of drill values and drill c...
MODULES & Modules()
Definition: class_board.h:227
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
wxStaticText * m_ThroughViasInfoMsg
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:481
Class TOOL_EVENT.
Definition: tool_event.h:171
wxRadioBox * m_Choice_Zeros_Format
Definition of file extensions used in Kicad.
VIATYPE_T GetViaType() const
Definition: class_track.h:346
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: class_board.h:550
void SetOutputDirectory(wxString aDir)
static DRILL_PRECISION precisionListForMetric(3, 3)
virtual void ClearMsgPanel()
Clear all messages from the message panel.
#define _(s)
DIALOG_GENDRILL(PCB_EDIT_FRAME *aPcbEditFrame, wxWindow *aParent)
Ctor.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
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 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
static int m_drillFileType
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define OvalHolesRouteModeKey
const std::string ReportFileExtension
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
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
Module description (excepted pads)
BOARD * GetBoard() const
void UpdateDrillParams()
Update board drill/plot parameters.
bool GetUseAuxOrigin() const
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...
wxRadioButton * m_rbGerberX2
TRACKS & Tracks()
Definition: class_board.h:218
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame
void SetMapFileFormat(PlotFormat aMapFmt)
Initialize the format for the drill map file.