Opening the scripting terminal I see version 0.9.8
I’ve dumped pasted scripts into
C:\Program Files\KiCad\share\kicad\scripting\plugins
as well as in the working directory for the current project I’m working on.
Seems that when I run execfile(’ ')
nothing happens, I’ve tried changing the path to the various paths for the script
Py 0.9.8
Python 2.7.14 (default, Jan 24 2018, 14:36:54) [GCC 7.2.0 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
Startup script executed: C:\Users\computer\AppData\Roaming\kicad\PyShell_pcbnew_startup.py
Py 0.9.8
Python 2.7.14 (default, Jan 24 2018, 14:36:54) [GCC 7.2.0 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
Startup script executed: C:\Users\computer\AppData\Roaming\kicad\PyShell_pcbnew_startup.py
cd C:\Program Files\KiCad\share\kicad\scripting\plugins
C:\Program Files\KiCad\share\kicad\scripting\plugins
I was trying to run a placement script to place components in a similar format as is in the schematic.
Mainly the issue is that I’ve placed the scripts in the python scripting folder then invoke them nothing happens.
I don’t even see the ones that are installed with the installation of KiCad when initially installing the software, I’ll note again that I’m running KiCad V5.0.0.
Okay I’ll try and get images of how I was going about it.
However the first problem is the icon that lets you access the scripts though the PCB viewer is not present.
In the previous version that I’d had installed and in screen shots of folks that were talking about the use of scripting had shown that there should be an icon to access the scripts to complete such actions as PCB placement based on the Eschema and the few scripts that were installed by default on the previous version of KiCad V4.x.x or something that isn’t V5.0.0
I think the path that I was using was this “C:\Program Files\KiCad\share\kicad\scripting\plugins” and this is what was mentioned on the few pages that I’d read though.
So, what I’d done was take the following code, and dump it into a text file and append the file with a .py to have it sometimes format to the proper icon that is associated with the python application that is needed to be installed on windows.
Nonetheless, I was able to “run” the program however there was nothing that seemed to have happened and certainly nothing within KiCad that placed the components in a manner that was similar to the layout of the schematic.
import pcbnew
import os.path
import re
def PlaceBySch():
board = pcbnew.GetBoard()
board_path = board.GetFileName()
sch_path = board_path.replace(".kicad_pcb", ".sch")
if (not os.path.isfile(sch_path)):
raise ValueError("file {} doesn't exist".format(sch_path))
# some documentation on eeschema file format
# https://en.wikibooks.org/wiki/Kicad/file_formats#Schematic_Files_Format
# schematic files are written here:
# eeschema/sch_legacy_plugin.cpp
# rotation is given here:
# TRANSFORM transform = aComponent->GetTransform();
# m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
# transform.x1, transform.y1, transform.x2, transform.y2 );
# $Comp
# L Device:LED D49
# U 1 1 5A3B7115
# P 6650 1500
# F 0 "D49" H 6641 1716 50 0000 C CNN
# F 1 "LED" H 6641 1625 50 0000 C CNN
# F 2 "Miles:LED_5730" H 6650 1500 50 0001 C CNN
# F 3 "~" H 6650 1500 50 0001 C CNN
# 1 6650 1500
# 1 0 0 -1
# $EndComp
newcomp_p = re.compile('\$Comp')
endcomp_p = re.compile('\$EndComp')
comp_label_p = re.compile('L (\S+) (\S+)')
trans_p = re.compile('\t([\-0-9]+)\s+([\-0-9]+)\s+([\-0-9]+)\s+([\-0-9]+)')
footprint_p = re.compile('F 2 "(\S+)" [VH] (\d+) (\d+)')
c1 = '$Comp'
l1 = 'L Device:LED D49'
t0 = ' 1 0 0 -1'
t0f = ' 1 0 0 1'
t180 = ' -1 0 0 1'
t180f = ' -1 0 0 -1' # this is flipped horizontally
tM90 = ' 0 1 1 0'
t90 = ' 0 -1 -1 0'
orientations = {
str(trans_p.match(t0).groups()): 0,
str(trans_p.match(t0f).groups()): 0, # TWS added to handle flipped, but no rotation
str(trans_p.match(t180).groups()): 180,
str(trans_p.match(t180f).groups()): 180,
str(trans_p.match(tM90).groups()): -90,
str(trans_p.match(t90).groups()): 90
}
def parse_eeschema(filename):
retval = {}
with open(filename) as f:
incomp = False
curcomp = "";
x = -1
y = -1
orient = 0;
for line in f:
bc = newcomp_p.match(line)
ec = endcomp_p.match(line)
l = comp_label_p.match(line)
t = trans_p.match(line)
fp = footprint_p.match(line)
if (bc):
incomp = True
#print("new comp")
elif (ec):
incomp = False
retval[curcomp] = [x, y, orient]
x = -1
y = -1
curcomp = ""
orient = 0;
#print("end comp")
if (not incomp):
continue
if (l):
curcomp = l.groups()[1];
#print("l {} {}".format(l.groups()[0], l.groups()[1]))
elif (t):
orient = orientations[str(t.groups())]
#print("orient {}".format(orient))
elif (fp):
x = int(fp.groups()[1])
y = int(fp.groups()[2])
#print("location {} {}".format(x,y))
return retval;
locs = parse_eeschema(sch_path)
for mod in board.GetModules():
ref = mod.GetReference()
if (ref not in locs):
print("couldn't get loc info for {}".format(ref))
continue
# eeschema stores stuff in 1000ths of an inch.
# pcbnew stores in 10e-6mm
# 1000ths inch * inch/1000ths inch * 25.4mm/inch * 10e6
# oldvalue * 25.4 / 10e4
newx = locs[ref][0] * 25.4 * 1000.0
newy = locs[ref][1] * 25.4 * 1000.0
mod.SetPosition(pcbnew.wxPoint(int(newx), int(newy)))
mod.SetOrientation(locs[ref][2]*10)
print("placing {} at {},{}".format(ref, newx, newy))
# when running as a plugin, this isn't needed. it's done for you
#pcbnew.Refresh();
I have never seen such an icon, I don’t think there is one.
Program Files is a really problematic location on Windows, if you put files there Windows actually puts them in the Virtual Store, and applications can’t see them. Rule of thumb is avoid putting your user files in Program Files.
The place to put scripts is C:\Users\your name\AppData\Roaming\kicad\scripting, but beware the AppData folder is hidden by default.
How to format code
Put triple backtick (not apostrophe!) on first line, then code, then another
triple backtick, e.g.
You also need the version of V5.0.0 that has action scripts enabled, the only way to tell is to look at About Kicad and then Show Version Info. You must have :
KICAD_SCRIPTING_ACTION_MENU=ON
That file won’t do anything by itself, it has no “main” code. In this case __init__.py is needed to register the script in pcbnew.