Custom design rule to check object proximity

I would like to create custom design rules able to check object proximities, for instance, that I always have a capacitor close to any supply pin, or a GND via close to any GND pad. It would be sort of the opposite of physical_clearance.

I tried to do this with a physical clearance rule that would dismiss all sets of objects that matched the minimum clearance, and then match all the non-dismissed others, but the Kicad custom rule law dismisses all sets of objects tested, not only the matched one, so that does not work.

Rules are evaluated in reverse order, meaning the last rule in the file is checked first. Once a matching rule is found for a given set objects being tested, no further rules will be checked.

This is what I tried:

(rule "No GND via close enough to pad"
  (severity warning)
  (condition "A.Type == 'Pad' && A.NetName == 'GND' && B.Type == 'Via' && B.NetName == 'GND'")
  (constraint assertion "A.Type == 'Pad'")
)
(rule "GND via close to pads, exclusion"
  (severity ignore)
  (condition "A.Type == 'Pad' && A.NetName == 'GND' && B.Type == 'Via' && B.NetName == 'GND'")
  (constraint physical_clearance (min 0.4mm))
)

Am I missing something here, or do I need to dig into the design rule parser to try to create a physical_proximity or a max physical_clearance thingy?

I think one basic problem is that the DRC check for clearance (proximity) works so that each item/item pair is checked. There’s no check for “there must exist some other item for this item”. For example, you have a pad of a footprint and a via which you can check. But you can’t tell “there must be at least one via near the pad”. You can only take one certain existing via which you can identify and tell that it must be near that pad.

You could use “max” instead of “min” for physical (same net) clearance if it worked, but you still couldn’t tell “for each GND pad, there must exist a via which…”. What you can do, is identify a pad (there are conditions to do that) and a via (you can add a via and make a named group which has only that via) and tell that “physical clearance between these two items is…”. If you can’t identify an item in some way, the rule will be applied to all similar items. Therefore you would have to limit the rule to one specific pad and one specific via. If the rule would be, for example “max clearance between this pad and via of this same nest is…”, it would require all vias of that same net to be near the pad.

So, for this to work – apart from the missing or non-working max physical_clearance check – you would need to name each item pair which would be much more work than just doing a normal manual sanity check. If you really feel you need to automate everything, you can write a python plugin which does those checks in more complicated way, implementing rules like “there must exist one and only one bypass capacitor near this pad”. I’m not sure if adding a new constraint type “there must exist an item” would be accepted even if you could implement it in C++. If you can think through the consequences, you can ask about implementation in KiCad code in the developers’ mailing list, or maybe someone comments here.

Thanks for your answer! I am not familiar with the code, it makes it clearer to that this is probably over-engineering. I guess I will stop messing around with the custom DRC panel and get back to actually reviewing my layout :grin: