2009-11-09 14:00:22 +00:00
|
|
|
/**********************/
|
|
|
|
/**** readgerb.cpp ****/
|
|
|
|
/**********************/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "common.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "confirm.h"
|
|
|
|
#include "kicad_string.h"
|
|
|
|
#include "gestfich.h"
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "gerbview.h"
|
|
|
|
#include "pcbplot.h"
|
|
|
|
#include "protos.h"
|
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
/* Format Gerber: NOTES:
|
|
|
|
* Functions history:
|
|
|
|
* Gn =
|
|
|
|
* G01 linear interpolation (right trace)
|
|
|
|
* G02, G20, G21 Circular interpolation, meaning trig <0
|
|
|
|
* G03, G30, G31 Circular interpolation, meaning trigo> 0
|
|
|
|
* G04 review
|
|
|
|
* G06 parabolic interpolation
|
|
|
|
* G07 Cubic Interpolation
|
|
|
|
* G10 linear interpolation (scale x10)
|
|
|
|
* G11 linear interpolation (0.1x range)
|
|
|
|
* G12 linear interpolation (0.01x scale)
|
|
|
|
* G52 plot symbol reference code by Dnn
|
|
|
|
* G53 plot symbol reference by Dnn; symbol rotates from -90 degrees
|
|
|
|
* G54 Selection Tool
|
|
|
|
* G55 Fashion photo exhibition
|
|
|
|
* G56 plot symbol reference code for DNN
|
|
|
|
* G57 displays the symbol link to the console
|
|
|
|
* G58 plot displays the symbol and link to the console
|
|
|
|
* G60 linear interpolation (scale x100)
|
|
|
|
* G70 Units = Inches
|
|
|
|
* G71 Units = Millimeters
|
|
|
|
* G74 circular interpolation removes 360 degree, has returned G01
|
|
|
|
* G75 Active circular interpolation on 360 degree
|
|
|
|
* G90 mode absolute coordinates
|
|
|
|
* G91 Fashion Related Contacts
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* X, Y coordinates
|
|
|
|
* X and Y are followed by + or - and m + n digits (not separated)
|
|
|
|
* m = integer part
|
|
|
|
* n = part after the comma
|
|
|
|
* Classic formats: m = 2, n = 3 (size 2.3)
|
|
|
|
* m = 3, n = 4 (size 3.4)
|
|
|
|
* eg
|
|
|
|
* G__ X00345Y-06123 * D__
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Tools and D_CODES
|
|
|
|
* Tool number (identification of shapes)
|
|
|
|
* 1 to 99 (Classical)
|
|
|
|
* 1 to 999
|
|
|
|
* D_CODES:
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* D01 ... D9 = action codes:
|
|
|
|
* D01 = activating light (lower pen) when placement
|
|
|
|
* D02 = light extinction (lift pen) when placement
|
|
|
|
* D03 = Flash
|
|
|
|
* D09 = VAPE Flash
|
|
|
|
* D51 = G54 preceded by -> Select VAPE
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* D10 ... D255 = Identification Tool (Opening)
|
|
|
|
* Not in order (see table in PCBPLOT.H)
|
2007-09-25 15:10:01 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
/* Routine to Read a file D Codes.
|
|
|
|
* Accepts standard format or ALSPCB
|
|
|
|
* A ';' starts a comment.
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Standard Format:
|
|
|
|
* Tool, Horiz, Vert, drill, speed, acc. Type; [dCode (comment)]
|
|
|
|
* Ex: 1, 12, 12, 0, 0, 0, 3; D10
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Format: ALSPCB
|
|
|
|
* Ver, Hor, Type, Tool, [Drill]
|
|
|
|
* Eg 0012, 0012, L, D10
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Rank the characters in buf_tmp tabular structures D_CODE.
|
|
|
|
* Returns:
|
|
|
|
* <0 if error:
|
|
|
|
* -1 = File not found
|
|
|
|
* -2 = Error reading file
|
|
|
|
* Rank D_code max lu (nbr of dCode)
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Internal Representation:
|
2007-11-02 09:58:42 +00:00
|
|
|
*
|
2009-11-09 14:00:22 +00:00
|
|
|
* Lines are represented as standard TRACKS
|
|
|
|
* The flash is represented as DRAWSEGMENTS
|
|
|
|
* - Round or oval: DRAWSEGMENTS
|
|
|
|
* - Rectangles DRAWSEGMENTS
|
|
|
|
* Reference to the D-CODE is set in the member Getnet()
|
2007-09-25 15:10:01 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
/* Read a gerber file (RS274D gold RS274X format).
|
|
|
|
* Normal size:
|
|
|
|
* Imperial
|
|
|
|
* Absolute
|
|
|
|
* End of block = *
|
|
|
|
* CrLf after each command
|
|
|
|
* G codes BROKE
|
|
|
|
*/
|
2009-11-21 20:44:19 +00:00
|
|
|
bool WinEDA_GerberFrame::Read_GERBER_File( const wxString& GERBER_FullFileName,
|
2007-09-25 15:10:01 +00:00
|
|
|
const wxString& D_Code_FullFileName )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-11-09 14:00:22 +00:00
|
|
|
int G_commande = 0,
|
|
|
|
D_commande = 0; /* command number for G or D commands
|
|
|
|
* (like G04 or D02) */
|
2008-11-07 07:55:28 +00:00
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
char line[GERBER_BUFZ];
|
2008-11-07 07:55:28 +00:00
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
wxString msg;
|
|
|
|
char* text;
|
|
|
|
int layer; /* current layer used in gerbview */
|
|
|
|
GERBER* gerber;
|
|
|
|
int error = 0;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2008-04-17 16:25:29 +00:00
|
|
|
layer = GetScreen()->m_Active_Layer;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
if( g_GERBER_List[layer] == NULL )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2008-11-08 06:44:07 +00:00
|
|
|
g_GERBER_List[layer] = new GERBER( layer );
|
2007-09-25 15:10:01 +00:00
|
|
|
}
|
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber = g_GERBER_List[layer];
|
2007-11-02 09:58:42 +00:00
|
|
|
|
|
|
|
/* Set the gerber scale: */
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->ResetDefaultValues();
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2007-11-02 09:58:42 +00:00
|
|
|
/* Read the gerber file */
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_Current_File = wxFopen( GERBER_FullFileName, wxT( "rt" ) );
|
|
|
|
if( gerber->m_Current_File == 0 )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
|
|
|
msg = _( "File " ) + GERBER_FullFileName + _( " not found" );
|
2007-11-02 09:58:42 +00:00
|
|
|
DisplayError( this, msg, 10 );
|
2007-09-25 15:10:01 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_FileName = GERBER_FullFileName;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2008-02-23 01:26:21 +00:00
|
|
|
wxString path = wxPathOnly( GERBER_FullFileName );
|
|
|
|
if( path != wxEmptyString )
|
|
|
|
wxSetWorkingDirectory( path );
|
2007-09-25 15:10:01 +00:00
|
|
|
|
|
|
|
wxBusyCursor show_wait;
|
2009-11-09 14:00:22 +00:00
|
|
|
SetLocaleTo_C_standard();
|
2007-09-25 15:10:01 +00:00
|
|
|
|
|
|
|
while( TRUE )
|
|
|
|
{
|
2009-11-09 14:00:22 +00:00
|
|
|
if( fgets( line, sizeof(line), gerber->m_Current_File ) == NULL )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2008-11-08 06:44:07 +00:00
|
|
|
if( gerber->m_FilesPtr == 0 )
|
2007-09-25 15:10:01 +00:00
|
|
|
break;
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
fclose( gerber->m_Current_File );
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_FilesPtr--;
|
|
|
|
gerber->m_Current_File =
|
|
|
|
gerber->m_FilesList[gerber->m_FilesPtr];
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2007-09-25 15:10:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2008-11-08 06:44:07 +00:00
|
|
|
text = StrPurge( line );
|
2007-09-25 15:10:01 +00:00
|
|
|
|
|
|
|
while( text && *text )
|
|
|
|
{
|
|
|
|
switch( *text )
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
text++;
|
|
|
|
break;
|
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
case '*': // End command
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_CommandState = END_BLOCK;
|
2007-09-25 15:10:01 +00:00
|
|
|
text++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M': // End file
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_CommandState = CMD_IDLE;
|
2007-09-25 15:10:01 +00:00
|
|
|
while( *text )
|
|
|
|
text++;
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2007-09-25 15:10:01 +00:00
|
|
|
break;
|
|
|
|
|
2007-11-02 09:58:42 +00:00
|
|
|
case 'G': /* Line type Gxx : command */
|
2008-11-08 06:44:07 +00:00
|
|
|
G_commande = gerber->ReturnGCodeNumber( text );
|
|
|
|
gerber->Execute_G_Command( text, G_commande );
|
2007-09-25 15:10:01 +00:00
|
|
|
break;
|
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
case 'D': /* Line type Dxx : Tool selection (xx > 0) or
|
|
|
|
* command if xx = 0..9 */
|
2008-11-08 06:44:07 +00:00
|
|
|
D_commande = gerber->ReturnDCodeNumber( text );
|
2009-11-21 20:44:19 +00:00
|
|
|
gerber->Execute_DCODE_Command( this, text, D_commande );
|
2007-09-25 15:10:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
2007-11-02 09:58:42 +00:00
|
|
|
case 'Y': /* Move or draw command */
|
2010-01-08 12:28:13 +00:00
|
|
|
gerber->m_CurrentPos = gerber->ReadXYCoord( text );
|
2007-09-25 15:10:01 +00:00
|
|
|
if( *text == '*' ) // command like X12550Y19250*
|
|
|
|
{
|
2009-11-21 20:44:19 +00:00
|
|
|
gerber->Execute_DCODE_Command( this, text,
|
2009-04-29 17:09:00 +00:00
|
|
|
gerber->m_Last_Pen_Command );
|
2007-09-25 15:10:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
2007-11-02 09:58:42 +00:00
|
|
|
case 'J': /* Auxiliary Move command */
|
2010-01-08 12:28:13 +00:00
|
|
|
gerber->m_IJPos = gerber->ReadIJCoord( text );
|
2007-09-25 15:10:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
2008-11-08 06:44:07 +00:00
|
|
|
if( gerber->m_CommandState != ENTER_RS274X_CMD )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_CommandState = ENTER_RS274X_CMD;
|
2007-11-02 09:58:42 +00:00
|
|
|
|
2009-11-21 20:44:19 +00:00
|
|
|
if( !gerber->ReadRS274XCommand( this, line, text ) )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
|
|
|
error++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //Error
|
|
|
|
{
|
|
|
|
error++;
|
2008-11-08 06:44:07 +00:00
|
|
|
gerber->m_CommandState = CMD_IDLE;
|
2007-09-25 15:10:01 +00:00
|
|
|
text++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-11-02 09:58:42 +00:00
|
|
|
text++;
|
2007-09-25 15:10:01 +00:00
|
|
|
error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "%d errors while reading Gerber file [%s]" ),
|
2009-11-21 20:44:19 +00:00
|
|
|
error, GetChars(GERBER_FullFileName) );
|
2007-09-25 15:10:01 +00:00
|
|
|
DisplayError( this, msg );
|
|
|
|
}
|
2008-11-08 06:44:07 +00:00
|
|
|
fclose( gerber->m_Current_File );
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
SetLocaleTo_Default();
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2009-11-09 14:00:22 +00:00
|
|
|
/* Init DCodes list and perhaps read a DCODES file,
|
|
|
|
* if the gerber file is only a RS274D file (without any aperture
|
|
|
|
* information)
|
2007-11-02 09:58:42 +00:00
|
|
|
*/
|
2008-11-09 02:57:42 +00:00
|
|
|
if( !gerber->m_Has_DCode )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
wxFileName fn;
|
|
|
|
|
2007-09-25 15:10:01 +00:00
|
|
|
if( D_Code_FullFileName.IsEmpty() )
|
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
wxString wildcard;
|
|
|
|
|
|
|
|
fn = GERBER_FullFileName;
|
|
|
|
fn.SetExt( g_PenFilenameExt );
|
|
|
|
wildcard.Printf( _( "Gerber DCODE files (%s)|*.%s" ),
|
2009-11-09 14:00:22 +00:00
|
|
|
GetChars( g_PenFilenameExt ),
|
|
|
|
GetChars( g_PenFilenameExt ) );
|
2009-04-05 20:49:15 +00:00
|
|
|
wildcard += AllFilesWildcard;
|
|
|
|
|
|
|
|
wxFileDialog dlg( this, _( "Load GERBER DCODE File" ),
|
|
|
|
wxEmptyString, fn.GetFullName(), wildcard,
|
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
2007-09-25 15:10:01 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-05 20:49:15 +00:00
|
|
|
fn = D_Code_FullFileName;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
if( fn.IsOk() )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
Read_D_Code_File( fn.GetFullPath() );
|
2007-09-25 15:10:01 +00:00
|
|
|
CopyDCodesSizeToItems();
|
|
|
|
}
|
2009-04-05 20:49:15 +00:00
|
|
|
else
|
|
|
|
return false;
|
2007-09-25 15:10:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
return true;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|