realthunder / kicad_parser

KiCAD parser written in python
MIT License
15 stars 14 forks source link

Parse Kicad Schematics hierarchy #4

Open leoheck opened 2 years ago

leoheck commented 2 years ago

Hi @realthunder

I am trying to parse the hierarchy of Kicad schematics, which is not a direct task. Then I am using your parser to tackle this issue.

I can get data from sheet instances... but sometimes the parser returns things that are not SexpParser objects. Is this correct?. This happens in the extra .kicad_sch files, but not with the main .kicad_sch

<class 'submodules.kicad_parser.sexp_parser.sexp_parser.SexpParser'>
<class 'submodules.kicad_parser.sexp_parser.sexp_parser.SexpParser'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>

Then I am using this try...expect to solve that. But I would like to check with you if this is the correct behavior of kicad_parser.

  # (sheet (at 114.3 86.36) (size 36.83 20.32) (fields_autoplaced)
  #   (stroke (width 0.1524) (type solid) (color 0 0 0 0))
  #   (fill (color 0 0 0 0.0000))
  #   (uuid 005b247a-5ac7-4cdf-ac41-9dda744d73ba)
  #   (property "Sheet name" "Page_2_Instance_2" (id 0) (at 114.3 85.6484 0)
  #     (effects (font (size 1.27 1.27)) (justify left bottom))
  #   )
  #   (property "Sheet file" "sch/page_2.kicad_sch" (id 1) (at 114.3 107.2646 0)
  #     (effects (font (size 1.27 1.27)) (justify left top))
  #   )
  # )

    sch = KicadPCB.load(sch_file_path)

    sheet_name = 0
    sheet_file = 1

    for i, k in enumerate(sch.sheet):
        try:
            print(i, k['uuid'], k['property'][0][1], k['property'][1][1])
        except:
            # sometimes it returns strings.. 
            pass

My current result to show UUID, Sheet Instance Name, and Sheet Instance File is something like this.

0 0999d842-9bc9-4370-bae4-7c28aeba0392 "A" "sch/a.kicad_sch"
1 6483d8c3-df4d-40b4-b616-c993cba432d4 "A1" "sch/a.kicad_sch"
leoheck commented 2 years ago

Ah, I found out that if there are multiple instances of sheets inside the schematic, the parser returns a SexpList, but sometimes there is just a single instance, where the parser returns a single object and then there are times where there is no instance. So, I have to treat each case differently.

realthunder commented 2 years ago

This problem is mentioned in the Readme, paragraph starting with If you are not sure whether a key in the object model holds a single expression.

What KicadPCB does is adding a set of keywords to a class attribute _defaults. So, you can either modify _defaults directly, or write your own class deriving from KicadPCB and override _defaults.

The _defaults attribute is read by sexp_parser.SexpParser here. Check the docstring of sexp_parser.Sexp._addDefaults() for more information.

leoheck commented 2 years ago

Nice, thanks for a detailed explanation. This repo could have a kicad_sch.py too, what do you think? I will try to make a simple one for me, then I could make a PR if you think it is a valid idea.

leoheck commented 2 years ago

Since I have started this I will keep a (Work in progress) PR here so we can collaborate, if you are not interested just close it and it is ok. Cool?