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/msgdlg.h>
45 #include <wx/dirdlg.h>
46 #include <wx/filedlg.h>
47 
48 // list of allowed precision for EXCELLON files, for integer format:
49 // Due to difference between inches and mm,
50 // there are 2 precision values, one for inches and one for metric
51 // Note: for decimla format, the precision is not used
54 
55 
56 /* This function displays the dialog frame for drill tools
57  */
59 {
60  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
61  DIALOG_GENDRILL dlg( editFrame, editFrame );
62 
63  dlg.ShowModal();
64  return 0;
65 }
66 
67 
68 DIALOG_GENDRILL::DIALOG_GENDRILL( PCB_EDIT_FRAME* aPcbEditFrame, wxWindow* aParent ) :
69  DIALOG_GENDRILL_BASE( aParent )
70 {
71  m_pcbEditFrame = aPcbEditFrame;
74 
75  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
76  // that requires us to correct the button labels here.
77  m_sdbSizerOK->SetLabel( _( "Generate Drill File" ) );
78  m_sdbSizerApply->SetLabel( _( "Generate Map File" ) );
79  m_sdbSizerCancel->SetLabel( _( "Close" ) );
80  m_buttonsSizer->Layout();
81 
82  m_sdbSizerOK->SetDefault();
83  SetReturnCode( 1 );
84  initDialog();
85  GetSizer()->SetSizeHints( this );
86 }
87 
88 
89 // Static members of DIALOG_GENDRILL
90 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
92 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
93 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
94 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
97 bool DIALOG_GENDRILL::m_UseRouteModeForOvalHoles = true; // Use G00 route mode to "drill" oval holes
98 
100 {
101 }
102 
103 
105 {
106  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
107 
108  m_Merge_PTH_NPTH = cfg->m_GenDrill.merge_pth_npth;
109  m_MinimalHeader = cfg->m_GenDrill.minimal_header;
110  m_Mirror = cfg->m_GenDrill.mirror;
111  m_UnitDrillIsInch = cfg->m_GenDrill.unit_drill_is_inch;
112  m_UseRouteModeForOvalHoles = cfg->m_GenDrill.use_route_for_oval_holes;
113  m_drillFileType = cfg->m_GenDrill.drill_file_type;
114  m_mapFileType = cfg->m_GenDrill.map_file_type;
115  m_ZerosFormat = cfg->m_GenDrill.zeros_format;
116 
118 
120 }
121 
122 
124 {
126 
127  m_rbExcellon->SetValue( m_drillFileType == 0 );
128  m_rbGerberX2->SetValue( m_drillFileType == 1 );
129  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
130  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
132  m_Check_Minimal->SetValue( m_MinimalHeader );
133 
134  m_Choice_Drill_Offset->SetSelection( m_drillOriginIsAuxAxis ? 1 : 0 );
135 
136  m_Check_Mirror->SetValue( m_Mirror );
138  m_Choice_Drill_Map->SetSelection( m_mapFileType );
139  m_radioBoxOvalHoleMode->SetSelection( m_UseRouteModeForOvalHoles ? 0 : 1 );
140 
143  m_throughViasCount = 0;
144  m_microViasCount = 0;
146 
147  for( FOOTPRINT* footprint : m_board->Footprints() )
148  {
149  for( PAD* pad : footprint->Pads() )
150  {
151  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
152  {
153  if( pad->GetDrillSize().x != 0 )
154  {
155  if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
157  else
159  }
160  }
161  else
162  {
163  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
164  {
165  if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
167  else
169  }
170  }
171  }
172  }
173 
174  for( PCB_TRACK* track : m_board->Tracks() )
175  {
176  const PCB_VIA *via = dynamic_cast<const PCB_VIA*>( track );
177 
178  if( via )
179  {
180  switch( via->GetViaType() )
181  {
182  case VIATYPE::THROUGH: m_throughViasCount++; break;
183  case VIATYPE::MICROVIA: m_microViasCount++; break;
185  default: break;
186  }
187  }
188  }
189 
190  // Display hole counts:
191  m_PlatedPadsCountInfoMsg->SetLabel( wxString() << m_platedPadsHoleCount );
192  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
193  m_ThroughViasInfoMsg->SetLabel( wxString() << m_throughViasCount );
194  m_MicroViasInfoMsg->SetLabel( wxString() << m_microViasCount );
195  m_BuriedViasInfoMsg->SetLabel( wxString() << m_blindOrBuriedViasCount );
196 
197  // Output directory
199 
200  wxCommandEvent dummy;
202 }
203 
204 
205 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
206 {
207  bool enbl_Excellon = m_rbExcellon->GetValue();
208 
209  m_drillFileType = enbl_Excellon ? 0 : 1;
210 
211  m_Choice_Unit->Enable( enbl_Excellon );
212  m_Choice_Zeros_Format->Enable( enbl_Excellon );
213  m_Check_Mirror->Enable( enbl_Excellon );
214  m_Check_Minimal->Enable( enbl_Excellon );
215  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
216  m_radioBoxOvalHoleMode->Enable( enbl_Excellon );
217 
218  if( enbl_Excellon )
219  {
221  }
222  else
223  {
224  m_staticTextPrecision->Enable( true );
225  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
226  }
227 }
228 
229 
231 {
233 
234  auto cfg = m_pcbEditFrame->GetPcbNewSettings();
235 
237  cfg->m_GenDrill.minimal_header = m_MinimalHeader;
238  cfg->m_GenDrill.mirror = m_Mirror;
239  cfg->m_GenDrill.unit_drill_is_inch = m_UnitDrillIsInch;
240  cfg->m_GenDrill.use_route_for_oval_holes = m_UseRouteModeForOvalHoles;
241  cfg->m_GenDrill.drill_file_type = m_drillFileType;
242  cfg->m_GenDrill.map_file_type = m_mapFileType;
243  cfg->m_GenDrill.zeros_format = m_ZerosFormat;
244 }
245 
246 
247 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
248 {
250 }
251 
252 
253 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
254 {
255  GenDrillAndMapFiles( false, true );
256 }
257 
258 
259 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
260 {
261  GenDrillAndMapFiles( true, false );
262 }
263 
264 
265 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
266 {
268 }
269 
270 
272 {
273  if( m_Choice_Unit->GetSelection()== 1 )
274  {
275  // Units = inches
277  }
278  else
279  {
280  // metric options
282  }
283 
285  m_staticTextPrecision->Enable( false );
286  else
287  m_staticTextPrecision->Enable( true );
288 }
289 
290 
292 {
293  // Build the absolute path of current output directory to preselect it in the file browser.
294  wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
295  path = Prj().AbsolutePath( path );
296 
297  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
298 
299  if( dirDialog.ShowModal() == wxID_CANCEL )
300  return;
301 
302  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
303  wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) );
304  wxString defaultPath = fn.GetPathWithSep();
305  wxString msg;
306  msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath );
307 
308  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
309  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
310 
311  if( dialog.ShowModal() == wxID_YES )
312  {
313  if( !dirName.MakeRelativeTo( defaultPath ) )
314  {
315  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
316  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
317  }
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 );
333  m_drillOriginIsAuxAxis = m_Choice_Drill_Offset->GetSelection() == 1;
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_DrillFileOffset = wxPoint( 0, 0 );
347  else
349 
350  if( m_UnitDrillIsInch )
352  else
354 
356  {
359  }
360 }
361 
362 
363 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
364 {
365  UpdateConfig(); // set params and Save drill options
366 
369 
370  const PLOT_FORMAT filefmt[6] = {
371  // Keep these format ids in the same order than m_Choice_Drill_Map choices
378  };
379 
380  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
381 
382  if( choice >= arrayDim( filefmt ) )
383  choice = 1;
384 
385  // Create output directory if it does not exist (also transform it in
386  // absolute form). Bail if it fails
388  wxFileName outputDir = wxFileName::DirName( path );
389  wxString boardFilename = m_board->GetFileName();
390 
391  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
392  {
393  wxString msg;
394  msg.Printf( _( "Could not write drill and/or map files to folder '%s'." ),
395  outputDir.GetPath() );
396  DisplayError( this, msg );
397  return;
398  }
399 
400  if( m_drillFileType == 0 )
401  {
402  EXCELLON_WRITER excellonWriter( m_board );
407  excellonWriter.SetMapFileFormat( filefmt[choice] );
408 
409  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
410  &reporter );
411  }
412  else
413  {
414  GERBER_WRITER gerberWriter( m_board );
415  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
416  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
417  // the integer part precision is always 4, and units always mm
418  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
419  gerberWriter.SetOptions( m_DrillFileOffset );
420  gerberWriter.SetMapFileFormat( filefmt[choice] );
421 
422  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(), aGenDrill, aGenMap,
423  &reporter );
424  }
425 }
426 
427 
428 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
429 {
430  UpdateConfig(); // set params and Save drill options
431 
432  wxFileName fn = m_board->GetFileName();
433 
434  fn.SetName( fn.GetName() + wxT( "-drl" ) );
435  fn.SetExt( ReportFileExtension );
436 
437  wxString defaultPath = ExpandEnvVarSubstitutions( m_plotOpts.GetOutputDirectory(), &Prj() );
438  defaultPath = Prj().AbsolutePath( defaultPath );
439 
440  if( defaultPath.IsEmpty() )
441  defaultPath = PATHS::GetDefaultUserProjectsPath();
442 
443  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
444  ReportFileWildcard(), wxFD_SAVE );
445 
446  if( dlg.ShowModal() == wxID_CANCEL )
447  return;
448 
449  bool success;
450 
451  // Info is slightly different between Excellon and Gerber
452  // (file ext, Merge PTH/NPTH option)
453  if( m_drillFileType == 0 )
454  {
455  EXCELLON_WRITER excellonWriter( m_board );
456  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
457  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
458  }
459  else
460  {
461  GERBER_WRITER gerberWriter( m_board );
462  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
463  }
464 
465  wxString msg;
466 
467  if( ! success )
468  {
469  msg.Printf( _( "Failed to create file '%s'." ), dlg.GetPath() );
470  m_messagesBox->AppendText( msg );
471  }
472  else
473  {
474  msg.Printf( _( "Report file '%s' created." ), dlg.GetPath() );
475  m_messagesBox->AppendText( msg );
476  }
477 }
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:271
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.
void SetOutputDirectory(const wxString &aDir)
static bool m_Merge_PTH_NPTH
static int m_ZerosFormat
static bool m_UseRouteModeForOvalHoles
void SetMergeOption(bool aMerge)
Set the option to make separate drill files for PTH and NPTH.
wxPoint m_DrillFileOffset
void SetFormat(int aRightDigits=6)
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
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:589
Helper to handle drill precision format in excellon files.
A wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:137
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:269
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:72
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
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=nullptr)
Create the full set of Excellon drill file for the board filenames are computed from the board name,...
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
void SetOptions(bool aMirror, bool aMinimalHeader, const wxPoint &aOffset, bool aMerge_PTH_NPTH)
Initialize internal parameters to match drill options.
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: board.h:539
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 SetOptions(const wxPoint &aOffset)
Initialize internal parameters to match drill options.
static bool m_Mirror
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:139
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 OnGenMapFile(wxCommandEvent &event) override
void GenDrillAndMapFiles(bool aGenDrill, bool aGenMap)
Call the functions to create EXCELLON drill files and/or drill map files.
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