Move a component via (python) command line?

Is there a way to quickly snap a component to the cursor via (python) command line ? I’m used to the powerful command line that comes with EAGLE. There I can run something like

move R4

which attaches R4 to the cursor so that there is no need to search for it.
Thanks.

within eeschema or pcbnew?

if you open cvpcb from within eeschema and click on the component in the list there eeschema will focus on the part - at least in BZR6097.

if you have eeschema and pcbnew open at the same time, clicking on a component in either program will focus the other programs view on that component.

Have you tried using shortcut ‘T’ in Pcbnew, it will open a window where you type the component ref like R4 and it will snap to your cursor position and you can move and place it where you want.

I hope this is what you are looking for.

2 Likes

Hi,

In 2018 it is possible to use the python bindings to load / edit / save the file.
http://docs.kicad.org/stable/en/pcbnew.html#_kicad_scripting_reference

I here is a (simple) example that places D1 at 100,100 with a 45 degree rotation

#!/usr/bin/env python
import pcbnew

# Load the board
pcb = pcbnew.LoadBoard("take2.kicad_pcb")

# Find the component
c = pcb.FindModuleByReference("D1")

# Place it somewhere
c.SetPosition(pcbnew.wxPointMM(100,100))

# Rotate it (angle in 1/10 degreee)
c.SetOrientation(45 * 10)

# and save the file under a different name
pcb.Save("take2_mod.kicad_pcb")

This does require you to close the project and run from the command line. pcbnew also has a plugin system (that I tried yesterday based on sample code found here)


Combing this knowledge it should not be very hard any more to create a “python console” to execute the changes on a running document.

For now I am (generating) a layout.cvs file(with component name , x , y , angle)

D1, 133.00, 67.00, 270.00
D2, 130.40, 67.23, 280.00
D3, 127.87, 67.90, 290.00
D4, 125.50, 69.01, 300.00
D5, 123.36, 70.51, 310.00
D6, 121.51, 72.36, 320.00

and the following python script

#!/usr/bin/env python
import sys
import pcbnew
import numpy as np

data  = np.genfromtxt('layout.csv',delimiter=',',dtype=None)
print (data)
filename="take2.kicad_pcb"
pcb = pcbnew.LoadBoard(filename)

for i in data:
    c = pcb.FindModuleByReference(i[0])
    c.SetPosition(pcbnew.wxPointMM(float(i[1]),float(i[2])))
    c.SetOrientation(1.0 * i[3] * 10)

pcb.Save("take2_mod.kicad_pcb")

Additionally I figured out it is possible to do the same on an existing project. for that create a file in ~/.kicad/scritping/plugins called “layout_using_csv.py” with the following contents

import pcbnew

import Tkinter
import numpy as np
from tkFileDialog import askopenfilename

import os

class PlaceMyComponent(pcbnew.ActionPlugin):
    """
    contents of the layout.csv should be something like this (reference name, posx posy, angle * 10)

    D10, 100.00, 69.00, 450.00
    D9, 98.09, 69.17, 460.00
    D8, 96.24, 69.66, 470.00
    D7, 94.50, 70.47, 480.00
    """
    def defaults( self ):
        self.name = "Layout using CSV"
        self.category = "Modify PCB"
        self.description = "Move the components to match the layout.csv file in the project diretory"

    def Run( self ):
        print(os.environ['KIPRJMOD'])
        pcb = pcbnew.GetBoard()
        data  = np.genfromtxt( "%s%s%s" %(os.environ['KIPRJMOD'], os.path.sep  ,'layout.csv'),delimiter=',',dtype=None)
        print (data)
        for i in data:
            c = pcb.FindModuleByReference(i[0])
            c.SetPosition(pcbnew.wxPointMM(float(i[1]),float(i[2])))
            c.SetOrientation(1.0 * i[3] * 10)


PlaceMyComponent().register()

To place my components where I like.

2 Likes