KiCad PCB EDA Suite
readgerb.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) 2007-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2016 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 <fctsys.h>
26 #include <common.h>
27 #include <confirm.h>
28 #include <kicad_string.h>
29 #include <gerbview.h>
30 #include <gerbview_frame.h>
33 #include <view/view.h>
34 
35 #include <html_messagebox.h>
36 #include <macros.h>
37 
38 /* Read a gerber file, RS274D, RS274X or RS274X2 format.
39  */
40 bool GERBVIEW_FRAME::Read_GERBER_File( const wxString& GERBER_FullFileName )
41 {
42  wxString msg;
43 
44  int layer = GetActiveLayer();
46  GERBER_FILE_IMAGE* gerber = GetGbrImage( layer );
47 
48  if( gerber == NULL )
49  {
50  gerber = new GERBER_FILE_IMAGE( layer );
51  images->AddGbrImage( gerber, layer );
52  }
53 
54  /* Read the gerber file */
55  bool success = gerber->LoadGerberFile( GERBER_FullFileName );
56 
57  if( !success )
58  {
59  msg.Printf( _( "File <%s> not found" ), GetChars( GERBER_FullFileName ) );
60  DisplayError( this, msg, 10 );
61  return false;
62  }
63 
64  // Display errors list
65  if( gerber->GetMessages().size() > 0 )
66  {
67  HTML_MESSAGE_BOX dlg( this, _("Errors") );
68  dlg.ListSet(gerber->GetMessages());
69  dlg.ShowModal();
70  }
71 
72  /* if the gerber file is only a RS274D file
73  * (i.e. without any aperture information, but with items), warn the user:
74  */
75  if( !gerber->m_Has_DCode && gerber->GetItemsList() )
76  {
77  msg = _("Warning: this file has no D-Code definition\n"
78  "It is perhaps an old RS274D file\n"
79  "Therefore the size of items is undefined");
80  wxMessageBox( msg );
81  }
82 
83  auto canvas = GetGalCanvas();
84  if( canvas )
85  {
86  auto view = canvas->GetView();
87 
88  if( gerber->m_ImageNegative )
89  {
90  // TODO: find a way to handle negative images
91  // (maybe convert geometry into positives?)
92  }
93 
94  for( auto item = gerber->GetItemsList(); item; item = item->Next() )
95  {
96  view->Add( (KIGFX::VIEW_ITEM*) item );
97  }
98  }
99 
100  return true;
101 }
102 
103 
104 bool GERBER_FILE_IMAGE::LoadGerberFile( const wxString& aFullFileName )
105 {
106  int G_command = 0; // command number for G commands like G04
107  int D_commande = 0; // command number for D commands like D02
108  char line[GERBER_BUFZ];
109  char* text;
110 
111  ClearMessageList( );
113 
114  // Read the gerber file */
115  m_Current_File = wxFopen( aFullFileName, wxT( "rt" ) );
116 
117  if( m_Current_File == 0 )
118  return false;
119 
120  m_FileName = aFullFileName;
121 
122  LOCALE_IO toggleIo;
123 
124  wxString msg;
125 
126  while( true )
127  {
128  if( fgets( line, sizeof(line), m_Current_File ) == NULL )
129  break;
130 
131  m_LineNum++;
132  text = StrPurge( line );
133 
134  while( text && *text )
135  {
136  switch( *text )
137  {
138  case ' ':
139  case '\r':
140  case '\n':
141  text++;
142  break;
143 
144  case '*': // End command
146  text++;
147  break;
148 
149  case 'M': // End file
151  while( *text )
152  text++;
153  break;
154 
155  case 'G': /* Line type Gxx : command */
156  G_command = GCodeNumber( text );
157  Execute_G_Command( text, G_command );
158  break;
159 
160  case 'D': /* Line type Dxx : Tool selection (xx > 0) or
161  * command if xx = 0..9 */
162  D_commande = DCodeNumber( text );
163  Execute_DCODE_Command( text, D_commande );
164  break;
165 
166  case 'X':
167  case 'Y': /* Move or draw command */
168  m_CurrentPos = ReadXYCoord( text );
169  if( *text == '*' ) // command like X12550Y19250*
170  {
171  Execute_DCODE_Command( text,
173  }
174  break;
175 
176  case 'I':
177  case 'J': /* Auxiliary Move command */
178  m_IJPos = ReadIJCoord( text );
179 
180  if( *text == '*' ) // command like X35142Y15945J504*
181  {
182  Execute_DCODE_Command( text,
184  }
185  break;
186 
187  case '%':
189  {
191  ReadRS274XCommand( line, text );
192  }
193  else //Error
194  {
195  AddMessageToList( wxT("Expected RS274X Command") );
197  text++;
198  }
199  break;
200 
201  default:
202  text++;
203  msg.Printf( wxT("Unexpected symbol <%c>"), *text );
204  AddMessageToList( msg );
205  break;
206  }
207  }
208  }
209 
210  fclose( m_Current_File );
211 
212  m_InUse = true;
213 
214  return true;
215 }
void AddMessageToList(const wxString &aMessage)
Function AddMessageToList Add a message to the message list.
bool Execute_DCODE_Command(char *&text, int D_command)
Definition: rs274d.cpp:584
char * StrPurge(char *text)
Function StrPurge removes leading and training spaces, tabs and end of line chars in text return a po...
Definition: string.cpp:194
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:165
wxPoint ReadXYCoord(char *&Text)
Function ReadXYCoord Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm) ...
This file is part of the common library.
wxPoint ReadIJCoord(char *&Text)
Function ReadIJCoord Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm) These...
bool Read_GERBER_File(const wxString &GERBER_FullFileName)
Definition: readgerb.cpp:40
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
void ClearMessageList()
Function ClearMessageList Clear the message list Call it before reading a Gerber file.
int AddGbrImage(GERBER_FILE_IMAGE *aGbrImage, int aIdx)
Add a GERBER_FILE_IMAGE* at index aIdx or at the first free location if aIdx < 0. ...
Class VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW...
Definition: view_item.h:83
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
int GetActiveLayer()
Function SetActiveLayer returns the active layer.
This file contains miscellaneous commonly used macros and functions.
int GCodeNumber(char *&Text)
Definition: rs274d.cpp:415
int DCodeNumber(char *&Text)
Definition: rs274d.cpp:438
void ListSet(const wxString &aList)
Function ListSet Add a list of items.
bool LoadGerberFile(const wxString &aFullFileName)
Read and load a gerber file.
Definition: readgerb.cpp:104
const wxArrayString & GetMessages() const
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
GERBER_DRAW_ITEM * Next() const
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Accessors to GERBER_FILE_IMAGE_LIST and GERBER_FILE_IMAGE data.
Class HTML_MESSAGE_BOX.
bool Execute_G_Command(char *&text, int G_command)
Definition: rs274d.cpp:458
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
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
The common library.
#define GERBER_BUFZ
size of single line of a text from a gerber file.
Definition: gerbview.h:39
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:870
GERBER_FILE_IMAGE * GetGbrImage(int aIdx) const
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
bool ReadRS274XCommand(char *aBuff, char *&text)
Function ReadRS274XCommand reads a single RS274X command terminated with a %.
Definition: rs274x.cpp:192