summaryrefslogtreecommitdiff
path: root/gerbonara/utils.py
diff options
context:
space:
mode:
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.