From f1eaee6cece33b5a438872004351065a3b469069 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Sat, 30 Mar 2024 19:31:12 -0300 Subject: [PATCH] [BoM][Added] Support for SVG format in the logos Converting them to PNG Closes #383 --- CHANGELOG.md | 1 + docs/samples/generic_plot.kibot.yaml | 12 +++-- docs/source/Changelog.rst | 1 + docs/source/configuration/outputs/bom.rst | 8 +++- .../outputs/navigate_results.rst | 2 +- docs/source/dependencies.rst | 1 + kibot/out_bom.py | 45 +++++++++++++++++-- src/kibot-check | 11 ++++- 8 files changed, 69 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71968beb..3356ee5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 options. So one entry can be something like *R10-R20*. Can be disabled using the global option `allow_component_ranges`. (See yaqwsx/PcbDraw#159) - Navigate results: A header and navigation bar (#582) +- BoM: support for SVG format in the logos (#383) ### Changed - CI/CD: we now filter some warnings that are always generated by docker diff --git a/docs/samples/generic_plot.kibot.yaml b/docs/samples/generic_plot.kibot.yaml index e9941b27..e7500bda 100644 --- a/docs/samples/generic_plot.kibot.yaml +++ b/docs/samples/generic_plot.kibot.yaml @@ -503,8 +503,11 @@ outputs: # [boolean|string|list(string)=''] Column/s containing LCSC part numbers, will be linked to web page. # Use **true** to copy the value indicated by the `field_lcsc_part` global option lcsc_link: '' - # [string|boolean=''] PNG file to use as logo, use false to remove + # [string|boolean=''] PNG/SVG file to use as logo, use false to remove. + # Note that when using an SVG this is first converted to a PNG using `logo_width` logo: '' + # [number=370] Used when the logo is an SVG image. This width is used to render the SVG image + logo_width: 370 # [string|list(string)=''] Column/s containing Mouser part numbers, will be linked to web page mouser_link: '' # [string='modern-blue'] Page style. Internal styles: modern-blue, modern-green, modern-red and classic. @@ -597,10 +600,13 @@ outputs: # [boolean|string|list(string)=''] Column/s containing LCSC part numbers, will be linked to web page. # Use **true** to copy the value indicated by the `field_lcsc_part` global option lcsc_link: '' - # [string|boolean=''] PNG file to use as logo, use false to remove + # [string|boolean=''] PNG/SVG file to use as logo, use false to remove. + # Note that when using an SVG this is first converted to a PNG using `logo_width` logo: '' # [number=2] Scaling factor for the logo. Note that this value isn't honored by all spreadsheet software logo_scale: 2 + # [number=370] Used when the logo is an SVG image. This width is used to render the SVG image + logo_width: 370 # [number=60] [20,999] Maximum column width (characters) max_col_width: 60 # [string|list(string)=''] Column/s containing Mouser part numbers, will be linked to web page @@ -1759,7 +1765,7 @@ outputs: output: '%f-%i%I%v.%x' # [boolean=false] Skip outputs with `run_by_default: false` skip_not_run: false - # [string=''] Title for the page, when empy KiBot will try using the schematic or PCB title. + # [string=''] Title for the page, when empty KiBot will try using the schematic or PCB title. # If they are empty the name of the project, schematic or PCB file is used. # You can use %X values and KiCad variables here title: '' diff --git a/docs/source/Changelog.rst b/docs/source/Changelog.rst index c422791f..7943b979 100644 --- a/docs/source/Changelog.rst +++ b/docs/source/Changelog.rst @@ -26,6 +26,7 @@ Added Can be disabled using the global option ``allow_component_ranges``. (See yaqwsx/PcbDraw#159) - Navigate results: A header and navigation bar (#582) +- BoM: support for SVG format in the logos (#383) Changed ~~~~~~~ diff --git a/docs/source/configuration/outputs/bom.rst b/docs/source/configuration/outputs/bom.rst index c97fb64c..88953405 100644 --- a/docs/source/configuration/outputs/bom.rst +++ b/docs/source/configuration/outputs/bom.rst @@ -100,7 +100,8 @@ Parameters: - **datasheet_as_link** :index:`: ` [string=''] Column with links to the datasheet. - **generate_dnf** :index:`: ` [boolean=true] Generate a separated section for DNF (Do Not Fit) components. - - **logo** :index:`: ` [string|boolean=''] PNG file to use as logo, use false to remove. + - **logo** :index:`: ` [string|boolean=''] PNG/SVG file to use as logo, use false to remove. + Note that when using an SVG this is first converted to a PNG using `logo_width`. - **title** :index:`: ` [string='KiBot Bill of Materials'] BoM title. - ``col_colors`` :index:`: ` [boolean=true] Use colors to show the field type. @@ -114,6 +115,7 @@ Parameters: - ``lcsc_link`` :index:`: ` [boolean|string|list(string)=''] Column/s containing LCSC part numbers, will be linked to web page. Use **true** to copy the value indicated by the `field_lcsc_part` global option. + - ``logo_width`` :index:`: ` [number=370] Used when the logo is an SVG image. This width is used to render the SVG image. - ``mouser_link`` :index:`: ` [string|list(string)=''] Column/s containing Mouser part numbers, will be linked to web page. - ``style`` :index:`: ` [string='modern-blue'] Page style. Internal styles: modern-blue, modern-green, modern-red and classic. @@ -134,7 +136,8 @@ Parameters: - **generate_dnf** :index:`: ` [boolean=true] Generate a separated section for DNF (Do Not Fit) components. - **kicost** :index:`: ` [boolean=false] Enable KiCost worksheet creation. Note: an example of how to use it on CI/CD can be found `here `__. - - **logo** :index:`: ` [string|boolean=''] PNG file to use as logo, use false to remove. + - **logo** :index:`: ` [string|boolean=''] PNG/SVG file to use as logo, use false to remove. + Note that when using an SVG this is first converted to a PNG using `logo_width`. - **specs** :index:`: ` [boolean=false] Enable Specs worksheet creation. Contains specifications for the components. Works with only some KiCost APIs. @@ -161,6 +164,7 @@ Parameters: Use **true** to copy the value indicated by the `field_lcsc_part` global option. - ``logo_scale`` :index:`: ` [number=2] Scaling factor for the logo. Note that this value isn't honored by all spreadsheet software. + - ``logo_width`` :index:`: ` [number=370] Used when the logo is an SVG image. This width is used to render the SVG image. - ``max_col_width`` :index:`: ` [number=60] [20,999] Maximum column width (characters). - ``mouser_link`` :index:`: ` [string|list(string)=''] Column/s containing Mouser part numbers, will be linked to web page. diff --git a/docs/source/configuration/outputs/navigate_results.rst b/docs/source/configuration/outputs/navigate_results.rst index 5bcb986c..647f2436 100644 --- a/docs/source/configuration/outputs/navigate_results.rst +++ b/docs/source/configuration/outputs/navigate_results.rst @@ -32,7 +32,7 @@ Parameters: - ``logo_url`` :index:`: ` [string='https://github.com/INTI-CMNB/KiBot/'] Target link when clicking the logo. - ``nav_bar`` :index:`: ` [boolean=true] Add a side navigation bar to quickly access to the outputs. - ``skip_not_run`` :index:`: ` [boolean=false] Skip outputs with `run_by_default: false`. - - ``title`` :index:`: ` [string=''] Title for the page, when empy KiBot will try using the schematic or PCB title. + - ``title`` :index:`: ` [string=''] Title for the page, when empty KiBot will try using the schematic or PCB title. If they are empty the name of the project, schematic or PCB file is used. You can use %X values and KiCad variables here. - ``title_url`` :index:`: ` [string|boolean=''] Target link when clicking the title, use false to remove. diff --git a/docs/source/dependencies.rst b/docs/source/dependencies.rst index 6fbd856c..9ab3ac93 100644 --- a/docs/source/dependencies.rst +++ b/docs/source/dependencies.rst @@ -105,6 +105,7 @@ - Optional to: + - SVG logos for the BoM for `bom` - Create outputs preview for `navigate_results` - Create PNG icons for `navigate_results` - Create PDF, PNG, PS and EPS formats for `pcb_print` diff --git a/kibot/out_bom.py b/kibot/out_bom.py index ca35a9be..b4bcbf34 100644 --- a/kibot/out_bom.py +++ b/kibot/out_bom.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2020-2023 Salvador E. Tropea -# Copyright (c) 2020-2023 Instituto Nacional de TecnologĂ­a Industrial +# Copyright (c) 2020-2024 Salvador E. Tropea +# Copyright (c) 2020-2024 Instituto Nacional de TecnologĂ­a Industrial # License: MIT # Project: KiBot (formerly KiPlot) """ @@ -10,6 +10,8 @@ - from: KiCost role: Find components costs and specs version: 1.1.8 + - from: RSVG + role: SVG logos for the BoM - name: XLSXWriter role: Create XLSX files python_module: true @@ -22,12 +24,13 @@ from copy import deepcopy import os import re +from tempfile import NamedTemporaryFile from .gs import GS from .misc import W_BADFIELD, W_NEEDSPCB, DISTRIBUTORS, W_NOPART, W_MISSREF, DISTRIBUTORS_STUBS, DISTRIBUTORS_STUBS_SEPS from .optionable import Optionable, BaseOptions from .registrable import RegOutput from .error import KiPlotConfigurationError -from .kiplot import get_board_comps_data, load_any_sch, register_xmp_import, expand_fields +from .kiplot import get_board_comps_data, load_any_sch, register_xmp_import, expand_fields, run_command from .kicad.v5_sch import SchematicComponent, SchematicField from .bom.columnlist import ColumnList, BoMError from .bom.bom import do_bom @@ -201,7 +204,10 @@ def __init__(self): self.highlight_empty = True """ Use a color for empty cells. Applies only when `col_colors` is `true` """ self.logo = Optionable - """ *[string|boolean=''] PNG file to use as logo, use false to remove """ + """ *[string|boolean=''] PNG/SVG file to use as logo, use false to remove. + Note that when using an SVG this is first converted to a PNG using `logo_width` """ + self.logo_width = 370 + """ Used when the logo is an SVG image. This width is used to render the SVG image """ self.title = 'KiBot Bill of Materials' """ *BoM title """ self.extra_info = Optionable @@ -892,6 +898,29 @@ def aggregate_comps(self, comps): comps.extend(new_comps) prj.source = os.path.basename(prj.file) + def solve_logo(self): + if self.format == 'html': + logo = self.html.logo + w = self.html.logo_width + elif self.format == 'xlsx': + logo = self.xlsx.logo + w = self.xlsx.logo_width + else: + return None + ext = os.path.splitext(logo)[1] + if ext.lower() != '.svg': + return None + with NamedTemporaryFile(mode='w', suffix='.png', delete=False) as f: + png = f.name + cmd = [self.ensure_tool('RSVG'), '-w', str(w), '-f', 'png', '-o', png, logo] + run_command(cmd) + self._old_logo = logo + if self.format == 'html': + self.html.logo = png + elif self.format == 'xlsx': + self.xlsx.logo = png + return png + def run(self, output): format = self.format.lower() if format == 'xlsx': @@ -951,10 +980,18 @@ def run(self, output): # To translate project to ID if self.source_by_id: self.source_to_id = {prj.name: prj.ref_id for prj in self.aggregate} + tmp_png = self.solve_logo() try: do_bom(output, format, comps, self) except BoMError as e: raise KiPlotConfigurationError(str(e)) + finally: + if tmp_png: + os.remove(tmp_png) + if self.format == 'html': + self.html.logo = self._old_logo + elif self.format == 'xlsx': + self.xlsx.logo = self._old_logo # Undo the reference prefix if self.ref_id: l_id = len(self.ref_id) diff --git a/src/kibot-check b/src/kibot-check index 1ce5bc13..a109e13e 100755 --- a/src/kibot-check +++ b/src/kibot-check @@ -1068,17 +1068,24 @@ deps = '{\ "extra_arch": null,\ "extra_deb": null,\ "help_option": "--version",\ - "importance": 4,\ + "importance": 5,\ "in_debian": true,\ "is_kicad_plugin": false,\ "is_python": false,\ "name": "RSVG tools",\ "no_cmd_line_version": false,\ "no_cmd_line_version_old": false,\ - "output": "navigate_results",\ + "output": "bom",\ "plugin_dirs": null,\ "pypi_name": "RSVG tools",\ "role": [\ + {\ + "desc": "SVG logos for the BoM",\ + "mandatory": false,\ + "max_version": null,\ + "output": "bom",\ + "version": null\ + },\ {\ "desc": "Create outputs preview",\ "mandatory": false,\