KiCad PCB EDA Suite
rs274x.cpp File Reference
#include <fctsys.h>
#include <common.h>
#include <macros.h>
#include <base_units.h>
#include <gerbview.h>
#include <class_gerber_file_image.h>
#include <class_X2_gerber_attributes.h>

Go to the source code of this file.

Macros

#define CODE(x, y)   ( ( (x) << 8 ) + (y) )
 

Enumerations

enum  RS274X_PARAMETERS {
  AXIS_SELECT = CODE( 'A', 'S' ), FORMAT_STATEMENT = CODE( 'F', 'S' ), MIRROR_IMAGE = CODE( 'M', 'I' ), MODE_OF_UNITS = CODE( 'M', 'O' ),
  INCH = CODE( 'I', 'N' ), MILLIMETER = CODE( 'M', 'M' ), OFFSET = CODE( 'O', 'F' ), SCALE_FACTOR = CODE( 'S', 'F' ),
  IMAGE_JUSTIFY = CODE( 'I', 'J' ), IMAGE_NAME = CODE( 'I', 'N' ), IMAGE_OFFSET = CODE( 'I', 'O' ), IMAGE_POLARITY = CODE( 'I', 'P' ),
  IMAGE_ROTATION = CODE( 'I', 'R' ), AP_DEFINITION = CODE( 'A', 'D' ), AP_MACRO = CODE( 'A', 'M' ), FILE_ATTRIBUTE = CODE( 'T', 'F' ),
  NET_ATTRIBUTE = CODE( 'T', 'O' ), APERTURE_ATTRIBUTE = CODE( 'T', 'A' ), REMOVE_APERTURE_ATTRIBUTE = CODE( 'T', 'D' ), KNOCKOUT = CODE( 'K', 'O' ),
  STEP_AND_REPEAT = CODE( 'S', 'R' ), ROTATE = CODE( 'R', 'O' ), LOAD_POLARITY = CODE( 'L', 'P' ), LOAD_NAME = CODE( 'L', 'N' )
}
 

Functions

int ReadInt (char *&text, bool aSkipSeparator=true)
 Function ReadInt reads an int from an ASCII character buffer. More...
 
double ReadDouble (char *&text, bool aSkipSeparator=true)
 Function ReadDouble reads a double from an ASCII character buffer. More...
 
static int ReadXCommand (char *&text)
 Function ReadXCommand reads in two bytes of data and assembles them into an int with the first byte in the sequence put into the most significant part of a 16 bit value and the second byte put into the least significant part of the 16 bit value. More...
 
static const wxString fromGerberString (const wxString &aGbrString)
 Convert a string read from a gerber file to an unicode string Usual chars (ASCII7 values) are the only values allowed in Gerber files, and are just copied. More...
 

Macro Definition Documentation

#define CODE (   x,
 
)    ( ( (x) << 8 ) + (y) )

Definition at line 42 of file rs274x.cpp.

Enumeration Type Documentation

Enumerator
AXIS_SELECT 
FORMAT_STATEMENT 
MIRROR_IMAGE 
MODE_OF_UNITS 
INCH 
MILLIMETER 
OFFSET 
SCALE_FACTOR 
IMAGE_JUSTIFY 
IMAGE_NAME 
IMAGE_OFFSET 
IMAGE_POLARITY 
IMAGE_ROTATION 
AP_DEFINITION 
AP_MACRO 
FILE_ATTRIBUTE 
NET_ATTRIBUTE 
APERTURE_ATTRIBUTE 
REMOVE_APERTURE_ATTRIBUTE 
KNOCKOUT 
STEP_AND_REPEAT 
ROTATE 
LOAD_POLARITY 
LOAD_NAME 

Definition at line 55 of file rs274x.cpp.

