Does anyone know how to set all the selected line widths in one go?

When I import DXF files the line width defaults to 2.54mm. On the OS X version I can only change the widths one line at a time, except for one time I selected the lines and an option appeared to set them all at once to the default width set in Preferences. I cannot find that option again.

Does anyone know how to set all the selected line widths in one go?

1 Like

Besides using a text editor…

Is python accepted:

import pcbnew

board = pcbnew.GetBoard()

for drw in board.GetDrawings():
    if drw.IsSelected():
        drw.SetWidth(int(0.1 * 1e6))

Above snippet sets line width for all selected drawing elements (lines etc.) to 0.1 mm. Note that 1e6 multiplier is required because internal units of kicad is nanometers.

3 Likes

I was hoping for something in the UI, but if it works….

Thanks,
Ron

and this script is useful to change Layer of selected lines

import pcbnew
board = pcbnew.GetBoard()

Dwgs_User=40

for drw in board.GetDrawings():
   if drw.IsSelected():
       drw.SetLayer(Dwgs_User)

where Layers nbrs are:
(0 F.Cu signal)
(31 B.Cu signal)
(32 B.Adhes user)
(33 F.Adhes user)
(34 B.Paste user)
(35 F.Paste user)
(36 B.SilkS user)
(37 F.SilkS user)
(38 B.Mask user)
(39 F.Mask user)
(40 Dwgs.User user)
(41 Cmts.User user)
(42 Eco1.User user hide)
(43 Eco2.User user)
(44 Edge.Cuts user)
(45 Margin user hide)
(46 B.CrtYd user)
(47 F.CrtYd user)
(48 B.Fab user)
(49 F.Fab user)

By the way pcbnew module has definitions for layer IDs, you don’t have to re-define them. Here is the full list:

F_Cu
In1_Cu
In2_Cu
In3_Cu
In4_Cu
In5_Cu
In6_Cu
In7_Cu
In8_Cu
In9_Cu
In10_Cu
In11_Cu
In12_Cu
In13_Cu
In14_Cu
In15_Cu
In16_Cu
In17_Cu
In18_Cu
In19_Cu
In20_Cu
In21_Cu
In22_Cu
In23_Cu
In24_Cu
In25_Cu
In26_Cu
In27_Cu
In28_Cu
In29_Cu
In30_Cu
B_Cu
B_Adhes
F_Adhes
B_Paste
F_Paste
B_SilkS
F_SilkS
B_Mask
F_Mask
Dwgs_User
Cmts_User
Eco1_User
Eco2_User
Edge_Cuts
Margin
B_CrtYd
F_CrtYd
B_Fab
F_Fab

You can use them like this; pcbnew.F_Cu

These definitions could be missing from the stable release though, I’m not sure.

3 Likes

Hi, dumb question. I’ve tried this but can’t seem to get it to work. I select my lines I want to change in OpenGL view, drop this code into the Shell tab of the Scripting console, hit enter and nothing changes. Am I missing a step? I never use the scripting console, just CLI from an xterm. This would be a great way of doing things if I could get it to work:)

Well, it seems that if I use the shell and run this using execfile(“thatfile.py”) it works. Not sure why it wouldn’t work the other way… very confusing. Anyway, nice to know that I can do this within pcbnew, this make life much easier!!!

In most cases, you have to refresh the canvas to see the changes. Currently only way I know for doing this which doesn’t involve editing the board is to switch canvases back and forth.

Funny, I tried that with just dropping the code into the shell, but it didn’t seem to work. Tried F3/redraw with no luck as well. Running the scrip via execfile seemed the be the only thing that worked. Now I’m off to find out how to append a board file:)

Nice thing about running it as a script is I can used raw_input like so:

import pcbnew
board = pcbnew.GetBoard()
raw_input(“Please select lines to change (Enter to continue)”)
new_width = float(raw_input("Please enter the new Width in mm: "))
new_width =new_width * 1e6
for drw in board.GetDrawings():
if drw.IsSelected():
drw.SetWidth(int(new_width))

Oh, I got another dumb question. Is there a way to select all elements of a particular layer? For example, going back the the original question regarding the DXF coming in at 2.54mm, could you automatically select everything on layer 44 and and then exec this code?

How does this work?
Never used this kind of things with kicad before.

@Mike_Lemon you enter those Python commands into the “scripting console”. You can copy and paste it in there as well. You can open the console from the “Tools” menu. I’m not sure if kicad stable version does have it. I think you have to install the nightly version.

Ah ok sorry for the delay I’m currently using a quite recent nightly version and after selecting the silk screen lines in openGL

opening the scripting console pasting this in:

import pcbnew
board = pcbnew.GetBoard()
raw_input(“Please select lines to change (Enter to continue)”)
new_width = float(raw_input("Please enter the new Width in mm: "))
new_width =new_width * 1e6
for drw in board.GetDrawings():
if drw.IsSelected():
drw.SetWidth(int(new_width))

and hitting enter it doesn’t do anything.

I know it might seem like I’m as dumb as a rock but I don’t realy understand this kicad phyton thingy.

@Mike_Lemon I also noticed pasting the script doesn’t work sometimes. Try pasting code with the Edit menu -> Paste Plust command. This is in the menu of the scripting console.

Also, to see the results, you should somehow trigger a re-draw. Sometimes moving the cursor works. Sometimes I have to switch between old canvas and backto new opengl canvas. You can use F9 and F11 shortcuts.

Easier way to run a script is to add it as an external plugin. You should check this thread for info on that.