pyaltiumlib 0.1.0__tar.gz

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.
Files changed (53) hide show
  1. pyaltiumlib-0.1.0/LICENSE.txt +21 -0
  2. pyaltiumlib-0.1.0/PKG-INFO +69 -0
  3. pyaltiumlib-0.1.0/README.md +48 -0
  4. pyaltiumlib-0.1.0/setup.cfg +4 -0
  5. pyaltiumlib-0.1.0/setup.py +37 -0
  6. pyaltiumlib-0.1.0/src/pyaltiumlib/__init__.py +58 -0
  7. pyaltiumlib-0.1.0/src/pyaltiumlib/base.py +172 -0
  8. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/__init__.py +48 -0
  9. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/binaryreader.py +125 -0
  10. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/coordinate.py +197 -0
  11. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/mapping.py +13 -0
  12. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/parametercollection.py +157 -0
  13. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/parametercolor.py +27 -0
  14. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/parameterfont.py +19 -0
  15. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/pcblayerdefinition.py +109 -0
  16. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/pcbmapping.py +75 -0
  17. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/schematicmapping.py +167 -0
  18. pyaltiumlib-0.1.0/src/pyaltiumlib/datatypes/schematicpin.py +55 -0
  19. pyaltiumlib-0.1.0/src/pyaltiumlib/libcomponent.py +187 -0
  20. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/__init__.py +1 -0
  21. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/footprint.py +90 -0
  22. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/lib.py +66 -0
  23. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/PCBComponentBody.py +25 -0
  24. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/PCBPad.py +320 -0
  25. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/PCBString.py +212 -0
  26. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/PCBTrack.py +122 -0
  27. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/__init__.py +19 -0
  28. pyaltiumlib-0.1.0/src/pyaltiumlib/pcblib/records/base.py +61 -0
  29. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/__init__.py +8 -0
  30. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/lib.py +92 -0
  31. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchArc.py +111 -0
  32. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchBezier.py +124 -0
  33. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchComponent.py +81 -0
  34. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchDesignator.py +42 -0
  35. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchEllipse.py +72 -0
  36. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchEllipticalArc.py +112 -0
  37. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchImplementationList.py +42 -0
  38. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchLabel.py +106 -0
  39. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchLine.py +65 -0
  40. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchParameter.py +32 -0
  41. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchPin.py +150 -0
  42. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchPolygon.py +111 -0
  43. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchPolyline.py +106 -0
  44. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchRectangle.py +73 -0
  45. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/SchRoundRectangle.py +79 -0
  46. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/__init__.py +40 -0
  47. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/records/base.py +136 -0
  48. pyaltiumlib-0.1.0/src/pyaltiumlib/schlib/symbol.py +127 -0
  49. pyaltiumlib-0.1.0/src/pyaltiumlib.egg-info/PKG-INFO +69 -0
  50. pyaltiumlib-0.1.0/src/pyaltiumlib.egg-info/SOURCES.txt +51 -0
  51. pyaltiumlib-0.1.0/src/pyaltiumlib.egg-info/dependency_links.txt +1 -0
  52. pyaltiumlib-0.1.0/src/pyaltiumlib.egg-info/requires.txt +2 -0
  53. pyaltiumlib-0.1.0/src/pyaltiumlib.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Chris Hoyer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.2
