|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -# Copyright (c) 2022-2024 Salvador E. Tropea |
3 | | -# Copyright (c) 2022-2024 Instituto Nacional de Tecnología Industrial |
| 2 | +# Copyright (c) 2022-2026 Salvador E. Tropea |
| 3 | +# Copyright (c) 2022-2026 Instituto Nacional de Tecnología Industrial |
4 | 4 | # License: AGPL-3.0 |
5 | 5 | # Project: KiBot (formerly KiPlot) |
6 | 6 | # KiCad bugs: |
7 | 7 | # - Text bold doesn't work |
8 | 8 | # - Shape Line and Rect swapped |
9 | 9 | """ |
10 | | -KiCad v5/6/7/8 Worksheet format. |
| 10 | +KiCad v5/6/7/8/9 Worksheet format. |
11 | 11 | A basic implementation of the .kicad_wks file format. |
12 | 12 | Documentation: https://dev-docs.kicad.org/en/file-formats/sexpr-worksheet/ |
13 | 13 | """ |
14 | 14 | from base64 import b64decode |
| 15 | +from copy import deepcopy |
15 | 16 | import io |
16 | 17 | from pcbnew import wxPoint, wxSize, FromMM, wxPointMM |
17 | 18 | from ..gs import GS |
|
30 | 31 | from .pcb import get_embedded_file |
31 | 32 | from .pcb_draw_helpers import (GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_RIGHT, GR_TEXT_HJUSTIFY_CENTER, |
32 | 33 | GR_TEXT_VJUSTIFY_TOP, GR_TEXT_VJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_BOTTOM) |
33 | | -from .sexpdata import load, dumps, SExpData |
| 34 | +from .sexpdata import load, dumps, SExpData, Symbol |
34 | 35 | from .sexp_helpers import (_check_is_symbol_list, _check_float, _check_integer, _check_symbol_value, _check_str, _check_symbol, |
35 | 36 | _check_relaxed, _get_points, _check_symbol_str, Color) |
36 | 37 | from ..svgutils.transform import ImageElement, GroupElement |
|
48 | 49 | 'C3': 'COMMENT4', 'C4': 'COMMENT5', 'C5': 'COMMENT6', 'C6': 'COMMENT7', 'C7': 'COMMENT8', |
49 | 50 | 'C8': 'COMMENT9', 'Y': 'COMPANY', 'F': 'FILENAME', 'D': 'ISSUE_DATE', 'Z': 'PAPER', 'R': 'REVISION', |
50 | 51 | 'P': 'SHEETNAME', 'T': 'TITLE'} |
| 52 | +REVERSE_OPTION = {'page1only': 'notonpage1', 'notonpage1': 'page1only'} |
51 | 53 |
|
52 | 54 |
|
53 | 55 | class WksError(Exception): |
@@ -589,8 +591,27 @@ def expand(self, vars, remove_images=False): |
589 | 591 | new_sexp.append(e) |
590 | 592 | self.sexp = new_sexp |
591 | 593 |
|
592 | | - def save(self, fname): |
593 | | - """ Save the sexp to a file """ |
| 594 | + def save(self, fname, page=None): |
| 595 | + """ Save the sexp to a file. |
| 596 | + Can also adapt the `option` to match the page when using a tool for only one page """ |
| 597 | + sexp = self.sexp |
| 598 | + if page is not None and page != 1: |
| 599 | + # A page number is provided |
| 600 | + # Here we change things so KiCad can print it as page 1, but the page looks correct |
| 601 | + logger.debugl(2, f" - Changing things for page {page}") |
| 602 | + sexp = deepcopy(self.sexp) |
| 603 | + for e in sexp[1:]: |
| 604 | + e_type = _check_is_symbol_list(e) |
| 605 | + for o in e[1:]: |
| 606 | + if isinstance(o, list): |
| 607 | + smb = _check_is_symbol_list(o) |
| 608 | + if smb == 'option': |
| 609 | + # page1only -> notonpage1 |
| 610 | + # notonpage1 -> page1only |
| 611 | + old = o[1] |
| 612 | + o[1] = Symbol(REVERSE_OPTION[_check_symbol(o, 1, smb)]) |
| 613 | + logger.debugl(3, f" - Changing {e_type}: {old} -> {o[1]}") |
| 614 | + |
594 | 615 | with open(fname, 'wt') as f: |
595 | | - f.write(dumps(self.sexp)) |
| 616 | + f.write(dumps(sexp)) |
596 | 617 | f.write('\n') |
0 commit comments