55  {
56  // Directive parameters: single usage recommended
57  // Must be at the beginning of the file
58  AXIS_SELECT = CODE( 'A', 'S' ), // Default: A=X, B=Y
59  FORMAT_STATEMENT = CODE( 'F', 'S' ), // no default: this command must exists
60  MIRROR_IMAGE = CODE( 'M', 'I' ), // Default: mo mirror
61  MODE_OF_UNITS = CODE( 'M', 'O' ), // Default: inch
62  INCH = CODE( 'I', 'N' ),
63  MILLIMETER = CODE( 'M', 'M' ),
64  OFFSET = CODE( 'O', 'F' ), // Default: A = 0, B = 0
65  SCALE_FACTOR = CODE( 'S', 'F' ), // Default: A = 1.0, B = 1.0
66 
67  // Image parameters:
68  // commands used only once at the beginning of the file, and are deprecated
69  IMAGE_JUSTIFY = CODE( 'I', 'J' ), // Default: no justification
70  IMAGE_NAME = CODE( 'I', 'N' ), // Default: void
71  IMAGE_OFFSET = CODE( 'I', 'O' ), // Default: A = 0, B = 0
72  IMAGE_POLARITY = CODE( 'I', 'P' ), // Default: Positive
73  IMAGE_ROTATION = CODE( 'I', 'R' ), // Default: 0
74 
75  // Aperture parameters:
76  // Usually for the whole file
77  AP_DEFINITION = CODE( 'A', 'D' ),
78  AP_MACRO = CODE( 'A', 'M' ),
79 
80  // X2 extention attribute commands
81  // Mainly are found standard attributes and user attributes
82  // standard attributes commands are:
83  // TF (file attribute) TO (net attribute)
84  // TA (aperture attribute) and TD (delete aperture attribute)
85  FILE_ATTRIBUTE = CODE( 'T', 'F' ),
86 
87  // X2 extention Net attribute info
88  // Net attribute options are:
89  // TO (net attribute data): TO.CN or TO.P TO.N or TO.C
90  NET_ATTRIBUTE = CODE( 'T', 'O' ),
91 
92  // X2 extention Aperture attribute TA
93  APERTURE_ATTRIBUTE = CODE( 'T', 'A' ),
94 
95  // TD (delete aperture/object attribute):
96  // Delete aperture attribute added by %TA or Oblect attribute added b %TO
97  // TD (delete all) or %TD<attr name> to delete <attr name>.
98  // eg: TD.P or TD.N or TD.C ...
99  REMOVE_APERTURE_ATTRIBUTE = CODE( 'T', 'D' ),
100 
101  // Layer specific parameters
102  // May be used singly or may be layer specfic
103  // theses parameters are at the beginning of the file or layer
104  // and reset some layer parameters (like interpolation)
105  KNOCKOUT = CODE( 'K', 'O' ), // Default: off
106  STEP_AND_REPEAT = CODE( 'S', 'R' ), // Default: A = 1, B = 1
107  ROTATE = CODE( 'R', 'O' ), // Default: 0
108 
109  LOAD_POLARITY = CODE( 'L', 'P' ), //LPC or LPD. Default: Dark (LPD)
110  LOAD_NAME = CODE( 'L', 'N' ), // Deprecated: equivalent to G04
111 };
#define CODE(x, y)
Definition: rs274x.cpp:42
Definition: rs274x.cpp:62

Function Documentation

static const wxString fromGerberString ( const wxString &  aGbrString)
static

Convert a string read from a gerber file to an unicode string Usual chars (ASCII7 values) are the only values allowed in Gerber files, and are just copied.

However Gerber format allows using non ASCII7 values by coding them in a sequence of 4 hexadecimal chars (16 bits hexadecimal value) Hexadecimal coded values ("\hhhh") are converted to the unicode char value

Definition at line 156 of file rs274x.cpp.

Referenced by GERBER_FILE_IMAGE::ExecuteRS274XCommand().

