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-2021 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 <board_design_settings.h>
36 #include <footprint.h>
37 #include <pad.h>
38 #include <pcb_track.h>
39 #include <paths.h>
40 #include <project.h>
41 #include <dialog_gendrill.h>
43 #include <reporter.h>
44 #include <wx/stdpaths.h>
45 #include <wx/msgdlg.h>
46 #include <wx/dirdlg.h>
47 #include <wx/filedlg.h>
48 
49 // list of allowed precision for EXCELLON files, for integer format:
50 // Due to difference between inches and mm,
51 // there are 2 precision values, one for inches and one for metric
52 // Note: for decimla format, the precision is not used
55 
56 
57 /* This function displays the dialog frame for drill tools
58  */
60 {
61  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
62  DIALOG_GENDRILL dlg( editFrame, editFrame );
63 
64  dlg.ShowModal();
65  return 0;
66 }
67 
68 
69 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
70  DIALOG_GENDRILL_BASE( aParent )
71 {
72  m_pcbEditFrame = aPcbEditFrame;
75 
76  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
77  // that requires us to correct the button labels here.
78  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
79  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
80  m_sdbSizerCancel->SetLabel( _( "Close" ) );
81  m_buttonsSizer->Layout();
82 
83  m_sdbSizerOK->SetDefault();
84  SetReturnCode( 1 );
85  initDialog();
86  GetSizer()->SetSizeHints( this );
87 }
88 
89 
90 // Static members of DIALOG_GENDRILL
91 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
93 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
94 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
95 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
98 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
99 
101 {
102 }
103 
104 
106 {
107  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
108 
109  m_Merge_PTH_NPTH = cfg->m_GenDrill.merge_pth_npth;
110  m_MinimalHeader = cfg->m_GenDrill.minimal_header;
111  m_Mirror = cfg->m_GenDrill.mirror;
112  m_UnitDrillIsInch = cfg->m_GenDrill.unit_drill_is_inch;
113  m_UseRouteModeForOvalHoles = cfg->m_GenDrill.use_route_for_oval_holes;
114  m_drillFileType = cfg->m_GenDrill.drill_file_type;
115  m_mapFileType = cfg->m_GenDrill.map_file_type;
116  m_ZerosFormat = cfg->m_GenDrill.zeros_format;
117 
119 
121 }
122 
123 
125 {
127 
128  m_rbExcellon->SetValue( m_drillFileType == 0 );
129  m_rbGerberX2->SetValue( m_drillFileType == 1 );
130  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
131  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
133  m_Check_Minimal->SetValue( m_MinimalHeader );
134 
135  m_Choice_Drill_Offset->SetSelection( m_drillOriginIsAuxAxis ? 1 : 0 );
136 
137  m_Check_Mirror->SetValue( m_Mirror );
139  m_Choice_Drill_Map->SetSelection( m_mapFileType );
140  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
141 
144  m_throughViasCount = 0;
145  m_microViasCount = 0;
147 
148  for( FOOTPRINT* footprint : m_board->Footprints() )
149  {
150  for( PAD* pad : footprint->Pads() )
151  {
152  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
153  {
154  if( pad->GetDrillSize().x != 0 )
155  {
156  if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
158  else
160  }
161  }
162  else
163  {
164  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
165  {
166  if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
168  else
170  }
171  }
172  }
173  }
174 
175  for( PCB_TRACK* track : m_board->Tracks() )
176  {
177  const PCB_VIA *via = dynamic_cast<const PCB_VIA*>( track );
178 
179  if( via )
180  {
181  switch( via->GetViaType() )
182  {
183  case VIATYPE::THROUGH: m_throughViasCount++; break;
184  case VIATYPE::MICROVIA: m_microViasCount++; break;
186  default: break;
187  }
188  }
189  }
190 
191  // Display hole counts:
192  m_PlatedPadsCountInfoMsg->SetLabel( wxString() << m_platedPadsHoleCount );
193  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
194  m_ThroughViasInfoMsg->SetLabel( wxString() << m_throughViasCount );
195  m_MicroViasInfoMsg->SetLabel( wxString() << m_microViasCount );
196  m_BuriedViasInfoMsg->SetLabel( wxString() << m_blindOrBuriedViasCount );
197 
198  // Output directory
200 
201  wxCommandEvent dummy;
203 }
204 
205 
206 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
207 {
208  bool enbl_Excellon = m_rbExcellon->GetValue();
209 
210  m_drillFileType = enbl_Excellon ? 0 : 1;
211 
212  m_Choice_Unit->Enable( enbl_Excellon );
213  m_Choice_Zeros_Format->Enable( enbl_Excellon );
214  m_Check_Mirror->Enable( enbl_Excellon );
215  m_Check_Minimal->Enable( enbl_Excellon );
216  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
217  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
218 
219  if( enbl_Excellon )
221  else
222  {
223  m_staticTextPrecision->Enable( true );
224  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
225  }
226 }
227 
228 
230 {
232 
233  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
234 
236  cfg->m_GenDrill.minimal_header = m_MinimalHeader;
237  cfg->m_GenDrill.mirror = m_Mirror;
238  cfg->m_GenDrill.unit_drill_is_inch = m_UnitDrillIsInch;
239  cfg->m_GenDrill.use_route_for_oval_holes = m_UseRouteModeForOvalHoles;
240  cfg->m_GenDrill.drill_file_type = m_drillFileType;
241  cfg->m_GenDrill.map_file_type = m_mapFileType;
242  cfg->m_GenDrill.zeros_format = m_ZerosFormat;
243 }
244 
245 
246 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
247 {
249 }
250 
251 
252 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
253 {
254  GenDrillAndMapFiles( false, true );
255 }
256 
257 
258 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
259 {
260  GenDrillAndMapFiles( true, false );
261 }
262 
263 
264 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
265 {
267 }
268 
269 
271 {
272  if( m_Choice_Unit->GetSelection()== 1 )
273  {
274  // Units = inches
276  }
277  else
278  {
279  // metric options
281  }
282 
284  m_staticTextPrecision->Enable( false );
285  else
286  m_staticTextPrecision->Enable( true );
287 }
288 
289 
291 {
292  // Build the absolute path of current output directory to preselect it in the file browser.
293  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
294  path = Prj().AbsolutePath( path );
295 
296  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
297 
298  if( dirDialog.ShowModal() == wxID_CANCEL )
299  return;
300 
301  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
302  wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) );
303  wxString defaultPath = fn.GetPathWithSep();
304  wxString msg;
305  msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath );
306 
307  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
308  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
309 
310  if( dialog.ShowModal() == wxID_YES )
311  {
312  if( !dirName.MakeRelativeTo( defaultPath ) )
313  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
314  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
315  }
316 
317  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
318 }
319 
320 
322 {
323  wxString msg;
324 
325  // Set output directory and replace backslashes with forward ones
326  wxString dirStr;
327  dirStr = m_outputDirectoryName->GetValue();
328  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
329  m_plotOpts.SetOutputDirectory( dirStr );
330  m_drillOriginIsAuxAxis = m_Choice_Drill_Offset->GetSelection() == 1;
332 
333  m_mapFileType = m_Choice_Drill_Map->GetSelection();
334 
335  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
336  m_MinimalHeader = m_Check_Minimal->IsChecked();
337  m_Mirror = m_Check_Mirror->IsChecked();
339  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
340  m_UseRouteModeForOvalHoles = m_radioBoxOvalHoleMode->GetSelection() == 0;
341 
342  if( m_Choice_Drill_Offset->GetSelection() == 0 )
343  m_FileDrillOffset = wxPoint( 0, 0 );
344  else
346 
347  if( m_UnitDrillIsInch )
349  else
351 
353  {
356  }
357 }
358 
359 
360 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
361 {
362  UpdateConfig(); // set params and Save drill options
363 
366 
367  const PLOT_FORMAT filefmt[6] = {
368  // Keep these format ids in the same order than m_Choice_Drill_Map choices
371  };
372 
373  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
374 
375  if( choice >= arrayDim( filefmt ) )
376  choice = 1;
377 
378  // Create output directory if it does not exist (also transform it in
379  // absolute form). Bail if it fails
381  wxFileName outputDir = wxFileName::DirName( path );
382  wxString boardFilename = m_board->GetFileName();
383 
384  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
385  {
386  wxString msg;
387  msg.Printf( _( "Could not write drill and/or map files to folder '%s'." ),
388  outputDir.GetPath() );
389  DisplayError( this, msg );
390  return;
391  }
392 
393  if( m_drillFileType == 0 )
394  {
395  EXCELLON_WRITER excellonWriter( m_board );
400  excellonWriter.SetMapFileFormat( filefmt[choice] );
401 
402  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
403  &reporter );
404  }
405  else
406  {
407  GERBER_WRITER gerberWriter( m_board );
408  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
409  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
410  // the integer part precision is always 4, and units always mm
411  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
412  gerberWriter.SetOptions( m_FileDrillOffset );
413  gerberWriter.SetMapFileFormat( filefmt[choice] );
414 
415  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
416  aGenDrill, aGenMap, &reporter );
417  }
418 }
419 
420 
421 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
422 {
423  UpdateConfig(); // set params and Save drill options
424 
425  wxFileName fn = m_board->GetFileName();
426 
427  fn.SetName( fn.GetName() + wxT( "-drl" ) );
428  fn.SetExt( ReportFileExtension );
429 
430  wxString defaultPath = ExpandEnvVarSubstitutions( m_plotOpts.GetOutputDirectory(), &Prj() );
431  defaultPath = Prj().AbsolutePath( defaultPath );
432 
433  if( defaultPath.IsEmpty() )
434  defaultPath = PATHS::GetDefaultUserProjectsPath();
435 
436  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
437  ReportFileWildcard(), wxFD_SAVE );
438 
439  if( dlg.ShowModal() == wxID_CANCEL )
440  return;
441 
442  bool success;
443 
444  // Info is slightly different between Excellon and Gerber
445  // (file ext, Merge PTH/NPTH option)
446  if( m_drillFileType == 0 )
447  {
448  EXCELLON_WRITER excellonWriter( m_board );
449  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
450  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
451  }
452  else
453  {
454  GERBER_WRITER gerberWriter( m_board );
455  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
456  }
457 
458  wxString msg;
459 
460  if( ! success )
461  {
462  msg.Printf( _( "** Unable to create %s **\n" ), dlg.GetPath() );
463  m_messagesBox->AppendText( msg );
464  }
465  else
466  {
467  msg.Printf( _( "Report file %s created\n" ), dlg.GetPath() );
468  m_messagesBox->AppendText( msg );
469  }
470 }
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:252
wxStaticText * m_PlatedPadsCountInfoMsg
void OnModify() override
Must be called after a board change to set the modified flag.
void SetRouteModeForOvalHoles(bool aUseRouteModeForOvalHoles)
void OnGenDrillFile(wxCommandEvent &event) override
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: board.h:538
void SetUseAuxOrigin(bool aAux)
This file is part of the common library.
wxRadioBox * m_Choice_Drill_Offset
void OnSelZerosFmtSelected(wxCommandEvent &event) override
Create Excellon drill, drill map, and drill report files.
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.
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:279
GERBER_WRITER is a class mainly used to create Gerber drill files.
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:228
wxStaticText * m_NotPlatedPadsCountInfoMsg
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:588
Helper to handle drill precision format in excellon files.
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.
Definition: project.cpp:271
void SetFormat(bool aMetric, ZEROS_FMT aZerosFmt=DECIMAL_FORMAT, int aLeftDigits=0, int aRightDigits=0)
Initialize internal parameters to match the given format.
Board plot function definition file.
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:70
wxRadioBox * m_radioBoxOvalHoleMode
static bool m_MinimalHeader
wxStaticText * m_MicroViasInfoMsg
like PAD_PTH, but not plated
int GetGerberPrecision() const
wxRadioButton * m_rbExcellon
void SetMapFileFormat(PLOT_FORMAT aMapFmt)
Initialize the format for the drill map file.
bool GenDrillReportFile(const wxString &aFullFileName)
Create a plain text report file giving a list of drill values and drill count for through holes,...
bool IsSameAs(const PCB_PLOT_PARAMS &aPcbPlotParams) const
Compare current settings to aPcbPlotParams, including not saved parameters in brd file.
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:307
FOOTPRINTS & Footprints()
Definition: board.h:233
Generic, UI-independent tool event.
Definition: tool_event.h:152
wxRadioBox * m_Choice_Zeros_Format
Definition of file extensions used in Kicad.
#define _(s)
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:539
void SetOutputDirectory(wxString aDir)
static DRILL_PRECISION precisionListForMetric(3, 3)
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
virtual void ClearMsgPanel()
Clear all messages from the message panel.
DIALOG_GENDRILL(PCB_EDIT_FRAME *aPcbEditFrame, wxWindow *aParent)
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
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:130
DRILL_PRECISION m_Precision
void onFileFormatSelection(wxCommandEvent &event) override
static int m_drillFileType
const std::string ReportFileExtension
The main frame for Pcbnew.
PCBNEW_SETTINGS * GetPcbNewSettings() const
Classes used in drill files, map files and report files generation.
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=nullptr)
Create the full set of Excellon drill file for the board.
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:57
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
void SetOptions(bool aMirror, bool aMinimalHeader, wxPoint aOffset, bool aMerge_PTH_NPTH)
Initialize internal parameters to match drill options.
void OnGenMapFile(wxCommandEvent &event) override
void GenDrillAndMapFiles(bool aGenDrill, bool aGenMap)
Call the functions to create EXCELLON drill files and/or drill map files.
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:230
static DRILL_PRECISION precisionListForInches(2, 4)
PCB_EDIT_FRAME * m_pcbEditFrame