Skip to content

Commit

Permalink
[Preflights][Added] update_footprint
Browse files Browse the repository at this point in the history
- Updates one or more footprints from the libs
- Useful for external QR codes, logos, etc.

Closes #492 related to #483
  • Loading branch information
set-soft committed Apr 1, 2024
1 parent 742fb6d commit ef6628b
Show file tree
Hide file tree
Showing 50 changed files with 13,638 additions and 81 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


## [1.6.6] - Unreleased
### Added
- New preflights:
- update_footprint: updates one or more footprints from the libs.
Useful for external QR codes, logos, etc. (#492 #483)


## [1.6.5] - 2024-03-31
Expand Down
3 changes: 3 additions & 0 deletions docs/samples/generic_plot.kibot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ preflight:
command: 'git log -1 --format="%h" "$KIBOT_PCB_NAME"'
before: 'Git hash: <'
after: '>'
# [string|list(string)=''] Updates footprints from the libs, you must provide one or more references to be updated.
# This is useful to replace logos using freshly created versions.
update_footprint: QR1, QR2
# [boolean=false] Update the QR codes.
# Complements the `qr_lib` output.
# The KiCad 6 files and the KiCad 5 PCB needs manual update, generating a new library isn't enough.
Expand Down
67 changes: 40 additions & 27 deletions docs/source/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@ Changelog <https://keepachangelog.com/en/1.0.0/>`__, and this project
adheres to `Semantic
Versioning <https://semver.org/spec/v2.0.0.html>`__.

[1.6.6] - Unreleased
--------------------

Added
~~~~~

- New preflights:

- update_footprint: updated one or more footprints from the libs.
Useful for external QR codes, logos, etc. (#492 #483)

[1.6.5] - 2024-03-31
--------------------

.. _added-1:

Added
~~~~~

Expand Down Expand Up @@ -70,7 +83,7 @@ Fixed
[1.6.4] - 2024-02-02
--------------------

.. _added-1:
.. _added-2:

Added
~~~~~
Expand Down Expand Up @@ -351,7 +364,7 @@ Fixed
[1.6.3] - 2023-06-26
--------------------

.. _added-2:
.. _added-3:

Added
~~~~~
Expand Down Expand Up @@ -521,7 +534,7 @@ Fixed
[1.6.2] - 2023-04-24
--------------------

.. _added-3:
.. _added-4:

Added
~~~~~
Expand Down Expand Up @@ -639,7 +652,7 @@ Changed:
[1.6.1] - 2023-03-16
--------------------

.. _added-4:
.. _added-5:

Added
~~~~~
Expand Down Expand Up @@ -688,7 +701,7 @@ Fixed
[1.6.0] - 2023-02-06
--------------------

.. _added-5:
.. _added-6:

Added
~~~~~
Expand Down Expand Up @@ -819,7 +832,7 @@ Fixed
[1.5.0] - 2022-12-16
--------------------

.. _added-6:
.. _added-7:

Added
~~~~~
Expand Down Expand Up @@ -933,7 +946,7 @@ Fixed
[1.4.0] - 2022-10-12
--------------------

.. _added-7:
.. _added-8:

Added
~~~~~
Expand Down Expand Up @@ -1051,7 +1064,7 @@ Changed
[1.3.0] - 2022-09-08
--------------------

.. _added-8:
.. _added-9:

Added
~~~~~
Expand Down Expand Up @@ -1151,7 +1164,7 @@ Changed
[1.2.0] - 2022-06-15
--------------------

.. _added-9:
.. _added-10:

Added
~~~~~
Expand Down Expand Up @@ -1210,7 +1223,7 @@ Changed
[1.1.0] - 2022-05-24
--------------------

.. _added-10:
.. _added-11:

Added
~~~~~
Expand Down Expand Up @@ -1241,7 +1254,7 @@ Fixed
[1.0.0] - 2022-05-10
--------------------

.. _added-11:
.. _added-12:

Added
~~~~~
Expand Down Expand Up @@ -1419,7 +1432,7 @@ Fixed
[0.11.0] - 2021-04-25
---------------------

.. _added-12:
.. _added-13:

Added
~~~~~
Expand Down Expand Up @@ -1475,7 +1488,7 @@ Fixed
[0.10.1] - 2021-02-22
---------------------

.. _added-13:
.. _added-14:

Added
~~~~~
Expand Down Expand Up @@ -1524,7 +1537,7 @@ Fixed
[0.10.0] - 2021-02-12
---------------------

.. _added-14:
.. _added-15:

Added
~~~~~
Expand Down Expand Up @@ -1587,7 +1600,7 @@ Fixed
[0.9.0] - 2021-01-04
--------------------

.. _added-15:
.. _added-16:

Added
~~~~~
Expand Down Expand Up @@ -1625,7 +1638,7 @@ Fixed
[0.8.1] - 2020-12-09
--------------------

.. _added-16:
.. _added-17:

Added
~~~~~
Expand All @@ -1646,7 +1659,7 @@ Fixed
[0.8.0] - 2020-11-06
--------------------

.. _added-17:
.. _added-18:

Added
~~~~~
Expand Down Expand Up @@ -1677,7 +1690,7 @@ Fixed
[0.7.0] - 2020-09-11
--------------------

.. _added-18:
.. _added-19:

Added
~~~~~
Expand Down Expand Up @@ -1741,7 +1754,7 @@ Fixed
[0.6.1] - 2020-08-20
--------------------

.. _added-19:
.. _added-20:

Added
~~~~~
Expand All @@ -1768,7 +1781,7 @@ Fixed
[0.6.0] - 2020-08-18
--------------------

.. _added-20:
.. _added-21:

Added
~~~~~
Expand Down Expand Up @@ -1836,7 +1849,7 @@ Changed
- Now we test the PCB and/or SCH only when we are doing something that
needs them.

.. _added-21:
.. _added-22:

Added
~~~~~
Expand Down Expand Up @@ -1905,7 +1918,7 @@ Fixed
[0.4.0] - 2020-06-17
--------------------

.. _added-22:
.. _added-23:

Added
~~~~~
Expand All @@ -1918,7 +1931,7 @@ Added
[0.3.0] - 2020-06-14
--------------------

.. _added-23:
.. _added-24:

Added
~~~~~
Expand Down Expand Up @@ -1947,7 +1960,7 @@ Fixed
[0.2.5] - 2020-06-11
--------------------

.. _added-24:
.. _added-25:

Added
~~~~~
Expand Down Expand Up @@ -1988,7 +2001,7 @@ Fixed
[0.2.3] - 2020-04-23
--------------------

.. _added-25:
.. _added-26:

Added
~~~~~
Expand Down Expand Up @@ -2026,7 +2039,7 @@ Fixed
[0.2.0] - 2020-03-28
--------------------

.. _added-26:
.. _added-27:

Added
~~~~~
Expand Down Expand Up @@ -2056,7 +2069,7 @@ Fixed
[0.1.1] - 2020-03-13
--------------------

.. _added-27:
.. _added-28:

Added
~~~~~
Expand Down
2 changes: 2 additions & 0 deletions docs/source/configuration/sup_preflights.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ Supported preflights
- ``text`` :index:`: <pair: preflight - set_text_variables; text>` [string=''] Text to insert instead of the variable.
- *variable* :index:`: <pair: preflight - set_text_variables; variable>` Alias for name.
- **update_footprint**: :index:`: <pair: preflights; update_footprint>` [string|list(string)=''] Updates footprints from the libs, you must provide one or more references to be updated.
This is useful to replace logos using freshly created versions.
- **update_qr**: :index:`: <pair: preflights; update_qr>` [boolean=false] Update the QR codes.
Complements the `qr_lib` output. |br|
The KiCad 6 files and the KiCad 5 PCB needs manual update, generating a new library isn't enough.
Expand Down
66 changes: 63 additions & 3 deletions kibot/kicad/sexp_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021-2023 Salvador E. Tropea
# Copyright (c) 2021-2023 Instituto Nacional de Tecnología Industrial
# Copyright (c) 2021-2024 Salvador E. Tropea
# Copyright (c) 2021-2024 Instituto Nacional de Tecnología Industrial
# License: AGPL-3.0
# Project: KiBot (formerly KiPlot)
import os
from tempfile import NamedTemporaryFile
from .error import SchError
from .sexpdata import Symbol
from ..error import KiPlotConfigurationError
from ..gs import GS
from .sexpdata import Symbol, Sep, dumps, SExpData, load
TO_SEPARATE = {'kicad_pcb', 'general', 'title_block', 'layers', 'setup', 'pcbplotparams', 'net_class', 'module',
'kicad_sch', 'lib_symbols', 'symbol', 'sheet', 'sheet_instances', 'symbol_instances'}


class Point(object):
Expand All @@ -18,6 +24,55 @@ def parse(items):
return Point(items)


def make_separated(sexp):
""" Add separators to make the file more readable """
if not isinstance(sexp, list):
return sexp
if not isinstance(sexp[0], Symbol) or sexp[0].value() not in TO_SEPARATE:
return sexp
separated = []
for s in sexp:
separated.append(make_separated(s))
if isinstance(s, list):
separated.append(Sep())
return separated


def load_sexp_file(fname):
with open(fname, 'rt') as fh:
error = None
try:
ki_file = load(fh)
except SExpData as e:
error = str(e)
if error:
raise KiPlotConfigurationError(error)
return ki_file


def save_pcb_from_sexp(pcb, logger):
# Make it readable
separated = make_separated(pcb[0])
# Save it to a temporal
with NamedTemporaryFile(mode='wt', suffix='.kicad_pcb', delete=False) as f:
logger.debug('- Saving updated PCB to: '+f.name)
f.write(dumps(separated))
f.write('\n')
tmp_pcb = f.name
# Also copy the project
GS.copy_project(tmp_pcb)
# Reload it
logger.debug('- Loading the temporal PCB')
GS.load_board(tmp_pcb, forced=True)
# Create a back-up and save it in the original place
logger.debug('- Replacing the old PCB')
os.remove(tmp_pcb)
GS.make_bkp(GS.pcb_file)
GS.board.Save(GS.pcb_file)
# After saving the file the name isn't changed, we must force it!!!
GS.board.SetFileName(GS.pcb_file)


def _check_is_symbol_list(e, allow_orphan_symbol=()):
# Each entry is a list
if not isinstance(e, list):
Expand Down Expand Up @@ -159,3 +214,8 @@ def _get_points(items):
def _get_size(items, pos, name):
value = _check_symbol_value(items, pos, name, 'size')
return _get_xy(value)


def _get_symbol_name(items):
""" Check if items is a list and starts with a symbol, return its name, otherwise None """
return None if not isinstance(items, list) or not isinstance(items[0], Symbol) else items[0].value()
Loading

0 comments on commit ef6628b

Please sign in to comment.