Plot scripting/API - color choice for layers?

OK, I’m at the stage when one starts to pull out hair :smile:

My aim - grey/gray toned outputs for layers from the API (as I don’t want to change the pcbnew colors being used while doing layout work).

Problem - set color option/command doesn’t work…

pctl = PLOT_CONTROLLER(GetBoard())
popt = pctl.GetPlotOptions()
pctl.SetColorMode(True)
popt.SetColor(CYAN)

Reference:
https://github.com/blairbonnett-mirrors/kicad/blob/master/demos/python_scripts_examples/plot_board.py

There also seems to be some development going on in that part of the code, but up to BZR6403 it’s not available it seems. Anyone know if there is a compiled binary for windows that contains this code?
There the initialization is a bit different…

popt = pctl.AccessPlotOpts()

Anyhow, for the time being I’ll probably need to pull out each layer one by one in black&white and compose it over each other with some 3rd party tool/scripting. Any idea what workflow is best for this?
Output from the plot API is svg, pdf, gerber and ps it seems.

My sample code for above problem case:
KiCADinfo_plotAPI.py (552 Bytes)

1 Like

Seems I just grabbed the wrong discussion thread there, otherwise I would have read that this doesn’t work at all (currently) but is being worked on…
https://lists.launchpad.net/kicad-developers/msg09761.html

So back to plan B - any workflow for overlaying several svgs/pdfs/postscripts/gerbers and getting them onto a single page via an automated process? Please. :worried:

Anyone got any experience with this tool?

OK, got nowhere. Installing breaks down when I’m chasing the 3rd or 4th dependency and I just don’t have the time for that now.

Different approach.
The colors that are being loaded when KiCAD is being run are the ones that count… so, nothing more simple than just having a copy of the pcbnew file that is located here in Win7:

C:\Users\JohnDoe\AppData\Roaming\kicad\pcbnew

And modifying the layer colors how I need them for the grey toned print out.
Once I’m done producing the documentation for a particular board I just swap the pcbnew file back in with the real colors for the layers and can keep on working.
Rather crude, but I can use the native print facility from within KiCAD to get the stuff done.

Have you tried using an integer as the parameter:

popt.SetColor(14)

According the kicad’s scripting documentation SetColor method expects an argument in the type of EDA_COLOR_T which I couldn’t find a definition in the python bindings file (pcbnew.py).

But EDA_COLOR_T is an enum as defined here. I got the number ‘14’ for ‘CYAN’ color by simply counting. In C/C++ you can pass an integer instead of an enum value, so it may work in python bindings as well.

1 Like

Yeah, same thing, doesn’t work.

Looking at the pcbnew API and searching within the different xy_PLOTTER’s (PDF, POST, SVG, GERBER) only gerber seems to have this feature.
It’s missing in PDF_PLOTTER and in SVG_PLOTTER doesn’t work (as I expect it).

Anyhow… the workaround from above with the init file and restarting KiCAD might work on-the-fly when I look at this pcbnew.COLORS_DESIGN_SETTINGS class here:
I would then adapt the plot script to read out the current layer colors and swap them for the ones I desire, print and swap them back - no need to restart KiCAD and mocking around with init files, or?
Well, I’ll play with it tonight if I have some time to spare…

OK, for anyone who wants grey-scale prints of boards for assembling them by hand, here are my current pcbnew color settings for the layers to get some decent print out on a black and white laser printer:

