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-2017 Jean_Pierre Charras <jp.charras at wanadoo.fr>
9  * Copyright (C) 1992-2017 KiCad Developers, see change_log.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 <wxPcbStruct.h>
34 #include <pcbplot.h>
36 #include <gendrill_gerber_writer.h>
37 
38 #include <class_board.h>
39 #include <class_track.h>
40 #include <class_module.h>
41 
42 #include <dialog_gendrill.h>
44 #include <reporter.h>
45 
46 #include <wx/stdpaths.h>
47 
48 
49 // Keywords for read and write config
50 #define ZerosFormatKey wxT( "DrillZerosFormat" )
51 #define PrecisionKey wxT( "DrilltPrecisionOpt" )
52 #define MirrorKey wxT( "DrillMirrorYOpt" )
53 #define MinimalHeaderKey wxT( "DrillMinHeader" )
54 #define MergePTHNPTHKey wxT( "DrillMergePTHNPTH" )
55 #define UnitDrillInchKey wxT( "DrillUnit" )
56 #define DrillOriginIsAuxAxisKey wxT( "DrillAuxAxis" )
57 #define DrillMapFileTypeKey wxT( "DrillMapFileType" )
58 #define DrillFileFormatKey wxT( "DrillFileType" )
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
65 
66 
67 /* This function displays the dialog frame for drill tools
68  */
69 void PCB_EDIT_FRAME::InstallDrillFrame( wxCommandEvent& event )
70 {
71  DIALOG_GENDRILL dlg( this );
72 
73  dlg.ShowModal();
74 }
75 
76 
78  DIALOG_GENDRILL_BASE( parent )
79 {
80  m_parent = parent;
81  m_board = parent->GetBoard();
84 
85  SetReturnCode( 1 );
86  initDialog();
87  GetSizer()->SetSizeHints( this );
88 }
89 
90 
91 // Static members of DIALOG_GENDRILL
92 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
94 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
95 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
96 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
99 
100 
102 {
103  UpdateConfig();
104 }
105 
106 
108 {
110  m_config->Read( MirrorKey, &m_Mirror );
117 
119 }
120 
121 
123 {
124  wxString msg;
125 
126  m_rbFileFormat->SetSelection( m_drillFileType );
127  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
128  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
130  m_Check_Minimal->SetValue( m_MinimalHeader );
131 
133  m_Choice_Drill_Offset->SetSelection( 1 );
134 
135  m_Check_Mirror->SetValue( m_Mirror );
137  m_Choice_Drill_Map->SetSelection( m_mapFileType );
138  m_ViaDrillValue->SetLabel( _( "Use Netclass values" ) );
139  m_MicroViaDrillValue->SetLabel( _( "Use Netclass values" ) );
140 
141  // See if we have some buried vias or/and microvias, and display
142  // microvias drill value if so
143  m_throughViasCount = 0;
144  m_microViasCount = 0;
146 
147  for( TRACK* track = m_parent->GetBoard()->m_Track; track != NULL; track = track->Next() )
148  {
149  const VIA *via = dynamic_cast<const VIA*>( track );
150  if( via )
151  {
152  switch( via->GetViaType() )
153  {
154  case VIA_THROUGH:
156  break;
157 
158  case VIA_MICROVIA:
160  break;
161 
162  case VIA_BLIND_BURIED:
164  break;
165 
166  default:
167  break;
168  }
169  }
170  }
171 
173 
174  // Count plated pad holes and not plated pad holes:
177 
178  for( MODULE* module = m_parent->GetBoard()->m_Modules; module; module = module->Next() )
179  {
180  for( D_PAD* pad = module->PadsList(); pad != NULL; pad = pad->Next() )
181  {
182  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
183  {
184  if( pad->GetDrillSize().x != 0 )
185  {
186  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
188  else
190  }
191  }
192  else
193  {
194  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
195  {
196  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
198  else
200  }
201  }
202  }
203  }
204 
205  // Display hole counts:
206  msg = m_PlatedPadsCountInfoMsg->GetLabel();
207  msg << wxT( " " ) << m_platedPadsHoleCount;
208  m_PlatedPadsCountInfoMsg->SetLabel( msg );
209 
210  msg = m_NotPlatedPadsCountInfoMsg->GetLabel();
211  msg << wxT( " " ) << m_notplatedPadsHoleCount;
212  m_NotPlatedPadsCountInfoMsg->SetLabel( msg );
213 
214  msg = m_ThroughViasInfoMsg->GetLabel();
215  msg << wxT( " " ) << m_throughViasCount;
216  m_ThroughViasInfoMsg->SetLabel( msg );
217 
218  msg = m_MicroViasInfoMsg->GetLabel();
219  msg << wxT( " " ) << m_microViasCount;
220  m_MicroViasInfoMsg->SetLabel( msg );
221 
222  msg = m_BuriedViasInfoMsg->GetLabel();
223  msg << wxT( " " ) << m_blindOrBuriedViasCount;
224  m_BuriedViasInfoMsg->SetLabel( msg );
225 
226  // Output directory
228 
229  wxCommandEvent dummy;
230  onFileFormatSelection( dummy );
231 }
232 
233 
234 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
235 {
236  m_drillFileType = m_rbFileFormat->GetSelection();
237  bool enbl_Excellon = m_drillFileType == 0;
238 
239  m_Choice_Unit->Enable( enbl_Excellon );
240  m_Choice_Zeros_Format->Enable( enbl_Excellon );
241  m_Check_Mirror->Enable( enbl_Excellon );
242  m_Check_Minimal->Enable( enbl_Excellon );
243  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
244 
245  if( enbl_Excellon )
247  else
248  {
249  m_staticTextPrecision->Enable( true );
250  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
251  }
252 }
253 
254 
256 {
257  SetParams();
258 
260  m_config->Write( MirrorKey, m_Mirror );
266 }
267 
268 
269 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
270 {
272 }
273 
274 
275 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
276 {
277  GenDrillAndMapFiles( false, true);
278 }
279 
280 
281 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
282 {
283  GenDrillAndMapFiles(true, false);
284 }
285 
286 
287 void DIALOG_GENDRILL::OnCancelClick( wxCommandEvent& event )
288 {
289  UpdateConfig(); // Save drill options:
290  EndModal( wxID_CANCEL ); // Process the default cancel event (close dialog)
291 }
292 
293 
294 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
295 {
297 }
298 
299 
301 {
302  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
304  else
305  {
306  // metric options
308  }
309 
311  m_staticTextPrecision->Enable( false );
312  else
313  m_staticTextPrecision->Enable( true );
314 }
315 
316 
318 {
319  // Build the absolute path of current output plot directory
320  // to preselect it when opening the dialog.
321  wxFileName fn( m_outputDirectoryName->GetValue() );
322  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
323 
324  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
325 
326  if( dirDialog.ShowModal() == wxID_CANCEL )
327  return;
328 
329  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
330 
331  fn = Prj().AbsolutePath( m_parent->GetBoard()->GetFileName() );
332  wxString defaultPath = fn.GetPathWithSep();
333  wxString msg;
334  msg.Printf( _( "Do you want to use a path relative to\n'%s'" ), GetChars( defaultPath ) );
335 
336  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
337  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
338 
339  if( dialog.ShowModal() == wxID_YES )
340  {
341  if( !dirName.MakeRelativeTo( defaultPath ) )
342  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
343  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
344  }
345 
346  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
347 }
348 
349 
351 {
352  wxString msg;
353 
354  // Set output directory and replace backslashes with forward ones
355  wxString dirStr;
356  dirStr = m_outputDirectoryName->GetValue();
357  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
358  m_plotOpts.SetOutputDirectory( dirStr );
361 
362  m_mapFileType = m_Choice_Drill_Map->GetSelection();
363 
364  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
365  m_MinimalHeader = m_Check_Minimal->IsChecked();
366  m_Mirror = m_Check_Mirror->IsChecked();
368  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
369 
370  if( m_Choice_Drill_Offset->GetSelection() == 0 )
371  m_FileDrillOffset = wxPoint( 0, 0 );
372  else
374 
375  if( m_UnitDrillIsInch )
377  else
379 
381 }
382 
383 
384 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
385 {
386  UpdateConfig(); // set params and Save drill options
387 
390 
391  const PlotFormat filefmt[6] =
392  { // Keep these format ids in the same order than m_Choice_Drill_Map choices
395  };
396 
397  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
398 
399  if( choice >= DIM( filefmt ) )
400  choice = 1;
401 
402  // Create output directory if it does not exist (also transform it in
403  // absolute form). Bail if it fails
404  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
405  wxString boardFilename = m_parent->GetBoard()->GetFileName();
406 
407  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
408  {
409  wxString msg;
410  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
411  GetChars( outputDir.GetPath() ) );
412  DisplayError( this, msg );
413  return;
414  }
415 
416  if( m_drillFileType == 0 )
417  {
418  EXCELLON_WRITER excellonWriter( m_parent->GetBoard() );
421  excellonWriter.SetOptions( m_Mirror, m_MinimalHeader, m_FileDrillOffset, m_Merge_PTH_NPTH );
422  excellonWriter.SetMapFileFormat( filefmt[choice] );
423 
424  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
425  aGenDrill, aGenMap, &reporter );
426  }
427  else
428  {
429  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
430  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
431  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
432  // the integer part precision is always 4, and units always mm
433  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
434  gerberWriter.SetOptions( m_FileDrillOffset );
435  gerberWriter.SetMapFileFormat( filefmt[choice] );
436 
437  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
438  aGenDrill, aGenMap, &reporter );
439  }
440 }
441 
442 
443 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
444 {
445  UpdateConfig(); // set params and Save drill options
446 
447  wxFileName fn = m_parent->GetBoard()->GetFileName();
448 
449  fn.SetName( fn.GetName() + wxT( "-drl" ) );
450  fn.SetExt( ReportFileExtension );
451 
452  wxString defaultPath = Prj().AbsolutePath( m_plotOpts.GetOutputDirectory() );
453 
454  if( defaultPath.IsEmpty() )
455  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
456 
457  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
458  wxGetTranslation( ReportFileWildcard ), wxFD_SAVE );
459 
460  if( dlg.ShowModal() == wxID_CANCEL )
461  return;
462 
463  bool success;
464 
465  // Info is slightly different between Excellon and Gerber
466  // (file ext, Merge PTH/NPTH option)
467  if( m_drillFileType == 0 )
468  {
469  EXCELLON_WRITER excellonWriter( m_parent->GetBoard() );
470  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
471  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
472  }
473  else
474  {
475  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
476  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
477  }
478 
479  wxString msg;
480 
481  if( ! success )
482  {
483  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( dlg.GetPath() ) );
484  m_messagesBox->AppendText( msg );
485  }
486  else
487  {
488  msg.Printf( _( "Report file %s created\n" ), GetChars( dlg.GetPath() ) );
489  m_messagesBox->AppendText( msg );
490  }
491 }
#define DrillMapFileTypeKey
#define UnitDrillInchKey
static int m_UnitDrillIsInch
#define DIM(x)
of elements in an array
Definition: macros.h:98
wxStaticText * m_PlatedPadsCountInfoMsg
void OnGenDrillFile(wxCommandEvent &event) override
wxConfigBase * m_config
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:65
void SetUseAuxOrigin(bool aAux)
This file is part of the common library.
#define MinimalHeaderKey
PlotFormat
Enum PlotFormat is the set of supported output plot formats.
Definition: plot_common.h:49
wxRadioBox * m_Choice_Drill_Offset
#define MergePTHNPTHKey
void OnSelZerosFmtSelected(wxCommandEvent &event) override
Class BOARD to handle a board.
MODULE * Next() const
Definition: class_module.h:100
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
BOARD * GetBoard() const
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
bool GetUseAuxOrigin() const
#define DrillFileFormatKey
GERBER_WRITER is a class mainly used to create Gerber drill files.
wxStaticText * m_NotPlatedPadsCountInfoMsg
wxStaticText * m_ViaDrillValue
virtual const PCB_PLOT_PARAMS & GetPlotSettings() const
Function GetPlotSettings returns the PCB_PLOT_PARAMS for the BOARD owned by this frame.
const wxPoint & GetAuxOrigin() const override
Function GetAuxOrigin returns 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:97
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.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
static bool m_MinimalHeader
wxStaticText * m_MicroViasInfoMsg
wxString GetOutputDirectory() const
bool GenDrillReportFile(const wxString &aFullFileName)
Function GenDrillReportFile Create a plain text report file giving a list of drill values and drill c...
VIATYPE_T GetViaType() const
Definition: class_track.h:439
wxStaticText * m_BuriedViasInfoMsg
const wxString & GetFileName() const
Definition: class_board.h:234
wxStaticText * m_ThroughViasInfoMsg
void OnCancelClick(wxCommandEvent &event) override
wxTextCtrl * m_outputDirectoryName
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter)
Helper function EnsureFileDirectoryExists make aTargetFullFileName absolute and creates the path of t...
Definition: common.cpp:267
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:371
D_PAD * Next() const
Definition: class_pad.h:145
wxRadioBox * m_Choice_Zeros_Format
The common library.
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
PCB_PLOT_PARAMS m_plotOpts
void SetPlotOptions(const PCB_PLOT_PARAMS &aOptions)
Definition: class_board.h:551
void SetOutputDirectory(wxString aDir)
static DRILL_PRECISION precisionListForMetric(3, 3)
static bool m_Mirror
DRILL_PRECISION m_Precision
TRACK * Next() const
Definition: class_track.h:98
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
wxStaticText * m_MicroViaDrillValue
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...
DLIST< MODULE > m_Modules
Definition: class_board.h:245
void InitDisplayParams(void)
const wxString ReportFileExtension
Classes used in drill files, map files and report files generation.
wxStaticText * m_staticTextPrecision
const wxString ReportFileWildcard
Classes used in drill files, map files and report files generation.
void OnGenReportFile(wxCommandEvent &event) override
DLIST< TRACK > m_Track
Definition: class_board.h:246
Module description (excepted pads)
DIALOG_GENDRILL(PCB_EDIT_FRAME *parent)
void OnOutputDirectoryBrowseClicked(wxCommandEvent &event) override
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:71
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 ClearMsgPanel(void)
Clear all messages from the message panel.
Definition: draw_frame.cpp:764
PCB_EDIT_FRAME * m_parent
static DRILL_PRECISION precisionListForInches(2, 4)
int GetGerberPrecision() const