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>
31 #include <gerber_file_image.h>
32 #include <gerber_file_image_list.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  Erase_Current_DrawLayer( false );
51  }
52 
53  gerber = new GERBER_FILE_IMAGE( layer );
54 
55  // Read the gerber file. The image will be added only if it can be read
56  // to avoid broken data.
57  bool success = gerber->LoadGerberFile( GERBER_FullFileName );
58 
59  if( !success )
60  {
61  delete gerber;
62  msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName );
63  DisplayError( this, msg, 10 );
64  return false;
65  }
66 
67  images->AddGbrImage( gerber, layer );
68 
69  // Display errors list
70  if( gerber->GetMessages().size() > 0 )
71  {
72  HTML_MESSAGE_BOX dlg( this, _("Errors") );
73  dlg.ListSet(gerber->GetMessages());
74  dlg.ShowModal();
75  }
76 
77  /* if the gerber file is only a RS274D file
78  * (i.e. without any aperture information, but with items), warn the user:
79  */
80  if( !gerber->m_Has_DCode && gerber->GetItemsList() )
81  {
82  msg = _("Warning: this file has no D-Code definition\n"
83  "It is perhaps an old RS274D file\n"
84  "Therefore the size of items is undefined");
85  wxMessageBox( msg );
86  }
87 
88  if( GetCanvas() )
89  {
90  if( gerber->m_ImageNegative )
91  {
92  // TODO: find a way to handle negative images
93  // (maybe convert geometry into positives?)
94  }
95 
96  for( auto item = gerber->GetItemsList(); item; item = item->Next() )
97  GetCanvas()->GetView()->Add( (KIGFX::VIEW_ITEM*) item );
98  }
99 
100  return true;
101 }
102 
103 
104 
105 // size of a single line of text from a gerber file.
106 // warning: some files can have *very long* lines, so the buffer must be large.
107 #define GERBER_BUFZ 1000000
108 // A large buffer to store one line
109 static char lineBuffer[GERBER_BUFZ+1];
110 
111 bool GERBER_FILE_IMAGE::LoadGerberFile( const wxString& aFullFileName )
112 {
113  int G_command = 0; // command number for G commands like G04
114  int D_commande = 0; // command number for D commands like D02
115  char* text;
116 
117  ClearMessageList( );
119 
120  // Read the gerber file */
121  m_Current_File = wxFopen( aFullFileName, wxT( "rt" ) );
122 
123  if( m_Current_File == 0 )
124  return false;
125 
126  m_FileName = aFullFileName;
127 
128  LOCALE_IO toggleIo;
129 
130  wxString msg;
131 
132  while( true )
133  {
134  if( fgets( lineBuffer, GERBER_BUFZ, m_Current_File ) == NULL )
135  break;
136 
137  m_LineNum++;
138  text = StrPurge( lineBuffer );
139 
140  while( text && *text )
141  {
142  switch( *text )
143  {
144  case ' ':
145  case '\r':
146  case '\n':
147  text++;
148  break;
149 
150  case '*': // End command
152  text++;
153  break;
154 
155  case 'M': // End file
157  while( *text )
158  text++;
159  break;
160 
161  case 'G': /* Line type Gxx : command */
162  G_command = GCodeNumber( text );
163  Execute_G_Command( text, G_command );
164  break;
165 
166  case 'D': /* Line type Dxx : Tool selection (xx > 0) or
167  * command if xx = 0..9 */
168  D_commande = DCodeNumber( text );
169  Execute_DCODE_Command( text, D_commande );
170  break;
171 
172  case 'X':
173  case 'Y': /* Move or draw command */
174  m_CurrentPos = ReadXYCoord( text );
175  if( *text == '*' ) // command like X12550Y19250*
176  {
178  }
179  break;
180 
181  case 'I':
182  case 'J': /* Auxiliary Move command */
183  m_IJPos = ReadIJCoord( text );
184 
185  if( *text == '*' ) // command like X35142Y15945J504*
186  {
188  }
189  break;
190 
191  case '%':
193  {
196  }
197  else //Error
198  {
199  AddMessageToList( "Expected RS274X Command" );
201  text++;
202  }
203  break;
204 
205  default:
206  msg.Printf( "Unexpected char 0x%2.2X &lt;%c&lt;", *text, *text );
207  AddMessageToList( msg );
208  text++;
209  break;
210  }
211  }
212  }
213 
214  fclose( m_Current_File );
215 
216  m_InUse = true;
217 
218  return true;
219 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
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:579
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:310
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
This file is part of the common library.
wxPoint ReadIJCoord(char *&Text)
Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm) These coordinates are rela...
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
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:84
void Erase_Current_DrawLayer(bool query)
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:411
static char lineBuffer[GERBER_BUFZ+1]
Definition: readgerb.cpp:109
int DCodeNumber(char *&Text)
Definition: rs274d.cpp:434
const wxArrayString & GetMessages() const
void ListSet(const wxString &aList)
Add a list of items.
GERBER_FILE_IMAGE * GetGbrImage(int aIdx) const
bool LoadGerberFile(const wxString &aFullFileName)
Read and load a gerber file.
Definition: readgerb.cpp:111
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
#define GERBER_BUFZ
Definition: readgerb.cpp:107
Class HTML_MESSAGE_BOX.
#define _(s)
wxPoint ReadXYCoord(char *&aText, bool aExcellonMode=false)
Function ReadXYCoord Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm)
GERBER_DRAW_ITEM * Next() const
bool Execute_G_Command(char *&text, int G_command)
Definition: rs274d.cpp:454
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
bool ReadRS274XCommand(char *aBuff, unsigned int aBuffSize, char *&aText)
reads a single RS274X command terminated with a %
Definition: rs274x.cpp:186
The common library.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Accessors to GERBER_FILE_IMAGE_LIST and GERBER_FILE_IMAGE data.
virtual void ResetDefaultValues()