KiCad PCB EDA Suite
dialog_gendrill.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 1992-2019 Jean_Pierre Charras <jp.charras at wanadoo.fr>
9  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <fctsys.h>
30 #include <kiface_i.h>
31 #include <confirm.h>
32 #include <pcbnew.h>
33 #include <pcb_edit_frame.h>
34 #include <pcbplot.h>
36 #include <gendrill_gerber_writer.h>
37 #include <bitmaps.h>
38 
39 #include <class_board.h>
40 #include <class_track.h>
41 #include <class_module.h>
42 
43 #include <dialog_gendrill.h>
45 #include <reporter.h>
46 
47 #include <wx/stdpaths.h>
48 
49 
50 // Keywords for read and write parameters in project config
51 #define ZerosFormatKey wxT( "DrillZerosFormat" )
52 #define MirrorKey wxT( "DrillMirrorYOpt" )
53 #define MinimalHeaderKey wxT( "DrillMinHeader" )
54 #define MergePTHNPTHKey wxT( "DrillMergePTHNPTH" )
55 #define UnitDrillInchKey wxT( "DrillUnit" )
56 #define DrillMapFileTypeKey wxT( "DrillMapFileType" )
57 #define DrillFileFormatKey wxT( "DrillFileType" )
58 #define OvalHolesRouteModeKey wxT( "OvalHolesRouteMode" )
59 
60 // list of allowed precision for EXCELLON files, for integer format:
61 // Due to difference between inches and mm,
62 // there are 2 precision values, one for inches and one for metric
63 // Note: for decimla format, the precision is not used
66 
67 
68 /* This function displays the dialog frame for drill tools
69  */
70 void PCB_EDIT_FRAME::InstallDrillFrame( wxCommandEvent& event )
71 {
72  DIALOG_GENDRILL dlg( this, this );
73 
74  dlg.ShowModal();
75 }
76 
77 
78 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
79  DIALOG_GENDRILL_BASE( aParent )
80 {
81  m_pcbEditFrame = aPcbEditFrame;
85 
86  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
87  // that requires us to correct the button labels here.
88  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
89  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
90  m_sdbSizerCancel->SetLabel( _( "Close" ) );
91  m_buttonsSizer->Layout();
92 
93  m_sdbSizerOK->SetDefault();
94  SetReturnCode( 1 );
95  initDialog();
96  GetSizer()->SetSizeHints( this );
97 }
98 
99 
100 // Static members of DIALOG_GENDRILL
101 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
103 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
104 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
105 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
108 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
109 
111 {
112 }
113 
114 
116 {
118  m_config->Read( MirrorKey, &m_Mirror );
126 
128 }
129 
130 
132 {
133  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
134 
135  m_rbExcellon->SetValue( m_drillFileType == 0 );
136  m_rbGerberX2->SetValue( m_drillFileType == 1 );
137  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
138  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
140  m_Check_Minimal->SetValue( m_MinimalHeader );
141 
143  m_Choice_Drill_Offset->SetSelection( 1 );
144 
145  m_Check_Mirror->SetValue( m_Mirror );
147  m_Choice_Drill_Map->SetSelection( m_mapFileType );
148  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
149 
152  m_throughViasCount = 0;
153  m_microViasCount = 0;
155 
156  for( MODULE* module = m_board->m_Modules; module; module = module->Next() )
157  {
158  for( D_PAD* pad = module->PadsList(); pad != NULL; pad = pad->Next() )
159  {
160  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
161  {
162  if( pad->GetDrillSize().x != 0 )
163  {
164  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
166  else
168  }
169  }
170  else
171  {
172  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
173  {
174  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
176  else
178  }
179  }
180  }
181  }
182 
183  for( TRACK* track = m_board->m_Track; track != NULL; track = track->Next() )
184  {
185  const VIA *via = dynamic_cast<const VIA*>( track );
186  if( via )
187  {
188  switch( via->GetViaType() )
189  {
190  case VIA_THROUGH:
192  break;
193 
194  case VIA_MICROVIA:
196  break;
197 
198  case VIA_BLIND_BURIED:
200  break;
201 
202  default:
203  break;
204  }
205  }
206  }
207 
208  // Display hole counts:
209  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
210  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
211  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
212  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
213  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
214 
215  // Output directory
217 
218  wxCommandEvent dummy;
220 }
221 
222 
223 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
224 {
225  bool enbl_Excellon = m_rbExcellon->GetValue();
226 
227  m_drillFileType = enbl_Excellon ? 0 : 1;
228 
229  m_Choice_Unit->Enable( enbl_Excellon );
230  m_Choice_Zeros_Format->Enable( enbl_Excellon );
231  m_Check_Mirror->Enable( enbl_Excellon );
232  m_Check_Minimal->Enable( enbl_Excellon );
233  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
234  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
235 
236  if( enbl_Excellon )
238  else
239  {
240  m_staticTextPrecision->Enable( true );
241  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
242  }
243 }
244 
245 
247 {
249 
251  m_config->Write( MirrorKey, m_Mirror );
258 }
259 
260 
261 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
262 {
264 }
265 
266 
267 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
268 {
269  GenDrillAndMapFiles( false, true);
270 }
271 
272 
273 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
274 {
275  GenDrillAndMapFiles(true, false);
276 }
277 
278 
279 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
280 {
282 }
283 
284 
286 {
287  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
289  else
290  {
291  // metric options
293  }
294 
296  m_staticTextPrecision->Enable( false );
297  else
298  m_staticTextPrecision->Enable( true );
299 }
300 
301 
303 {
304  // Build the absolute path of current output plot directory
305  // to preselect it when opening the dialog.
306  wxFileName fn( m_outputDirectoryName->GetValue() );
307  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
308 
309  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
310 
311  if( dirDialog.ShowModal() == wxID_CANCEL )
312  return;
313 
314  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
315 
316  fn = Prj().AbsolutePath( m_board->GetFileName() );
317  wxString defaultPath = fn.GetPathWithSep();
318  wxString msg;
319  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
320 
321  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
322  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
323 
324  if( dialog.ShowModal() == wxID_YES )
325  {
326  if( !dirName.MakeRelativeTo( defaultPath ) )
327  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
328  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
329  }
330 
331  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
332 }
333 
334 
336 {
337  wxString msg;
338 
339  // Set output directory and replace backslashes with forward ones
340  wxString dirStr;
341  dirStr = m_outputDirectoryName->GetValue();
342  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
343  m_plotOpts.SetOutputDirectory( dirStr );
346 
347  m_mapFileType = m_Choice_Drill_Map->GetSelection();
348 
349  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
350  m_MinimalHeader = m_Check_Minimal->IsChecked();
351  m_Mirror = m_Check_Mirror->IsChecked();
353  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
354  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
355 
356  if( m_Choice_Drill_Offset->GetSelection() == 0 )
357  m_FileDrillOffset = wxPoint( 0, 0 );
358  else
360 
361  if( m_UnitDrillIsInch )
363  else
365 
367 }
368 
369 
370 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
371 {
372  UpdateConfig(); // set params and Save drill options
373 
376 
377  const PlotFormat filefmt[6] =
378  { // Keep these format ids in the same order than m_Choice_Drill_Map choices
381  };
382 
383  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
384 
385  if( choice >= arrayDim( filefmt ) )
386  choice = 1;
387 
388  // Create output directory if it does not exist (also transform it in
389  // absolute form). Bail if it fails
390  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
391  wxString boardFilename = m_board->GetFileName();
392 
393  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
394  {
395  wxString msg;
396  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
397  GetChars( outputDir.GetPath() ) );
398  DisplayError( this, msg );
399  return;
400  }
401 
402  if( m_drillFileType == 0 )
403  {
404  EXCELLON_WRITER excellonWriter( m_board );
409  excellonWriter.SetMapFileFormat( filefmt[choice] );
410 
411  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
412  aGenDrill, aGenMap, &reporter );
413  }
414  else
415  {
416  GERBER_WRITER gerberWriter( m_board );
417  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
418  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
419  // the integer part precision is always 4, and units always mm
420  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
421  gerberWriter.SetOptions( m_FileDrillOffset );
422  gerberWriter.SetMapFileFormat( filefmt[choice] );
423 
424  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
425  aGenDrill, aGenMap, &reporter );
426  }
427 }
428 
429 
430 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
431 {
432  UpdateConfig(); // set params and Save drill options
433 
434  wxFileName fn = m_board->GetFileName();
435 
436  fn.SetName( fn.GetName() + wxT( "-drl" ) );
437  fn.SetExt( ReportFileExtension );
438 
439  wxString defaultPath = Prj().AbsolutePath( m_plotOpts.GetOutputDirectory() );
440 
441  if( defaultPath.IsEmpty() )
442  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
443 
444  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
445  ReportFileWildcard(), wxFD_SAVE );
446 
447  if( dlg.ShowModal() == wxID_CANCEL )
448  return;
449 
450  bool success;
451 
452  // Info is slightly different between Excellon and Gerber
453  // (file ext, Merge PTH/NPTH option)
454  if( m_drillFileType == 0 )
455  {
456  EXCELLON_WRITER excellonWriter( m_board );
457  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
458  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
459  }
460  else
461  {
462  GERBER_WRITER gerberWriter( m_board );
463  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
464  }
465 
466  wxString msg;
467 
468  if( ! success )
469  {
470  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( dlg.GetPath() ) );
471  m_messagesBox->AppendText( msg );
472  }
473  else
474  {
475  msg.Printf( _( "Report file %s created\n" ), GetChars( dlg.GetPath() ) );
476  m_messagesBox->AppendText( msg );
477  }
478 }
#define DrillMapFileTypeKey
#define UnitDrillInchKey
static int m_UnitDrillIsInch
D_PAD * Next() const
Definition: class_pad.h:160
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
Class BOARD to handle a board.
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
Drill offset: 0,0 for absolute coordinates, or origin of the auxiliary axis.
wxPoint m_FileDrillOffset
Selected precision for drill files, in non decimal format.
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:238
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
Functions relatives to tracks, vias and segments used to fill 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...
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
wxStaticText * m_ThroughViasInfoMsg
void UpdateDrillParams(void)
Update board drill/plot parameters.
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:485
wxRadioBox * m_Choice_Zeros_Format
Definition of file extensions used in Kicad.
VIATYPE_T GetViaType() const
Definition: class_track.h:461
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: class_board.h:558
void SetOutputDirectory(wxString aDir)
static DRILL_PRECISION precisionListForMetric(3, 3)
DIALOG_GENDRILL(PCB_EDIT_FRAME *aPcbEditFrame, wxWindow *aParent)
Ctor.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:99
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:92
static int m_drillFileType
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
#define OvalHolesRouteModeKey
const std::string ReportFileExtension
DLIST< MODULE > m_Modules
Definition: class_board.h:249
void InitDisplayParams(void)
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
DLIST< TRACK > m_Track
Definition: class_board.h:250
Module description (excepted pads)
BOARD * GetBoard() const
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 DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:243
void GenDrillAndMapFiles(bool aGenDrill, bool aGenMap)
Function GenDrillAndMapFiles Calls the functions to create EXCELLON drill files and/or drill map file...
void InstallDrillFrame(wxCommandEvent &event)
void SetOptions(wxPoint aOffset)
Function SetOptions Initialize internal parameters to match drill options note: PTH and NPTH are alwa...
void ClearMsgPanel(void)
Clear all messages from the message panel.
wxRadioButton * m_rbGerberX2
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame
True to use a G00 route command for oval holes False to use a G85 canned mode for oval holes.
void SetMapFileFormat(PlotFormat aMapFmt)
Initialize the format for the drill map file.