KiCad 9.0 Python API

Thanks! I’ll do it right away.

I’m back with a few questions. Maybe I found another bug, maybe I did something wrong. I updated to the most recent nightly and relaunched KiCad 9. The two addons (mine and rounded tracks) are still in the folder.

After launching KiCad, a terminal window appeared:

And the KiCad UI took forever to load. At some point the UI was properly loaded though. I was unable to close the terminal window though and my systems CPU usage was at 100% until the KiCad UI was built (took around 20 seconds).

This is the process of the terminal window:

Closing it via the task manager worked. However, after restarting KiCad again, the issue with the terminal popup could not be reproduced. The slow loading time stayed unchanged though.

The plugin and content manager detects both plugins as installed:
image

However, in the UI only round tracks is shown (maybe there is another issue with my plugin, I don’t know. It looks good to me and KiCad doesn’t throw any errors, so I’m not sure where to look).

Pressing the round tracks UI doesn’t do anything though. @albin reported some issues with round tracks as well, also with a strange terminal. However, their obvservations were different than mine.

Ah. the very slow loading seems to be tied to an anti malware checker at my work PC:

image

I also just noticed that every action in KiCad triggers this, even just launching the PCB editor takes solid 30 seconds

I had similar issues the first time I opened the Nightly build with the round tracks example placed in the plugins directory. But after the first time it has worked okay, although the loading time seems to be longer sometimes. I meant to re-do these steps to be able to report it properly, but haven’t gotten to it yet.

For me the round tracks example works, but I believe that I have to open KiCad 9.0 Command Prompt and run pip install kicad-python and pip install wxPython, or pip install -r requirements.txt on the file in the round tracks folder. As I understand it, this has to be done if the folder is simply placed in the plugins folder, but not if the plugin is installed through PCM.

The problems I’m having with round tracks is only when I compile kicad-python myself, it works fine with kicad-python from pipy using pip.

I get the round tracks icon in PCB Editor, but it’s not listed as installed in PCM. Did you install it using PCM, or simply place it in plugins folder? I placed it in C:\Users\denne\Documents\KiCad\9.0\plugins (not C:\Users\denne\Documents\KiCad\9.0\scripting\plugins), because the documentation says so.

I tried it 10 times so far, the windows malware checker always chimes in.

ahh! I will try this later today and report back. I did not manually install any dependencies. Thank you!

I manually installed it by dropping it in the folder (after I downloaded it from GitLab). I used this path: <user documents>/KiCad/<version>/3rdparty/plugins/

Ah! That explains it. I moved it to that folder and now it shows up as installed for me as well.

1 Like

You will need to use the debugging tools listed in the dev docs site to get more info.

Although @Mineotopia I don’t think KiCad can do much about your antivirus

1 Like

That’s the PCM-controlled path. I would just use /KiCad/<version>/plugins for things you are manually adding.

This is not accurate – you need to do this if you are trying to use an unreleased version of kicad-python, where you might need to install from a locally-built wheel. You don’t need to do this if the plugin can use the version of kicad-python on PyPI, whether installed by PCM or not.

I created an issue for this: Kicad 9 Launch very Slow (due to Malware Check) (#19569) · Issues · KiCad / KiCad Source Code / kicad · GitLab

It is the windows built-in antivirus afaik

These dev docs are quite good, but I’m still unable to get round tracks to work. I’m on the most recent nightly combined with the most recent round_tracks version.

This is the only content of the command line, it is printed when launching the PCB editor. Clicking the “round tracks” button does nothing, there is no error.

Edit: The same goes for my coil generator, there is no error, but nothing happens. The icon is shown in the UI though

@albin was right though. As soon as I entered pip install -r requirements.txt in the “KiCad command prompt” it started working. And it did not work before. I didn’t compile anything myself, I only installed the nightly and dropped the plugin in the corresponding folder.

I still don’t get any errors in the console though, because round tracks is now working, coil gen is still broken

Okay @craftyjon. I spent the last few hours debugging KiCad together with our IT guy and I think we got it. I updated the issues and created new ones. I think all of it is tied to the fact that the automatic dependency creation is broken (on Windows).

2 Likes

@craftyjon if a plugin supports both the SWIG API and the IPC API in the same version of the plugin, is it possible to release it for both KiCad <9 and >=9?

Will it become a problem for KiCad 9 if the plugin has both the new plugin.json and the old init.py with action plugin registration?

I could publish two archives to the PCM and use kicad_version and kicad_version_max in the metadata.json to control which archive should be used, but will PCM be okay with me adding two archives with the same version information? It would be strange setting different versions of they are indeed the same version with just this difference.

I haven’t tested it, but I imagine that both actions would show up. I’d probably recommend using either different versions or different branches of your plugin.

I think you would need to at least use different version numbers. This would be helpful for your users too, I suspect, since they might run into bugs that only affect the new version for example.

That’s what I assumed. The reason I’m asking is that I’m planning to write a “plugin” that won’t use any of the APIs. But I still want to publish it to PCM and register it so that it can be loaded from PCB Editor. If I release two different versions, the only difference will be plugin.json vs init .py

You can make a single release that supports both old and new API, just check for kicad version in the __init__.py and don’t register the action if kicad is >= 9.0. That way only the IPC action will be active.

4 Likes

That’s a great idea! Thank you!

@craftyjon Just a big thank you for the updates you did in the last few days. It has changed from barely working to almost perfect on my machine. Thank you!

1 Like

Okay, I have a pretty specific question about the code of our coil generator. I got the UI working with the new API, that’s great. But now I struggle with the actual coil generation.

The current approach was hacky to be honest. We generated a footprint S-Expression, added it to the clipboard and triggered a paste to the editor. This doesn’t seem to work anymore. Maybe it is fixable, but I did not yet manage to fix it.

Looking through the examples, I noticed that there is probably a new and preferred way of doing exactly that.

for copper_layer in copper_layers:
    layer_text = BoardText()
    layer_text.layer = copper_layer.layer
    layer_text.value = "%d" % layer_idx
    layer_text.position = Vector2.from_xy(offset, 0)
    layer_text.attributes = defaults.text
    layer_text.attributes.visible = True
    fp.add_item(layer_text)

    padding = 1 if layer_idx == 9 else 0.5
    item_width = int((len(layer_text.value) + padding) * char_width)

    offset += item_width
    layer_idx += 1

created = [cast(FootprintInstance, i) for i in board.create_items(fpi)]

if len(created) == 1:
    board.interactive_move(created[0].id)

This looks to me as if this is the intended way of adding stuff to the board on the fly. But before I spent hours following this approach, I wanted to get some feedback on this. Mainly these two questions:

  1. Is this the correct approach? Am I able to create arcs, lines etc the same as I previously did in scripting?
  2. Am I able to save this then generated footprint in a footprint file?

Info: The current state of the SWIG to IPC conversion can be seen here: Update to IPC Python API by TimGoll · Pull Request #17 · DIaLOGIKa-GmbH/kicad-coil-creator · GitHub