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 <pcbnew_settings.h>
31 #include <pcbplot.h>
33 #include <gendrill_gerber_writer.h>
34 #include <bitmaps.h>
36 #include <class_board.h>
37 #include <class_track.h>
38 #include <class_module.h>
39 #include <dialog_gendrill.h>
41 #include <reporter.h>
42 #include <wx/stdpaths.h>
43 
44 
45 // list of allowed precision for EXCELLON files, for integer format:
46 // Due to difference between inches and mm,
47 // there are 2 precision values, one for inches and one for metric
48 // Note: for decimla format, the precision is not used
51 
52 
53 /* This function displays the dialog frame for drill tools
54  */
56 {
57  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
58  DIALOG_GENDRILL dlg( editFrame, editFrame );
59 
60  dlg.ShowModal();
61  return 0;
62 }
63 
64 
65 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
66  DIALOG_GENDRILL_BASE( aParent )
67 {
68  m_pcbEditFrame = aPcbEditFrame;
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 Drill File" ) );
75  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
76  m_sdbSizerCancel->SetLabel( _( "Close" ) );
77  m_buttonsSizer->Layout();
78 
79  m_sdbSizerOK->SetDefault();
80  SetReturnCode( 1 );
81  initDialog();
82  GetSizer()->SetSizeHints( this );
83 }
84 
85 
86 // Static members of DIALOG_GENDRILL
87 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
89 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
90 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
91 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
94 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
95 
97 {
98 }
99 
100 
102 {
103  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
104 
105  m_Merge_PTH_NPTH = cfg->m_GenDrill.merge_pth_npth;
106  m_MinimalHeader = cfg->m_GenDrill.minimal_header;
107  m_Mirror = cfg->m_GenDrill.mirror;
108  m_UnitDrillIsInch = cfg->m_GenDrill.unit_drill_is_inch;
109  m_UseRouteModeForOvalHoles = cfg->m_GenDrill.use_route_for_oval_holes;
110  m_drillFileType = cfg->m_GenDrill.drill_file_type;
111  m_mapFileType = cfg->m_GenDrill.map_file_type;
112  m_ZerosFormat = cfg->m_GenDrill.zeros_format;
113 
115 
117 }
118 
119 
121 {
122  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
123 
124  m_rbExcellon->SetValue( m_drillFileType == 0 );
125  m_rbGerberX2->SetValue( m_drillFileType == 1 );
126  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
127  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
129  m_Check_Minimal->SetValue( m_MinimalHeader );
130 
132  m_Choice_Drill_Offset->SetSelection( 1 );
133 
134  m_Check_Mirror->SetValue( m_Mirror );
136  m_Choice_Drill_Map->SetSelection( m_mapFileType );
137  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
138 
141  m_throughViasCount = 0;
142  m_microViasCount = 0;
144 
145  for( auto module : m_board->Modules() )
146  {
147  for( auto pad : module->Pads() )
148  {
149  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
150  {
151  if( pad->GetDrillSize().x != 0 )
152  {
153  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
155  else
157  }
158  }
159  else
160  {
161  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
162  {
163  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
165  else
167  }
168  }
169  }
170  }
171 
172  for( auto track : m_board->Tracks() )
173  {
174  const VIA *via = dynamic_cast<const VIA*>( track );
175  if( via )
176  {
177  switch( via->GetViaType() )
178  {
179  case VIATYPE::THROUGH:
181  break;
182 
183  case VIATYPE::MICROVIA:
185  break;
186 
189  break;
190 
191  default:
192  break;
193  }
194  }
195  }
196 
197  // Display hole counts:
198  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
199  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
200  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
201  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
202  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
203 
204  // Output directory
206 
207  wxCommandEvent dummy;
209 }
210 
211 
212 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
213 {
214  bool enbl_Excellon = m_rbExcellon->GetValue();
215 
216  m_drillFileType = enbl_Excellon ? 0 : 1;
217 
218  m_Choice_Unit->Enable( enbl_Excellon );
219  m_Choice_Zeros_Format->Enable( enbl_Excellon );
220  m_Check_Mirror->Enable( enbl_Excellon );
221  m_Check_Minimal->Enable( enbl_Excellon );
222  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
223  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
224 
225  if( enbl_Excellon )
227  else
228  {
229  m_staticTextPrecision->Enable( true );
230  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
231  }
232 }
233 
234 
236 {
238 
239  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
240 
242  cfg->m_GenDrill.minimal_header = m_MinimalHeader;
243  cfg->m_GenDrill.mirror = m_Mirror;
244  cfg->m_GenDrill.unit_drill_is_inch = m_UnitDrillIsInch;
245  cfg->m_GenDrill.use_route_for_oval_holes = m_UseRouteModeForOvalHoles;
246  cfg->m_GenDrill.drill_file_type = m_drillFileType;
247  cfg->m_GenDrill.map_file_type = m_mapFileType;
248  cfg->m_GenDrill.zeros_format = m_ZerosFormat;
249 }
250 
251 
252 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
253 {
255 }
256 
257 
258 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
259 {
260  GenDrillAndMapFiles( false, true );
261 }
262 
263 
264 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
265 {
266  GenDrillAndMapFiles( true, false );
267 }
268 
269 
270 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
271 {
273 }
274 
275 
277 {
278  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
280  else
281  {
282  // metric options
284  }
285 
287  m_staticTextPrecision->Enable( false );
288  else
289  m_staticTextPrecision->Enable( true );
290 }
291 
292 
294 {
295  // Build the absolute path of current output directory to preselect it in the file browser.
296  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
297  path = Prj().AbsolutePath( path );
298 
299  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
300 
301  if( dirDialog.ShowModal() == wxID_CANCEL )
302  return;
303 
304  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
305  wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) );
306  wxString defaultPath = fn.GetPathWithSep();
307  wxString msg;
308  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
309 
310  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
311  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
312 
313  if( dialog.ShowModal() == wxID_YES )
314  {
315  if( !dirName.MakeRelativeTo( defaultPath ) )
316  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
317  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
318  }
319 
320  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
321 }
322 
323 
325 {
326  wxString msg;
327 
328  // Set output directory and replace backslashes with forward ones
329  wxString dirStr;
330  dirStr = m_outputDirectoryName->GetValue();
331  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
332  m_plotOpts.SetOutputDirectory( dirStr );
335 
336  m_mapFileType = m_Choice_Drill_Map->GetSelection();
337 
338  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
339  m_MinimalHeader = m_Check_Minimal->IsChecked();
340  m_Mirror = m_Check_Mirror->IsChecked();
342  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
343  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
344 
345  if( m_Choice_Drill_Offset->GetSelection() == 0 )
346  m_FileDrillOffset = wxPoint( 0, 0 );
347  else
349 
350  if( m_UnitDrillIsInch )
352  else
354 
356 }
357 
358 
359 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
360 {
361  UpdateConfig(); // set params and Save drill options
362 
365 
366  const PLOT_FORMAT filefmt[6] = {
367  // Keep these format ids in the same order than m_Choice_Drill_Map choices
370  };
371 
372  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
373 
374  if( choice >= arrayDim( filefmt ) )
375  choice = 1;
376 
377  // Create output directory if it does not exist (also transform it in
378  // absolute form). Bail if it fails
380  wxFileName outputDir = wxFileName::DirName( path );
381  wxString boardFilename = m_board->GetFileName();
382 
383  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
384  {
385  wxString msg;
386  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
387  GetChars( outputDir.GetPath() ) );
388  DisplayError( this, msg );
389  return;
390  }
391 
392  if( m_drillFileType == 0 )
393  {
394  EXCELLON_WRITER excellonWriter( m_board );
399  excellonWriter.SetMapFileFormat( filefmt[choice] );
400 
401  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
402  &reporter );
403  }
404  else
405  {
406  GERBER_WRITER gerberWriter( m_board );
407  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
408  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
409  // the integer part precision is always 4, and units always mm
410  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
411  gerberWriter.SetOptions( m_FileDrillOffset );
412  gerberWriter.SetMapFileFormat( filefmt[choice] );
413 
414  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
415  aGenDrill, aGenMap, &reporter );
416  }
417 }
418 
419 
420 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
421 {
422  UpdateConfig(); // set params and Save drill options
423 
424  wxFileName fn = m_board->GetFileName();
425 
426  fn.SetName( fn.GetName() + wxT( "-drl" ) );
427  fn.SetExt( ReportFileExtension );
428 
429  wxString defaultPath = ExpandEnvVarSubstitutions( m_plotOpts.GetOutputDirectory(), &Prj() );
430  defaultPath = Prj().AbsolutePath( defaultPath );
431 
432  if( defaultPath.IsEmpty() )
433  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
434 
435  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
436  ReportFileWildcard(), wxFD_SAVE );
437 
438  if( dlg.ShowModal() == wxID_CANCEL )
439  return;
440 
441  bool success;
442 
443  // Info is slightly different between Excellon and Gerber
444  // (file ext, Merge PTH/NPTH option)
445  if( m_drillFileType == 0 )
446  {
447  EXCELLON_WRITER excellonWriter( m_board );
448  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
449  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
450  }
451  else
452  {
453  GERBER_WRITER gerberWriter( m_board );
454  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
455  }
456 
457  wxString msg;
458 
459  if( ! success )
460  {
461  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( dlg.GetPath() ) );
462  m_messagesBox->AppendText( msg );
463  }
464  else
465  {
466  msg.Printf( _( "Report file %s created\n" ), GetChars( dlg.GetPath() ) );
467  m_messagesBox->AppendText( msg );
468  }
469 }
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:239
wxStaticText * m_PlatedPadsCountInfoMsg
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
void SetRouteModeForOvalHoles(bool aUseRouteModeForOvalHoles)
void OnGenDrillFile(wxCommandEvent &event) override
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:85
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
Function GetDesignSettings.
Definition: class_board.h:553
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:574
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:255
wxStaticText * m_NotPlatedPadsCountInfoMsg
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
WX_TEXT_CTRL_REPORTER is wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:131
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:277
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:80
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...
MODULES & Modules()
Definition: class_board.h:266
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
wxStaticText * m_ThroughViasInfoMsg
PLOT_FORMAT
Enum PlotFormat is the set of supported output plot formats.
Definition: plotter.h:51
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:602
TOOL_EVENT.
Definition: tool_event.h:171
wxRadioBox * m_Choice_Zeros_Format
Definition of file extensions used in Kicad.
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: class_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.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:160
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:153
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: class_track.h:378
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
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: class_board.h:257
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame