kicad/gerbview/rs274x.cpp

1040 lines
31 KiB
C++
Raw Normal View History

/**************/
/* rs274x.cpp */
/**************/
#include "fctsys.h"
#include "common.h"
//#include "macros.h"
#include "gerbview.h"
#include "class_GERBER.h"
#define CODE( x, y ) ( ( (x) << 8 ) + (y) )
// Helper function to read a primitive macro param (TODO: make it DCODE_PARAM function)
static bool ReadMacroParam( DCODE_PARAM& aParam, char*& aText );
// See rs274xrevd_e.pdf, table 1: RS-274X parameters order of entry
// in gerber files, when a coordinate is given (like X78Y600 or I0J80):
// Y and Y are logical coordinates
// A and B are plotter coordiantes
// Usually A = X, B = Y
// But we can have A = Y, B = X and/or offset, mirror, scale;
// Also:
// Image is what you must plot (the entire data of the file).
// Layer is just a set of data blocks with their parameters. An image can have more than one layer
// So a gerber layer is not like a board layer or the graphic layers used in Gerbview to show a file.
enum RS274X_PARAMETERS {
// Directive parameters: single usage recommended
// Must be at the beginning of the file
AXIS_SELECT = CODE( 'A', 'S' ), // Default: A=X, B=Y
FORMAT_STATEMENT = CODE( 'F', 'S' ), // no default: this command must exists
MIRROR_IMAGE = CODE( 'M', 'I' ), // Default: mo mirror
MODE_OF_UNITS = CODE( 'M', 'O' ), // Default: inch
INCH = CODE( 'I', 'N' ),
MILLIMETER = CODE( 'M', 'M' ),
OFFSET = CODE( 'O', 'F' ), // Default: A = 0, B = 0
SCALE_FACTOR = CODE( 'S', 'F' ), // Default: A = 1.0, B = 1.0
// Image parameters:
// commands used only once at the beginning of the file
IMAGE_JUSTIFY = CODE( 'I', 'J' ), // Default: no justification
IMAGE_NAME = CODE( 'I', 'N' ), // Default: void
IMAGE_OFFSET = CODE( 'I', 'O' ), // Default: A = 0, B = 0
IMAGE_POLARITY = CODE( 'I', 'P' ), // Default: Positive
IMAGE_ROTATION = CODE( 'I', 'R' ), // Default: 0
PLOTTER_FILM = CODE( 'P', 'M' ),
// Aperture parameters:
// Usually for the whole file
AP_DEFINITION = CODE( 'A', 'D' ),
AP_MACRO = CODE( 'A', 'M' ),
// Layer specific parameters
// May be used singly or may be layer specfic
// theses parameters are at the beginning of the file or layer
2010-10-17 16:42:06 +00:00
// and reset some layer parameters (like interpolation)
LAYER_NAME = CODE( 'L', 'N' ), // Default: Positive
LAYER_POLARITY = CODE( 'L', 'P' ),
KNOCKOUT = CODE( 'K', 'O' ), // Default: off
STEP_AND_REPEAT = CODE( 'S', 'R' ), // Default: A = 1, B = 1
ROTATE = CODE( 'R', 'O' ), // Default: 0
// Miscellaneous parameters:
INCLUDE_FILE = CODE( 'I', 'F' )
};
2008-11-07 07:55:28 +00:00
/**
* Function ReadXCommand
* reads in two bytes of data and assembles them into an int with the first
2008-11-07 07:55:28 +00:00
* 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.
* @param text A reference to a pointer to read bytes from and to advance as
* they are read.
2008-11-07 07:55:28 +00:00
* @return int - with 16 bits of data in the ls bits, upper bits zeroed.
2007-09-25 15:10:01 +00:00
*/
static int ReadXCommand( char*& text )
{
2007-09-25 15:10:01 +00:00
int result;
if( text && *text )
2008-11-07 07:55:28 +00:00
result = *text++ << 8;
2007-09-25 15:10:01 +00:00
else
return -1;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
if( text && *text )
2008-11-07 07:55:28 +00:00
result += *text++;
2007-09-25 15:10:01 +00:00
else
return -1;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
return result;
}
2008-11-07 07:55:28 +00:00
/**
* Function ReadInt
2008-11-08 07:06:14 +00:00
* reads an int from an ASCII character buffer. If there is a comma after the
* int, then skip over that.
2008-11-07 07:55:28 +00:00
* @param text A reference to a character pointer from which bytes are read
* and the pointer is advanced for each byte read.
* @param aSkipSeparator = true (default) to skip comma
* @return int - The int read in.
2008-11-07 07:55:28 +00:00
*/
static int ReadInt( char*& text, bool aSkipSeparator = true )
{
2008-11-08 06:44:07 +00:00
int ret = (int) strtol( text, &text, 10 );
if( *text == ',' || isspace( *text ) )
if( aSkipSeparator )
++text;
2008-11-08 06:44:07 +00:00
return ret;
}
2008-11-07 07:55:28 +00:00
/**
* Function ReadDouble
* reads a double from an ASCII character buffer. If there is a comma after
* the double, then skip over that.
2008-11-07 07:55:28 +00:00
* @param text A reference to a character pointer from which the ASCII double
* is read from and the pointer advanced for each character read.
* @param aSkipSeparator = true (default) to skip comma
2008-11-07 07:55:28 +00:00
* @return double
*/
static double ReadDouble( char*& text, bool aSkipSeparator = true )
{
2008-11-08 06:44:07 +00:00
double ret = strtod( text, &text );
if( *text == ',' || isspace( *text ) )
if( aSkipSeparator )
++text;
2008-11-08 06:44:07 +00:00
return ret;
}
2007-09-25 15:10:01 +00:00
bool GERBER_IMAGE::ReadRS274XCommand( char buff[GERBER_BUFZ], char*& text )
{
2007-09-25 15:10:01 +00:00
bool ok = true;
int code_command;
text++;
for( ; ; )
2007-09-25 15:10:01 +00:00
{
while( *text )
{
switch( *text )
{
2008-11-08 06:44:07 +00:00
case '%': // end of command
2007-09-25 15:10:01 +00:00
text++;
m_CommandState = CMD_IDLE;
goto exit; // success completion
case ' ':
case '\r':
case '\n':
text++;
break;
case '*':
text++;
break;
default:
code_command = ReadXCommand( text );
ok = ExecuteRS274XCommand( code_command, buff, text );
if( !ok )
goto exit;
break;
}
}
2008-11-08 06:44:07 +00:00
// end of current line, read another one.
2008-11-07 07:55:28 +00:00
if( fgets( buff, GERBER_BUFZ, m_Current_File ) == NULL )
2007-09-25 15:10:01 +00:00
{
2008-11-08 06:44:07 +00:00
// end of file
2007-09-25 15:10:01 +00:00
ok = false;
break;
}
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
text = buff;
}
exit:
return ok;
}
bool GERBER_IMAGE::ExecuteRS274XCommand( int command,
char buff[GERBER_BUFZ],
char*& text )
{
2007-09-25 15:10:01 +00:00
int code;
int xy_seq_len, xy_seq_char;
bool ok = TRUE;
2008-11-07 07:55:28 +00:00
char line[GERBER_BUFZ];
2007-09-25 15:10:01 +00:00
wxString msg;
double fcoord;
double conv_scale = m_GerbMetric ? PCB_INTERNAL_UNIT / 25.4 : PCB_INTERNAL_UNIT;
2007-09-25 15:10:01 +00:00
// D( printf( "%22s: Command <%c%c>\n", __func__, (command >> 8) & 0xFF, command & 0xFF ); )
2007-09-25 15:10:01 +00:00
switch( command )
{
2008-11-07 07:55:28 +00:00
case FORMAT_STATEMENT:
2007-09-25 15:10:01 +00:00
xy_seq_len = 2;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
while( *text != '*' )
{
switch( *text )
{
case ' ':
text++;
break;
case 'L': // No Leading 0
m_NoTrailingZeros = FALSE;
text++;
break;
case 'T': // No trailing 0
m_NoTrailingZeros = TRUE;
text++;
break;
case 'A': // Absolute coord
2008-11-07 07:55:28 +00:00
m_Relative = FALSE;
text++;
2007-09-25 15:10:01 +00:00
break;
case 'I': // Absolute coord
2008-11-07 07:55:28 +00:00
m_Relative = TRUE;
text++;
2007-09-25 15:10:01 +00:00
break;
case 'N': // Sequence code (followed by the number of digits
// for the X,Y command
2007-09-25 15:10:01 +00:00
text++;
2008-11-07 07:55:28 +00:00
xy_seq_char = *text++;
2007-09-25 15:10:01 +00:00
if( (xy_seq_char >= '0') && (xy_seq_char <= '9') )
xy_seq_len = -'0';
break;
case 'X':
case 'Y': // Values transmitted :2 (really xy_seq_len : FIX
// ME) digits
{
code = *(text++);
char ctmp = *(text++) - '0';
if( code == 'X' )
2007-09-25 15:10:01 +00:00
{
// number of digits after the decimal point (0 to 6 allowed)
m_FmtScale.x = *text - '0';
m_FmtLen.x = ctmp + m_FmtScale.x;
// m_FmtScale is 0 to 6
if( m_FmtScale.x < 0 )
m_FmtScale.x = 0;
if( m_FmtScale.x > 6 )
m_FmtScale.x = 6;
2007-09-25 15:10:01 +00:00
}
else
{
m_FmtScale.y = *text - '0';
m_FmtLen.y = ctmp + m_FmtScale.y;
if( m_FmtScale.y < 0 )
m_FmtScale.y = 0;
if( m_FmtScale.y > 6 )
m_FmtScale.y = 6;
}
text++;
}
break;
2007-09-25 15:10:01 +00:00
case '*':
break;
default:
GetEndOfBlock( buff, text, m_Current_File );
ok = FALSE;
break;
}
}
2007-09-25 15:10:01 +00:00
break;
case AXIS_SELECT: // command ASAXBY*% or %ASAYBX*%
m_SwapAxis = false;
if( strnicmp( text, "AYBX", 4 ) == 0 )
m_SwapAxis = true;
break;
case MIRROR_IMAGE: // commanf %MIA0B0*%, %MIA0B1*%, %MIA1B0*%, %MIA1B1*%
m_MirrorA = m_MirrorB = 0;
while( *text && *text != '*' )
{
switch( *text )
{
case 'A': // Mirror A axis ?
text++;
if( *text == '1' )
m_MirrorA = true;
break;
case 'B': // Mirror B axis ?
text++;
if( *text == '1' )
m_MirrorB = true;
break;
default:
text++;
break;
}
}
2007-09-25 15:10:01 +00:00
break;
case MODE_OF_UNITS:
code = ReadXCommand( text );
if( code == INCH )
m_GerbMetric = FALSE;
else if( code == MILLIMETER )
m_GerbMetric = TRUE;
conv_scale = m_GerbMetric ? PCB_INTERNAL_UNIT / 25.4 : PCB_INTERNAL_UNIT;
2007-09-25 15:10:01 +00:00
break;
case OFFSET: // command: OFAnnBnn (nn = float number) = layer Offset
2007-09-25 15:10:01 +00:00
m_Offset.x = m_Offset.y = 0;
while( *text != '*' )
{
switch( *text )
{
case 'A': // A axis offset in current unit (inch or mm)
2007-09-25 15:10:01 +00:00
text++;
fcoord = ReadDouble( text );
m_Offset.x = wxRound( fcoord * conv_scale );
2007-09-25 15:10:01 +00:00
break;
case 'B': // B axis offset in current unit (inch or mm)
2007-09-25 15:10:01 +00:00
text++;
fcoord = ReadDouble( text );
m_Offset.y = wxRound( fcoord * conv_scale );
2007-09-25 15:10:01 +00:00
break;
}
}
break;
case SCALE_FACTOR:
m_Scale.x = m_Scale.y = 1.0;
while( *text != '*' )
{
switch( *text )
{
case 'A': // A axis scale
text++;
m_Scale.x = ReadDouble( text );
break;
case 'B': // B axis scale
text++;
m_Scale.y = ReadDouble( text );
break;
}
}
break;
case IMAGE_OFFSET: // command: IOAnnBnn (nn = float number) = Image Offset
m_ImageOffset.x = m_ImageOffset.y = 0;
while( *text != '*' )
{
switch( *text )
{
case 'A': // A axis offset in current unit (inch or mm)
text++;
fcoord = ReadDouble( text );
m_ImageOffset.x = wxRound( fcoord * conv_scale );
break;
case 'B': // B axis offset in current unit (inch or mm)
text++;
fcoord = ReadDouble( text );
m_ImageOffset.y = wxRound( fcoord * conv_scale );
break;
}
}
break;
case IMAGE_ROTATION: // command IR0* or IR90* or IR180* or IR270*
if( strnicmp( text, "0*", 2 ) == 0 )
m_ImageRotation = 0;
if( strnicmp( text, "90*", 2 ) == 0 )
2010-10-17 16:42:06 +00:00
m_ImageRotation = 90;
if( strnicmp( text, "180*", 2 ) == 0 )
2010-10-17 16:42:06 +00:00
m_ImageRotation = 180;
if( strnicmp( text, "270*", 2 ) == 0 )
2010-10-17 16:42:06 +00:00
m_ImageRotation = 270;
else
ReportMessage( _( "RS274X: Command \"IR\" rotation value not allowed" ) );
break;
case STEP_AND_REPEAT: // command SR, like %SRX3Y2I5.0J2*%
2010-10-17 16:42:06 +00:00
m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
GetLayerParams().m_StepForRepeat.x = 0.0;
GetLayerParams().m_StepForRepeat.x = 0.0; // offset for Step and Repeat command
GetLayerParams().m_XRepeatCount = 1;
GetLayerParams().m_YRepeatCount = 1; // The repeat count
GetLayerParams().m_StepForRepeatMetric = m_GerbMetric; // the step units
while( *text && *text != '*' )
{
switch( *text )
{
case 'I': // X axis offset
text++;
GetLayerParams().m_StepForRepeat.x = ReadDouble( text );
break;
case 'J': // Y axis offset
text++;
GetLayerParams().m_StepForRepeat.y = ReadDouble( text );
break;
case 'X': // X axis repeat count
text++;
GetLayerParams().m_XRepeatCount = ReadInt( text );
break;
case 'Y': // Y axis offset
text++;
GetLayerParams().m_YRepeatCount = ReadInt( text );
break;
default:
text++;
break;
}
}
break;
case IMAGE_JUSTIFY: // Command IJAnBn*
m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
m_ImageJustifyOffset = wxPoint(0,0); // Image Justify Offset on XY axis (default = 0,0)
while( *text && *text != '*' )
{
// IJ command is (for A or B axis) AC or AL or A<coordinate>
switch( *text )
{
case 'A': // A axis justify
text++;
if( *text == 'C' )
{
m_ImageJustifyXCenter = true;
text++;
}
else if( *text == 'L' )
{
m_ImageJustifyXCenter = true;
text++;
}
else m_ImageJustifyOffset.x = wxRound( ReadDouble( text ) * conv_scale);
break;
case 'B': // B axis justify
text++;
if( *text == 'C' )
{
m_ImageJustifyYCenter = true;
text++;
}
else if( *text == 'L' )
{
m_ImageJustifyYCenter = true;
text++;
}
else m_ImageJustifyOffset.y = wxRound( ReadDouble( text ) * conv_scale);
break;
default:
text++;
break;
}
}
if( m_ImageJustifyXCenter )
m_ImageJustifyOffset.x = 0;
if( m_ImageJustifyYCenter )
m_ImageJustifyOffset.y = 0;
break;
2007-09-25 15:10:01 +00:00
case KNOCKOUT:
2010-10-17 16:42:06 +00:00
m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
msg = _( "RS274X: Command KNOCKOUT ignored by Gerbview" ) ;
ReportMessage( msg );
break;
case PLOTTER_FILM: // Command PF <string>
// This is an info about film that must be used to plot this file
// Has no meaning here. We just display this string
2010-10-18 12:34:45 +00:00
msg = wxT( "Plotter Film info:<br>" );
while( *text != '*' )
{
msg.Append( *text++ );
}
ReportMessage( msg );
2007-09-25 15:10:01 +00:00
break;
case ROTATE: // Layer rotation: command like %RO45*%
2010-10-17 16:42:06 +00:00
m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
m_LocalRotation =ReadDouble( text ); // Store layer rotation in degrees
break;
2007-09-25 15:10:01 +00:00
case IMAGE_NAME:
m_ImageName.Empty();
2007-09-25 15:10:01 +00:00
while( *text != '*' )
{
m_ImageName.Append( *text++ );
2007-09-25 15:10:01 +00:00
}
break;
2007-09-25 15:10:01 +00:00
case LAYER_NAME:
2010-10-17 16:42:06 +00:00
m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Start a new Gerber layer
GetLayerParams( ).m_LayerName.Empty();
while( *text != '*' )
{
GetLayerParams( ).m_LayerName.Append( *text++ );
}
2007-09-25 15:10:01 +00:00
break;
case IMAGE_POLARITY:
if( strnicmp( text, "NEG", 3 ) == 0 )
m_ImageNegative = true;
2007-09-25 15:10:01 +00:00
else
m_ImageNegative = false;
D( printf( "%22s: IMAGE_POLARITY m_ImageNegative=%s\n", __func__,
m_ImageNegative ? "true" : "false" ); )
2007-09-25 15:10:01 +00:00
break;
case LAYER_POLARITY:
if( *text == 'C' )
GetLayerParams().m_LayerNegative = true;
2007-09-25 15:10:01 +00:00
else
GetLayerParams().m_LayerNegative = false;
D( printf( "%22s: LAYER_POLARITY m_LayerNegative=%s\n", __func__,
GetLayerParams().m_LayerNegative ? "true" : "false" ); )
2007-09-25 15:10:01 +00:00
break;
case INCLUDE_FILE:
if( m_FilesPtr >= INCLUDE_FILES_CNT_MAX )
2007-09-25 15:10:01 +00:00
{
ok = FALSE;
ReportMessage( _( "Too many include files!!" ) );
2007-09-25 15:10:01 +00:00
break;
}
2008-11-07 07:55:28 +00:00
strcpy( line, text );
strtok( line, "*%%\n\r" );
2007-09-25 15:10:01 +00:00
m_FilesList[m_FilesPtr] = m_Current_File;
2008-11-07 07:55:28 +00:00
m_Current_File = fopen( line, "rt" );
2007-09-25 15:10:01 +00:00
if( m_Current_File == 0 )
{
msg.Printf( wxT( "include file <%s> not found." ), line );
ReportMessage( msg );
2007-09-25 15:10:01 +00:00
ok = FALSE;
m_Current_File = m_FilesList[m_FilesPtr];
break;
}
m_FilesPtr++;
break;
case AP_MACRO: // lines like %AMMYMACRO*
// 5,1,8,0,0,1.08239X$1,22.5*
// %
ok = ReadApertureMacro( buff, text, m_Current_File );
break;
2008-11-08 06:44:07 +00:00
case AP_DEFINITION:
/* input example: %ADD30R,0.081800X0.101500*%
* Aperture definition has 4 options: C, R, O, P
* (Circle, Rect, Oval, regular Polygon)
* and shapes can have a hole (round or rectangular).
* All optional parameters values start by X
* at this point, text points to 2nd 'D'
*/
if( *text++ != 'D' )
2007-09-25 15:10:01 +00:00
{
2008-11-07 07:55:28 +00:00
ok = FALSE;
break;
2007-09-25 15:10:01 +00:00
}
m_Has_DCode = TRUE;
2008-11-07 07:55:28 +00:00
code = ReadInt( text );
2008-11-07 07:55:28 +00:00
D_CODE* dcode;
dcode = GetDCODE( code );
2007-09-25 15:10:01 +00:00
if( dcode == NULL )
break;
2008-11-07 07:55:28 +00:00
// at this point, text points to character after the ADD<num>,
// i.e. R in example above. If text[0] is one of the usual
// apertures: (C,R,O,P), there is a comma after it.
if( text[1] == ',' )
2007-09-25 15:10:01 +00:00
{
char stdAperture = *text;
text += 2; // skip "C," for example
2007-09-25 15:10:01 +00:00
dcode->m_Size.x = dcode->m_Size.y =
wxRound( ReadDouble( text ) * conv_scale );
2007-09-25 15:10:01 +00:00
switch( stdAperture ) // Aperture desceiption has optional parameters. Read them
2007-09-25 15:10:01 +00:00
{
case 'C': // Circle
2008-11-08 06:44:07 +00:00
dcode->m_Shape = APT_CIRCLE;
2007-09-25 15:10:01 +00:00
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Drill.x = dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
dcode->m_DrillShape = APT_DEF_ROUND_HOLE;
2007-09-25 15:10:01 +00:00
}
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
2008-11-07 07:55:28 +00:00
dcode->m_DrillShape = APT_DEF_RECT_HOLE;
2007-09-25 15:10:01 +00:00
}
dcode->m_Defined = TRUE;
break;
2008-11-08 06:44:07 +00:00
case 'O': // oval
2007-09-25 15:10:01 +00:00
case 'R': // rect
dcode->m_Shape = (stdAperture == 'O') ? APT_OVAL : APT_RECT;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Size.y =
wxRound( ReadDouble( text ) * conv_scale );
2007-09-25 15:10:01 +00:00
}
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Drill.x = dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
dcode->m_DrillShape = APT_DEF_ROUND_HOLE;
2007-09-25 15:10:01 +00:00
}
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
while( *text == ' ' )
text++;
2010-09-28 14:42:05 +00:00
if( *text == 'X' )
2007-09-25 15:10:01 +00:00
{
text++;
dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
dcode->m_DrillShape = APT_DEF_RECT_HOLE;
2007-09-25 15:10:01 +00:00
}
dcode->m_Defined = TRUE;
break;
case 'P':
/* Regular polygon: a command line like %ADD12P,0.040X10X25X0.025X0.025X0.0150*%
* params are: <diameter>, X<edge count>, X<Rotation>, X<X hole dim>, X<Y hole dim>
*/
dcode->m_Shape = APT_POLYGON;
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_EdgesCount = ReadInt( text );
}
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Rotation = ReadDouble( text );
}
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Drill.x = dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
dcode->m_DrillShape = APT_DEF_ROUND_HOLE;
}
while( *text == ' ' )
text++;
if( *text == 'X' )
{
text++;
dcode->m_Drill.y =
wxRound( ReadDouble( text ) * conv_scale );
dcode->m_DrillShape = APT_DEF_RECT_HOLE;
}
dcode->m_Defined = TRUE;
2007-09-25 15:10:01 +00:00
break;
}
}
else // text[0] starts an aperture macro name
{
APERTURE_MACRO am_lookup;
while( *text && *text != '*' && *text != ',' )
am_lookup.name.Append( *text++ );
// When an aperture definition is like %AMLINE2* 22,1,$1,$2,0,0,-45*
// the ADDxx<MACRO_NAME> command has parameters, like %ADD14LINE2,0.8X0.5*%
if( *text == ',' )
{ // Read aperture macro parameters and store them
text++; // text points the first parameter
while( *text && *text != '*' )
{
double param = ReadDouble( text );
dcode->AppendParam( param );
while( isspace( *text ) ) text++;
if( *text == 'X' )
++text;
}
}
// lookup the aperture macro here.
APERTURE_MACRO* pam = FindApertureMacro( am_lookup );
if( !pam )
{
msg.Printf( wxT( "RS274X: aperture macro %s not found\n" ),
CONV_TO_UTF8( am_lookup.name ) );
ReportMessage( msg );
ok = false;
break;
}
2008-11-14 07:46:43 +00:00
dcode->m_Shape = APT_MACRO;
dcode->SetMacro( (APERTURE_MACRO*) pam );
}
2007-09-25 15:10:01 +00:00
break;
default:
ok = FALSE;
break;
}
ok = GetEndOfBlock( buff, text, m_Current_File );
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
return ok;
}
2007-09-25 15:10:01 +00:00
2008-11-07 07:55:28 +00:00
bool GetEndOfBlock( char buff[GERBER_BUFZ], char*& text, FILE* gerber_file )
{
for( ; ; )
2007-09-25 15:10:01 +00:00
{
2008-11-07 07:55:28 +00:00
while( (text < buff + GERBER_BUFZ) && *text )
2007-09-25 15:10:01 +00:00
{
if( *text == '*' )
return TRUE;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
if( *text == '%' )
return TRUE;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
text++;
}
2008-11-07 07:55:28 +00:00
if( fgets( buff, GERBER_BUFZ, gerber_file ) == NULL )
2007-09-25 15:10:01 +00:00
break;
2008-11-07 07:55:28 +00:00
2007-09-25 15:10:01 +00:00
text = buff;
}
return FALSE;
}
/**
* Function GetNextLine
* test for an end of line
* if an end of line is found:
* read a new line
* @param aBuff[GERBER_BUFZ] = buffer to fill with a new line
* @param aText = pointer to the last useful char in aBuff
* on return: points the beginning of the next line.
* @param aFile = the opened GERBER file to read
* @return a pointer to the beginning of the next line or NULL if end of file
*/
static char* GetNextLine( char aBuff[GERBER_BUFZ], char* aText, FILE* aFile )
{
for( ; ; )
{
switch (*aText )
{
case ' ': // skip blanks
case '\n':
case '\r': // Skip line terminators
++aText;
break;
case 0: // End of text found in aBuff: Read a new string
if( fgets( aBuff, GERBER_BUFZ, aFile ) == NULL )
return NULL;
aText = aBuff;
return aText;
default:
return aText;
}
}
return aText;
}
bool GERBER_IMAGE::ReadApertureMacro( char buff[GERBER_BUFZ],
char*& text,
FILE* gerber_file )
{
wxString msg;
2008-11-14 18:26:56 +00:00
APERTURE_MACRO am;
2007-09-25 15:10:01 +00:00
2008-11-08 06:44:07 +00:00
// read macro name
while( *text )
2007-09-25 15:10:01 +00:00
{
if( *text == '*' )
2008-11-08 06:44:07 +00:00
{
++text;
2007-09-25 15:10:01 +00:00
break;
2008-11-08 06:44:07 +00:00
}
2008-11-07 07:55:28 +00:00
2008-11-08 06:44:07 +00:00
am.name.Append( *text++ );
2007-09-25 15:10:01 +00:00
}
// Read aperture macro parameters
for( ; ; )
2007-09-25 15:10:01 +00:00
{
if( *text == '*' )
2008-11-08 06:44:07 +00:00
++text;
2008-11-07 07:55:28 +00:00
text = GetNextLine( buff, text, gerber_file ); // Get next line
if( text == NULL ) // End of File
return false;
2008-11-08 06:44:07 +00:00
// text points the beginning of a new line.
// Test for the last line in aperture macro lis:
// last line is % or *% sometime found.
if( *text == '*' )
++text;
2008-11-08 06:44:07 +00:00
if( *text == '%' )
break; // exit with text still pointing at %
int paramCount = 0;
2010-10-25 09:57:00 +00:00
int primitive_type = AMP_UNKNOWN;
// Test for a valid symbol at the beginning of a description:
// it can be: a parameter declaration like $1=$2/4
// or a digit (macro primitive selection)
// all other symbols are illegal.
if( *text == '$' ) // parameter declaration, not yet supported
{
msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": Operator $ not yet supported here, line: \"%s\"" ),
GetChars( am.name ), GetChars( CONV_FROM_UTF8( buff ) ) );
ReportMessage( msg );
primitive_type = AMP_COMMENT;
}
else if( !isdigit(*text) ) // Ill. symbol
{
msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": ill. symbol, line: \"%s\"" ),
GetChars( am.name ), GetChars( CONV_FROM_UTF8( buff ) ) );
ReportMessage( msg );
primitive_type = AMP_COMMENT;
}
else
primitive_type = ReadInt( text );
switch( primitive_type )
2008-11-08 06:44:07 +00:00
{
case AMP_COMMENT: // lines starting by 0 are a comment
paramCount = 0;
2010-10-25 09:57:00 +00:00
// Skip comment
while( *text && (*text != '*') )
text++;
break;
2008-11-08 06:44:07 +00:00
case AMP_CIRCLE:
paramCount = 4;
break;
2008-11-08 06:44:07 +00:00
case AMP_LINE2:
case AMP_LINE20:
paramCount = 7;
break;
2008-11-08 06:44:07 +00:00
case AMP_LINE_CENTER:
case AMP_LINE_LOWER_LEFT:
paramCount = 6;
break;
2008-11-08 06:44:07 +00:00
case AMP_EOF:
paramCount = 0;
break;
2008-11-08 06:44:07 +00:00
case AMP_OUTLINE:
paramCount = 4;
break;
2008-11-08 06:44:07 +00:00
case AMP_POLYGON:
paramCount = 6;
2008-11-08 06:44:07 +00:00
break;
2008-11-08 06:44:07 +00:00
case AMP_MOIRE:
paramCount = 9;
break;
2008-11-08 06:44:07 +00:00
case AMP_THERMAL:
paramCount = 6;
break;
default:
// @todo, there needs to be a way of reporting the line number
msg.Printf( wxT( "RS274X: Aperture Macro \"%s\": Invalid primitive id code %d, line: \"%s\"" ),
2010-10-18 12:34:45 +00:00
GetChars( am.name ), primitive_type, GetChars( CONV_FROM_UTF8( buff ) ) );
ReportMessage( msg );
return false;
2008-11-08 06:44:07 +00:00
}
AM_PRIMITIVE prim( m_GerbMetric );
prim.primitive_id = (AM_PRIMITIVE_ID) primitive_type;
int i;
for( i = 0; i < paramCount && *text && *text != '*'; ++i )
2008-11-08 06:44:07 +00:00
{
2008-11-14 18:26:56 +00:00
prim.params.push_back( DCODE_PARAM() );
DCODE_PARAM& param = prim.params.back();
text = GetNextLine( buff, text, gerber_file );
if( text == NULL) // End of File
return false;
ReadMacroParam( param, text );
2008-11-08 06:44:07 +00:00
}
if( i < paramCount )
{
// maybe some day we can throw an exception and track a line number
2010-10-18 12:34:45 +00:00
msg.Printf( wxT( "RS274X: read macro descr type %d: read %d parameters, insufficient parameters\n" ),
prim.primitive_id, i );
ReportMessage( msg );
}
// there are more parameters to read if this is an AMP_OUTLINE
2008-11-08 06:44:07 +00:00
if( prim.primitive_id == AMP_OUTLINE )
{
// so far we have read [0]:exposure, [1]:#points, [2]:X start, [3]: Y start
// Now read all the points, plus trailing rotation in degrees.
// params[1] is a count of polygon points, so it must be given
// in advance, i.e. be immediate.
wxASSERT( prim.params[1].IsImmediate() );
paramCount = (int) prim.params[1].GetValue( 0 ) * 2 + 1;
2008-11-08 06:44:07 +00:00
for( int i = 0; i < paramCount && *text != '*'; ++i )
2008-11-08 06:44:07 +00:00
{
2008-11-14 18:26:56 +00:00
prim.params.push_back( DCODE_PARAM() );
DCODE_PARAM& param = prim.params.back();
text = GetNextLine( buff, text, gerber_file );
if( text == NULL ) // End of File
return false;
ReadMacroParam( param, text );
2008-11-08 06:44:07 +00:00
}
}
am.primitives.push_back( prim );
2007-09-25 15:10:01 +00:00
}
2008-11-08 06:44:07 +00:00
m_aperture_macros.insert( am );
return true;
}
/**
* Function ReadMacroParam
* Read one aperture macro parameter
* a parameter can be:
* a number
* a reference to an aperture definition parameter value: $1 ot $3 ...
* a parameter definition can be complex and have operators between numbers and/or other parameter
* like $1+3 or $2x2..
* Parameters are separated by a comma ( of finish by *)
* Return if a param is read, or false
*/
static bool ReadMacroParam( DCODE_PARAM& aParam, char*& aText )
{
bool found = false;
if( *aText == '$' ) // value defined later, in aperture description
{
++aText;
aParam.SetIndex( ReadInt( aText, false ) );
found = true;
}
else
{
aParam.SetValue( ReadDouble( aText, false ) );
found = true;
}
// Skip extra characters and separator
while( *aText && (*aText != ',') && (*aText != '*') )
aText++;
if( *aText == ',' )
aText++;
return found;
}