Moving components with script Kicad 8.0.2

Hi,

In the thread: https://forum.kicad.info/t/move-a-component-via-python-command-line/ user keesj posted the code for “layout_using_csv.py” which I’ve put the scripting/plugins folder (running windows).

The thing is, it doesn’t seem to register under anywhere. Not in any menu, no button.

I’ve also put the example simple_plugin.py there, which is supposed to just print “hello world!”. This does have a button register in the action menu, but pressing it does nothing. Where is the “hello world!” printing to anyway?

I’m trying to move bunch of pogo pins in know coordinates, which I can easily format the way the script wants to a csv file. But it seems I’m stuck doing hundred of points manually one by one, since I can’t script the moving part. I don’t know python enough to write myself a program that would move the parts.

I’ve been trying to figure out this stuff for hours, because I want to try using kicad, but it’s quite hard when it comes to doing stuff from command line perspective. I’d be done already in few minutes if I’d hop to Eagle and just use the “move * (x y)” command and just import that with kicad, but this is not a route I want to take.

What version are you running? Post the output of Help > About KiCad > Version info Edit: ok it’s in your title.

That topic is from 2018 and almost certainly the Python API has changed so the script would have to be brought up to date. Sorry, not something I’m competent with. Others may be of help.

I can’t help with scripting either; but if you could explain in more detail, there may be other methods available.

The task is quite simple. I need to move bunch of components to locations specified the same way they’re listed in a pick’n’place file. Loading in a csv file as-is is not even requirement, but would help. I can use excel & regexp to format the list to even python commands, but currently I have no clue how to move components scripted. Examples I’ve found have been from few years ago when the scripting seems to be different.

This is a valid use case for scripting. Open your board, open the scripting console and then

Step 1: get the board object
board = pcbnew.GetBoard()

Step 2: move the footprint D7 to position (1, 2)mm for example
board.FindFootprintByReference("D7").SetPos(pcbnew.VECTOR2I_MM(1, 2))
repeat as necessary for other footprints

Step 3: update the window
pcbnew.Refresh()

Done

1 Like

If I add “import pcbnew” in the beginning, this works and does exactly what I’ve been trying to for hours. Thanks! This is definitely something I can use now.

What is the Kicad 8 way to execute a script from a file? Loads of examples have some old execfile command which doesn’t seem to work.

Execfile should work, if you show what you tried and what error you got I may be able to help more.

Also you can run the script completely outside of kicad as a standalone python program, just replace
board = pcbnew.GetBoard()
with
board = pcbnew.LoadBoard(full_file_path)
and
pcbnew.Refresh()
with
board.Save(full_file_path)

Also if you are on windows/mac then use python shipped with kicad install, not any other python you may have on your system.

2 Likes
execfile("C:\Work\test.py")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'execfile' is not defined

Also the tooltips on the pcbnew autocompletes give some wx error, but it’s not really important, since I’m not going to be doing this out of the KiPython console anyway.

exec(open(file).read())

was what I was looking for, so

exec(open("C:\\Work\\test.py").read())

works just fine

1 Like

It seems execfile was dropped in Python3 and replaced by what you showed.

2 Likes

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