From 63e1eae8d81cb7940d3547511488f8ec4acd4d1c Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 28 Dec 2021 21:40:22 +0100 Subject: WIP --- gerbonara/gerber/utils.py | 168 ---------------------------------------------- 1 file changed, 168 deletions(-) (limited to 'gerbonara/gerber/utils.py') diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py index 492321a..122dd5a 100644 --- a/gerbonara/gerber/utils.py +++ b/gerbonara/gerber/utils.py @@ -29,148 +29,6 @@ from math import radians, sin, cos, sqrt, atan2, pi MILLIMETERS_PER_INCH = 25.4 -def parse_gerber_value(value, settings): - """ Convert gerber/excellon formatted string to floating-point number - - .. note:: - Format and zero suppression are configurable. Note that the Excellon - and Gerber formats use opposite terminology with respect to leading - and trailing zeros. The Gerber format specifies which zeros are - suppressed, while the Excellon format specifies which zeros are - included. This function uses the Gerber-file convention, so an - Excellon file in LZ (leading zeros) mode would use - `zero_suppression='trailing'` - - - Parameters - ---------- - value : string - A Gerber/Excellon-formatted string representing a numerical value. - - format : tuple (int,int) - Gerber/Excellon precision format expressed as a tuple containing: - (number of integer-part digits, number of decimal-part digits) - - zero_suppression : string - Zero-suppression mode. May be 'leading', 'trailing' or 'none' - - Returns - ------- - value : float - The specified value as a floating-point number. - - """ - - if not value: - return None - - # Handle excellon edge case with explicit decimal. "That was easy!" - if '.' in value: - return float(value) - - # Format precision - integer_digits, decimal_digits = settings.format - MAX_DIGITS = integer_digits + decimal_digits - - # Absolute maximum number of digits supported. This will handle up to - # 6:7 format, which is somewhat supported, even though the gerber spec - # only allows up to 6:6 - if MAX_DIGITS > 13 or integer_digits > 6 or decimal_digits > 7: - raise ValueError('Parser only supports precision up to 6:7 format') - - # Remove extraneous information - value = value.lstrip('+') - negative = '-' in value - if negative: - value = value.lstrip('-') - - missing_digits = MAX_DIGITS - len(value) - - if settings.zero_suppression == 'trailing': - digits = list(value + ('0' * missing_digits)) - elif settings.zero_suppression == 'leading': - digits = list(('0' * missing_digits) + value) - else: - digits = list(value) - - result = float( - ''.join(digits[:integer_digits] + ['.'] + digits[integer_digits:])) - return -result if negative else result - - -def write_gerber_value(value, settings): - """ Convert a floating point number to a Gerber/Excellon-formatted string. - - .. note:: - Format and zero suppression are configurable. Note that the Excellon - and Gerber formats use opposite terminology with respect to leading - and trailing zeros. The Gerber format specifies which zeros are - suppressed, while the Excellon format specifies which zeros are - included. This function uses the Gerber-file convention, so an - Excellon file in LZ (leading zeros) mode would use - `zero_suppression='trailing'` - - Parameters - ---------- - value : float - A floating point value. - - format : tuple (n=2) - Gerber/Excellon precision format expressed as a tuple containing: - (number of integer-part digits, number of decimal-part digits) - - zero_suppression : string - Zero-suppression mode. May be 'leading', 'trailing' or 'none' - - Returns - ------- - value : string - The specified value as a Gerber/Excellon-formatted string. - """ - - if format[0] == float: - return "%f" %value - - # Format precision - integer_digits, decimal_digits = settings.format - MAX_DIGITS = integer_digits + decimal_digits - - if MAX_DIGITS > 13 or integer_digits > 6 or decimal_digits > 7: - raise ValueError('Parser only supports precision up to 6:7 format') - - # Edge case... (per Gerber spec we should return 0 in all cases, see page - # 77) - if value == 0: - return '0' - - # negative sign affects padding, so deal with it at the end... - negative = value < 0.0 - if negative: - value = -1.0 * value - - # Format string for padding out in both directions - fmtstring = '%%0%d.0%df' % (MAX_DIGITS + 1, decimal_digits) - digits = [val for val in fmtstring % value if val != '.'] - - # If all the digits are 0, return '0'. - digit_sum = sum([int(digit) for digit in digits]) - if digit_sum == 0: - return '0' - - # Suppression... - if settings.zero_suppression == 'trailing': - while digits and digits[-1] == '0': - digits.pop() - elif settings.zero_suppression == 'leading': - while digits and digits[0] == '0': - digits.pop(0) - - if not digits: - return '0' - - return ''.join(digits) if not negative else ''.join(['-'] + digits) - - def decimal_string(value, precision=6, padding=False): """ Convert float to string with limited precision @@ -208,32 +66,6 @@ def decimal_string(value, precision=6, padding=False): else: return int(floatstr) - -def detect_file_format(data): - """ Determine format of a file - - Parameters - ---------- - data : string - string containing file data. - - Returns - ------- - format : string - File format. 'excellon' or 'rs274x' or 'unknown' - """ - lines = data.split('\n') - for line in lines: - if 'M48' in line: - return 'excellon' - elif '%FS' in line: - return 'rs274x' - elif ((len(line.split()) >= 2) and - (line.split()[0] == 'P') and (line.split()[1] == 'JOB')): - return 'ipc_d_356' - return 'unknown' - - def validate_coordinates(position): if position is not None: if len(position) != 2: -- cgit