[Report][Added] Expansion for KiCad text variables and environment variables

This commit is contained in:
Salvador E. Tropea 2023-01-19 21:05:32 -03:00
parent 72baea47df
commit 47bdc26154
7 changed files with 90 additions and 15 deletions

View File

@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added a mechanism to create a page for each copper layer. (#365)
- Plot related outputs and PCB_Print:
- Added support for the KiCad 6 "sketch_pads_on_fab_layers" option. (#356)
- Report:
- Expansion for KiCad text variables and environment variables (See #368)
- *SCH_Print:
- Added options to select the color theme and enable background color. (#362)
- SVG:

View File

@ -1608,7 +1608,7 @@ Notes:
format, 'ply' is Polygon File Format (Stanford).
Note that some formats includes the light and camera and others are just the 3D model
(i.e. STL and PLY).
- `output`: [string='%f-%i%I%v.%x'] Name for the generated file (%i='blender' %x=VARIABLE).
- `output`: [string='%f-%i%I%v.%x'] Name for the generated file (%i='3D_blender_$VIEW' %x=VARIABLE).
The extension is selected from the type. Affected by global options.
- `pcb_import`: Options to configure how Blender imports the PCB.
The default values are good for most cases.
@ -4189,6 +4189,8 @@ Notes:
* Type: `report`
* Description: Generates a report about the design.
Mainly oriented to be sent to the manufacturer or check PCB details.
You can expand internal values, KiCad text variables and environment
variables using `${VARIABLE}`
* Valid keys:
- **`comment`**: [string=''] A comment for documentation purposes. It helps to identify the output.
- **`dir`**: [string='./'] Output directory for the generated files.

View File

@ -45,10 +45,10 @@ preflight:
# This is useful for KiCad 5, use `set_text_variables` when using KiCad 6.
# This preflight modifies the PCB. Even when a back-up is done use it carefully.
pcb_replace:
date_command: "git log -1 --format='%as' -- $KIBOT_PCB_NAME"
date_command: 'git log -1 --format="%as" -- "$KIBOT_PCB_NAME"'
replace_tags:
- tag: '@git_hash@'
command: 'git log -1 --format="%h" $KIBOT_PCB_NAME'
command: 'git log -1 --format="%h" "$KIBOT_PCB_NAME"'
before: 'Git hash: <'
after: '>'
# [boolean=false] Runs the DRC (Distance Rules Check). To ensure we have a valid PCB.
@ -63,10 +63,10 @@ preflight:
# This is useful for KiCad 5, use `set_text_variables` when using KiCad 6.
# This preflight modifies the schematics. Even when a back-up is done use it carefully.
sch_replace:
date_command: "git log -1 --format='%as' -- $KIBOT_SCH_NAME"
date_command: 'git log -1 --format="%as" -- "$KIBOT_SCH_NAME"'
replace_tags:
- tag: '@git_hash@'
command: 'git log -1 --format="%h" $KIBOT_SCH_NAME'
command: 'git log -1 --format="%h" "$KIBOT_SCH_NAME"'
before: 'Git hash: <'
after: '>'
# [dict|list(dict)] Defines KiCad 6 variables.
@ -135,7 +135,7 @@ outputs:
no_virtual: false
# [dict|list(dict)] Outputs to generate in the same run
outputs:
# [string='%f-%i%I%v.%x'] Name for the generated file (%i='blender' %x=VARIABLE).
# [string='%f-%i%I%v.%x'] Name for the generated file (%i='3D_blender_$VIEW' %x=VARIABLE).
# The extension is selected from the type. Affected by global options
- output: '%f-%i%I%v.%x'
# [string='render'] [fbx,obj,x3d,gltf,stl,ply,blender,render] The format for the output.
@ -2886,6 +2886,8 @@ outputs:
zoom: 0
# Design report:
# Mainly oriented to be sent to the manufacturer or check PCB details.
# You can expand internal values, KiCad text variables and environment
# variables using `${VARIABLE}`
- name: 'report_example'
comment: 'Generates a report about the design.'
type: 'report'

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Salvador E. Tropea
# Copyright (c) 2022 Instituto Nacional de Tecnología Industrial
# Copyright (c) 2022-2023 Salvador E. Tropea
# Copyright (c) 2022-2023 Instituto Nacional de Tecnología Industrial
# License: GPL-3.0
# Project: KiBot (formerly KiPlot)
"""
@ -31,6 +31,7 @@ from .kiplot import config_output
from .dep_downloader import get_dep_data
from .macros import macros, document, output_class # noqa: F401
from . import log
from . import __version__
logger = log.get_logger()
INF = float('inf')
@ -214,6 +215,7 @@ class ReportOptions(BaseOptions):
dep = get_dep_data('report', 'PanDoc')
deb_text = 'In Debian/Ubuntu environments: install '+list_nice([dep.deb_package]+dep.extra_deb)
self._help_do_convert += ".\n"+'\n'.join(dep.comments)+'\n'+deb_text
self._shown_defined = False
def config(self, parent):
super().config(parent)
@ -284,7 +286,10 @@ class ReportOptions(BaseOptions):
rep = str(val)
line = line.replace('${'+var_ori+'}', rep)
else:
print('Error: Unable to expand `{}`'.format(var))
logger.error('Unable to expand `{}`'.format(var))
if not self._shown_defined:
self._shown_defined = True
logger.error('Defined values: {}'.format([v for v in defined.keys() if v[0] != '_']))
return line
def context_defined_tracks(self, line):
@ -747,9 +752,28 @@ class ReportOptions(BaseOptions):
logger.debug('- Result `{}`'.format(res))
return res
def get_kicad_vars(self):
vars = {}
vars['KICAD_VERSION'] = 'KiCad E.D.A. '+GS.kicad_version+' + KiBot v'+__version__
GS.load_pcb_title_block()
for num in range(9):
vars['COMMENT'+str(num+1)] = GS.pcb_com[num]
vars['COMPANY'] = GS.pcb_comp
vars['ISSUE_DATE'] = GS.pcb_date
vars['REVISION'] = GS.pcb_rev
vars['TITLE'] = GS.pcb_title
vars['FILENAME'] = GS.pcb_basename+'.kicad_pcb'
return vars
def do_template(self, template_file, output_file):
text = ''
logger.debug("Report template: `{}`".format(template_file))
# Collect the thing we could expand
defined = {}
defined.update(os.environ)
defined.update(self.get_kicad_vars())
defined.update(GS.load_pro_variables())
defined.update(self.__dict__)
with open(template_file, "rt") as f:
skip_next = False
for line in f:
@ -774,7 +798,7 @@ class ReportOptions(BaseOptions):
raise KiPlotConfigurationError("Unknown context: `{}`".format(context))
if not done:
# Just replace using any data member (_* excluded)
line = self.do_replacements(line, self.__dict__)
line = self.do_replacements(line, defined)
text += line
logger.debug("Report output: `{}`".format(output_file))
if self.to_ascii:
@ -880,7 +904,9 @@ class ReportOptions(BaseOptions):
class Report(BaseOutput): # noqa: F821
""" Design report
Generates a report about the design.
Mainly oriented to be sent to the manufacturer or check PCB details. """
Mainly oriented to be sent to the manufacturer or check PCB details.
You can expand internal values, KiCad text variables and environment
variables using `${VARIABLE}` """
def __init__(self):
super().__init__()
with document:

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Salvador E. Tropea
# Copyright (c) 2021 Instituto Nacional de Tecnología Industrial
# Copyright (c) 2021-2023 Salvador E. Tropea
# Copyright (c) 2021-2023 Instituto Nacional de Tecnología Industrial
# License: GPL-3.0
# Project: KiBot (formerly KiPlot)
import os
@ -80,10 +80,10 @@ class Base_Replace(BasePreFlight): # noqa: F821
@classmethod
def get_example(cls):
""" Returns a YAML value for the example config """
return ("\n date_command: \"git log -1 --format='%as' -- $KIBOT_{}_NAME\""
return ("\n date_command: 'git log -1 --format=\"%as\" -- \"$KIBOT_{}_NAME\"'"
"\n replace_tags:"
"\n - tag: '@git_hash@'"
"\n command: 'git log -1 --format=\"%h\" $KIBOT_{}_NAME'"
"\n command: 'git log -1 --format=\"%h\" \"$KIBOT_{}_NAME\"'"
"\n before: 'Git hash: <'"
"\n after: '>'".format(cls._context, cls._context))

View File

@ -0,0 +1,16 @@
---
title: |
| ACME GmbH
| PCB Specifications
subtitle: |
| Project ${PROJECT} Rev ${REVISION}
| ${SAP}
author: ${AUTHOR}
date: ${ISSUE_DATE}
footer: ${git_hash}
geometry: "left=2cm, right=2cm, top=3cm, bottom=3cm"
---
Git hash: ${git_hash}
\newpage

View File

@ -0,0 +1,27 @@
# Example KiBot config file
kibot:
version: 1
preflight:
set_text_variables:
- name: PROJECT
text: Test project
- name: SAP
text: XYZ
- name: AUTHOR
text: Juan de los palotes
- name: date
command: 'git log -1 --format="%as" -- "$KIBOT_PCB_NAME"'
- name: git_hash
command: 'git log -1 --format="%h" "$KIBOT_PCB_NAME"'
before: 'Git hash: <'
after: '>'
- name: gh
command: 'git log -1 --format="%h" "$KIBOT_PCB_NAME"'
outputs:
- name: 'report_vars'
comment: "Various variables"
type: report
options:
template: tests/data/report_vars.txt