ColorPCBLayer_F.Cu=L.Yellow
ColorPCBLayer_In1.Cu=Gray 3
ColorPCBLayer_In2.Cu=Gray 3
ColorPCBLayer_In3.Cu=Gray 3
ColorPCBLayer_In4.Cu=Gray 3
ColorPCBLayer_In5.Cu=Gray 3
ColorPCBLayer_In6.Cu=Gray 3
ColorPCBLayer_In7.Cu=Gray 3
ColorPCBLayer_In8.Cu=Gray 3
ColorPCBLayer_In9.Cu=Gray 3
ColorPCBLayer_In10.Cu=Gray 3
ColorPCBLayer_In11.Cu=Gray 3
ColorPCBLayer_In12.Cu=Gray 3
ColorPCBLayer_In13.Cu=Gray 3
ColorPCBLayer_In14.Cu=Gray 3
ColorPCBLayer_In15.Cu=Gray 3
ColorPCBLayer_In16.Cu=Gray 3
ColorPCBLayer_In17.Cu=Gray 3
ColorPCBLayer_In18.Cu=Gray 3
ColorPCBLayer_In19.Cu=Gray 3
ColorPCBLayer_In20.Cu=Gray 3
ColorPCBLayer_In21.Cu=Gray 3
ColorPCBLayer_In22.Cu=Gray 3
ColorPCBLayer_In23.Cu=Gray 3
ColorPCBLayer_In24.Cu=Gray 3
ColorPCBLayer_In25.Cu=Gray 3
ColorPCBLayer_In26.Cu=Gray 3
ColorPCBLayer_In27.Cu=Gray 3
ColorPCBLayer_In28.Cu=Gray 3
ColorPCBLayer_In29.Cu=Gray 3
ColorPCBLayer_In30.Cu=Gray 3
ColorPCBLayer_B.Cu=Gray 3
ColorPCBLayer_B.Adhes=Black
ColorPCBLayer_F.Adhes=Black
ColorPCBLayer_B.Paste=Gray 1
ColorPCBLayer_F.Paste=Gray 1
ColorPCBLayer_B.SilkS=Black
ColorPCBLayer_F.SilkS=Black
ColorPCBLayer_B.Mask=Gray 2
ColorPCBLayer_F.Mask=Gray 2
ColorPCBLayer_Dwgs.User=Black
ColorPCBLayer_Cmts.User=Black
ColorPCBLayer_Eco1.User=Gray 1
ColorPCBLayer_Eco2.User=Gray 1
ColorPCBLayer_Edge.Cuts=Black
ColorPCBLayer_Margin=Gray 2
ColorPCBLayer_B.CrtYd=Gray 1
ColorPCBLayer_F.CrtYd=Gray 1
ColorPCBLayer_B.Fab=Gray 1
ColorPCBLayer_F.Fab=Gray 1
ColorTxtFrontEx=Black
ColorTxtBackEx=Black
ColorTxtInvisEx=Gray 2
ColorAnchorEx=Black
ColorPadBackEx=Yellow 4
ColorPadFrontEx=Yellow 4
ColorViaThruEx=Gray 3
ColorViaBBlindEx=Gray 2
ColorViaMicroEx=Gray 2
ColorNonPlatedEx=Gray 3
ColorRatsEx=White

These settings reside in an ini file called pcbnew (in the same folder are other settings files for eeschema or kicad etc…). Under Windows 7 it’s located here:

C:\Users\JohnDoe\AppData\Roaming\kicad\pcbnew

Create another file with extension .PRINT (copy pcbnew) and replace above lines in there.

Procedure if you want to print something with those color settings:

  • quit KiCAD completely
  • swap pcbnew ini with pcbnew.PRINT ini by doing this:
    – 1st rename ‘normal’ pcbnew into pcbnew.LAYOUT
    – 2nd rename pcbnew.PRINT into pcbnew
  • start KiCAD/pcbnew
  • open print dialog and set it to Print Mode [Color]
  • print
  • quit pcbnew/KiCAD and revert the pcbnew ini file swap from above

I’ll see if I can whip up some script that would do this, but it will take some time. Maybe someone else can create a script that swaps layer colors for printing and reverses those settings to the layout colors once printing is done?

Result (Top):

PS: I know that the XH connector symbols are the wrong way around and some bottom devices outlines show up, still working on it (have 1/3rd of my local lib adapted so far phew). :wink:

PPS: I have REF** on Eco1 and VAL** as well as the device outlines on F.Fab. There is information on silkscreen, but I don’t print that. Problem with REF being on a non-Top/Bottom layer… both top and bottom devices REF fields are visible on the print. If you stick with KiCAD library ‘rules’ you shouldn’t have this problem though, just print the silkscreen layers to get the REF fields for the correct side.

