Can we change copper-edge-clearance using python API?

Using KiCad 8.0, is there a way to adjust the copper-to-edge clearance used by zones that is accessible from the python bindings / scripting API?

I can see some glimmers of hope with BOARD.Get/SetProperties() but I am too inexperienced with python to understand how to vary the items in it. Even then I’m not sure if I can do anything about this, as the setting isn’t even controlled/recorded in the .kicad_pcb file, but in the .kicad_pro file:

      "rules": {
        ...
        "min_copper_edge_clearance": 0.5,
        ...
      },

Is there a way to introduce a custom rule to the BOARD rather than the project which might override this? It seems a bit strange that there wouldn’t be control over the distance of copper to edges available in principle, although I can see why it might not have been implemented yet if no-one has asked.

For clarity, in this case I would like to make the copper-edge-clearance smaller, but the 0.5mm default rule is preventing this from allowing my copper zones getting as close to the edge as I have requested when creating zones with the script (although I have manually adjusted the zone clearance in the zone properties GUI because I haven’t found how / got around to setting that in script yet).

Thank you for any clues, insight or help you can offer and thank you for your patience, in advance.

BOARD class has GetDesignSettings() method which will return a BOARD_DESIGN_SETTINGS instance which has m_CopperEdgeClearance you can read/write.

1 Like

Amazing, thank you @qu1ck ! I tried this just now and have had promising results - I’ll go and check it works in a separate plugin .py file. If I have issues, will come back but assume it worked if I don’t.

For reference in case it helps, here’s the quick test I did in the KiPython scripting console (PCB Editor->Tools->Scripting Console)

import pcbnew
board = pcbnew.GetBoard()
des = board.GetDesignSettings()
print pcbnew.ToMM(des.m_CopperEdgeClearance)
# 0.5
des.m_CopperEdgeClearance = pcbnew.FromMM(0.125)
print pcbnew.ToMM(des.m_CopperEdgeClearance)
# 0.125

Well that seemed very promising but I can’t get the command to refill to obey the new setting. Steps to reproduce:

  1. Draw a rectangle in Edge.Cuts
  2. Add a filled zone on a copper layer with the boundary of the filled zone outside the rectangle on Edge.Cuts.
  3. Use the scripts above to amend the Copper to Edge clearance
  4. Press b to refill all zones

Result:


I get the original clearance dimension applied to the new copper fill. I can check the new dimension has been set by immediately opening the board setup window and checking. Notably, once I close that window by clicking the OK button and then press b again, the zone clearance becomes the new dimension given.

Is there a “refresh board design settings” property or command that can / needs to be set before changes to m_CopperEdgeClearance will be acted upon?

For info, I’m using this in my script for the refill, although I’m seeing the same behaviour in the GUI:

	filler = pcbnew.ZONE_FILLER(self.board)
	filler.Fill(self.board.Zones())

The change you make will take effect if you save the board (either from script or from the gui) and reload the board. I don’t think there is specific “reload settings” function you can call but you can try copying the design settings object and calling board.SetDesignSettings(), maybe that will refresh the internal state.

In general expect such things to not work, scripting interface gives you visibility to some board objects and state but not to kicad’s window and app state. The distinction is blurry sometimes and some things may work or not work but the general idea stands.

Why are you trying to change design settings on the fly from the plugin?

1 Like

Thanks - will try this next.

I’m just starting to learn plugin scripting and I’m probably going to try to do things which would be better achieved elsewhere. However, I have a desire to be able to setup a simple board from a single script and, possibly one day, add a plugin GUI for it. It’s a personal project and I imagine won’t be of interest for others but it seems that if I’m going to give myself some timesaving shortcuts, I would like to build as many of them into the script as I can and minimise the number of GUI menus I need to go through to change defaults.

Project templates seem like a better fit for that purpose.

1 Like

This didn’t work, as expected.

I used

	board = pcbnew.GetBoard()
	design = board.GetDesignSettings()
	design.m_CopperEdgeClearance = FromMM(0.125)
	name = board.GetFileName()
	board.Save(name)
	pcbnew.LoadBoard(name)
	self.board = pcbnew.GetBoard()

Then drew my filled zones on F.Cu and B.Cu. Then:

	# Refill zones
	filler = pcbnew.ZONE_FILLER(self.board)
	filler.Fill(self.board.Zones())

And I still see the 0.5 mm edge clearance. What might I be doing wrong in the save / reload / refill steps?

That doesn’t load the board into active pcbnew window, it just gives you a resulting object, pcbnew still has the old board. The only way to reload it is through gui (close, reopen the window).

1 Like

OK, thanks for highlighting the difference and confirming the working method.