157 {
158  wxString text;
159 
160  for( unsigned ii = 0; ii < aGbrString.size(); ++ii )
161  {
162  if( aGbrString[ii] == '\\' )
163  {
164  unsigned value = 0;
165 
166  for( int jj = 0; jj < 4; jj++ )
167  { // Convert 4 hexa digits to binary value:
168  ii++;
169  value <<= 4;
170  int digit = aGbrString[ii];
171 
172  if( digit >= '0' && digit <= '9' )
173  digit -= '0';
174  else if( digit >= 'A' && digit <= 'F' )
175  digit -= 'A' - 10;
176  else if( digit >= 'a' && digit <= 'f' )
177  digit -= 'a' - 10;
178  else digit = 0;
179 
180  value += digit & 0xF;
181  }
182 
183  text.Append( wxUniChar( value ) );
184  }
185  else
186  text.Append( aGbrString[ii] );
187  }
188 
189  return text;
190 }
double ReadDouble ( char *&  text,
bool  aSkipSeparator = true 
)

Function ReadDouble reads a double from an ASCII character buffer.

If there is a comma after the double, then skip over that.

Parameters
textA reference to a character pointer from which the ASCII double is read from and the pointer advanced for each character read.
aSkipSeparator= true (default) to skip comma
Returns
double

Definition at line 302 of file rs274_read_XY_and_IJ_coordinates.cpp.

Referenced by GERBER_FILE_IMAGE::ExecuteRS274XCommand().

303 {
304  double ret;
305 
306  // For strtod, a string starting by 0X or 0x is a valid number in hexadecimal or octal.
307  // However, 'X' is a separator in Gerber strings with numbers.
308  // We need to detect that
309  if( strncasecmp( text, "0X", 2 ) == 0 )
310  {
311  text++;
312  ret = 0.0;
313  }
314  else
315  ret = strtod( text, &text );
316 
317  if( *text == ',' || isspace( *text ) )
318  {
319  if( aSkipSeparator )
320  ++text;
321  }
322 
323  return ret;
324 }
int ReadInt ( char *&  text,
bool  aSkipSeparator = true 
)

Function ReadInt reads an int from an ASCII character buffer.

If there is a comma after the int, then skip over that.

Parameters
textA reference to a character pointer from which bytes are read and the pointer is advanced for each byte read.
aSkipSeparator= true (default) to skip comma
Returns
int - The int read in.

Definition at line 268 of file rs274_read_XY_and_IJ_coordinates.cpp.

Referenced by GERBER_FILE_IMAGE::ExecuteRS274XCommand(), and GERBER_FILE_IMAGE::ReadApertureMacro().

269 {
270  int ret;
271 
272  // For strtol, a string starting by 0X or 0x is a valid number in hexadecimal or octal.
273  // However, 'X' is a separator in Gerber strings with numbers.
274  // We need to detect that
275  if( strncasecmp( text, "0X", 2 ) == 0 )
276  {
277  text++;
278  ret = 0;
279  }
280  else
281  ret = (int) strtol( text, &text, 10 );
282 
283  if( *text == ',' || isspace( *text ) )
284  {
285  if( aSkipSeparator )
286  ++text;
287  }
288 
289  return ret;
290 }
static int ReadXCommand ( char *&  text)
static

Function ReadXCommand reads in two bytes of data and assembles them into an int with the first byte in the sequence put into the most significant part of a 16 bit value and the second byte put into the least significant part of the 16 bit value.

Parameters
textA reference to a pointer to read bytes from and to advance as they are read.
Returns
int - with 16 bits of data in the ls bits, upper bits zeroed.

Definition at line 123 of file rs274x.cpp.

Referenced by GERBER_FILE_IMAGE::ExecuteRS274XCommand(), and GERBER_FILE_IMAGE::ReadRS274XCommand().

124 {
125  int result;
126  int currbyte;
127 
128  if( text && *text )
129  {
130  currbyte = *text++;
131  result = ( currbyte & 0xFF ) << 8;
132  }
133  else
134  return -1;
135 
136  if( text && *text )
137  {
138  currbyte = *text++;
139  result += currbyte & 0xFF;
140  }
141  else
142  return -1;
143 
144  return result;
145 }