pyaltiumlib 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyaltiumlib/__init__.py +58 -0
- pyaltiumlib/base.py +172 -0
- pyaltiumlib/datatypes/__init__.py +48 -0
- pyaltiumlib/datatypes/binaryreader.py +125 -0
- pyaltiumlib/datatypes/coordinate.py +197 -0
- pyaltiumlib/datatypes/mapping.py +13 -0
- pyaltiumlib/datatypes/parametercollection.py +157 -0
- pyaltiumlib/datatypes/parametercolor.py +27 -0
- pyaltiumlib/datatypes/parameterfont.py +19 -0
- pyaltiumlib/datatypes/pcblayerdefinition.py +109 -0
- pyaltiumlib/datatypes/pcbmapping.py +75 -0
- pyaltiumlib/datatypes/schematicmapping.py +167 -0
- pyaltiumlib/datatypes/schematicpin.py +55 -0
- pyaltiumlib/libcomponent.py +187 -0
- pyaltiumlib/pcblib/__init__.py +1 -0
- pyaltiumlib/pcblib/footprint.py +90 -0
- pyaltiumlib/pcblib/lib.py +66 -0
- pyaltiumlib/pcblib/records/PCBComponentBody.py +25 -0
- pyaltiumlib/pcblib/records/PCBPad.py +320 -0
- pyaltiumlib/pcblib/records/PCBString.py +212 -0
- pyaltiumlib/pcblib/records/PCBTrack.py +122 -0
- pyaltiumlib/pcblib/records/__init__.py +19 -0
- pyaltiumlib/pcblib/records/base.py +61 -0
- pyaltiumlib/schlib/__init__.py +8 -0
- pyaltiumlib/schlib/lib.py +92 -0
- pyaltiumlib/schlib/records/SchArc.py +111 -0
- pyaltiumlib/schlib/records/SchBezier.py +124 -0
- pyaltiumlib/schlib/records/SchComponent.py +81 -0
- pyaltiumlib/schlib/records/SchDesignator.py +42 -0
- pyaltiumlib/schlib/records/SchEllipse.py +72 -0
- pyaltiumlib/schlib/records/SchEllipticalArc.py +112 -0
- pyaltiumlib/schlib/records/SchImplementationList.py +42 -0
- pyaltiumlib/schlib/records/SchLabel.py +106 -0
- pyaltiumlib/schlib/records/SchLine.py +65 -0
- pyaltiumlib/schlib/records/SchParameter.py +32 -0
- pyaltiumlib/schlib/records/SchPin.py +150 -0
- pyaltiumlib/schlib/records/SchPolygon.py +111 -0
- pyaltiumlib/schlib/records/SchPolyline.py +106 -0
- pyaltiumlib/schlib/records/SchRectangle.py +73 -0
- pyaltiumlib/schlib/records/SchRoundRectangle.py +79 -0
- pyaltiumlib/schlib/records/__init__.py +40 -0
- pyaltiumlib/schlib/records/base.py +136 -0
- pyaltiumlib/schlib/symbol.py +127 -0
- pyaltiumlib-0.1.0.dist-info/LICENSE.txt +21 -0
- pyaltiumlib-0.1.0.dist-info/METADATA +69 -0
- pyaltiumlib-0.1.0.dist-info/RECORD +48 -0
- pyaltiumlib-0.1.0.dist-info/WHEEL +5 -0
- pyaltiumlib-0.1.0.dist-info/top_level.txt +1 -0
pyaltiumlib/__init__.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""
|
|
2
|
+
pyAltiumLib is a reader and renderer for Altium Library files
|
|
3
|
+
implemented in Python.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
AUTHOR_NAME = 'Chris Hoyer'
|
|
7
|
+
AUTHOR_EMAIL = 'info@chrishoyer.de'
|
|
8
|
+
CYEAR = '2024-2025'
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
__author__ = "Chris Hoyer <info@chrishoyer.de>"
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
from typing import Union
|
|
15
|
+
from pyaltiumlib.schlib.lib import SchLib
|
|
16
|
+
from pyaltiumlib.pcblib.lib import PcbLib
|
|
17
|
+
|
|
18
|
+
# Set up logging
|
|
19
|
+
import logging
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def read(filepath: str) -> Union[SchLib, PcbLib]:
|
|
24
|
+
"""
|
|
25
|
+
Reads an Altium library file and returns the corresponding library object.
|
|
26
|
+
|
|
27
|
+
This method determines whether the given file is a schematic library (`.SchLib`)
|
|
28
|
+
or a PCB library (`.PcbLib`) and returns the appropriate class instance.
|
|
29
|
+
|
|
30
|
+
:param filepath: The path to the Altium library file.
|
|
31
|
+
:type filepath: str
|
|
32
|
+
|
|
33
|
+
:return: An instance of either :class:`SchLib` or :class:`PcbLib`,
|
|
34
|
+
depending on the file type.
|
|
35
|
+
:rtype: :class:`pyaltiumlib.schlib.lib.SchLib` or :class:`pyaltiumlib.pcblib.lib.PcbLib`
|
|
36
|
+
|
|
37
|
+
:raises FileNotFoundError: If the specified file does not exist.
|
|
38
|
+
:raises ValueError: If the file type is not recognized as `.SchLib` or `.PcbLib`.
|
|
39
|
+
"""
|
|
40
|
+
if not os.path.isfile( filepath ):
|
|
41
|
+
logger.error(f"{filepath} does not exist.")
|
|
42
|
+
raise
|
|
43
|
+
|
|
44
|
+
# Choose the correct class
|
|
45
|
+
if filepath.lower().endswith('.schlib'):
|
|
46
|
+
return SchLib( filepath )
|
|
47
|
+
|
|
48
|
+
elif filepath.lower().endswith('.pcblib'):
|
|
49
|
+
return PcbLib( filepath )
|
|
50
|
+
|
|
51
|
+
else:
|
|
52
|
+
logger.error(f"Invalid file type: {filepath}.")
|
|
53
|
+
raise
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
pyaltiumlib/base.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
from pyaltiumlib.datatypes import ParameterColor
|
|
2
|
+
|
|
3
|
+
import olefile
|
|
4
|
+
from typing import List, Optional, Dict, Any
|
|
5
|
+
|
|
6
|
+
# Set up logging
|
|
7
|
+
import logging
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
class GenericLibFile:
|
|
11
|
+
"""
|
|
12
|
+
Base class for handling Altium Designer library files.
|
|
13
|
+
|
|
14
|
+
This class provides fundamental functionality for reading library files
|
|
15
|
+
in Altium Designer format.
|
|
16
|
+
|
|
17
|
+
:param string filepath: The path to the library file
|
|
18
|
+
|
|
19
|
+
:raises FileNotFoundError: If file is not a supported file.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
LibType = None
|
|
23
|
+
"""
|
|
24
|
+
`string` that specifies the type of the library.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
LibHeader = ''
|
|
28
|
+
"""`string` that contains the file path to the library.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
FilePath = ''
|
|
32
|
+
"""
|
|
33
|
+
`string` that stores the header information of the library.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
ComponentCount = 0
|
|
37
|
+
"""
|
|
38
|
+
`int` with total number of components in the library.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
Parts = []
|
|
42
|
+
"""
|
|
43
|
+
`List[any]` is a collection of components derived from :class:`pyaltiumlib.libcomponent.LibComponent` in their specific class
|
|
44
|
+
contained in the library.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
def __init__(self, filepath: str):
|
|
48
|
+
"""
|
|
49
|
+
Initialize a GenericLibFile object.
|
|
50
|
+
"""
|
|
51
|
+
if not olefile.isOleFile( filepath ):
|
|
52
|
+
logger.error(f"{filepath} is not a supported file.")
|
|
53
|
+
raise
|
|
54
|
+
|
|
55
|
+
self.LibType = type(self)
|
|
56
|
+
self.FilePath = filepath
|
|
57
|
+
|
|
58
|
+
self._olefile = None
|
|
59
|
+
self._olefile_open = False
|
|
60
|
+
|
|
61
|
+
# extracted file content
|
|
62
|
+
self._FileHeader = None
|
|
63
|
+
|
|
64
|
+
self._BackgroundColor = ParameterColor.from_hex("#6D6A69")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def __repr__(self) -> Dict:
|
|
68
|
+
"""
|
|
69
|
+
Converts public attributes of the high level file to a dictionary.
|
|
70
|
+
|
|
71
|
+
:return: A dict representation of the content of the object
|
|
72
|
+
:rtype: Dict
|
|
73
|
+
"""
|
|
74
|
+
return self.read_meta()
|
|
75
|
+
|
|
76
|
+
# =============================================================================
|
|
77
|
+
# External access
|
|
78
|
+
# =============================================================================
|
|
79
|
+
|
|
80
|
+
def read_meta(self) -> Dict:
|
|
81
|
+
"""
|
|
82
|
+
Converts public attributes of the high level file to a dictionary.
|
|
83
|
+
|
|
84
|
+
:return: A dict representation of the content of the object
|
|
85
|
+
:rtype: Dict
|
|
86
|
+
"""
|
|
87
|
+
public_attributes = {
|
|
88
|
+
key: value if isinstance(value, str) else str(value)
|
|
89
|
+
for key, value in self.__dict__.items()
|
|
90
|
+
if not key.startswith("_")
|
|
91
|
+
}
|
|
92
|
+
return public_attributes
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def list_parts(self) -> List[str]:
|
|
96
|
+
"""
|
|
97
|
+
List the names of all parts in the library.
|
|
98
|
+
|
|
99
|
+
:return: A list of part names
|
|
100
|
+
:rtype: List[str]
|
|
101
|
+
"""
|
|
102
|
+
return [x.Name for x in self.Parts]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def get_part(self, name: str) -> Optional[Any]:
|
|
106
|
+
"""
|
|
107
|
+
Get a part of the library by its name.
|
|
108
|
+
|
|
109
|
+
:param string name: The name of the part.
|
|
110
|
+
|
|
111
|
+
:return: The part class derived from :class:`pyaltiumlib.libcomponent.LibComponent` if found, otherwise None.
|
|
112
|
+
:rtype: Optional[Any]
|
|
113
|
+
"""
|
|
114
|
+
for part in self.Parts:
|
|
115
|
+
if part.Name == name:
|
|
116
|
+
return part
|
|
117
|
+
return None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# =============================================================================
|
|
121
|
+
# Internal file handling related functions
|
|
122
|
+
# =============================================================================
|
|
123
|
+
|
|
124
|
+
def _OpenFile(self) -> None:
|
|
125
|
+
"""
|
|
126
|
+
Open the library file for reading.
|
|
127
|
+
"""
|
|
128
|
+
if self._olefile_open:
|
|
129
|
+
raise ValueError(f"file: { self.FilePath }. Already open!")
|
|
130
|
+
|
|
131
|
+
try:
|
|
132
|
+
self._olefile = olefile.OleFileIO( self.FilePath )
|
|
133
|
+
self._olefile_open = True
|
|
134
|
+
except Exception as e:
|
|
135
|
+
logger.error(f"Failed to open file: {self.FilePath}. Error: {e}")
|
|
136
|
+
raise
|
|
137
|
+
|
|
138
|
+
def _OpenStream(self, container: str, stream: str) -> Any:
|
|
139
|
+
"""
|
|
140
|
+
Open a stream within the library file.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
container (str): The container name.
|
|
144
|
+
stream (str): The stream name.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
Any: The opened stream.
|
|
148
|
+
"""
|
|
149
|
+
if not self._olefile_open:
|
|
150
|
+
logger.error(f"file: { self.FilePath }. File not open!")
|
|
151
|
+
raise
|
|
152
|
+
|
|
153
|
+
if not container == "":
|
|
154
|
+
|
|
155
|
+
illegal_characters = '<>:"/\\|?*\x00'
|
|
156
|
+
container = "".join("_" if char in illegal_characters else char for char in container)
|
|
157
|
+
|
|
158
|
+
if not self._olefile.exists( container ):
|
|
159
|
+
logger.error(f"Part '{container}' does not exist in file '{self.FilePath}'!")
|
|
160
|
+
raise
|
|
161
|
+
|
|
162
|
+
return self._olefile.openstream( f"{container}/{stream}" if container else stream )
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _CloseFile(self) -> None:
|
|
166
|
+
"""
|
|
167
|
+
Close the library file.
|
|
168
|
+
"""
|
|
169
|
+
if hasattr(self, '_olefile') and self._olefile is not None:
|
|
170
|
+
self._olefile.close()
|
|
171
|
+
self._olefile_open = False
|
|
172
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .parametercollection import ParameterCollection
|
|
6
|
+
from .parametercolor import ParameterColor
|
|
7
|
+
from .parameterfont import ParameterFont
|
|
8
|
+
from .binaryreader import BinaryReader
|
|
9
|
+
from .coordinate import Coordinate, CoordinatePoint
|
|
10
|
+
|
|
11
|
+
# Schematic related
|
|
12
|
+
from .schematicpin import SchematicPin
|
|
13
|
+
from .schematicmapping import (
|
|
14
|
+
SchematicLineWidth, SchematicLineStyle, SchematicLineShape,
|
|
15
|
+
SchematicPinSymbol, SchematicPinElectricalType, SchematicTextOrientation,
|
|
16
|
+
SchematicTextJustification
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# PCB related
|
|
20
|
+
from .pcblayerdefinition import PCBLayerDefinition
|
|
21
|
+
from .pcbmapping import ( PCBPadShape, PCBHoleShape, PCBStackMode,
|
|
22
|
+
PCBTextJustification, PCBStrokeFont, PCBTextKind
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"ParameterCollection",
|
|
28
|
+
"ParameterColor",
|
|
29
|
+
"ParameterFont",
|
|
30
|
+
"BinaryReader",
|
|
31
|
+
"Coordinate",
|
|
32
|
+
"CoordinatePoint",
|
|
33
|
+
"SchematicPin",
|
|
34
|
+
"SchematicLineWidth",
|
|
35
|
+
"SchematicLineStyle",
|
|
36
|
+
"SchematicLineShape",
|
|
37
|
+
"SchematicPinSymbol",
|
|
38
|
+
"SchematicPinElectricalType",
|
|
39
|
+
"SchematicTextOrientation",
|
|
40
|
+
"SchematicTextJustification",
|
|
41
|
+
"PCBLayerDefinition",
|
|
42
|
+
"PCBPadShape",
|
|
43
|
+
"PCBStackMode",
|
|
44
|
+
"PCBHoleShape",
|
|
45
|
+
"PCBTextJustification",
|
|
46
|
+
"PCBStrokeFont",
|
|
47
|
+
"PCBTextKind"
|
|
48
|
+
]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
from pyaltiumlib.datatypes.coordinate import Coordinate, CoordinatePoint
|
|
2
|
+
|
|
3
|
+
class BinaryReader:
|
|
4
|
+
|
|
5
|
+
def __init__(self, data):
|
|
6
|
+
self.data = data
|
|
7
|
+
self.offset = 0
|
|
8
|
+
|
|
9
|
+
@classmethod
|
|
10
|
+
def from_stream(cls, stream, size_length=4):
|
|
11
|
+
length = int.from_bytes( stream.read( size_length ), "little" )
|
|
12
|
+
data = stream.read( length )
|
|
13
|
+
|
|
14
|
+
if len(data) != length:
|
|
15
|
+
raise ValueError("Stream does not match the declared block length.")
|
|
16
|
+
|
|
17
|
+
return cls( data )
|
|
18
|
+
|
|
19
|
+
def has_content(self):
|
|
20
|
+
return not len(self.data) == 0
|
|
21
|
+
|
|
22
|
+
def length(self):
|
|
23
|
+
return len(self.data)
|
|
24
|
+
|
|
25
|
+
def read(self, length):
|
|
26
|
+
|
|
27
|
+
if self.offset + length > len(self.data):
|
|
28
|
+
raise ValueError("Not enough data to read the requested length.")
|
|
29
|
+
|
|
30
|
+
result = self.data[self.offset:self.offset + length]
|
|
31
|
+
self.offset += length
|
|
32
|
+
return result
|
|
33
|
+
|
|
34
|
+
def read_byte(self):
|
|
35
|
+
if self.offset + 1 > len(self.data):
|
|
36
|
+
raise ValueError("Not enough data to read.")
|
|
37
|
+
return self.read(1)
|
|
38
|
+
|
|
39
|
+
def read_int8(self, signed=False):
|
|
40
|
+
return int.from_bytes(self.read_byte(), signed=signed)
|
|
41
|
+
|
|
42
|
+
def read_int16(self, signed=False):
|
|
43
|
+
if self.offset + 2 > len(self.data):
|
|
44
|
+
raise ValueError("Not enough data to read an Int16.")
|
|
45
|
+
|
|
46
|
+
value = int.from_bytes(self.data[self.offset:self.offset + 2], byteorder="little", signed=signed)
|
|
47
|
+
self.offset += 2
|
|
48
|
+
return value
|
|
49
|
+
|
|
50
|
+
def read_int32(self, signed=False):
|
|
51
|
+
if self.offset + 4 > len(self.data):
|
|
52
|
+
raise ValueError("Not enough data to read an Int32.")
|
|
53
|
+
|
|
54
|
+
value = int.from_bytes(self.data[self.offset:self.offset + 4], byteorder="little", signed=signed)
|
|
55
|
+
self.offset += 4
|
|
56
|
+
return value
|
|
57
|
+
|
|
58
|
+
def read_double(self):
|
|
59
|
+
if self.offset + 8 > len(self.data):
|
|
60
|
+
raise ValueError("Not enough data to read a Double.")
|
|
61
|
+
|
|
62
|
+
raw_bytes = self.data[self.offset:self.offset + 8]
|
|
63
|
+
self.offset += 8
|
|
64
|
+
return self._decode_double(raw_bytes)
|
|
65
|
+
|
|
66
|
+
def read_string_block(self, size_string=1):
|
|
67
|
+
|
|
68
|
+
length_string = int.from_bytes( self.read( size_string ), "little" )
|
|
69
|
+
string_data = self.read( length_string )
|
|
70
|
+
|
|
71
|
+
if len(string_data) != length_string:
|
|
72
|
+
raise ValueError("String does not match the declared string length.")
|
|
73
|
+
|
|
74
|
+
return string_data.decode('windows-1252')
|
|
75
|
+
|
|
76
|
+
def read_bin_coord(self, scaley=-1.0):
|
|
77
|
+
x = self.read(4)
|
|
78
|
+
y = self.read(4)
|
|
79
|
+
return CoordinatePoint( Coordinate.parse_bin(x), Coordinate.parse_bin(y, scale=scaley))
|
|
80
|
+
|
|
81
|
+
def read_unicode_text(self, length=32, encoding='utf-16-le'):
|
|
82
|
+
|
|
83
|
+
pos = self.offset
|
|
84
|
+
data = []
|
|
85
|
+
|
|
86
|
+
while len(data) < length:
|
|
87
|
+
if self.offset + 2 > len(self.data):
|
|
88
|
+
raise ValueError("Not enough data to read.")
|
|
89
|
+
|
|
90
|
+
# Read 2 bytes (1 Unicode character)
|
|
91
|
+
unicode_char = self.read(2)
|
|
92
|
+
if unicode_char == b'\x00\x00': # Null terminator
|
|
93
|
+
break
|
|
94
|
+
data.extend(unicode_char)
|
|
95
|
+
|
|
96
|
+
# Ensure we skip the remaining bytes to read exactly `length` bytes
|
|
97
|
+
self.offset = pos + length
|
|
98
|
+
|
|
99
|
+
return bytes(data).decode(encoding)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# =================================0
|
|
103
|
+
|
|
104
|
+
def _decode_double(self, raw_bytes):
|
|
105
|
+
# Decode IEEE 754 double-precision format
|
|
106
|
+
value = 0
|
|
107
|
+
for i, b in enumerate(raw_bytes):
|
|
108
|
+
value |= b << (i * 8)
|
|
109
|
+
|
|
110
|
+
sign = (value >> 63) & 0x1
|
|
111
|
+
exponent = (value >> 52) & 0x7FF
|
|
112
|
+
mantissa = value & ((1 << 52) - 1)
|
|
113
|
+
|
|
114
|
+
if exponent == 0x7FF:
|
|
115
|
+
if mantissa == 0:
|
|
116
|
+
return float('inf') if sign == 0 else float('-inf')
|
|
117
|
+
return float('nan')
|
|
118
|
+
|
|
119
|
+
if exponent == 0:
|
|
120
|
+
result = (mantissa / (1 << 52)) * (2 ** (-1022))
|
|
121
|
+
else:
|
|
122
|
+
result = (1 + (mantissa / (1 << 52))) * (2 ** (exponent - 1023))
|
|
123
|
+
|
|
124
|
+
return -result if sign == 1 else result
|
|
125
|
+
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
# =============================================================================
|
|
4
|
+
# Single Coordinate
|
|
5
|
+
# =============================================================================
|
|
6
|
+
|
|
7
|
+
class Coordinate:
|
|
8
|
+
def __init__(self, value):
|
|
9
|
+
self.value = value
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def parse_dpx(cls, key, data, scale=1.0):
|
|
13
|
+
num = int(data.get(key, 0))
|
|
14
|
+
frac = int(data.get(key + "_frac", 0))
|
|
15
|
+
|
|
16
|
+
coord = (num * 10.0 + frac / 10000.0)
|
|
17
|
+
|
|
18
|
+
return cls( scale * coord / 10 )
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def parse_bin(cls, x_bytes, scale=1.0):
|
|
22
|
+
x = int.from_bytes(x_bytes, byteorder="little", signed=True)
|
|
23
|
+
|
|
24
|
+
return cls( scale * x / 10000.0 )
|
|
25
|
+
|
|
26
|
+
def __repr__(self):
|
|
27
|
+
return f"{self.value}"
|
|
28
|
+
|
|
29
|
+
def __float__(self):
|
|
30
|
+
return float(self.value)
|
|
31
|
+
|
|
32
|
+
def __int__(self):
|
|
33
|
+
return int(self.value)
|
|
34
|
+
|
|
35
|
+
# ================== Math Functions =========================================
|
|
36
|
+
|
|
37
|
+
def __abs__(self):
|
|
38
|
+
return abs( int(self.value) )
|
|
39
|
+
|
|
40
|
+
def __truediv__(self, other):
|
|
41
|
+
if isinstance(other, (int, float)):
|
|
42
|
+
if other == 0:
|
|
43
|
+
raise ZeroDivisionError("Division by zero is not allowed.")
|
|
44
|
+
return Coordinate(self.value / other)
|
|
45
|
+
elif isinstance(other, Coordinate):
|
|
46
|
+
if other.value == 0:
|
|
47
|
+
raise ZeroDivisionError("Division by zero is not allowed.")
|
|
48
|
+
return Coordinate(self.value / other.value)
|
|
49
|
+
return NotImplemented
|
|
50
|
+
|
|
51
|
+
def __mul__(self, other):
|
|
52
|
+
if isinstance(other, (int, float)):
|
|
53
|
+
return Coordinate(self.value * other)
|
|
54
|
+
elif isinstance(other, Coordinate):
|
|
55
|
+
return Coordinate(self.value * other.value)
|
|
56
|
+
return NotImplemented
|
|
57
|
+
|
|
58
|
+
def __add__(self, other):
|
|
59
|
+
if isinstance(other, (int, float)):
|
|
60
|
+
return Coordinate(self.value + other)
|
|
61
|
+
elif isinstance(other, Coordinate):
|
|
62
|
+
return Coordinate(self.value + other.value)
|
|
63
|
+
return NotImplemented
|
|
64
|
+
|
|
65
|
+
def __sub__(self, other):
|
|
66
|
+
if isinstance(other, (int, float)):
|
|
67
|
+
return Coordinate(self.value - other)
|
|
68
|
+
elif isinstance(other, Coordinate):
|
|
69
|
+
return Coordinate(self.value - other.value)
|
|
70
|
+
return NotImplemented
|
|
71
|
+
|
|
72
|
+
def __rtruediv__(self, other):
|
|
73
|
+
return self.__div__(other)
|
|
74
|
+
|
|
75
|
+
def __rmul__(self, other):
|
|
76
|
+
return self.__mul__(other)
|
|
77
|
+
|
|
78
|
+
def __radd__(self, other):
|
|
79
|
+
return self.__add__(other)
|
|
80
|
+
|
|
81
|
+
def __rsub__(self, other):
|
|
82
|
+
return self.__sub__(other)
|
|
83
|
+
|
|
84
|
+
def __lt__(self, other):
|
|
85
|
+
if isinstance(other, Coordinate):
|
|
86
|
+
return self.value < other.value
|
|
87
|
+
elif isinstance(other, (int, float)):
|
|
88
|
+
return self.value < other
|
|
89
|
+
return NotImplemented
|
|
90
|
+
|
|
91
|
+
def __gt__(self, other):
|
|
92
|
+
if isinstance(other, Coordinate):
|
|
93
|
+
return self.value > other.value
|
|
94
|
+
elif isinstance(other, (int, float)):
|
|
95
|
+
return self.value > other
|
|
96
|
+
return NotImplemented
|
|
97
|
+
|
|
98
|
+
def __le__(self, other):
|
|
99
|
+
return self < other or self == other
|
|
100
|
+
|
|
101
|
+
def __ge__(self, other):
|
|
102
|
+
return self > other or self == other
|
|
103
|
+
|
|
104
|
+
# =============================================================================
|
|
105
|
+
# 2D Coordinate Point
|
|
106
|
+
# =============================================================================
|
|
107
|
+
|
|
108
|
+
class CoordinatePoint:
|
|
109
|
+
def __init__(self, x, y):
|
|
110
|
+
if not isinstance(x, Coordinate):
|
|
111
|
+
x = Coordinate(x)
|
|
112
|
+
if not isinstance(y, Coordinate):
|
|
113
|
+
y = Coordinate(y)
|
|
114
|
+
|
|
115
|
+
self.x = x
|
|
116
|
+
self.y = y
|
|
117
|
+
|
|
118
|
+
def __repr__(self):
|
|
119
|
+
return f"({self.x};{self.y})"
|
|
120
|
+
|
|
121
|
+
def to_int(self):
|
|
122
|
+
return CoordinatePoint( int(self.x), int(self.y))
|
|
123
|
+
|
|
124
|
+
def to_int_tuple(self):
|
|
125
|
+
return ( int(self.x), int(self.y))
|
|
126
|
+
|
|
127
|
+
def expand(self, size):
|
|
128
|
+
if isinstance(size, (int, float)):
|
|
129
|
+
return CoordinatePoint(self.x + size, self.y + size)
|
|
130
|
+
if isinstance(size, (int, Coordinate)):
|
|
131
|
+
return CoordinatePoint(self.x + size.value, self.y - size.value)
|
|
132
|
+
|
|
133
|
+
def rotate(self, center, angle):
|
|
134
|
+
|
|
135
|
+
theta = math.radians(angle)
|
|
136
|
+
x_rel = self.x - center.x
|
|
137
|
+
y_rel = self.y - center.y
|
|
138
|
+
|
|
139
|
+
x_rot = x_rel * math.cos(theta) - y_rel * math.sin(theta)
|
|
140
|
+
y_rot = x_rel * math.sin(theta) + y_rel * math.cos(theta)
|
|
141
|
+
|
|
142
|
+
self.x = x_rot + center.x
|
|
143
|
+
self.y = y_rot + center.y
|
|
144
|
+
return self
|
|
145
|
+
|
|
146
|
+
def offset(self, offset_x, offset_y):
|
|
147
|
+
|
|
148
|
+
self.x = self.x + offset_x
|
|
149
|
+
self.y = self.y + offset_y
|
|
150
|
+
return self
|
|
151
|
+
|
|
152
|
+
def copy(self):
|
|
153
|
+
return CoordinatePoint(self.x, self.y)
|
|
154
|
+
|
|
155
|
+
# ================== Math Functions =========================================
|
|
156
|
+
|
|
157
|
+
def __abs__(self):
|
|
158
|
+
return CoordinatePoint( abs(self.x), abs(self.y))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def __add__(self, other):
|
|
162
|
+
if isinstance(other, CoordinatePoint):
|
|
163
|
+
return CoordinatePoint(self.x + other.x, self.y + other.y)
|
|
164
|
+
return NotImplemented
|
|
165
|
+
|
|
166
|
+
def __sub__(self, other):
|
|
167
|
+
if isinstance(other, CoordinatePoint):
|
|
168
|
+
return CoordinatePoint(self.x - other.x, self.y - other.y)
|
|
169
|
+
return NotImplemented
|
|
170
|
+
|
|
171
|
+
def __truediv__(self, other):
|
|
172
|
+
if isinstance(other, (int, float)):
|
|
173
|
+
if other == 0:
|
|
174
|
+
raise ZeroDivisionError("Division by zero is not allowed.")
|
|
175
|
+
return CoordinatePoint(self.x / other, self.y / other)
|
|
176
|
+
elif isinstance(other, Coordinate):
|
|
177
|
+
if other.value == 0:
|
|
178
|
+
raise ZeroDivisionError("Division by zero is not allowed.")
|
|
179
|
+
return CoordinatePoint(self.x / other.x, self.y / other.y)
|
|
180
|
+
return NotImplemented
|
|
181
|
+
|
|
182
|
+
def __mul__(self, other):
|
|
183
|
+
if isinstance(other, (int, float)):
|
|
184
|
+
return CoordinatePoint(self.x * other, self.y * other)
|
|
185
|
+
elif isinstance(other, CoordinatePoint):
|
|
186
|
+
return CoordinatePoint(self.x * other.x, self.y * other.y)
|
|
187
|
+
return NotImplemented
|
|
188
|
+
|
|
189
|
+
def __rmul__(self, other):
|
|
190
|
+
return self.__mul__(other)
|
|
191
|
+
|
|
192
|
+
def __radd__(self, other):
|
|
193
|
+
return self.__add__(other)
|
|
194
|
+
|
|
195
|
+
def __rsub__(self, other):
|
|
196
|
+
return self.__sub__(other)
|
|
197
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class _MappingBase:
|
|
2
|
+
|
|
3
|
+
def __init__(self, value: int):
|
|
4
|
+
if int(value) not in self._map:
|
|
5
|
+
raise ValueError(f"Invalid value: {value}")
|
|
6
|
+
self.value = int(value)
|
|
7
|
+
self.name = self._map[int(value)]
|
|
8
|
+
|
|
9
|
+
def __repr__(self):
|
|
10
|
+
return f"{self.name}"
|
|
11
|
+
|
|
12
|
+
def to_int(self):
|
|
13
|
+
return self.value
|