Kicad 6 scripting - how to obtain/modify clipboard content

Hi, sorry if this is trivial or a noob question but I don’t really know where to look or what to search for.

Kicad seems incredibly easy to script for, as the clipboard content is essentially plain text that can be used/modified with <insert your favorite tool or script language>. Natively, it seems to support py scripts.

So I would like to make some scripts for stuff which Kicad doesn’t have but I need. Algorithmically, it often comes down to:

  1. copy something into clipboard
  2. modify that
  3. paste back into Kicad

Now even though I am not too experiences with Python, (2) seems the easiest for me. But how can I get (1) and (3) done ? So basically how to generate a variable that contains the code as if I copied-to-clipboard in Kicad and how to paste this code back to Kicad after modifying it ?

I am aware that this is probably not the smartest way to script, but it is the most intuitive for me as a beginner from absolute scratch…

1 Like

Can you ellaborate on Step #2?

Do you edit it outside of KiCad?

So the whole thing would look more like:

  1. Copy something from KiCAD into clipboard
  2. Paste it somewhere else
  3. Modify it
  4. Copy the now modified thing
  5. Paste back in KiCAD

Either way, here’s my train of thought - to copy something from KiCAD into the clipboard, even if you do the copying with code, you’d have to find a way to choose what goes into the keyboard, like selecting it for example.

My suggestion would be to copy it the regular way, then find a way to trigger a python script when the clipboard contents change (i.e. something new gets copied there) and use something like the answer here: https://stackoverflow.com/a/38171680 to get the contents of the clipboard into the script.

Then do something with them, then put the result back into the clipboard basically using the same method as above.

Then use plain paste in KiCad again to put the clipboard contents into KiCad.

Yes. Manually, I would copy the clipboard into e.g. notepad, modify it, copy-paste it back to KiCAD.

So basically I am looking for the simplest way to access this information (that you can normally obtain by Ctrl-C) in the python script environment.

As far as I have understood, KiCad scripting API doesn’t give you access to the file format or the board as text file. You use python objects representing board items. They can be accessed through the pcbnew API (which isn’t the most easy to use). You could of course write a python program – using a GUI library and even start it as a KiCad action plugin – which handles the system clipboard. It could manipulate the text in the clipboard and you could paste it back to KiCad using the normal KiCad paste feature. But you couldn’t manipulate the selection or the board in KiCad directly that way.

EDIT: I think plain Python doesn’t have access to the system clipboard; you have to use a library for it, and KiCad is tied to wxPython already, so it would be natural to use it. See wx.Clipboard — wxPython Phoenix 4.1.1 documentation.

I posted this a week ago in response to a somewhat similar post. It may give you the idea… my post is at about the middle of the full post.

Regarding doing it in Python, yes, you can and can prompt user’s input with a wx window

EDIT: The Green Window is a wx window called from a Kicad plugin.
The video in my referred-to post is a Java window called from a Kicad plugin that calls the Java App.
I prefer coding in Java…

Sorry for offtopic, @BlackCoffee, I have seen your plugin toolbar several times in the screenshots. Could you share what extensions are you using? I believe these are personal created, what problems do they solve for you? Just curious, I am not using many plugins, maybe I am missing something :slightly_smiling_face:

• The action codes are either Java or Python
• Yes, they’re my codes
• The ‘plugin’ part is well documented (just follow tutorials for the Simple Plugin and the Complex Plugin. Look at the many codes at github/elsewhere for ideas on ‘How’ to get there)
• The Problems they solve for me are performing/taking actions on more than a dozen of the things folks at this forum want to do that Kicad can’t do with the Tools it has or, is tedious and cumbersome to do manually and quickly, or can’t do…

Lastly, the real Problem they solve for me is filling the need to keep the Neural Connections growing in ways that interest me…

Probably not…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.