Hello Joan,
I’ve read a number of post and it seems you have a good understanding for the python interface to Kicad. I’ve written a few python script but mostly for educational purposes. I’m trying to learn how to do replicate some of the kicad dialog box behaviors in python. Since this post had python and plotting, I’ll explain what I would like to learn to do. I would like to plot gerber and pdf files via a python script. The main purpose is to generate these files without forgetting one of the options. (back in my VLSI days we loved to call this the “make chip” script) For example, when I plot gerbers, I turn off all the options except “use aux axis”, I select the cooper layers, paste, sliks, masks, and edge. When I plot pdfs, I turn on a number of the options, turn off the cooper layers, paste, mask, and edges. Then I turn on the fab layers, I mirror plot the back layers, so this translates into another set of options and layers. You would be amazed how much grief this causes keeping track of what I’ve done and if I did it right (so much validations…). In addition, there are things like setting pad mask clearances that need to be checked before generating the gerbers. But that’s later, I think I might be able to figure out how to do this if I can get my head around a script that will generate the plots. I think if I can see how to do the plots, I’m 99% sure I’ll become dangerous. So, here’s the issue, I really don’t even know how to find the functions (def) to call and what parameters to pass, or even how to init the environment, like if I need to load the board file for example if I’m using the built in python IDE… Sad I know. I just can’t find an example that does this… Help?

So, I found http://ci.kicad.org/job/kicad-doxygen/ws/build/pcbnew/doxygen-python/html/annotated.html and PCB_PLOT_PARAMS which looks like I can set many of the plot options. I’ve been playing with this and it seem I’m a bit more dangerous than before. Still can’t figure out what uses these params and writes a gerber file. The GERBER_PLOTTER class looks like it deals with converting shapes and whatnots, so I don’t think this is the correct path. I’m going to start looking at the C code, maybe something will open my eyes…

Ok, found a duplicate post with the info I’m looking for.


There is a plot_board.py script that works!!! and I almost get what is going on.

oops, this is the correct link:

1 Like

Sorry, that can’t be me… I only do stuff with python outside of KiCAD :head_bandage:

If you are already looking at the KiCAD C code and understand what @spuder did you’re ahead of me :wink:

No, it didn’t work for me with the python interface :_/

I’m trying to have a resulting DXF file with BLUE borders and all the following attempts failed, defaulting back to BLACK for lines:

        # Do the BRD edges in blue, compatible with Ponoko.com laser engraving service:
        # https://ponoko.zendesk.com/hc/en-us/community/posts/210090388-File-won-t-upload-and-says-Oops-Please-Check-Your-Stroke-Colors-
        # Blue for cutting, Red for line engraving
        #popt.SetColor(COLOR4D.BLACK)
        #
        # BLUE in https://github.com/KiCad/kicad-source-mirror/blob/d7bf44eee0a1ac4cf9a852d66c4ed65fff9cb788/include/colors.h
        # According to https://forum.kicad.info/t/plot-scripting-api-color-choice-for-layers/2074/4
        # https://www.ponoko.com/starter-kits/inkscape#inkscape_section_6_1
        # "And set the stroke color to blue with the 'Stroke paint' RGBA values of 0, 0, 255, 255"
        popt.SetColor(COLOR4D(0, 0, 255, 255))
        #print(dir(COLOR4D))
        #print(dir(popt.SetColor))
        #popt.SetColor(COLOR4D.SetToNearestLegacyColor(COLOR4D.EDA_COLOR_T('BLUE')))
        #popt.SetColor(ColorGetName(12))
        #popt.SetColor(YELLOW)

Please let me know if it’s possible at all or those color settings have nothing to do with DXF export :-S

You are not alone (see https://stackoverflow.com/questions/43645628/how-to-plot-colored-output-from-pcbnew-via-python-scripting) and I couldn’t get this to work either.
I have tried substituting colours within the generated svg but you can end up with problems of islands not being coloured (I wanted to produce a negative image - with the layer contents coloured and the background black - I think you may need to do some tweaking of the ‘evenodd’ descriptor to get this to work within the svg as swapping, say black for red and then white for black in the svg didn’t work well). I ended up producing my svgs and post-processing them with image magick. (See https://github.com/Gasman2014/KiCad-Diff - a work in progress!)

2 Likes

I just went for InkScape right after as the Ponoko InkScape FAQ suggests (changing the lines to blue and 0,01mm width)…

Even the author of OpenFixture went through the same struggle and gave up in favor of InkScape manual fixes (importing the DXF, changing color, exporting to SVG, submitting to Ponoko):