Scripting Issues

Running V5.0.0 on x86_64

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 also don’t see under tools any of the predefined scripts that are in the program files folder for kicad.

Seems that in some of the videos posted using these placement scripts show them and the scripts work.

Thanks for the help.

You should see something, even if just an error:

pyver.py is :

import sys

print ("hello Python %s world!" % sys.version.split()[0])

There are several kinds of scripts. Exactly which script are you trying to run?

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.

Which one?

Which one?

Which ones?

Which one? There are at least 3 versions claiming to be 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.

```
  some code
```

becomes

  some code
2 Likes

So the script you are trying to run is https://github.com/mmccoo/kicad_mmccoo/blob/master/place_by_sch/place_by_sch.py, I don’t know why you didn’t just post that link in the first place!

You need to put both files from https://github.com/mmccoo/kicad_mmccoo/tree/master/place_by_sch into the user script folder, C:\Users\*your name*\AppData\Roaming\kicad\scripting

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.

2 Likes

Sorry rather, its not an icon but the last position of the dropdown menu in the tools pallet.

I think that menu option will only appear if KiCad was built with KICAD_SCRIPTING_ACTION_MENU=ON

The plugin commands in the sub menu only appear if KiCad found valid plugins to load.

1 Like

As for program files being problematic, sure I don’t doubt it windows has been a constant disappointment since windows vista.

Thanks for the code format.

Hmm interesting on the AppData, why is this, also why is program files a problem?

What’s the difference were I to install the program on an external drive and run everything from an external “disk”…

I didn’t have the link handy. Also I don’t like GitHub, just one more thing to confuse everything and everyone in the coding world.

Just my opinion, but my code is my own and I don’t want anyone or anything having access. Just the nature of how old school I am I suppose.

Thanks for the pointers, I’ll let you know how is goes when I get around to trying these fixes.

~SGG

Yeah, I don’t have that external plugins option…

You need to download kicad v5.0.0 again from the KiCad website and install it.

Thankfully not everyone is so selfish.

3 Likes

I do have V5.0.0 installed.

Well maybe my code is involved in national security issues?

So feel free to share your code if you will.

Yeah I just reinstalled the software and still don’t see the tab under the tool dropdown.

Should I be installing with the environmental variable’s box checked or not?

Also I didn’t install the nightly build, as I assume based on the name that it’s less stable than the other “Stable” version…

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