Total Courtyard area info

Hi all,

Is there any way to find out a total Courtyard area (total for all the components on the PCB)?

Many thanks

1 Like

Interesting idea for real estate estimate purposes. Is that your use case?

If there was a way to get a single courtyard area, then getting the total would be a very simple python script. But I don’t know if there is one.

I’m still using v7 (and don’t intend to upgrade).

In v7, I do this to get Area:

For object of interest:
• On a Copper-Layer of your choice, Draw a Polygon-Shape to match the desired outline
• Select the Shape you just created
• Right-Click it and Create a Zone from it
• Select the Zone, Right-Click, Fill Selected

The Area will display at bottom of PCB Panel…
Multiple Zone Areas are not Additive so must do individually (and get out your calculator…)

ADDED: you can use other Layers to do it but, not all of them… User Layer works…

@Heath_Raftery - that’s exactly my use case. Big board with lots of components went through a major updates. Just out of curiosity wanted to find out total Courtyard info before and after.

@BlackCoffee - this is the selected zone area. I use it already, but for different use case. What I asked for is a total Courtyard area - all components Courtyard areas added together. They are scattered around the board and with gaps in between them, so drawing a zone won’t work.

Yes, I know what you asked for and I answered ā€˜they are not additive… get out your calculator’

It’s over 1200 total components, (around 25 different footprint used multiple times).

I’ll think about a script rather than do it manually on a calculator, but thanks for your advise anyway.

I had a quick squizz and noticed you can query a footprint for it’s ā€œbounding hullā€ and its ā€œcached courtyardā€ which sound encouraging.

Further, once you have a SHAPE_POLY_SET, finding its area looks very straight forward too:

So given that courtyards are by definition not overlapping, on the surface this seems eminently doable.

Well that was shockingly simple:

import pcbnew
pcb = pcbnew.GetBoard()

area = 0.0
for fp in pcb.GetFootprints():
    cy = fp.GetCachedCourtyard(pcbnew.F_CrtYd)
    area = area + cy.Area()

area / (pcbnew.PCB_IU_PER_MM*pcbnew.PCB_IU_PER_MM)

Type that into the pcbnew console and away you go?

7 Likes

I do know there is documentation on: https://docs.kicad.org/ but now I see for the first time there is documentation about python scripting hidden on that same docs.kicad.org site.

There is a small section about scripting in the PCB manual: https://docs.kicad.org/9.0/en/pcbnew/pcbnew.html#scripting but that also does not mention the doxygen stuff.

Yes, I’ve learnt to keep a link handy. It’s very hard to discover, despite being irreplaceable documentation.

1 Like

It returns 0.0, but it a good start. I’ll experiment with this later when have a moment. Thanks!

I created a bug report for it:

1 Like

I ran it on a completed board, that only has components on the front. The number seemed plausible to me (3822mm2 on a lightly packed 100x100 board).

I wonder if a ā€œcached courtyardā€ needs to be triggered by something.

Anyway, I’ll let you have a play. It will be interesting what you discover.

It is a Good start. And, after deleting Cached from Courtyard, it works (though, I did Not compare to actual Areas… just wanted to check ā€˜Cached’ issue…

import pcbnew
pcb = pcbnew.GetBoard()

area = 0.0
for fp in pcb.GetFootprints():
cy = fp.GetCourtyard(pcbnew.F_CrtYd)
area = area + cy.Area()

Here’s the Result:
area / (pcbnew.PCB_IU_PER_MM*pcbnew.PCB_IU_PER_MM)
1494.524441916688

1 Like

I tried it on a few my projects as well as downloaded from GitHub. No luck. Always returns 0.0.

However I created a new project with two random parts and it gives the result as it should.

Perhaps it’s something to do with older / newer KiCad version used for the project they don’t work. I don’t know, but will investigate.

The script works fine! Many thanks again @Heath_Raftery !

EDIT: Actually it’s very random. I closed and re-open the new project I just made and now also shows 0.0. Will investigate.

I can’t say because I still use v7.

However, for morning Fun while drinking black coffee…
I added Courtyard-Area calc to my Selected-Track’s-Area Plugin.

Tested with Two Courtyards at 25x25mm. I left it such that User needs to select desired Tracks and Courtyards are on Front

Plugin attached - old style so, just put it into the proper folder on your system and Edit the .PNG name to your desired Icon.

my_Area_Calc.py (1.5 KB)

Many thanks for this.

Tried it on a few different projects and it reposrts tracks OK, but no Courtyards.

Tried selecting them with F.Cu or F.Courtyard layer active. No luck:

It there something wrong with my system (Xubuntu + KiCad 9.0.5)?

What do I do wrong?

I’ll check it on another machine (Win11 + KiCad 9.0.4) after the weekend and let you know.

I placed a bunch of footprints and all of their courtyards got added… I’m on a Mac.

Perhaps other users can chime-in and help you with Linux and perhaps Windows will work out ok…

The code I posted does Not use ā€˜Selected’ Courtyard but, perhaps you can add ā€˜Selected’ to the code and report back…

A few years ago, I tried to do something similar with the aim of estimating the available PCB area for a given design.

1 Like

How strange!

I started to write down some debugging ideas, but quickly discovered I can reproduce your result!

I have the same project open in a Mac Studio running Sequoia 15.6.1, KiCad 9.0.4, and on a MacBook Air running Sequoia 15.6, KiCad 9.0.4. On the MacBook all is well, on the Mac all the Areas are zero!!

Something fishy going on here…