summaryrefslogtreecommitdiff
path: root/gerbonara/utils.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-04-29 01:00:45 +0200
committerjaseg <git@jaseg.de>2023-04-29 01:00:45 +0200
commit778e81974580d910eac5e3f977acf79744d3e085 (patch)
tree92d4c5e3ff87aadb972251f16a8763f7b33ddeda /gerbonara/utils.py
parent958b47ab471053798ff55194c4aff4cf52f7602a (diff)
downloadgerbonara-778e81974580d910eac5e3f977acf79744d3e085.tar.gz
gerbonara-778e81974580d910eac5e3f977acf79744d3e085.tar.bz2
gerbonara-778e81974580d910eac5e3f977acf79744d3e085.zip
Freeze apertures and aperture macros, make gerbonara faster
Diffstat (limited to 'gerbonara/utils.py')
-rw-r--r--gerbonara/utils.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/gerbonara/utils.py b/gerbonara/utils.py
index c1868af..12ac787 100644
--- a/gerbonara/utils.py
+++ b/gerbonara/utils.py
@@ -25,6 +25,7 @@ gerber.utils
This module provides utility functions for working with Gerber and Excellon files.
"""
+from dataclasses import dataclass
import os
import re
import textwrap
@@ -57,6 +58,7 @@ class RegexMatcher:
return False
+@dataclass(frozen=True, slots=True)
class LengthUnit:
""" Convenience length unit class. Used in :py:class:`.GraphicObject` and :py:class:`.Aperture` to store lenght
information. Provides a number of useful unit conversion functions.
@@ -64,13 +66,9 @@ class LengthUnit:
Singleton, use only global instances ``utils.MM`` and ``utils.Inch``.
"""
- def __init__(self, name, shorthand, this_in_mm):
- self.name = name
- self.shorthand = shorthand
- self.factor = this_in_mm
-
- def __hash__(self):
- return hash((self.name, self.shorthand, self.factor))
+ name: str
+ shorthand: str
+ this_in_mm: float
def convert_from(self, unit, value):
""" Convert ``value`` from ``unit`` into this unit.
@@ -112,6 +110,19 @@ class LengthUnit:
max_y = self.convert_from(unit, max_y)
return (min_x, min_y), (max_x, max_y)
+ def convert_bounds_to(self, unit, value):
+ """ :py:meth:`.LengthUnit.convert_to` but for ((min_x, min_y), (max_x, max_y)) bounding box tuples. """
+
+ if value is None:
+ return None
+
+ (min_x, min_y), (max_x, max_y) = value
+ min_x = self.convert_to(unit, min_x)
+ min_y = self.convert_to(unit, min_y)
+ max_x = self.convert_to(unit, max_x)
+ max_y = self.convert_to(unit, max_y)
+ return (min_x, min_y), (max_x, max_y)
+
def format(self, value):
""" Return a human-readdable string representing value in this unit.