2
+ Name: pyaltiumlib
3
+ Version: 0.1.0
4
+ Summary: PyAltiumLib is a tool to read Altium Designer library files.
5
+ Home-page: https://github.com/ChrisHoyer/pyAltiumLib.git
6
+ Author: Chris Hoyer
7
+ Author-email: info@chrishoyer.de
8
+ Requires-Python: >=3.7
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE.txt
11
+ Requires-Dist: olefile>=0.47
12
+ Requires-Dist: svgwrite>=1.4.3
13
+ Dynamic: author
14
+ Dynamic: author-email
15
+ Dynamic: description
16
+ Dynamic: description-content-type
17
+ Dynamic: home-page
18
+ Dynamic: requires-dist
19
+ Dynamic: requires-python
20
+ Dynamic: summary
21
+
22
+ # pyAltiumLib
23
+
24
+ PyAltiumLib is a tool to read Altium Designer library files. The included components are extracted. Metadata such as the description and the name of the component can be listed. It is also possible to visualize the component using the svgwrite package.
25
+
26
+ See full documentation here: [ReadTheDocs](https://pyaltiumlib.readthedocs.io/)
27
+
28
+ ## Example - Read the File and Retrieve Components
29
+
30
+ Load the file and retrieve the list of components.
31
+
32
+ ```python
33
+ import pyaltiumlib
34
+
35
+ # Path to the .schlib or .pcblib file
36
+ filepath = "Libfile.schlib"
37
+
38
+ # Load File
39
+ LibFile = pyaltiumlib.read(filepath)
40
+
41
+ # Read Meta Data
42
+ json = LibFile.read_meta()
43
+
44
+ # Get a List of All Components
45
+ all_parts = LibFile.list_parts()
46
+ ```
47
+
48
+ ## Example - Render Components as SVG
49
+
50
+ Render each component as an SVG file. The components are saved as individual SVG files in the img_sch directory.
51
+
52
+ ```python
53
+ import svgwrite
54
+
55
+ # Iterate over all components to draw them
56
+ for partname in all_parts:
57
+
58
+ # Choose element
59
+ Component = LibFile.get_part(partname)
60
+
61
+ # Create a new image with a white background
62
+ dwg = svgwrite.Drawing(f"img_sch/{partname}.svg", size=(400, 400))
63
+
64
+ # Draw component on svg drawing with size 400px 400px
65
+ Component.draw_svg(dwg, 400, 400)
66
+
67
+ # Save the SVG
68
+ dwg.save()
69
+ ```
@@ -0,0 +1,48 @@
1
+ # pyAltiumLib
2
+
3
+ PyAltiumLib is a tool to read Altium Designer library files. The included components are extracted. Metadata such as the description and the name of the component can be listed. It is also possible to visualize the component using the svgwrite package.
4
+
5
+ See full documentation here: [ReadTheDocs](https://pyaltiumlib.readthedocs.io/)
6
+
7
+ ## Example - Read the File and Retrieve Components
8
+
9
+ Load the file and retrieve the list of components.
10
+
11
+ ```python
12
+ import pyaltiumlib
13
+
14
+ # Path to the .schlib or .pcblib file
15
+ filepath = "Libfile.schlib"
16
+
17
+ # Load File
18
+ LibFile = pyaltiumlib.read(filepath)
19
+
20
+ # Read Meta Data
21
+ json = LibFile.read_meta()
22
+
23
+ # Get a List of All Components
24
+ all_parts = LibFile.list_parts()
25
+ ```
26
+
27
+ ## Example - Render Components as SVG
28
+
29
+ Render each component as an SVG file. The components are saved as individual SVG files in the img_sch directory.
30
+
31
+ ```python
32
+ import svgwrite
33
+
34
+ # Iterate over all components to draw them
35
+ for partname in all_parts:
36
+
37
+ # Choose element
38
+ Component = LibFile.get_part(partname)
39
+
40
+ # Create a new image with a white background
41
+ dwg = svgwrite.Drawing(f"img_sch/{partname}.svg", size=(400, 400))
42
+
43
+ # Draw component on svg drawing with size 400px 400px
44
+ Component.draw_svg(dwg, 400, 400)
45
+
46
+ # Save the SVG
47
+ dwg.save()
48
+ ```
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,37 @@
1
+ from pathlib import Path
2
+ from setuptools import setup, find_packages
3
+
4
+ AUTHOR_NAME = 'Chris Hoyer'
5
+ AUTHOR_EMAIL = 'info@chrishoyer.de'
6
+
7
+ ROOT = Path(__file__).resolve().parent
8
+
9
+ # get latest version from source code
10
+ def get_version():
11
+ v = {}
12
+ for line in (ROOT / "src" / "pyaltiumlib" / "__init__.py").read_text().splitlines():
13
+ if line.strip().startswith('__version__'):
14
+ exec(line, v)
15
+ return v['__version__']
16
+ raise IOError('__version__ string not found')
17
+
18
+ # get description from readme.md
19
+ def get_description():
20
+ long_description = ""
21
+ with open("README.md", "r") as f:
22
+ long_description = f.read()
23
+ return long_description
24
+
25
+ setup(name='pyaltiumlib',
26
+ version=get_version(),
27
+ description='PyAltiumLib is a tool to read Altium Designer library files.',
28
+ url='https://github.com/ChrisHoyer/pyAltiumLib.git',
29
+ author=AUTHOR_NAME,
30
+ author_email=AUTHOR_EMAIL,
31
+ packages=find_packages(where="src"),
32
+ install_requires=['olefile>=0.47', 'svgwrite>=1.4.3'],
33
+ package_dir={"": "src"},
34
+ python_requires=">=3.7",
35
+ long_description=get_description(),
36
+ long_description_content_type="text/markdown",
37
+ )
@@ -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
+
@@ -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
+