Mathematical operation in value field

Is there a way to make mathematical operations in value fields in Kicad ?

Or maybe is there a field that accepts operations like + and * ?


1 Like

KiCad doesn’t evaluate mathematical operations “lazily”, i.e. it doesn’t evaluate them when the result would be used. There are GUI dialog fields in KiCad where operations are evaluated, but it happens immediately when the input is accepted by the user and only the result is then used. The formula is dispensed with and can’t be edited later.

The symbols fields handle the input literally. Only the text variables are evaluated and they are defined elsewhere and evaluated for each use.

That would be a great idea! Even better, if one could reference values of different components. Something like “R1 * 2”. Catia allows operations, but you can’t referenc other values (AFAIK). The operation should not just evaluate, but persist as the operation. Maybe the operation could be kept in a special field in the attributes. Wait … maybe this can alredy be done with a macro?

Yes I was thinking of something similar than spreadsheet software where the formula is stored and the value display. That is if the formula references an other value, an update on the value, updates the other.

For example keeping track of a cutoff frequency in an RC circuit, changing R would cause a change in C value.

I am dubious that KiCad should acquire spreadsheet functionality but this engenders the idea that perhaps a plugin can export field data to Libreoffice Calc via its Python API and receive transformed results back from it.

I am quite new to designing, I don’t know how it’s done in industry, but sometimes I make some computations and it would be nice to keep track of it in the kicad file and not in a random file elsewhere. But maybe you’re right it’s not the place for it in the schematics.

Spreadsheet functionality looks to me like a overshot. I think it would be sufficient if the recalculation is triggered by user action. OTOH, the time to scan all values an see where and if they reference some other value is not so time expensiv. Even more, if KiCAD would default to “do not recalculate”.
It might get a bit tricky when calculated values are referenced by other calculated values. Or if someone decides to want a cyclic depedency. :slight_smile:

Anyhow, I think it is an interesting idea for the long run.

I’ve found it very useful to make Plugins for the PCB Editor (including Input GUI pop-up panel) because there are things in/for PCB that I do. But, I have not needed plugins for the Schematic…

That said, since v6, the Schematic has an Icon for the Python console. I haven’t bothered to explore it but, you may find it useful to do what you want with the Fields. Others may have relevant knowledge about the console in schematic.

You can at least Add two numbers and, if it’s as robust as the console in PCB then, making a GUI Pop-Up panel for value input and calculation may be just around the corner…

The Console in Schematic

If I recall right, the value field is alphanumeric and hence it would be difficult to perform mathematical operations.

Yes but you know how it ends up. If you put in arithmetic, somebody will want transcendental functions and on it goes. Already one of the suggestions could cause cyclic dependencies. Better to hive off the work to another program which can be as complex as you want.

That would only make it easier to implement functions “sqrt(R1 + R2)” (whatever that means). A parser for formulas is not that complicated to implement.

Maybe a spreadsheet is not the best external program. Something like Octave might. An export filter that looks for a special attribute called say Formula in symbols would extract those plus any referenced symbol as a series of equations for Octave to solve, and then an import filter would put the results back into the Value field.

1 Like

Perhaps a better choice would be the bc command line calculator. See bc - GNU Project - Free Software Foundation It is a POSIX standard, included on macOS and Linux, and readily available for Windows.

1 Like

Octave is cross platform too. But any calculator will need a lot of support in filtering. Consider how people like to write values in all kinds of formats, 4k7, 4.7e3, 100n, etc.

1 Like

Isn’t the handling of the input formats already implemented, considering that the component values are forwarded to SPICE simulation?

What do you think about the relevance of this functionality ? I may find workarounds for my hobbyist projects, but I would be also interested to help develop an implementation of this only if this have some value for other users.

People here suggested to make it a plugin. Could it not be included in a future release ? I’m not familiar with the procedure to make this a feature request or so in the gitlab page.

I can’t say that I know in a broader perspective how large part of the user base of KiCad would “need” or benefit from this potential feature, but for myself I could expect that I would have use for it in different situations.

I’d be interested in this functionality. The example mentioned earlier of computing the cutoff frequency for an RC filter automatically is the type of thing I need to do often while I’m tweaking component values. As a hobbyist, I’m surprised there isn’t already something like this, could anyone who works in the industry shed some light on how this is handled in the industry (not for the RC filter specifically, but in general)?

I was thinking maybe if I could put “=R1*R2” in a textbox, a plugin could find all text inputs starting with “=”, parse the formula, and compute the result. But I’m not sure how to best display that result without overwriting the original formula…

I actually quite like that last idea: text boxes with arithmetic equations getting solved by a custom plug-in. If I were designing such a thing, I’d leave the original text box in place and just add the answer later in the text box with some relatively rare symbols to help the parser out. Maybe something like “[1]> R1.Value + R2.Value =” is extended to “[1]> R1.Value + R2.Value = 2000”.
As mentioned earlier in the thread, a huge portion of the work on such a thing would be handling different ways of writing values and catching errors and such. Not work that KiCad itself is really well set up to do.

To be clear: at the moment this is not possible / easy, as the schematic editor does not have a Python API. Technically quitting out, running a script that parses the schematic files and performs the arithmetic, and saving it back to the schematic files to be viewed on reopening is possible but not any easier than tracking everything elsewhere.

But why write it as a plugin and not full code in Kicad project ?

See here for related issues in gitlab : Mathematical operation in value field (#14658) · Issues · KiCad / KiCad Source Code / kicad · GitLab

1 Like