2014-10-21 18:36:45 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
|
2021-07-16 20:13:26 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-21 18:36:45 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2018-01-29 12:26:58 +00:00
|
|
|
#include <gerber_file_image.h>
|
2012-04-18 07:07:13 +00:00
|
|
|
#include <base_units.h>
|
2010-10-15 18:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* These routines read the text string point from Text.
|
2010-12-02 17:26:32 +00:00
|
|
|
* On exit, Text points the beginning of the sequence unread
|
2010-10-15 18:59:26 +00:00
|
|
|
*/
|
2012-04-18 07:07:13 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// conversion scale from gerber file units to Gerbview internal units
|
2012-04-18 07:07:13 +00:00
|
|
|
// depending on the gerber file format
|
|
|
|
// this scale list assumes gerber units are imperial.
|
|
|
|
// for metric gerber units, the imperial to metric conversion is made in read functions
|
2014-07-14 18:59:41 +00:00
|
|
|
#define SCALE_LIST_SIZE 9
|
2014-07-09 16:31:39 +00:00
|
|
|
static double scale_list[SCALE_LIST_SIZE] =
|
2012-04-18 07:07:13 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
1000.0 * GERB_IU_PER_MM * 0.0254, // x.1 format (certainly useless)
|
|
|
|
100.0 * GERB_IU_PER_MM * 0.0254, // x.2 format (certainly useless)
|
|
|
|
10.0 * GERB_IU_PER_MM * 0.0254, // x.3 format
|
|
|
|
1.0 * GERB_IU_PER_MM * 0.0254, // x.4 format
|
|
|
|
0.1 * GERB_IU_PER_MM * 0.0254, // x.5 format
|
|
|
|
0.01 * GERB_IU_PER_MM * 0.0254, // x.6 format
|
|
|
|
0.001 * GERB_IU_PER_MM * 0.0254, // x.7 format (currently the max allowed precision)
|
|
|
|
0.0001 * GERB_IU_PER_MM * 0.0254, // provided, but not used
|
|
|
|
0.00001 * GERB_IU_PER_MM * 0.0254, // provided, but not used
|
2012-04-18 07:07:13 +00:00
|
|
|
};
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a coordinate given in floating point to GerbView's internal units
|
|
|
|
* (currently = 10 nanometers).
|
2012-04-18 07:07:13 +00:00
|
|
|
*/
|
|
|
|
int scaletoIU( double aCoord, bool isMetric )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-05-18 16:56:03 +00:00
|
|
|
if( isMetric ) // gerber are units in mm
|
2022-02-03 01:15:01 +00:00
|
|
|
ret = KiROUND( aCoord * GERB_IU_PER_MM );
|
2013-05-18 16:56:03 +00:00
|
|
|
else // gerber are units in inches
|
2022-02-09 20:07:18 +00:00
|
|
|
ret = KiROUND( aCoord * GERB_IU_PER_MM * 25.4 );
|
2012-04-18 07:07:13 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
VECTOR2I GERBER_FILE_IMAGE::ReadXYCoord( char*& aText, bool aExcellonMode )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
VECTOR2I pos( 0, 0 );
|
|
|
|
bool is_float = false;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
std::string line;
|
|
|
|
|
|
|
|
// Reserve the anticipated length plus an optional sign and decimal
|
|
|
|
line.reserve( std::max( m_FmtLen.x, m_FmtLen.y ) + 3 );
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-09 20:07:18 +00:00
|
|
|
// Set up return value for case where aText == nullptr
|
|
|
|
if( !m_Relative )
|
2010-10-15 18:59:26 +00:00
|
|
|
pos = m_CurrentPos;
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( aText == nullptr )
|
2010-10-15 18:59:26 +00:00
|
|
|
return pos;
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
while( *aText && ( ( *aText == 'X' ) || ( *aText == 'Y' ) || ( *aText == 'A' ) ) )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
double decimal_scale = 1.0;
|
|
|
|
int nbdigits = 0;
|
|
|
|
int current_coord = 0;
|
|
|
|
char type_coord = *aText++;
|
|
|
|
|
|
|
|
line.clear();
|
|
|
|
|
|
|
|
while( IsNumber( *aText ) )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
if( *aText == '.' ) // Force decimal format if reading a floating point number
|
|
|
|
is_float = true;
|
2014-07-09 16:31:39 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
// count digits only (sign and decimal point are not counted)
|
|
|
|
if( (*aText >= '0') && (*aText <='9') )
|
|
|
|
nbdigits++;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
line.push_back( *( aText++ ) );
|
|
|
|
}
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
double val = strtod( line.data(), nullptr );
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( is_float )
|
|
|
|
{
|
2022-02-10 20:34:06 +00:00
|
|
|
current_coord = scaletoIU( val, m_GerbMetric );
|
2022-02-03 01:15:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int fmt_scale = (type_coord == 'X') ? m_FmtScale.x : m_FmtScale.y;
|
2014-07-09 16:31:39 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( m_NoTrailingZeros )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
// no trailing zero format, we need to add missing zeros.
|
|
|
|
int digit_count = (type_coord == 'X') ? m_FmtLen.x : m_FmtLen.y;
|
|
|
|
|
|
|
|
// Truncate the extra digits if the len is more than expected
|
|
|
|
// because the conversion to internal units expect exactly
|
|
|
|
// digit_count digits. Alternatively, add some additional digits
|
|
|
|
// to pad out to the missing zeros
|
|
|
|
if( nbdigits < digit_count || ( aExcellonMode && ( nbdigits > digit_count ) ) )
|
|
|
|
decimal_scale = std::pow<double>( 10, digit_count - nbdigits );
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
double real_scale = scale_list[fmt_scale];
|
|
|
|
|
|
|
|
if( m_GerbMetric )
|
|
|
|
real_scale = real_scale / 25.4;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
current_coord = KiROUND( val * real_scale * decimal_scale );
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
2022-02-03 01:15:01 +00:00
|
|
|
|
|
|
|
if( type_coord == 'X' )
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2022-02-09 20:07:18 +00:00
|
|
|
pos.x = current_coord;
|
2022-02-03 01:15:01 +00:00
|
|
|
}
|
|
|
|
else if( type_coord == 'Y' )
|
|
|
|
{
|
2022-02-09 20:07:18 +00:00
|
|
|
pos.y = current_coord;
|
2022-02-03 01:15:01 +00:00
|
|
|
}
|
|
|
|
else if( type_coord == 'A' )
|
|
|
|
{
|
|
|
|
m_ArcRadius = current_coord;
|
|
|
|
m_LastArcDataType = ARC_INFO_TYPE_RADIUS;
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 20:07:18 +00:00
|
|
|
if( m_Relative )
|
|
|
|
pos += m_CurrentPos;
|
|
|
|
|
2010-10-15 18:59:26 +00:00
|
|
|
m_CurrentPos = pos;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
VECTOR2I GERBER_FILE_IMAGE::ReadIJCoord( char*& aText )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-01-01 18:57:44 +00:00
|
|
|
VECTOR2I pos( 0, 0 );
|
2010-10-15 18:59:26 +00:00
|
|
|
bool is_float = false;
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
std::string line;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
// Reserve the anticipated length plus an optional sign and decimal
|
|
|
|
line.reserve( std::max( m_FmtLen.x, m_FmtLen.y ) + 3 );
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( aText == nullptr )
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
while( *aText && ( ( *aText == 'I' ) || ( *aText == 'J' ) ) )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
double decimal_scale = 1.0;
|
|
|
|
int nbdigits = 0;
|
|
|
|
int current_coord = 0;
|
|
|
|
char type_coord = *aText++;
|
|
|
|
|
|
|
|
line.clear();
|
|
|
|
|
|
|
|
while( IsNumber( *aText ) )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
if( *aText == '.' ) // Force decimal format if reading a floating point number
|
|
|
|
is_float = true;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
// count digits only (sign and decimal point are not counted)
|
|
|
|
if( (*aText >= '0') && (*aText <='9') )
|
|
|
|
nbdigits++;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
line.push_back( *( aText++ ) );
|
|
|
|
}
|
2014-07-09 16:31:39 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
double val = strtod( line.data(), nullptr );
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( is_float )
|
|
|
|
{
|
2022-02-10 20:34:06 +00:00
|
|
|
current_coord = scaletoIU( val, m_GerbMetric );
|
2022-02-03 01:15:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int fmt_scale = ( type_coord == 'I' ) ? m_FmtScale.x : m_FmtScale.y;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( m_NoTrailingZeros )
|
2010-10-15 18:59:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
// no trailing zero format, we need to add missing zeros.
|
|
|
|
int digit_count = ( type_coord == 'I' ) ? m_FmtLen.x : m_FmtLen.y;
|
|
|
|
|
|
|
|
// Truncate the extra digits if the len is more than expected
|
|
|
|
// because the conversion to internal units expect exactly
|
|
|
|
// digit_count digits. Alternatively, add some additional digits
|
|
|
|
// to pad out to the missing zeros
|
|
|
|
if( nbdigits < digit_count )
|
|
|
|
decimal_scale = std::pow<double>( 10, digit_count - nbdigits );
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
double real_scale = scale_list[fmt_scale];
|
2012-04-18 07:07:13 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( m_GerbMetric )
|
|
|
|
real_scale = real_scale / 25.4;
|
2010-10-15 18:59:26 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
current_coord = KiROUND( val * real_scale * decimal_scale );
|
|
|
|
}
|
2014-07-09 16:31:39 +00:00
|
|
|
|
2022-02-03 01:15:01 +00:00
|
|
|
if( type_coord == 'I' )
|
|
|
|
{
|
|
|
|
pos.x = current_coord;
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
2022-02-03 01:15:01 +00:00
|
|
|
else if( type_coord == 'J' )
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2022-02-03 01:15:01 +00:00
|
|
|
pos.y = current_coord;
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2010-10-15 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_IJPos = pos;
|
2018-09-22 11:04:20 +00:00
|
|
|
m_LastArcDataType = ARC_INFO_TYPE_CENTER;
|
2018-11-28 17:46:25 +00:00
|
|
|
m_LastCoordIsIJPos = true;
|
2018-09-22 11:04:20 +00:00
|
|
|
|
2010-10-15 18:59:26 +00:00
|
|
|
return pos;
|
|
|
|
}
|
2010-11-30 20:41:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Helper functions:
|
|
|
|
|
|
|
|
/**
|
2021-07-16 20:13:26 +00:00
|
|
|
* Read an integer from an ASCII character buffer.
|
|
|
|
*
|
|
|
|
* If there is a comma after the integer, then skip over that.
|
|
|
|
*
|
|
|
|
* @param text is a reference to a character pointer from which bytes are read
|
|
|
|
* and the pointer is advanced for each byte read.
|
|
|
|
* @param aSkipSeparator set to true (default) to skip comma.
|
|
|
|
* @return The integer read in.
|
2010-11-30 20:41:35 +00:00
|
|
|
*/
|
|
|
|
int ReadInt( char*& text, bool aSkipSeparator = true )
|
|
|
|
{
|
2017-04-08 10:53:40 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
// For strtol, a string starting by 0X or 0x is a valid number in hexadecimal or octal.
|
|
|
|
// However, 'X' is a separator in Gerber strings with numbers.
|
|
|
|
// We need to detect that
|
2017-04-08 12:26:32 +00:00
|
|
|
if( strncasecmp( text, "0X", 2 ) == 0 )
|
2017-04-08 10:53:40 +00:00
|
|
|
{
|
|
|
|
text++;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2017-04-08 10:53:40 +00:00
|
|
|
ret = (int) strtol( text, &text, 10 );
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2010-11-30 20:41:35 +00:00
|
|
|
|
|
|
|
if( *text == ',' || isspace( *text ) )
|
2014-07-09 16:31:39 +00:00
|
|
|
{
|
2010-11-30 20:41:35 +00:00
|
|
|
if( aSkipSeparator )
|
|
|
|
++text;
|
2014-07-09 16:31:39 +00:00
|
|
|
}
|
2010-11-30 20:41:35 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-16 20:13:26 +00:00
|
|
|
* Read a double precision floating point number from an ASCII character buffer.
|
|
|
|
*
|
|
|
|
* If there is a comma after the number, then skip over that.
|
|
|
|
*
|
|
|
|
* @param text is a reference to a character pointer from which the ASCII double
|
|
|
|
* is read from and the pointer advanced for each character read.
|
|
|
|
* @param aSkipSeparator set to true (default) to skip comma.
|
|
|
|
* @return number read.
|
2010-11-30 20:41:35 +00:00
|
|
|
*/
|
|
|
|
double ReadDouble( char*& text, bool aSkipSeparator = true )
|
|
|
|
{
|
2017-04-08 10:53:40 +00:00
|
|
|
double ret;
|
|
|
|
|
|
|
|
// For strtod, a string starting by 0X or 0x is a valid number in hexadecimal or octal.
|
|
|
|
// However, 'X' is a separator in Gerber strings with numbers.
|
|
|
|
// We need to detect that
|
2017-04-08 12:26:32 +00:00
|
|
|
if( strncasecmp( text, "0X", 2 ) == 0 )
|
2017-04-08 10:53:40 +00:00
|
|
|
{
|
|
|
|
text++;
|
|
|
|
ret = 0.0;
|
|
|
|
}
|
|
|
|
else
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2017-04-08 10:53:40 +00:00
|
|
|
ret = strtod( text, &text );
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2010-11-30 20:41:35 +00:00
|
|
|
|
|
|
|
if( *text == ',' || isspace( *text ) )
|
2014-07-09 16:31:39 +00:00
|
|
|
{
|
2010-11-30 20:41:35 +00:00
|
|
|
if( aSkipSeparator )
|
|
|
|
++text;
|
2014-07-09 16:31:39 +00:00
|
|
|
}
|
2010-11-30 20:41:35 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|