Custom rules in V9.0.3

My rules:
(rule ML_DATA
(condition “A.hasNetclass(‘DATA_’) && B.hasNetclass(‘DATA_’)”)
(constraint skew (max 5mil))
)
(rule ML_DIFF1
(condition “A.hasNetclass(‘DIFF1’) && B.hasNetclass(‘DIFF1’)”)
(constraint skew (max 5mil))
)

My classes are defined and contains appropriate nets.
But they don’t seem to work as they don’t report any warnings.

Can someone explain ?

Do you have a “version” line at the top of your custom rules? (It’s mandatory).

Yes I do …

(version 1)
(rule CourtYard
(constraint courtyard_clearance (min 2mil))
)
(rule Pin-Pin-Clr
(constraint clearance (min 6mil))
)
(rule ML_DATA
(severity warning)
(condition “A.hasNetclass(‘DATA_’) && B.hasNetclass(‘DATA_’)”)
(constraint length (min 1500mil) (opt 1666.07mil) (max 2000mil))
(constraint skew (max 5mil))
)
(rule ML_DIFF1
(severity warning)
(condition “A.inDiffPair(‘DIFF1’)”)
(constraint skew (max 5mil))
)

I added preformatted text format to your rules, otherwise the forum subtly changes some characters.

1 Like

Not sure I understand …

Nothing to do with your rules per se.

Try copying and pasting back to KiCad the rules from your first post and you see what I mean. The rules don’t work anymore because this forum software messed with some characters. When writing a post you can format your text. Use “preformatted text” for sections of code etc. Then it’s kept intact. It’s also easier to read.

1 Like

Hi Michel Dagenais,
Click on this Icon after you start a new paragraph for the preformatted text.

type or paste code here

ksnip_20250806-204335

Paste code between the triple back-ticks.

I don’t see any difference when copying back to KiCad.
The “Check Syntax” don’t see any errors also.

But anyway, that does not explain why my rules are not working as they don’t report any warnings after running a DRC…

(version 1)
(rule CourtYard
(constraint courtyard_clearance (min 2mil))
)
(rule Pin-Pin-Clr
(constraint clearance (min 6mil))
)
(rule ML_DATA
(severity warning)
(condition "A.hasNetclass('DATA_') && B.hasNetclass('DATA_')")
(constraint length (min 1500mil) (opt 1666.07mil) (max 2000mil))
(constraint skew (max 5mil))
)
(rule ML_DIFF1
(severity warning)
(condition "A.inDiffPair('DIFF1')")
(constraint skew (max 5mil))
)

Did you copy from this view, and not from the editor?

Look carefully at the quote characters there… I once copied shell commands from a post and spent an hour debugging, only because I didn’t know the forum changes the text.

so two things

  1. you have not put in quotes the rule name. This is for all of them
    (rule CourtYard => (rule “CourtYard”

  2. as @eelik is explaining, there is unfortunately three quote-like characters on a keyboard but not all of them are real quotes from the perspective of DRC rules
    ` is different from ’ is different from "
    Thus cross-check exactly what character is use. Its equivelent of using ; or ; in a C-code file. one is a semi-colon, the other is a question mark in Greek (U+037E)

Here are examples from KiCad Help

Examples

(version 1)
(rule HV
   (constraint clearance (min 1.5mm))
   (condition "A.hasNetclass('HV')"))
(rule HV
   (layer outer)
   (constraint clearance (min 1.5mm))
   (condition "A.hasNetclass('HV')"))
(rule HV_HV
   # wider clearance between HV tracks
   (constraint clearance (min "1.5mm + 2.0mm"))
   (condition "A.hasNetclass('HV') && B.hasNetclass('HV')"))
(rule HV_unshielded
   (constraint clearance (min 2mm))
   (condition "A.hasNetclass('HV') && !A.enclosedByArea('Shield*')"))
(rule heavy_thermals
   (constraint thermal_spoke_width (min 0.5mm))
   (condition "A.hasNetclass('HV')"))

I am using the same kind of quotes and the rule name is not in quotes in their example.

skew applies only to one item at a time, and implicitly compares it to other net matches. So it should only look at A:

(rule ML_DATA
  (severity warning)
  (condition "A.hasNetclass('DATA_')")
  (constraint skew (max 5mil))
)

Checks the total skew for the nets that match the rule condition, that is, the difference between the length of each net and the average of all the lengths of each net that is matched by the rule. If the absolute value of the difference between that average and the length of any one net is above the constraint max value, an error will be generated.

If there are no spaces in the name, technically you do not need quotes per normal s-expression rules. KiCad does quote all string values anyway for clarity in the formats it writes out (like kicad_pcb) and to make third-party parsing and round-tripping a little easier. When KiCad reads it in, there is technically no ambiguity (or difference).

Ok, now it is reporting the warnings after I removed the B condition.
Thank you very much for the hint.

However, the Diff Pair skew is still not reporting any warnings.

It’s a bit hard to guess when you don’t provide any other details, for example a minimal project or at least screenshots of the design in the area, but at a guess, the pair is actually called something like /DIFF1, not DIFF1. Or your nets don’t follow the pattern DIFF1_P/N.

Yessss … you found the trick.

In fact, using condition “inDiffPair”, we need the slash before the diff pair name.
Else, for the class, nets don’t need to be called with the slash.

Thank you very much.

Nice!

For the class names (which is a string you choose) they would need a slash if you named with a slash (IDK why you’d do that, but I think nothing will stop you:

For net names, e.g. in A.NetName = /DIFF1_P, I think you will still need a slash if the net name has a slash (and KiCad may define that based on the hierarchy)

To put a finer point on it, if you use a local label with the name mynet in the root sheet of your schematic, the resulting net name is /mynet (it is local to the / sheet)