Variable substitution does not work reliably

Hi all,

One of the many awesome improvements in version 6 was the introduction of variable substitution. This allows one to use a single source of truth for things like version number, name of the project, company information etc. However, I feel that something is missing for it to be used as a reliable source of truth. Here is an example.

I defined the following variables in PCBnew:

AUTHOR Mad Hatter
COMPANY Infinite Volatility
TITLE Obvious Error
VERSION 1.0.0

which I can then use to add the text field to the silkscreen as

${TITLE} ${VERSION}
${COMPANY} Ltd.

and also add to the title block info. These are all evaluated and rendered correctly, and inserted in the project file, and appear in the schematic file, too, so I can use them there.

However, if I copy the kicad_pcb file to another directory, and try to open it there, then some fields are not evaluated anymore, and instead of

Obvious Error 1.0.0
Infinite Volatility Ltd.

I get

${TITLE} 1.0.0
${COMPANY} Ltd.

I have checked the file, the variables are contained as

(property “AUTHOR” “Mad Hatter”)
(property “COMPANY” “Infinite Volatility”)
(property “TITLE” “Obvious Error”)
(property “VERSION” “1.0.0”)

and these are actually inserted both in the title block

(title_block
(title “${TITLE}”)
(date “2022-10-01”)
(rev “${VERSION}”)
(company “${COMPANY}”)
(comment 1 “${AUTHOR}”)
)

and in the silkscreen

