Skip to content

Commit 508d773

Browse files
Support updating text variables in .kicad_pcb file explicitly
Normally, the text variables in the .kicad_pcb file are cleared when requested, or when changing them with the set_text_variables preflight. This causes KiCad to load the variables from the project file when opening the PCB, but this does not work when the .kicad_pcb file is used in isolation (e.g. when submitting to a PCB manufacturer). This commit adds support for a new update_pcb_text_cache option, which causes kibot to explicitly write the (updated / current) variables to the .kicad_pcb file, making it self-contained. This fixes #860
1 parent fbf0464 commit 508d773

4 files changed

Lines changed: 33 additions & 9 deletions

File tree

kibot/globals.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,20 @@ def __init__(self):
392392
""" [string|list(string)] Name/s of the field/s used for the current raiting.
393393
You can use `_field_current` as field name to use it in most places """
394394
self.invalidate_pcb_text_cache = 'auto'
395-
""" [auto,yes,no] Remove any cached text variable in the PCB. This is needed in order to force a text
396-
variables update when using `set_text_variables`. You might want to disable it when applying some
397-
changes to the PCB and create a new copy to send to somebody without changing the cached values.
398-
Note that it will save the PCB with the cache erased.
399-
The `auto` value will remove the cached values only when using `set_text_variables` """
395+
""" [auto,yes,no] Clear the text variables cache in the PCB file. This is needed in order to force KiCad to read
396+
updated text variables from the project file when they are changed with `set_text_variables`. You might want to
397+
disable it when applying some changes to the PCB and create a new copy to send to somebody without changing the
398+
cached values.
399+
The `auto` value will remove the cached values only when using `set_text_variables`.
400+
Note that at least one of the `invalidate_pcb_text_cache` and `update_pcb_text_cache` config values must be set
401+
to 'no', otherwise an error is produced."""
402+
self.update_pcb_text_cache = 'no'
403+
""" [auto,yes,no] Update the text variables cache in the PCB file. This makes the PCB file self-contained (usable
404+
without the project file next to it) by copying all text variables from the project file (possibly modified by
405+
the `set_text_variables` preflight) into the PCB file (the cache is completely replaced).
406+
The `auto` value will update the cache only when using `set_text_variables`.
407+
Note that at least one of the `invalidate_pcb_text_cache` and `update_pcb_text_cache` config values must be set
408+
to 'no', otherwise an error is produced."""
400409
self.git_diff_strategy = 'worktree'
401410
""" [worktree,stash] When computing a PCB/SCH diff it configures how do we preserve the current
402411
working state. The *worktree* mechanism creates a separated worktree, that then is just removed.
@@ -581,6 +590,9 @@ def config(self, parent):
581590
KiConf.aliases_3D[alias.name] = alias.value
582591
logger.debugl(1, '- {}={}'.format(alias.name, alias.value))
583592
logger.debugl(1, 'Finished adding aliases')
593+
if GS.global_invalidate_pcb_text_cache != 'no' and GS.global_update_pcb_text_cache != 'no':
594+
raise KiPlotConfigurationError("At least one of `invalidate_pcb_text_cache` and `update_pcb_text_cache`"
595+
" option must be `no`")
584596

585597

586598
logger = get_logger(__name__)

kibot/gs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class GS(object):
185185
global_git_diff_strategy = None
186186
global_impedance_controlled = None
187187
global_invalidate_pcb_text_cache = None
188+
global_update_pcb_text_cache = None
188189
global_kiauto_time_out_scale = None
189190
global_kiauto_wait_start = None
190191
global_layer_defaults = None

kibot/kiplot.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,20 @@ def load_board(pcb_file=None, forced=False):
221221
raise KiPlotConfigurationError(f"Unknown layer used for the global `work_layer` option"
222222
f" (`{GS.global_work_layer}`)")
223223

224-
if GS.global_invalidate_pcb_text_cache == 'yes' and GS.ki6:
224+
if (GS.global_invalidate_pcb_text_cache == 'yes' or GS.global_update_pcb_text_cache == 'yes') and GS.ki6:
225225
# Workaround for unexpected KiCad behavior:
226226
# https://gitlab.com/kicad/code/kicad/-/issues/14360
227227
logger.debug('Current PCB text variables cache: {}'.format(board.GetProperties().items()))
228-
logger.debug('Removing cached text variables')
229-
board.SetProperties(pcbnew.MAP_STRING_STRING())
228+
229+
props = pcbnew.MAP_STRING_STRING()
230+
231+
if GS.global_update_pcb_text_cache == 'yes':
232+
for k, v in GS.load_pro_variables().items():
233+
props[k] = v
234+
logger.debug('Updating cached text variables')
235+
else:
236+
logger.debug('Removing cached text variables')
237+
board.SetProperties(props)
230238
# Save the PCB, so external tools also gets the reset, i.e. panelize, see #652
231239
GS.save_pcb(pcb_file, board)
232240
if BasePreFlight.get_option('check_zone_fills'):

kibot/pre_set_text_variables.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ def apply(self):
163163
if GS.board:
164164
# Force a project and PCB reload
165165
GS.reload_project(pro_name)
166-
# Check if we need to force a PCB text variables reset
166+
# Check if we need to force a PCB text variables reset or update
167167
if GS.global_invalidate_pcb_text_cache == 'auto':
168168
logger.debug('Forcing PCB text variables reset')
169169
GS.global_invalidate_pcb_text_cache = 'yes'
170+
if GS.global_update_pcb_text_cache == 'auto':
171+
logger.debug('Forcing PCB text variables update')
172+
GS.global_update_pcb_text_cache = 'yes'

0 commit comments

Comments
 (0)