mvnmgrx / kiutils

Simple and SCM-friendly KiCad file parser based on Python dataclasses for KiCad 6.0 and up.
GNU General Public License v3.0
78 stars 26 forks source link

Schematic can't be load if Position.angle is None for a certain Property #19

Closed MatteoTondelli closed 2 years ago

MatteoTondelli commented 2 years ago

Starting from an existing schematic I tried to add a new property with the following script:

from kiutils.schematic import Schematic
from kiutils.items.schitems import SchematicSymbol
from kiutils.items.common import Property

sch = Schematic().from_file("DummyKiCAD/DummyKiCAD.kicad_sch")
new_property = Property(key="MyProperty", id=5)
symbol[0].properties.append()
sch.to_file()

The script runs smoothly, but when I try to open the .kicad_sch file I got the following error from KiCAD:

Error loading schematic: Need a number for 'text angle' in ...path/to/file/file_name.kicad_sch

This is the line inside the file that generates the error:

(property "MyProperty" "" (id 4) (at 0.0 0.0))

If I change it like this:

(property "MyProperty" "" (id 4) (at 0.0 0.0 0))

KiCAD is able to load the schematic correctly.

The error is due to the default None value of [ANGLE] parameter in kiutils.items.common.Position class, however I think KiCAD documentation is not too clear on this topic because the angle attribute should be optional.

Maybe it can be considered a KiCAD bug, but a quick and dirty fix to the kiutils.items.common.Property class definition could be as follow:

@dataclass
class Property():
    """The `property` token defines a symbol property when used inside a `symbol` definition.

    Documentation:
        https://dev-docs.kicad.org/en/file-formats/sexpr-intro/index.html#_symbol_property
    """

    key: str = ""
    """The `key` string defines the name of the property and must be unique"""

    value: str = ""
    """The `value` string defines the value of the property"""

    id: int = 0
    """The id token defines an integer ID for the property and must be unique"""

    position: Position = Position(angle=0)
    """The `position` defines the X and Y coordinates and rotation angle of the property"""

    effects: Effects | None = None
    """The `effects` section defines how the text is displayed"""

Additional info:

mvnmgrx commented 2 years ago

This is not a bug in KiCad, but rather a limitation in kiutils. So far i am only doing end-to-end tests for parsing directly from and to files. Adding stuff to schematics or other things or building them from scratch is something i have yet to test.

Your fix should be the best solution for this problem, but let me create some test cases first :-)