(gr_text “${TITLE} ${VERSION}\n${COMPANY} Ltd.” (at 115 42.5) (layer “F.SilkS”) (tstamp 0150a597-9ca4-4081-88d2-6ff7d09ea1a2)

but not evaluated. The evaluation works only, if the project file is available.

Some PCB houses support direct KiCAD uploads (they require only the layout, and not the whole project), in fact, Aisler has even a button for this in KiCAD, and I got suspicious after receiving multiple boards with the wrong text on the silkscreen.

I was wondering, whether this is a known issue, or whether there are workarounds, or perhaps, it has already been fixed in the meantime. I understand that the project file has to contain the information, for otherwise, variables couldn’t be shared between the layout and the schematic. But I do think that one should be able to use the layout file without the project file, if interaction with the schematic is not required.

Application: KiCad PCB Editor

Version: 6.0.1-79c1e3a40b~116~ubuntu21.04.1, release build

Libraries:
wxWidgets 3.0.5
libcurl/7.74.0 OpenSSL/1.1.1j zlib/1.2.11 brotli/1.0.9 libidn2/2.3.0 libpsl/0.21.0 (+libidn2/2.3.0) libssh/0.9.5/openssl/zlib nghttp2/1.43.0 librtmp/2.3

Platform: Linux 5.11.0-49-generic x86_64, 64 bit, Little endian, wxGTK, cinnamon, x11

Build Info:
Date: Jan 15 2022 20:28:15
wxWidgets: 3.0.5 (wchar_t,wx containers,compatible with 2.8) GTK+ 3.24
Boost: 1.74.0
OCC: 7.5.2
Curl: 7.81.0
ngspice: 34
Compiler: GCC 10.3.0 with C++ ABI 1014

Build settings:
KICAD_USE_OCC=ON
KICAD_SPICE=ON

2 Likes

i think that there is a bug in the releases <= 6.04 related to that.

try with >=6.07, it should be ok.

1 Like

Thanks, I’ll try that.

You should be able to upgrade seamlessly to the latest stable release 6.0.8
As always, backup your projects first

I still have the same issue, in the silk screen text, one of the variables is evaluated properly, the rest aren’t. So, I can’t even test, whether the problem is that there are multiply substitutions in the same expression.

Also, fields in the title block are not evaluated at all.

I attach my kicad_pcb file here.
test1.kicad_pcb (3.1 KB)

Application: KiCad PCB Editor (64-bit)

Version: (6.0.8), release build

Libraries:

  wxWidgets 3.1.7

  libcurl/7.83.1-DEV Schannel zlib/1.2.12

Platform: Windows 11 (build 22000), 64-bit edition, 64 bit, Little endian, wxMSW

Build Info:

  Date: Sep 29 2022 00:21:26

  wxWidgets: 3.1.7 (wchar_t,wx containers)

  Boost: 1.79.0

  OCC: 7.6.0

  Curl: 7.83.1-DEV

  ngspice: 37

  Compiler: Visual C++ 1929 without C++ ABI

Build settings:

  KICAD_USE_OCC=ON

  KICAD_SPICE=ON

Yes, I think this is the same issue. However, I am a bit confused by what you say on gitlab: the board file should contain everything that’s required to generate the fabrication outputs. Otherwise, PCB houses’ support for KiCAD files is meaningless. Having access to the source allows manufacturers to extract relevant information (e.g., the size of the boad etc.), or check the design rules on the fly, while some can’t do that based on the gerber files. So, it is not just that I want to save two mouse clicks when exporting the gerber files (although, since I don’t actually need the gerber files, that does create clutter), it does have added value.

That being the case, I really wonder, whether such data should be moved to the board file. I really don’t understand why the project file should be concerned with PCB design rules. I still maintain that bringing in variable substitution was a brilliant idea, because e.g., if you have the same, version-dependent, label 100 times on a board, then you can have a single source of truth.

1 Like

I didn’t take part on that discussion, and I haven’t tested how this works in v6.99 (the unstable development version, “nightly builds”). Can you test with the nightly builds and tell if your problem has been fixed there? I think that saving vs. not saving the variable data in the pcb file is a feature change, and if in v6.0 they have not been saved in the pcb file, this won’t be fixed in the v6.0.x bugfix releases, only in the feature development version which is now 6.99.

Seems to work fine for me (as long as I believe this feature applies to Only the Project wherein the Variable(s) are defined. (see below…))

Mac OSX Monterey
Kicad 6.0.8

The Text Variable(s) apply to only the Project/PCB/Schematic where they are defined BUT, I could be wrong about that.

On one-hand, it makes sense. On the other-hand, it would be good if the Text-Variable(s) would apply across all projects.

To make them apply across all projects, for me, since v5, I make my own Text-Varibles combined with a Plugin. That only works on the PCB’s (all PCB’s). So, I’m glad to now see this feature in 6.0.8 as it works on both PCB and Schematic (in the project wherein they’re defined).

The downside is that I need to make Text-Variable in each PCB/Schematic. BUT, that has an Easy Solution! Simply setup the Text-Variable(s) in a Template’s PCB and/or Schematic. This works Perfectly Well!

I created several Variables - some specific for PCB, some specific for Schematic and, some Common to both PCB and Schematic.
I did Not place the variables onto the PCB/Schematic until after opening the project thus, I can place whatever I want as needed (knowing they are all defined). Simple!

Anyway, below shows your posted file with your original variables but with my (needed to add) variables in the PCB setup panel. As soon as I set those, your PCB populated with the Text…

ADDED: it makes sense to post this link to my Making Templates. Thus, define the Text-Variable(s) in either the PCB or Schematic. Then, when creating new project with the Template, you need only to place the ${your variable} onto PCB and/or Schematic.

Defined the Variables/Text in the PCB’s setup panel

After defining the Variables, this is the result in your posted PCB

I understand this, but my whole point was that I do not want to re-define the variables. My point was that the file that I posted should work without having to tamper with it. I am fully aware of how I can define the text variables, but you should see

Obvious Error 1.0.0
Infinite Volatility Ltd.

if you open the file that I uploaded. Right away. Without editing anything.

And your example is just as baffling: you don’t define the VERSION variable, yet, you see its value.

You don’t have to redefine the variables if you create them in a template.

Demonstrating that ‘a’ variable will work doesn’t require demonstrating every variable. I did only two of them.

Houdini - I imagine he could magically make it work on PCB/Schematic that are unrelated to the Specific Project where the Variables were defined.

You have to put money in the Bank Account in order to write many checks against the Account.

Thus, in a similar manner, you need to put Variables in an Account for all the PCB/Schematics (related to the same account) to work.

The easiest way to do that is to make a Template that contains All your desired variables. Then, the PCB/Schematics (based on using the Template) will work.

Yes, sorry, in my haste I didn’t pay attention to who was posting where and what.

As for testing this with the nightly build, I would be glad, but I cannot install that side by side with the stable release. Perhaps, what could help here is having nightlies in the snap, or flatpak, because those can be installed in the home directory. But I haven’t found this option.

But version information, almost by definition, cannot be part of the template, can it?

But the variables are actually in the PCB file. They are just not taken into account.

I deleted the Text Variables so there wouldn’t be any text to populate. You can see (in video), No Errors.

In the Template, You can define a list of Variables as long as your arm (so to speak) on either the PCB or Schematic (they use the same info so, only need to do it on one of them (pcb or schematic).
If you don’t place any of the Variables Onto the PCB or Schematic in the Template, they won’t populate until you actually place them onto PCB/Schematic but, you Won’t need to Re-define them - They remain defined in the Template’s PCB/Schematic, ready to be used, meaning, they Will Be Taken Into Account.

EDIT: Think about it - How would Project A know what Project B has defined for Variabes unless Kicad had setup this feature to be a ‘Cross-Project’ tool?
Thus, you (user) can make it a Cross-Project feature by doing it for a Template and using the Template for a New Project… get it!

still_works

I have the feeling that I didn’t explain properly what the problem is: the kicad_pcb file contains the required information. It is just not used, and this leads to errors as described in the gitlab issue. So, given that the information is available, Project A could actually learn what Project B defined by inspecting the contents of the PCB file. But that is probably beside the point. In the OP, I had a single file, and didn’t want to share variables. PCB houses require a single file, the kicad_pcb file, nothing more. Thus, this file should contain everything that is necessary for the fabrication.

And again, version information cannot be part of the template. The solution that you suggest could be used as a workaround in certain cases, but is definitely not how the issue should be solved. If I define variables in a template, then create a project from said template, then everything works, but as soon as I copy the single board file to a new directory, I have the same problem, so the board file cannot be handed over for production.

I guess I’m confused so, I leave it for others to help you…

That said, I have No Problem copying the PCB to a new/other folder, it still works because the variables are defined in the original file and remain defined in the Copied/Pasted files.

Consider just Copy/Paste the Text as needed…

good luck

I just tested, and me neither (have no problem).

  1. Add a project variable in a project.
  2. Use that variable in a pcb file text item.
  3. Copy the file to a new empty folder.
  4. Open the new file with standalone Pcbnew.
  5. The variable is there, resolved as it should.

I tested with both 6.0 and 6.99.

Version: 6.0.7-f9a2dced07~116~ubuntu22.04.1, release build

Version: (6.99.0-3706-gc87a5ff0ab), release build

What is a project variable, as opposed to a text variable defined in PCBNew? Do you mean a variable that comes from a template?

And how does this relate then to the gitlab issue that you linked to? That is only a week old, so 6.0.7 cannot contain the fix for that.

If you define a variable in pcbnew Board Setup, it will be a project variable. You know this already. If there is any confusion, it is caused by something which prevents you seeing a variable as resolved in the board. As I said, it works for me, and I can’t tell why it doesn’t work for you.