Query/create active layer in pcbnew scripting API

I’m trying to write a script to load any number of .csv files containing x/y coordinates and draw the tracks on a different layer.

I’m having trouble sorting through the docs. Right now my scripts just default to F.Cu but if possible I’d like it to be able create a copper layer for each imported file.

I would like the code below to create a layer based on fname rather that F.Cu

import pcbnew
import csv
import glob

fout = 'shim-coil.kicad_pcb'
board = pcbnew.BOARD()

def add_track(ri, rf, width):
    k = 25400   # convert mils to namometers
    track = pcbnew.TRACK(board)
    track.SetStart(pcbnew.wxPoint(k*ri[0], k*ri[1]))
    track.SetEnd(pcbnew.wxPoint(k*rf[0], k*rf[1]))
    track.SetWidth(int(width*k))
    board.Add(track)


def load_pts(fname):
    """load trace coordaintes from .tsv file"""
    PTS = []
    print('Loading "' + fname + '" ...')
    with open(fname,'r') as csvfile:
        lines = csv.reader(csvfile, delimiter='\t')
        for line in lines:
            pts = [float(x) for x in line]
            PTS.append(pts)
    return(PTS)

# Import Tracks
files = glob.glob('./*.tsv')
for fname in files:
    PTS = load_pts(fname)
    for i in range(0,len(PTS)-1):
        add_track(PTS[i], PTS[i+1], 10)

board.Save(fout)
print('Exported "' + fout + '" !')

Use the SetLayer method available for tracks. You can use pcbnew layer definitions (pcbnew.F_Cu, pcbnew.In1_Cu, … , pcbnew.B_Cu)

any suggestions how to programatically query available layers or better yet ,create new layers so that I can iterate through them?

I don’t have slightest clue. Sorry

That’s fine, it seems that the layers are already present, they just need to be activated in pcbnew upon opening the editor.

You don’t really “create new layers”, board file stores list of enabled layers.

BOARD object has SetEnabledLayers()

You can use them like this

board = pcbnew.BOARD()
s = board.GetEnabledLayers()
print(list(s.Seq()))
s.AddLayer(pcbnew.In4_Cu)
board.SetEnabledLayers(s)
print(list(s.Seq()))

This prints

[0, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[0, 4, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

These IDs correspond to this enum

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