summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/utils.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-11-08 13:06:23 +0100
committerjaseg <git@jaseg.de>2021-11-08 13:06:23 +0100
commit125eb821b9f5d4c58b17d43e318e9a6829120d03 (patch)
tree6564a0c275e8ddb256e1198a2af00c7c4b348182 /gerbonara/gerber/utils.py
parent5aa184757e4fb5c2fc141c41aed2fccc0c96ce7e (diff)
downloadgerbonara-125eb821b9f5d4c58b17d43e318e9a6829120d03.tar.gz
gerbonara-125eb821b9f5d4c58b17d43e318e9a6829120d03.tar.bz2
gerbonara-125eb821b9f5d4c58b17d43e318e9a6829120d03.zip
Parser half-refactored
Diffstat (limited to 'gerbonara/gerber/utils.py')
-rw-r--r--gerbonara/gerber/utils.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py
index 3d39df9..c2e81cd 100644
--- a/gerbonara/gerber/utils.py
+++ b/gerbonara/gerber/utils.py
@@ -29,7 +29,7 @@ from math import radians, sin, cos, sqrt, atan2, pi
MILLIMETERS_PER_INCH = 25.4
-def parse_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
+def parse_gerber_value(value, settings):
""" Convert gerber/excellon formatted string to floating-point number
.. note::
@@ -60,12 +60,16 @@ def parse_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
The specified value as a floating-point number.
"""
+
+ if value is None:
+ return None
+
# Handle excellon edge case with explicit decimal. "That was easy!"
if '.' in value:
return float(value)
# Format precision
- integer_digits, decimal_digits = format
+ integer_digits, decimal_digits = settings.format
MAX_DIGITS = integer_digits + decimal_digits
# Absolute maximum number of digits supported. This will handle up to
@@ -82,9 +86,9 @@ def parse_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
missing_digits = MAX_DIGITS - len(value)
- if zero_suppression == 'trailing':
+ if settings.zero_suppression == 'trailing':
digits = list(value + ('0' * missing_digits))
- elif zero_suppression == 'leading':
+ elif settings.zero_suppression == 'leading':
digits = list(('0' * missing_digits) + value)
else:
digits = list(value)
@@ -94,7 +98,7 @@ def parse_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
return -result if negative else result
-def write_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
+def write_gerber_value(value, settings):
""" Convert a floating point number to a Gerber/Excellon-formatted string.
.. note::
@@ -128,7 +132,7 @@ def write_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
return "%f" %value
# Format precision
- integer_digits, decimal_digits = format
+ integer_digits, decimal_digits = settings.format
MAX_DIGITS = integer_digits + decimal_digits
if MAX_DIGITS > 13 or integer_digits > 6 or decimal_digits > 7:
@@ -154,10 +158,10 @@ def write_gerber_value(value, format=(2, 5), zero_suppression='trailing'):
return '0'
# Suppression...
- if zero_suppression == 'trailing':
+ if settings.zero_suppression == 'trailing':
while digits and digits[-1] == '0':
digits.pop()
- elif zero_suppression == 'leading':
+ elif settings.zero_suppression == 'leading':
while digits and digits[0] == '0':
digits.pop(0)