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 <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 config
51 #define ZerosFormatKey wxT( "DrillZerosFormat" )
52 #define PrecisionKey wxT( "DrilltPrecisionOpt" )
53 #define MirrorKey wxT( "DrillMirrorYOpt" )
54 #define MinimalHeaderKey wxT( "DrillMinHeader" )
55 #define MergePTHNPTHKey wxT( "DrillMergePTHNPTH" )
56 #define UnitDrillInchKey wxT( "DrillUnit" )
57 #define DrillOriginIsAuxAxisKey wxT( "DrillAuxAxis" )
58 #define DrillMapFileTypeKey wxT( "DrillMapFileType" )
59 #define DrillFileFormatKey wxT( "DrillFileType" )
60 
61 // list of allowed precision for EXCELLON files, for integer format:
62 // Due to difference between inches and mm,
63 // there are 2 precision values, one for inches and one for metric
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 );
73 
74  dlg.ShowModal();
75 }
76 
77 
79  DIALOG_GENDRILL_BASE( parent )
80 {
81  m_parent = parent;
82  m_board = parent->GetBoard();
85 
86  SetReturnCode( 1 );
87  initDialog();
88  GetSizer()->SetSizeHints( this );
89 }
90 
91 
92 // Static members of DIALOG_GENDRILL
93 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
95 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
96 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
97 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
100 
101 
103 {
104  UpdateConfig();
105 }
106 
107 
109 {
111  m_config->Read( MirrorKey, &m_Mirror );
118 
120 }
121 
122 
124 {
125  m_browseButton->SetBitmap( KiBitmap( browse_files_xpm ) );
126 
127  m_rbFileFormat->SetSelection( m_drillFileType );
128  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
129  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
131  m_Check_Minimal->SetValue( m_MinimalHeader );
132 
134  m_Choice_Drill_Offset->SetSelection( 1 );
135 
136  m_Check_Mirror->SetValue( m_Mirror );
138  m_Choice_Drill_Map->SetSelection( m_mapFileType );
139  m_ViaDrillValue->SetLabel( _( "Use Netclass values" ) );
140  m_MicroViaDrillValue->SetLabel( _( "Use Netclass values" ) );
141 
142  // See if we have some buried vias or/and microvias, and display
143  // microvias drill value if so
144  m_throughViasCount = 0;
145  m_microViasCount = 0;
147 
148  for( TRACK* track = m_parent->GetBoard()->m_Track; track != NULL; track = track->Next() )
149  {
150  const VIA *via = dynamic_cast<const VIA*>( track );
151  if( via )
152  {
153  switch( via->GetViaType() )
154  {
155  case VIA_THROUGH:
157  break;
158 
159  case VIA_MICROVIA:
161  break;
162 
163  case VIA_BLIND_BURIED:
165  break;
166 
167  default:
168  break;
169  }
170  }
171  }
172 
174 
175  // Count plated pad holes and not plated pad holes:
178 
179  for( MODULE* module = m_parent->GetBoard()->m_Modules; module; module = module->Next() )
180  {
181  for( D_PAD* pad = module->PadsList(); pad != NULL; pad = pad->Next() )
182  {
183  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
184  {
185  if( pad->GetDrillSize().x != 0 )
186  {
187  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
189  else
191  }
192  }
193  else
194  {
195  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
196  {
197  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
199  else
201  }
202  }
203  }
204  }
205 
206  // Display hole counts:
207  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
208  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
209  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
210  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
211  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
212 
213  // Output directory
215 
216  wxCommandEvent dummy;
217  onFileFormatSelection( dummy );
218 }
219 
220 
221 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
222 {
223  m_drillFileType = m_rbFileFormat->GetSelection();
224  bool enbl_Excellon = m_drillFileType == 0;
225 
226  m_Choice_Unit->Enable( enbl_Excellon );
227  m_Choice_Zeros_Format->Enable( enbl_Excellon );
228  m_Check_Mirror->Enable( enbl_Excellon );
229  m_Check_Minimal->Enable( enbl_Excellon );
230  m_Check_Merge_PTH_NPTH->Enable( enbl_Excellon );
231 
232  if( enbl_Excellon )
234  else
235  {
236  m_staticTextPrecision->Enable( true );
237  m_staticTextPrecision->SetLabel( m_plotOpts.GetGerberPrecision() == 6 ? "4.6" : "4.5" );
238  }
239 }
240 
241 
243 {
244  SetParams();
245 
247  m_config->Write( MirrorKey, m_Mirror );
253 }
254 
255 
256 void DIALOG_GENDRILL::OnSelDrillUnitsSelected( wxCommandEvent& event )
257 {
259 }
260 
261 
262 void DIALOG_GENDRILL::OnGenMapFile( wxCommandEvent& event )
263 {
264  GenDrillAndMapFiles( false, true);
265 }
266 
267 
268 void DIALOG_GENDRILL::OnGenDrillFile( wxCommandEvent& event )
269 {
270  GenDrillAndMapFiles(true, false);
271 }
272 
273 
274 void DIALOG_GENDRILL::OnCancelClick( wxCommandEvent& event )
275 {
276  UpdateConfig(); // Save drill options:
277  EndModal( wxID_CANCEL ); // Process the default cancel event (close dialog)
278 }
279 
280 
281 void DIALOG_GENDRILL::OnSelZerosFmtSelected( wxCommandEvent& event )
282 {
284 }
285 
286 
288 {
289  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
291  else
292  {
293  // metric options
295  }
296 
298  m_staticTextPrecision->Enable( false );
299  else
300  m_staticTextPrecision->Enable( true );
301 }
302 
303 
305 {
306  // Build the absolute path of current output plot directory
307  // to preselect it when opening the dialog.
308  wxFileName fn( m_outputDirectoryName->GetValue() );
309  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
310 
311  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
312 
313  if( dirDialog.ShowModal() == wxID_CANCEL )
314  return;
315 
316  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
317 
318  fn = Prj().AbsolutePath( m_parent->GetBoard()->GetFileName() );
319  wxString defaultPath = fn.GetPathWithSep();
320  wxString msg;
321  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
322 
323  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
324  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
325 
326  if( dialog.ShowModal() == wxID_YES )
327  {
328  if( !dirName.MakeRelativeTo( defaultPath ) )
329  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
330  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
331  }
332 
333  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
334 }
335 
336 
338 {
339  wxString msg;
340 
341  // Set output directory and replace backslashes with forward ones
342  wxString dirStr;
343  dirStr = m_outputDirectoryName->GetValue();
344  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
345  m_plotOpts.SetOutputDirectory( dirStr );
348 
349  m_mapFileType = m_Choice_Drill_Map->GetSelection();
350 
351  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
352  m_MinimalHeader = m_Check_Minimal->IsChecked();
353  m_Mirror = m_Check_Mirror->IsChecked();
355  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
356 
357  if( m_Choice_Drill_Offset->GetSelection() == 0 )
358  m_FileDrillOffset = wxPoint( 0, 0 );
359  else
361 
362  if( m_UnitDrillIsInch )
364  else
366 
368 }
369 
370 
371 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
372 {
373  UpdateConfig(); // set params and Save drill options
374 
377 
378  const PlotFormat filefmt[6] =
379  { // Keep these format ids in the same order than m_Choice_Drill_Map choices
382  };
383 
384  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
385 
386  if( choice >= DIM( filefmt ) )
387  choice = 1;
388 
389  // Create output directory if it does not exist (also transform it in
390  // absolute form). Bail if it fails
391  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
392  wxString boardFilename = m_parent->GetBoard()->GetFileName();
393 
394  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
395  {
396  wxString msg;
397  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
398  GetChars( outputDir.GetPath() ) );
399  DisplayError( this, msg );
400  return;
401  }
402 
403  if( m_drillFileType == 0 )
404  {
405  EXCELLON_WRITER excellonWriter( m_parent->GetBoard() );
408  excellonWriter.SetOptions( m_Mirror, m_MinimalHeader, m_FileDrillOffset, m_Merge_PTH_NPTH );
409  excellonWriter.SetMapFileFormat( filefmt[choice] );
410 
411  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
412  aGenDrill, aGenMap, &reporter );
413  }
414  else
415  {
416  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
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_parent->GetBoard()->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_parent->GetBoard() );
457  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
458  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
459  }
460  else
461  {
462  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
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
#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
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:121
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
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.
const wxString ReportFileExtension
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:103
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.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
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:455
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
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&#39;s directory to be an absolu...
Definition: project.cpp:380
D_PAD * Next() const
Definition: class_pad.h:160
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)
wxBitmapButton * m_browseButton
static bool m_Mirror
DRILL_PRECISION m_Precision
TRACK * Next() const
Definition: class_track.h:99
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)
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: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:241
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:781
PCB_EDIT_FRAME * m_parent
static DRILL_PRECISION precisionListForInches(2, 4)
int GetGerberPrecision() const