1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
* 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
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
#include <math/util.h> // for KiROUND
#include <gerber_file_image.h>
#include <base_units.h>
/* These routines read the text string point from Text.
* On exit, Text points the beginning of the sequence unread
*/
// conversion scale from gerber file units to Gerbview internal units
// 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
#define SCALE_LIST_SIZE 9
static double scale_list[SCALE_LIST_SIZE] =
{
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
};
/**
* Convert a coordinate given in floating point to GerbView's internal units
* (currently = 10 nanometers).
*/
int scaletoIU( double aCoord, bool isMetric )
{
int ret;
if( isMetric ) // gerber are units in mm
ret = KiROUND( aCoord * GERB_IU_PER_MM );
else // gerber are units in inches
ret = KiROUND( aCoord * GERB_IU_PER_MM * 25.4 );
return ret;
}
// An useful function used when reading gerber files
static bool IsNumber( char x )
{
return ( ( x >= '0' ) && ( x <='9' ) )
|| ( x == '-' ) || ( x == '+' ) || ( x == '.' );
}
VECTOR2I GERBER_FILE_IMAGE::ReadXYCoord( char*& aText, bool aExcellonMode )
{
VECTOR2I pos( 0, 0 );
bool is_float = false;
std::string line;
// Reserve the anticipated length plus an optional sign and decimal
line.reserve( std::max( m_FmtLen.x, m_FmtLen.y ) + 3 );
// Set up return value for case where aText == nullptr
if( !m_Relative )
pos = m_CurrentPos;
if( aText == nullptr )
return pos;
while( *aText && ( ( *aText == 'X' ) || ( *aText == 'Y' ) || ( *aText == 'A' ) ) )
{
double decimal_scale = 1.0;
int nbdigits = 0;
int current_coord = 0;
char type_coord = *aText++;
line.clear();
while( IsNumber( *aText ) )
{
if( *aText == '.' ) // Force decimal format if reading a floating point number
is_float = true;
// count digits only (sign and decimal point are not counted)
if( (*aText >= '0') && (*aText <='9') )
nbdigits++;
line.push_back( *( aText++ ) );
}
double val = strtod( line.data(), nullptr );
if( is_float )
{
current_coord = scaletoIU( val, m_GerbMetric );
}
else
{
int fmt_scale = (type_coord == 'X') ? m_FmtScale.x : m_FmtScale.y;
if( m_NoTrailingZeros )
{
// 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 );
}
double real_scale = scale_list[fmt_scale];
if( m_GerbMetric )
real_scale = real_scale / 25.4;
current_coord = KiROUND( val * real_scale * decimal_scale );
}
if( type_coord == 'X' )
{
pos.x = current_coord;
}
else if( type_coord == 'Y' )
{
pos.y = current_coord;
}
else if( type_coord == 'A' )
{
m_ArcRadius = current_coord;
m_LastArcDataType = ARC_INFO_TYPE_RADIUS;
}
}
if( m_Relative )
pos += m_CurrentPos;
m_CurrentPos = pos;
return pos;
}
VECTOR2I GERBER_FILE_IMAGE::ReadIJCoord( char*& aText )
{
VECTOR2I pos( 0, 0 );
bool is_float = false;
std::string line;
// Reserve the anticipated length plus an optional sign and decimal
line.reserve( std::max( m_FmtLen.x, m_FmtLen.y ) + 3 );
if( aText == nullptr )
return pos;
while( *aText && ( ( *aText == 'I' ) || ( *aText == 'J' ) ) )
{
double decimal_scale = 1.0;
int nbdigits = 0;
int current_coord = 0;
char type_coord = *aText++;
line.clear();
while( IsNumber( *aText ) )
{
if( *aText == '.' ) // Force decimal format if reading a floating point number
is_float = true;
// count digits only (sign and decimal point are not counted)
if( (*aText >= '0') && (*aText <='9') )
nbdigits++;
line.push_back( *( aText++ ) );
}
double val = strtod( line.data(), nullptr );
if( is_float )
{
current_coord = scaletoIU( val, m_GerbMetric );
}
else
{
int fmt_scale = ( type_coord == 'I' ) ? m_FmtScale.x : m_FmtScale.y;
if( m_NoTrailingZeros )
{
// 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 );
}
double real_scale = scale_list[fmt_scale];
if( m_GerbMetric )
real_scale = real_scale / 25.4;
current_coord = KiROUND( val * real_scale * decimal_scale );
}
if( type_coord == 'I' )
{
pos.x = current_coord;
}
else if( type_coord == 'J' )
{
pos.y = current_coord;
}
}
m_IJPos = pos;
m_LastArcDataType = ARC_INFO_TYPE_CENTER;
m_LastCoordIsIJPos = true;
return pos;
}
// Helper functions:
/**
* 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.
*/
int ReadInt( char*& text, bool aSkipSeparator = true )
{
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
if( strncasecmp( text, "0X", 2 ) == 0 )
{
text++;
ret = 0;
}
else
{
ret = (int) strtol( text, &text, 10 );
}
if( *text == ',' || isspace( *text ) )
{
if( aSkipSeparator )
++text;
}
return ret;
}
/**
* 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.
*/
double ReadDouble( char*& text, bool aSkipSeparator = true )
{
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
if( strncasecmp( text, "0X", 2 ) == 0 )
{
text++;
ret = 0.0;
}
else
{
ret = strtod( text, &text );
}
if( *text == ',' || isspace( *text ) )
{
if( aSkipSeparator )
++text;
}
return ret;
}
|