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.
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 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)
#!/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()