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 MirrorKey wxT( "DrillMirrorYOpt" )
53 #define MinimalHeaderKey wxT( "DrillMinHeader" )
54 #define MergePTHNPTHKey wxT( "DrillMergePTHNPTH" )
55 #define UnitDrillInchKey wxT( "DrillUnit" )
56 #define DrillMapFileTypeKey wxT( "DrillMapFileType" )
57 #define DrillFileFormatKey wxT( "DrillFileType" )
58 
59 // list of allowed precision for EXCELLON files, for integer format:
60 // Due to difference between inches and mm,
61 // there are 2 precision values, one for inches and one for metric
64 
65 
66 /* This function displays the dialog frame for drill tools
67  */
68 void PCB_EDIT_FRAME::InstallDrillFrame( wxCommandEvent& event )
69 {
70  DIALOG_GENDRILL dlg( this );
71 
72  dlg.ShowModal();
73 }
74 
75 
77  DIALOG_GENDRILL_BASE( parent )
78 {
79  m_parent = parent;
80  m_board = parent->GetBoard();
83 
84  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
85  // that requires us to correct the button labels here.
86  m_sdbSizer1OK->SetLabel( _( "Generate Drill File" ) );
87  m_sdbSizer1Apply->SetLabel( _( "Generate Map File" ) );
88  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
89  m_buttonsSizer->Layout();
90 
91  m_sdbSizer1OK->SetDefault();
92  SetReturnCode( 1 );
93  initDialog();
94  GetSizer()->SetSizeHints( this );
95 }
96 
97 
98 // Static members of DIALOG_GENDRILL
99 int DIALOG_GENDRILL::m_UnitDrillIsInch = true; // Only for Excellon format
101 bool DIALOG_GENDRILL::m_MinimalHeader = false; // Only for Excellon format
102 bool DIALOG_GENDRILL::m_Mirror = false; // Only for Excellon format
103 bool DIALOG_GENDRILL::m_Merge_PTH_NPTH = false; // Only for Excellon format
106 
107 
109 {
110  UpdateConfig();
111 }
112 
113 
115 {
117  m_config->Read( MirrorKey, &m_Mirror );
124 
126 }
127 
128 
130 {
131  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
132 
133  m_rbExcellon->SetValue( m_drillFileType == 0 );
134  m_rbGerberX2->SetValue( m_drillFileType == 1 );
135  m_Choice_Unit->SetSelection( m_UnitDrillIsInch ? 1 : 0 );
136  m_Choice_Zeros_Format->SetSelection( m_ZerosFormat );
138  m_Check_Minimal->SetValue( m_MinimalHeader );
139 
141  m_Choice_Drill_Offset->SetSelection( 1 );
142 
143  m_Check_Mirror->SetValue( m_Mirror );
145  m_Choice_Drill_Map->SetSelection( m_mapFileType );
146 
149  m_throughViasCount = 0;
150  m_microViasCount = 0;
152 
153  for( MODULE* module = m_parent->GetBoard()->m_Modules; module; module = module->Next() )
154  {
155  for( D_PAD* pad = module->PadsList(); pad != NULL; pad = pad->Next() )
156  {
157  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
158  {
159  if( pad->GetDrillSize().x != 0 )
160  {
161  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
163  else
165  }
166  }
167  else
168  {
169  if( pad->GetDrillSize().x != 0 && pad->GetDrillSize().y != 0 )
170  {
171  if( pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
173  else
175  }
176  }
177  }
178  }
179 
180  for( TRACK* track = m_parent->GetBoard()->m_Track; track != NULL; track = track->Next() )
181  {
182  const VIA *via = dynamic_cast<const VIA*>( track );
183  if( via )
184  {
185  switch( via->GetViaType() )
186  {
187  case VIA_THROUGH:
189  break;
190 
191  case VIA_MICROVIA:
193  break;
194 
195  case VIA_BLIND_BURIED:
197  break;
198 
199  default:
200  break;
201  }
202  }
203  }
204 
205  // Display hole counts:
206  m_PlatedPadsCountInfoMsg-> SetLabel( wxString() << m_platedPadsHoleCount );
207  m_NotPlatedPadsCountInfoMsg->SetLabel( wxString() << m_notplatedPadsHoleCount );
208  m_ThroughViasInfoMsg-> SetLabel( wxString() << m_throughViasCount );
209  m_MicroViasInfoMsg-> SetLabel( wxString() << m_microViasCount );
210  m_BuriedViasInfoMsg-> SetLabel( wxString() << m_blindOrBuriedViasCount );
211 
212  // Output directory
214 
215  wxCommandEvent dummy;
216  onFileFormatSelection( dummy );
217 }
218 
219 
220 void DIALOG_GENDRILL::onFileFormatSelection( wxCommandEvent& event )
221 {
222  bool enbl_Excellon = m_rbExcellon->GetValue();
223 
224  m_drillFileType = enbl_Excellon ? 0 : 1;
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::OnSelZerosFmtSelected( wxCommandEvent& event )
275 {
277 }
278 
279 
281 {
282  if( m_Choice_Unit->GetSelection()== 1 ) // Units = inches
284  else
285  {
286  // metric options
288  }
289 
291  m_staticTextPrecision->Enable( false );
292  else
293  m_staticTextPrecision->Enable( true );
294 }
295 
296 
298 {
299  // Build the absolute path of current output plot directory
300  // to preselect it when opening the dialog.
301  wxFileName fn( m_outputDirectoryName->GetValue() );
302  wxString path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );
303 
304  wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
305 
306  if( dirDialog.ShowModal() == wxID_CANCEL )
307  return;
308 
309  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
310 
311  fn = Prj().AbsolutePath( m_parent->GetBoard()->GetFileName() );
312  wxString defaultPath = fn.GetPathWithSep();
313  wxString msg;
314  msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
315 
316  wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
317  wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
318 
319  if( dialog.ShowModal() == wxID_YES )
320  {
321  if( !dirName.MakeRelativeTo( defaultPath ) )
322  wxMessageBox( _( "Cannot make path relative (target volume different from file volume)!" ),
323  _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
324  }
325 
326  m_outputDirectoryName->SetValue( dirName.GetFullPath() );
327 }
328 
329 
331 {
332  wxString msg;
333 
334  // Set output directory and replace backslashes with forward ones
335  wxString dirStr;
336  dirStr = m_outputDirectoryName->GetValue();
337  dirStr.Replace( wxT( "\\" ), wxT( "/" ) );
338  m_plotOpts.SetOutputDirectory( dirStr );
341 
342  m_mapFileType = m_Choice_Drill_Map->GetSelection();
343 
344  m_UnitDrillIsInch = (m_Choice_Unit->GetSelection() == 0) ? false : true;
345  m_MinimalHeader = m_Check_Minimal->IsChecked();
346  m_Mirror = m_Check_Mirror->IsChecked();
348  m_ZerosFormat = m_Choice_Zeros_Format->GetSelection();
349 
350  if( m_Choice_Drill_Offset->GetSelection() == 0 )
351  m_FileDrillOffset = wxPoint( 0, 0 );
352  else
354 
355  if( m_UnitDrillIsInch )
357  else
359 
361 }
362 
363 
364 void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
365 {
366  UpdateConfig(); // set params and Save drill options
367 
370 
371  const PlotFormat filefmt[6] =
372  { // Keep these format ids in the same order than m_Choice_Drill_Map choices
375  };
376 
377  unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
378 
379  if( choice >= DIM( filefmt ) )
380  choice = 1;
381 
382  // Create output directory if it does not exist (also transform it in
383  // absolute form). Bail if it fails
384  wxFileName outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
385  wxString boardFilename = m_parent->GetBoard()->GetFileName();
386 
387  if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
388  {
389  wxString msg;
390  msg.Printf( _( "Could not write drill and/or map files to folder \"%s\"." ),
391  GetChars( outputDir.GetPath() ) );
392  DisplayError( this, msg );
393  return;
394  }
395 
396  if( m_drillFileType == 0 )
397  {
398  EXCELLON_WRITER excellonWriter( m_parent->GetBoard() );
401  excellonWriter.SetOptions( m_Mirror, m_MinimalHeader, m_FileDrillOffset, m_Merge_PTH_NPTH );
402  excellonWriter.SetMapFileFormat( filefmt[choice] );
403 
404  excellonWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
405  aGenDrill, aGenMap, &reporter );
406  }
407  else
408  {
409  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
410  // Set gerber precision: only 5 or 6 digits for mantissa are allowed
411  // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
412  // the integer part precision is always 4, and units always mm
413  gerberWriter.SetFormat( m_plotOpts.GetGerberPrecision() );
414  gerberWriter.SetOptions( m_FileDrillOffset );
415  gerberWriter.SetMapFileFormat( filefmt[choice] );
416 
417  gerberWriter.CreateDrillandMapFilesSet( outputDir.GetFullPath(),
418  aGenDrill, aGenMap, &reporter );
419  }
420 }
421 
422 
423 void DIALOG_GENDRILL::OnGenReportFile( wxCommandEvent& event )
424 {
425  UpdateConfig(); // set params and Save drill options
426 
427  wxFileName fn = m_parent->GetBoard()->GetFileName();
428 
429  fn.SetName( fn.GetName() + wxT( "-drl" ) );
430  fn.SetExt( ReportFileExtension );
431 
432  wxString defaultPath = Prj().AbsolutePath( m_plotOpts.GetOutputDirectory() );
433 
434  if( defaultPath.IsEmpty() )
435  defaultPath = wxStandardPaths::Get().GetDocumentsDir();
436 
437  wxFileDialog dlg( this, _( "Save Drill Report File" ), defaultPath, fn.GetFullName(),
438  ReportFileWildcard(), wxFD_SAVE );
439 
440  if( dlg.ShowModal() == wxID_CANCEL )
441  return;
442 
443  bool success;
444 
445  // Info is slightly different between Excellon and Gerber
446  // (file ext, Merge PTH/NPTH option)
447  if( m_drillFileType == 0 )
448  {
449  EXCELLON_WRITER excellonWriter( m_parent->GetBoard() );
450  excellonWriter.SetMergeOption( m_Merge_PTH_NPTH );
451  success = excellonWriter.GenDrillReportFile( dlg.GetPath() );
452  }
453  else
454  {
455  GERBER_WRITER gerberWriter( m_parent->GetBoard() );
456  success = gerberWriter.GenDrillReportFile( dlg.GetPath() );
457  }
458 
459  wxString msg;
460 
461  if( ! success )
462  {
463  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( dlg.GetPath() ) );
464  m_messagesBox->AppendText( msg );
465  }
466  else
467  {
468  msg.Printf( _( "Report file %s created\n" ), GetChars( dlg.GetPath() ) );
469  m_messagesBox->AppendText( msg );
470  }
471 }
#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:123
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.
wxStaticText * m_NotPlatedPadsCountInfoMsg
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
Return 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:142
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)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
static bool m_MinimalHeader
wxStaticText * m_MicroViasInfoMsg
wxString GetOutputDirectory() const
wxRadioButton * m_rbExcellon
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:457
wxStaticText * m_BuriedViasInfoMsg
wxString ReportFileWildcard()
const wxString & GetFileName() const
Definition: class_board.h:237
wxStaticText * m_ThroughViasInfoMsg
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&#39;t yet exist...
Definition: common.cpp:469
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:557
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
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...
const std::string ReportFileExtension
DLIST< MODULE > m_Modules
Definition: class_board.h:248
void InitDisplayParams(void)
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
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:249
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:245
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.
wxRadioButton * m_rbGerberX2
PCB_EDIT_FRAME * m_parent
static DRILL_PRECISION precisionListForInches(2, 4)
int GetGerberPrecision() const