Python scripting layer information

Hello,

I am currently writing a Python script to automate kicad project creation, archiving and fabrication files generation.

I’d like to generate a Stackup file, containing information about each layer (thickness for instance).

I didn’t find a Python equivalent to this C++ class, that contains the functions I want to use (c++ Documentation).

Is it possible to use the C++ functions inside a Python script?
Or is there another way of obtaining the data I need?

Thanks

Not every cpp class is exported in the python api.

What you want is possible but not straightforward, here are a few options:

  1. Read and parse the s-expression data of the pcb file in python. Data you are looking for is in (setup (stackup)) entry.
  2. Patch kicad code to add the classes you need to swig interface so they are exported in the api, then use the api
  3. It’s probably possible to load the right symbols from dll/so directly using ctypes or alike. Kicad .kiface files are just dlls.

Forcing myself to start programming in Python was like pulling teeth. But, once started, it took about 1 hour. I will assume you’re skilled enough with programming C++ and minimally-skilled with Python.

Comparing Python to C/C++, Java,…etc: Programming in various languages is about the same - the main difference is the Syntax.
Meaning all the stuff like Loops, Conditional’s, etc… is mostly the same/similar - it’s mostly the words you use…

Thus, if you know C++, You can become a Python programmer very quickly by reviewing some Python codes, Kicad plugins (and a few tutorials online). That’s how I converted my programming knowledge and was able to quickly make KiCad Python plugins and Stand-Alone App’s to manipulate Kicad files (Kicad is human readable text).

Quick example below shows reading a Kicad PCB file. Naturally, once read, you can manipulate and do whatever with the data…

Note: I use wxFormBuilder to build the GUI (FYI - formbuilder can generate C++ GUI code (if desired). Screenshot below). The code for the GUI stuff is in a separate for cleaner programming.

The Content reader/lister is called from a Kicad Plugin (green Icon in video). I used quickly made minimal circuit for the example (thus, minimal list of contents to read…)

Also, if you really don’t want to do most of the programming in Python, build a C++ Executable App and Run it from a Kicad Plugin. Easy to do (one line of code to run it).

contentReader

Thanks you for your accurate answer.

I hadn’t thought of your first solution, but it seems clever and easy.
I’ll go with this one for now.

The script is meant to be used by multiple people, so the second one wont do.

I’ve seen KiCad .kicad_pcb files with and without lines breaks, I’ll work around this problem.

Thanks again.
Ccrea

This may help you https://github.com/mvnmgrx/kiutils

Link to useful Python Cytype’s info

Yeah, KiCad is my first real experience with Python, I mainly use Java and C++.

I also used wxFormBuilder for the GUI. It took about week to get used to it, but my Python application works fine.

My problem was retrieving data from automatically generated SWIG objects.

I was thinking of reading the memory address, but your and qu1ck’s posts made me realize reading the KiCad files is a better solution.

Running C++ from Python would be easier for me, but I didn’t find the C++ classes I need in KiCad dlls.

Thanks you for your insight.
Ccrea

FYI - You’re skilled enough to discover for yourself the various differences in Python’s ‘Print’, String_Tools and other output’s. And, their differences in how the desired data is pumped out…

It took some demo coding to get a handle on it and I offer a glimpse of only a couple of items. Worth noting the various String manipulations and how they handle NewLines…

My example (vid) shows Newlines between lines because that’s how I wanted them. But, ‘No’ Newlines is easy… If you look at Kicad’s files, you see they do Not contain Newlines between lines.

Screen Shot 2022-06-26 at 9.55.19 AM