KiCad PCB EDA Suite
gerbview/files.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) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
9  * Copyright (C) 2004-2017 KiCad Developers, see AUTHORS.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 <wx/fs_zip.h>
31 #include <wx/wfstream.h>
32 #include <wx/zipstrm.h>
33 
34 #include <common.h>
35 #include <class_drawpanel.h>
36 #include <reporter.h>
37 #include <html_messagebox.h>
38 
39 #include <gerbview_frame.h>
40 #include <gerbview_id.h>
44 
45 // HTML Messages used more than one time:
46 #define MSG_NO_MORE_LAYER\
47  _( "<b>No more available free graphic layer</b> in Gerbview to load files" )
48 #define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" )
49 
50 void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event )
51 {
52  wxString fn;
53 
54  fn = GetFileFromHistory( event.GetId(), _( "Gerber files" ) );
55 
56  if( !fn.IsEmpty() )
57  {
58  Erase_Current_DrawLayer( false );
59  LoadGerberFiles( fn );
60  }
61 }
62 
63 
64 void GERBVIEW_FRAME::OnDrlFileHistory( wxCommandEvent& event )
65 {
66  wxString fn;
67 
68  fn = GetFileFromHistory( event.GetId(), _( "Drill files" ), &m_drillFileHistory );
69 
70  if( !fn.IsEmpty() )
71  {
72  Erase_Current_DrawLayer( false );
73  LoadExcellonFiles( fn );
74  }
75 }
76 
77 
78 void GERBVIEW_FRAME::OnZipFileHistory( wxCommandEvent& event )
79 {
80  wxString filename;
81  filename = GetFileFromHistory( event.GetId(), _( "Zip files" ), &m_zipFileHistory );
82 
83  if( !filename.IsEmpty() )
84  {
85  Erase_Current_DrawLayer( false );
86  LoadZipArchiveFile( filename );
87  }
88 }
89 
90 
91 void GERBVIEW_FRAME::OnJobFileHistory( wxCommandEvent& event )
92 {
93  wxString filename;
94  filename = GetFileFromHistory( event.GetId(), _( "Job files" ), &m_jobFileHistory );
95 
96  if( !filename.IsEmpty() )
97  {
98  LoadGerberJobFile( filename );
99  }
100 }
101 
102 
103 /* File commands. */
104 void GERBVIEW_FRAME::Files_io( wxCommandEvent& event )
105 {
106  int id = event.GetId();
107 
108  switch( id )
109  {
110  case wxID_FILE:
111  Erase_Current_DrawLayer( false );
112  LoadGerberFiles( wxEmptyString );
113  break;
114 
116  Clear_DrawLayers( false );
117  Zoom_Automatique( false );
118  m_canvas->Refresh();
119  ClearMsgPanel();
120  break;
121 
123  LoadExcellonFiles( wxEmptyString );
124  m_canvas->Refresh();
125  break;
126 
128  LoadZipArchiveFile( wxEmptyString );
129  m_canvas->Refresh();
130  break;
131 
133  LoadGerberJobFile( wxEmptyString );
134  m_canvas->Refresh();
135  break;
136 
137  default:
138  wxFAIL_MSG( wxT( "File_io: unexpected command id" ) );
139  break;
140  }
141 }
142 
143 
144 bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFullFileName )
145 {
146  wxString filetypes;
147  wxArrayString filenamesList;
148  wxFileName filename = aFullFileName;
149  wxString currentPath;
150 
151  if( !filename.IsOk() )
152  {
153  /* Standard gerber filetypes
154  * (See http://en.wikipedia.org/wiki/Gerber_File)
155  * the .gbr (.pho in legacy files) extension is the default used in Pcbnew
156  * However there are a lot of other extensions used for gerber files
157  * Because the first letter is usually g, we accept g* as extension
158  * (Mainly internal copper layers do not have specific extension,
159  * and filenames are like *.g1, *.g2 *.gb1 ...).
160  * Now (2014) Ucamco (the company which manages the Gerber format) encourages
161  * use of .gbr only and the Gerber X2 file format.
162  */
163  filetypes = _( "Gerber files (.g* .lgr .pho)" );
164  filetypes << wxT("|");
165  filetypes += wxT("*.g*;*.G*;*.pho;*.PHO" );
166  filetypes << wxT("|");
167 
168  /* Special gerber filetypes */
169  filetypes += _( "Top layer (*.GTL)|*.GTL;*.gtl|" );
170  filetypes += _( "Bottom layer (*.GBL)|*.GBL;*.gbl|" );
171  filetypes += _( "Bottom solder resist (*.GBS)|*.GBS;*.gbs|" );
172  filetypes += _( "Top solder resist (*.GTS)|*.GTS;*.gts|" );
173  filetypes += _( "Bottom overlay (*.GBO)|*.GBO;*.gbo|" );
174  filetypes += _( "Top overlay (*.GTO)|*.GTO;*.gto|" );
175  filetypes += _( "Bottom paste (*.GBP)|*.GBP;*.gbp|" );
176  filetypes += _( "Top paste (*.GTP)|*.GTP;*.gtp|" );
177  filetypes += _( "Keep-out layer (*.GKO)|*.GKO;*.gko|" );
178  filetypes += _( "Mechanical layers (*.GMx)|*.GM1;*.gm1;*.GM2;*.gm2;*.GM3;*.gm3|" );
179  filetypes += _( "Top Pad Master (*.GPT)|*.GPT;*.gpt|" );
180  filetypes += _( "Bottom Pad Master (*.GPB)|*.GPB;*.gpb|" );
181 
182  // All filetypes
183  filetypes += AllFilesWildcard;
184 
185  // Use the current working directory if the file name path does not exist.
186  if( filename.DirExists() )
187  currentPath = filename.GetPath();
188  else
189  {
190  currentPath = m_mruPath;
191 
192  // On wxWidgets 3.1 (bug?) the path in wxFileDialog is ignored when
193  // finishing by the dir separator. Remove it if any:
194  if( currentPath.EndsWith( '\\' ) || currentPath.EndsWith( '/' ) )
195  currentPath.RemoveLast();
196  }
197 
198  wxFileDialog dlg( this, _( "Open Gerber File" ),
199  currentPath,
200  filename.GetFullName(),
201  filetypes,
202  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
203 
204  if( dlg.ShowModal() == wxID_CANCEL )
205  return false;
206 
207  dlg.GetPaths( filenamesList );
208 
209  // @todo Take a closer look at the CWD switching here. The current working directory
210  // gets changed by wxFileDialog because the wxFD_CHANGE_DIR flag is set. Is this the
211  // appropriate behavior? The current working directory is not returned to the previous
212  // value so this may be an issue elsewhere.
213  currentPath = wxGetCwd();
214  m_mruPath = currentPath;
215  }
216  else
217  {
218  filenamesList.Add( aFullFileName );
219  currentPath = filename.GetPath();
220  m_mruPath = currentPath;
221  }
222 
223  // Read gerber files: each file is loaded on a new GerbView layer
224  bool success = true;
225  int layer = getActiveLayer();
226 
227  // Manage errors when loading files
228  wxString msg;
229  WX_STRING_REPORTER reporter( &msg );
230 
231  for( unsigned ii = 0; ii < filenamesList.GetCount(); ii++ )
232  {
233  filename = filenamesList[ii];
234 
235  if( !filename.IsAbsolute() )
236  filename.SetPath( currentPath );
237 
238  m_lastFileName = filename.GetFullPath();
239 
240  setActiveLayer( layer, false );
241 
242  if( Read_GERBER_File( filename.GetFullPath() ) )
243  {
245 
246  layer = getNextAvailableLayer( layer );
247 
248  if( layer == NO_AVAILABLE_LAYERS && ii < filenamesList.GetCount()-1 )
249  {
250  success = false;
252 
253  // Report the name of not loaded files:
254  ii += 1;
255  while( ii < filenamesList.GetCount() )
256  {
257  filename = filenamesList[ii++];
258  wxString txt;
259  txt.Printf( MSG_NOT_LOADED,
260  GetChars( filename.GetFullName() ) );
261  reporter.Report( txt, REPORTER::RPT_ERROR );
262  }
263  break;
264  }
265 
266  setActiveLayer( layer, false );
267  }
268  }
269 
270  if( !success )
271  {
272  HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
273  mbox.ListSet( msg );
274  mbox.ShowModal();
275  }
276 
277  Zoom_Automatique( false );
278 
279  // Synchronize layers tools with actual active layer:
283  syncLayerBox();
284  return success;
285 }
286 
287 
288 bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFullFileName )
289 {
290  wxString filetypes;
291  wxArrayString filenamesList;
292  wxFileName filename = aFullFileName;
293  wxString currentPath;
294 
295  if( !filename.IsOk() )
296  {
297  filetypes = wxGetTranslation( DrillFileWildcard );
298  filetypes << wxT("|");
299  /* All filetypes */
300  filetypes += wxGetTranslation( AllFilesWildcard );
301 
302  /* Use the current working directory if the file name path does not exist. */
303  if( filename.DirExists() )
304  currentPath = filename.GetPath();
305  else
306  currentPath = m_mruPath;
307 
308  wxFileDialog dlg( this, _( "Open Drill File" ),
309  currentPath, filename.GetFullName(), filetypes,
310  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
311 
312  if( dlg.ShowModal() == wxID_CANCEL )
313  return false;
314 
315  dlg.GetPaths( filenamesList );
316  currentPath = wxGetCwd();
317  m_mruPath = currentPath;
318  }
319  else
320  {
321  filenamesList.Add( aFullFileName );
322  currentPath = filename.GetPath();
323  m_mruPath = currentPath;
324  }
325 
326  // Read Excellon drill files: each file is loaded on a new GerbView layer
327  bool success = true;
328  int layer = getActiveLayer();
329 
330  // Manage errors when loading files
331  wxString msg;
332  WX_STRING_REPORTER reporter( &msg );
333 
334  for( unsigned ii = 0; ii < filenamesList.GetCount(); ii++ )
335  {
336  filename = filenamesList[ii];
337 
338  if( !filename.IsAbsolute() )
339  filename.SetPath( currentPath );
340 
341  m_lastFileName = filename.GetFullPath();
342 
343  setActiveLayer( layer, false );
344 
345  if( Read_EXCELLON_File( filename.GetFullPath() ) )
346  {
347  // Update the list of recent drill files.
348  UpdateFileHistory( filename.GetFullPath(), &m_drillFileHistory );
349 
350  layer = getNextAvailableLayer( layer );
351 
352  if( layer == NO_AVAILABLE_LAYERS && ii < filenamesList.GetCount()-1 )
353  {
354  success = false;
356 
357  // Report the name of not loaded files:
358  ii += 1;
359  while( ii < filenamesList.GetCount() )
360  {
361  filename = filenamesList[ii++];
362  wxString txt;
363  txt.Printf( MSG_NOT_LOADED,
364  GetChars( filename.GetFullName() ) );
365  reporter.Report( txt, REPORTER::RPT_ERROR );
366  }
367  break;
368  }
369 
370  setActiveLayer( layer, false );
371  }
372  }
373 
374  if( !success )
375  {
376  HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
377  mbox.ListSet( msg );
378  mbox.ShowModal();
379  }
380 
381  Zoom_Automatique( false );
382 
383  // Synchronize layers tools with actual active layer:
387  syncLayerBox();
388 
389  return success;
390 }
391 
392 
393 bool GERBVIEW_FRAME::unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter )
394 {
395  wxString msg;
396 
397  // Extract the path of aFullFileName. We use it to store temporary files
398  wxFileName fn( aFullFileName );
399  wxString unzipDir = fn.GetPath();
400 
401  wxFFileInputStream zipFile( aFullFileName );
402 
403  if( !zipFile.IsOk() )
404  {
405  if( aReporter )
406  {
407  msg.Printf( _( "Zip file '%s' cannot be opened" ), GetChars( aFullFileName ) );
408  aReporter->Report( msg, REPORTER::RPT_ERROR );
409  }
410 
411  return false;
412  }
413 
414  // Update the list of recent zip files.
415  UpdateFileHistory( aFullFileName, &m_zipFileHistory );
416 
417  // The unzipped file in only a temporary file. Give it a filename
418  // which cannot conflict with an usual filename.
419  // TODO: make Read_GERBER_File() and Read_EXCELLON_File() able to
420  // accept a stream, and avoid using a temp file.
421  wxFileName temp_fn( "$tempfile.tmp" );
422  temp_fn.MakeAbsolute( unzipDir );
423  wxString unzipped_tempfile = temp_fn.GetFullPath();
424 
425 
426  bool success = true;
427  wxZipInputStream zipArchive( zipFile );
428  wxZipEntry* entry;
429  bool reported_no_more_layer = false;
430 
431  while( ( entry = zipArchive.GetNextEntry() ) )
432  {
433  wxString fname = entry->GetName();
434  wxFileName uzfn = fname;
435  wxString curr_ext = uzfn.GetExt().Lower();
436 
437  // The archive contains Gerber and/or Excellon drill files. Use the right loader.
438  // However it can contain a few other files (reports, pdf files...),
439  // which will be skipped.
440  // Gerber files ext is usually "gbr", but can be also an other value, starting by "g"
441  // old gerber files ext from kicad is .pho
442  // drill files do not have a well defined ext
443  // It is .drl in kicad, but .txt in Altium for instance
444  // Allows only .drl for drill files.
445  if( curr_ext[0] != 'g' && curr_ext != "pho" && curr_ext != "drl" )
446  {
447  if( aReporter )
448  {
449  msg.Printf( _( "Info: skip file <i>'%s'</i> (unknown type)\n" ),
450  GetChars( entry->GetName() ) );
451  aReporter->Report( msg, REPORTER::RPT_WARNING );
452  }
453 
454  continue;
455  }
456 
457  int layer = getActiveLayer();
458 
459  if( layer == NO_AVAILABLE_LAYERS )
460  {
461  success = false;
462 
463  if( aReporter )
464  {
465  if( !reported_no_more_layer )
467 
468  reported_no_more_layer = true;
469 
470  // Report the name of not loaded files:
471  msg.Printf( MSG_NOT_LOADED, GetChars( entry->GetName() ) );
472  aReporter->Report( msg, REPORTER::RPT_ERROR );
473  }
474 
475  delete entry;
476  continue;
477  }
478 
479  // Create the unzipped temporary file:
480  {
481  wxFFileOutputStream temporary_ofile( unzipped_tempfile );
482 
483  if( temporary_ofile.Ok() )
484  temporary_ofile.Write( zipArchive );
485  else
486  {
487  success = false;
488 
489  if( aReporter )
490  {
491  msg.Printf( _( "<b>Unable to create temporary file '%s'</b>\n"),
492  GetChars( unzipped_tempfile ) );
493  aReporter->Report( msg, REPORTER::RPT_ERROR );
494  }
495  }
496  }
497 
498  bool read_ok = true;
499 
500  if( curr_ext[0] == 'g' || curr_ext == "pho" )
501  {
502  // Read gerber files: each file is loaded on a new GerbView layer
503  read_ok = Read_GERBER_File( unzipped_tempfile );
504  }
505  else // if( curr_ext == "drl" )
506  {
507  read_ok = Read_EXCELLON_File( unzipped_tempfile );
508  }
509 
510  delete entry;
511 
512  // The unzipped file is only a temporary file, delete it.
513  wxRemoveFile( unzipped_tempfile );
514 
515  if( !read_ok )
516  {
517  success = false;
518 
519  if( aReporter )
520  {
521  msg.Printf( _("<b>unzipped file %s read error</b>\n"),
522  GetChars( unzipped_tempfile ) );
523  aReporter->Report( msg, REPORTER::RPT_ERROR );
524  }
525  }
526  else
527  {
528  GERBER_FILE_IMAGE* gerber_image = GetGbrImage( layer );
529 
530  if( gerber_image )
531  gerber_image->m_FileName = fname;
532 
533  layer = getNextAvailableLayer( layer );
534  setActiveLayer( layer, false );
535  }
536  }
537 
538  return success;
539 }
540 
541 
542 bool GERBVIEW_FRAME::LoadZipArchiveFile( const wxString& aFullFileName )
543 {
544 #define ZipFileExtension "zip"
545 #define ZipFileWildcard _( "Zip file (*.zip)|*.zip;.zip" )
546  wxFileName filename = aFullFileName;
547  wxString currentPath;
548 
549  if( !filename.IsOk() )
550  {
551  // Use the current working directory if the file name path does not exist.
552  if( filename.DirExists() )
553  currentPath = filename.GetPath();
554  else
555  currentPath = m_mruPath;
556 
557  wxFileDialog dlg( this,
558  _( "Open Zip File" ),
559  currentPath,
560  filename.GetFullName(),
562  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
563 
564  if( dlg.ShowModal() == wxID_CANCEL )
565  return false;
566 
567  filename = dlg.GetPath();
568  currentPath = wxGetCwd();
569  m_mruPath = currentPath;
570  }
571  else
572  {
573  currentPath = filename.GetPath();
574  m_mruPath = currentPath;
575  }
576 
577  wxString msg;
578  WX_STRING_REPORTER reporter( &msg );
579 
580  if( filename.IsOk() )
581  unarchiveFiles( filename.GetFullPath(), &reporter );
582 
583  Zoom_Automatique( false );
584 
585  // Synchronize layers tools with actual active layer:
589  syncLayerBox();
590 
591  if( !msg.IsEmpty() )
592  {
593  wxSafeYield(); // Allows slice of time to redraw the screen
594  // to refresh widgets, before displaying messages
595  HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
596  mbox.ListSet( msg );
597  mbox.ShowModal();
598  }
599 
600  return true;
601 }
wxString m_lastFileName
The last filename chosen to be proposed to the user.
bool Clear_DrawLayers(bool query)
wxString m_mruPath
Most recently used path.
Definition: wxstruct.h:160
void Files_io(wxCommandEvent &event)
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
void syncLayerBox(bool aRebuildLayerBox=false)
Function syncLayerBox updates the currently "selected" layer within m_SelLayerBox The currently activ...
const wxString AllFilesWildcard
GERBER_LAYER_WIDGET * m_LayersManager
bool Read_GERBER_File(const wxString &GERBER_FullFileName)
Definition: readgerb.cpp:39
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
void OnGbrFileHistory(wxCommandEvent &event)
Function OnGbrFileHistory deletes the current data and loads a Gerber file selected from history list...
wxFileHistory m_drillFileHistory
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void Erase_Current_DrawLayer(bool query)
void UpdateFileHistory(const wxString &FullFileName, wxFileHistory *aFileHistory=NULL)
Function UpdateFileHistory Updates the list of recently opened files.
Definition: basicframe.cpp:388
int getNextAvailableLayer(int aLayer=0) const
Function getNextAvailableLayer finds the next empty layer starting at aLayer and returns it to the ca...
const wxString DrillFileWildcard
bool LoadGerberJobFile(const wxString &aFileName)
Load a Gerber job file, and load gerber files found in job files.
#define MSG_NOT_LOADED
void Zoom_Automatique(bool aWarpPointer)
Function Zoom_Automatique redraws the screen with best zoom level and the best centering that shows a...
Definition: zoom.cpp:77
#define ZipFileWildcard
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED) override
Function Report is a pure virtual function to override in the derived object.
Definition: reporter.cpp:48
bool unarchiveFiles(const wxString &aFullFileName, REPORTER *aReporter=nullptr)
Extracts gerber and drill files from the zip archive, and load them.
#define NO_AVAILABLE_LAYERS
bool LoadGerberFiles(const wxString &aFileName)
function LoadGerberFiles Load a photoplot (Gerber) file or many files.
void ListSet(const wxString &aList)
Function ListSet Add a list of items.
bool LoadZipArchiveFile(const wxString &aFileName)
function LoadZipArchiveFileLoadZipArchiveFile Load a zipped archive file.
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
void UpdateLayerIcons()
Function UpdateLayerIcons Update all layer manager icons (layers only) Useful when loading a file or ...
The common library.
wxFileHistory m_jobFileHistory
Class HTML_MESSAGE_BOX.
#define MSG_NO_MORE_LAYER
void setActiveLayer(int aLayer, bool doLayerWidgetUpdate=true)
Function setActiveLayer will change the currently active layer to aLayer and also update the GERBER_L...
Class WX_STRING_REPORTER is a wrapper for reporting to a wxString object.
Definition: reporter.h:116
wxFileHistory m_zipFileHistory
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
void OnJobFileHistory(wxCommandEvent &event)
deletes the current data and load a gerber job file selected from the history list.
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
wxString GetFileFromHistory(int cmdId, const wxString &type, wxFileHistory *aFileHistory=NULL)
Function GetFileFromHistory fetches the file name from the file history list.
Definition: basicframe.cpp:400
int getActiveLayer()
Function getActiveLayer returns the active layer.
The common library.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void OnDrlFileHistory(wxCommandEvent &event)
Function OnDrlFileHistory deletes the current data and load a drill file in Excellon format selected ...
bool LoadExcellonFiles(const wxString &aFileName)
function LoadExcellonFiles Load a drill (EXCELLON) file or many files.
void OnZipFileHistory(wxCommandEvent &event)
Function OnZipFileHistory deletes the current data and load a zip archive file selected from the hist...
GERBER_FILE_IMAGE * GetGbrImage(int aIdx) const
void ClearMsgPanel(void)
Clear all messages from the message panel.
Definition: draw_frame.cpp:764
void ReFillLayerWidget()
Function ReFillLayerWidget changes out all the layers in m_Layers and may be called upon loading new ...
bool Read_EXCELLON_File(const wxString &aFullFileName)