Ajout FishPeper
This commit is contained in:
130
tinyFISH/doc/generate_svg.py
Normal file
130
tinyFISH/doc/generate_svg.py
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python
|
||||
# Copyright 2015 Scott Bezek
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import pcbnew
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
import pcb_util
|
||||
|
||||
from svg_processor import SvgProcessor
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PCB_FILENAME = '../tinyFISH.kicad_pcb'
|
||||
|
||||
# Have to use absolute path for build_directory otherwise pcbnew will output relative to the temp file
|
||||
BUILD_DIRECTORY = os.path.abspath('./')
|
||||
|
||||
def color_with_alpha(base_color, alpha):
|
||||
return (base_color & ~(0xFF << 24)) | ((alpha & 0xFF) << 24)
|
||||
|
||||
def render(job):
|
||||
temp_dir = os.path.join(BUILD_DIRECTORY, 'temp_layers')
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
try:
|
||||
os.makedirs(temp_dir)
|
||||
plot_job(job, BUILD_DIRECTORY, temp_dir)
|
||||
finally:
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
|
||||
def plot_job(job, output_directory, temp_dir):
|
||||
logger.info("processing job " + job["filename"])
|
||||
with pcb_util.get_plotter(PCB_FILENAME, temp_dir) as plotter:
|
||||
plotter.plot_options.SetMirror(job["mirror"])
|
||||
plotter.plot_options.SetExcludeEdgeLayer(False)
|
||||
processed_svg_files = []
|
||||
for i, layer in enumerate(job["layers"]):
|
||||
output_filename = plotter.plot(layer['layer'], pcbnew.PLOT_FORMAT_SVG)
|
||||
logger.info('Post-processing %s...', output_filename)
|
||||
processor = SvgProcessor(output_filename)
|
||||
def colorize(original):
|
||||
if original.lower() == '#000000':
|
||||
return layer['color']
|
||||
return original
|
||||
processor.apply_color_transform(colorize)
|
||||
processor.wrap_with_group({
|
||||
'opacity': str(layer['alpha']),
|
||||
})
|
||||
|
||||
output_filename2 = os.path.join(temp_dir, 'processed-' + os.path.basename(output_filename))
|
||||
processor.write(output_filename2)
|
||||
processed_svg_files.append((output_filename2, processor))
|
||||
|
||||
logger.info('merging layers...')
|
||||
final_svg = os.path.join(output_directory, job["filename"] + '.svg')
|
||||
|
||||
shutil.copyfile(processed_svg_files[0][0], final_svg)
|
||||
output_processor = SvgProcessor(final_svg)
|
||||
for _, processor in processed_svg_files:
|
||||
output_processor.import_groups(processor)
|
||||
output_processor.write(final_svg)
|
||||
|
||||
logger.info('rasterizing...')
|
||||
final_png = os.path.join(output_directory, job["filename"] + '.png')
|
||||
|
||||
subprocess.check_call([
|
||||
'inkscape',
|
||||
'--export-area-drawing',
|
||||
'--export-dpi=600',
|
||||
'--export-png', final_png,
|
||||
'--export-background', '#FFFFFF',
|
||||
final_svg,
|
||||
])
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
jobs = [
|
||||
#front placement
|
||||
{
|
||||
"filename" : "placement_front",
|
||||
"mirror" : False,
|
||||
"layers" : [
|
||||
{ 'layer': pcbnew.F_Cu, 'color': '#CC0000', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.F_SilkS, 'color': '#045a00', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.Eco1_User, 'color': '#002BFF', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.F_Fab, 'color': '#000000', 'alpha': 1.0, }
|
||||
]
|
||||
},
|
||||
#back placement
|
||||
{
|
||||
"filename" : "placement_back",
|
||||
"mirror" : True,
|
||||
"layers" : [
|
||||
{ 'layer': pcbnew.B_Cu, 'color': '#00DD00', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.B_SilkS, 'color': '#A000FF', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.Eco2_User, 'color': '#002BFF', 'alpha': 1.0, },
|
||||
{ 'layer': pcbnew.B_Fab, 'color': '#000000', 'alpha': 1.0, }
|
||||
]
|
||||
},
|
||||
#front rendering
|
||||
#{
|
||||
#"filename" : "rendered_front",
|
||||
#"mirror" : False,
|
||||
#"layers" : [
|
||||
# #005518
|
||||
# { 'layer': pcbnew.F_Cu, 'color': '#401264', 'alpha': 1.0, },
|
||||
# { 'layer': pcbnew.F_SilkS, 'color': '#FFFFFF', 'alpha': 1.0, },
|
||||
# { 'layer': pcbnew.F_Mask, 'color': '#FFE13A', 'alpha': 1.0, },
|
||||
# { 'layer': pcbnew.Edge_Cuts, 'color': '#000000', 'alpha': 1.0, },
|
||||
# ]
|
||||
#}
|
||||
]
|
||||
|
||||
for x in jobs:
|
||||
render(x)
|
||||
156
tinyFISH/doc/pcb_util.py
Normal file
156
tinyFISH/doc/pcb_util.py
Normal file
@@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env python
|
||||
# Copyright 2015 Scott Bezek
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
import pcbnew
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_LAYER_NAME = {
|
||||
pcbnew.F_Cu: 'F.Cu',
|
||||
pcbnew.B_Cu: 'B.Cu',
|
||||
pcbnew.F_Adhes: 'F.Adhes',
|
||||
pcbnew.B_Adhes: 'B.Adhes',
|
||||
pcbnew.F_SilkS: 'F.SilkS',
|
||||
pcbnew.B_SilkS: 'B.SilkS',
|
||||
pcbnew.F_Paste: 'F.Paste',
|
||||
pcbnew.B_Paste: 'B.Paste',
|
||||
pcbnew.F_Mask: 'F.Mask',
|
||||
pcbnew.B_Mask: 'B.Mask',
|
||||
pcbnew.Edge_Cuts: 'Edge.Cuts',
|
||||
pcbnew.Eco1_User: 'Eco1.User',
|
||||
pcbnew.Eco2_User: 'Eco2.User',
|
||||
pcbnew.F_Fab: 'F.Fab',
|
||||
pcbnew.B_Fab: 'B.Fab',
|
||||
#TODO: add the rest
|
||||
}
|
||||
|
||||
@contextmanager
|
||||
def versioned_board(filename):
|
||||
versioned_contents = _get_versioned_contents(filename)
|
||||
with tempfile.NamedTemporaryFile(suffix='.kicad_pcb') as temp_pcb:
|
||||
logger.debug('Writing to %s', temp_pcb.name)
|
||||
temp_pcb.write(versioned_contents)
|
||||
temp_pcb.flush()
|
||||
|
||||
logger.debug('Load board')
|
||||
board = pcbnew.LoadBoard(temp_pcb.name)
|
||||
yield board
|
||||
|
||||
def get_layer_name(kicad_layer_id):
|
||||
if kicad_layer_id in _LAYER_NAME:
|
||||
return _LAYER_NAME[kicad_layer_id]
|
||||
else:
|
||||
return 'Unknown(%r)' % (kicad_layer_id,)
|
||||
|
||||
@contextmanager
|
||||
def get_plotter(pcb_filename, build_directory):
|
||||
with versioned_board(pcb_filename) as board:
|
||||
yield GerberPlotter(board, build_directory)
|
||||
|
||||
class GerberPlotter(object):
|
||||
def __init__(self, board, build_directory):
|
||||
self.board = board
|
||||
self.build_directory = build_directory
|
||||
self.plot_controller = pcbnew.PLOT_CONTROLLER(board)
|
||||
self.plot_options = self.plot_controller.GetPlotOptions()
|
||||
self.plot_options.SetOutputDirectory(build_directory)
|
||||
|
||||
self.plot_options.SetPlotFrameRef(False)
|
||||
self.plot_options.SetLineWidth(pcbnew.FromMM(0.35))
|
||||
self.plot_options.SetScale(1)
|
||||
self.plot_options.SetUseAuxOrigin(True)
|
||||
self.plot_options.SetMirror(False)
|
||||
self.plot_options.SetExcludeEdgeLayer(True)
|
||||
|
||||
def plot(self, layer, plot_format):
|
||||
logger.info('Plotting layer %s (kicad layer=%r)', get_layer_name(layer), layer)
|
||||
layer_name = get_layer_name(layer)
|
||||
self.plot_controller.SetLayer(layer)
|
||||
self.plot_controller.OpenPlotfile(layer_name, plot_format , 'Plot')
|
||||
output_filename = self.plot_controller.GetPlotFileName()
|
||||
|
||||
self.plot_controller.PlotLayer()
|
||||
self.plot_controller.ClosePlot()
|
||||
return output_filename
|
||||
|
||||
def plot_drill(self):
|
||||
board_name = os.path.splitext(os.path.basename(self.board.GetFileName()))[0]
|
||||
logger.info('Plotting drill file')
|
||||
drill_writer = pcbnew.EXCELLON_WRITER(self.board)
|
||||
drill_writer.SetMapFileFormat(pcbnew.PLOT_FORMAT_PDF)
|
||||
|
||||
mirror = False
|
||||
minimalHeader = False
|
||||
offset = pcbnew.wxPoint(0, 0)
|
||||
merge_npth = True
|
||||
drill_writer.SetOptions(mirror, minimalHeader, offset, merge_npth)
|
||||
|
||||
metric_format = True
|
||||
drill_writer.SetFormat(metric_format)
|
||||
|
||||
generate_drill = True
|
||||
generate_map = True
|
||||
drill_writer.CreateDrillandMapFilesSet(self.build_directory, generate_drill, generate_map)
|
||||
|
||||
drill_file_name = os.path.join(
|
||||
self.build_directory,
|
||||
'%s.drl' % (board_name,)
|
||||
)
|
||||
|
||||
map_file_name = os.path.join(
|
||||
self.build_directory,
|
||||
'%s-drl_map.pdf' % (board_name,)
|
||||
)
|
||||
return drill_file_name, map_file_name
|
||||
|
||||
def _get_versioned_contents(filename):
|
||||
with open(filename, 'rb') as pcb:
|
||||
original_contents = pcb.read()
|
||||
version_info = get_version_info()
|
||||
return original_contents \
|
||||
.replace('COMMIT: deadbeef', 'COMMIT: ' + version_info['revision']) \
|
||||
.replace('DATE: YYYY-MM-DD', 'DATE: ' + version_info['date'])
|
||||
|
||||
def get_version_info():
|
||||
git_rev = subprocess.check_output([
|
||||
'git',
|
||||
'rev-parse',
|
||||
'--short',
|
||||
'HEAD',
|
||||
]).strip()
|
||||
|
||||
return {
|
||||
'revision': git_rev,
|
||||
'date': datetime.date.today().strftime('%Y-%m-%d'),
|
||||
}
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Test pcb util')
|
||||
parser.add_argument('input_file', help='Input .kicad_pcb file')
|
||||
args = parser.parse_args()
|
||||
with versioned_board(args.input_file) as board:
|
||||
logger.info('Loaded %s', board.GetFileName())
|
||||
for module in board.GetModules():
|
||||
logger.info('Module %s: %s', module.GetReference(), module.GetValue())
|
||||
|
||||
3151
tinyFISH/doc/placement_back.svg
Normal file
3151
tinyFISH/doc/placement_back.svg
Normal file
File diff suppressed because it is too large
Load Diff
|
After Width: | Height: | Size: 263 KiB |
3450
tinyFISH/doc/placement_front.svg
Normal file
3450
tinyFISH/doc/placement_front.svg
Normal file
File diff suppressed because it is too large
Load Diff
|
After Width: | Height: | Size: 347 KiB |
77
tinyFISH/doc/svg_processor.py
Normal file
77
tinyFISH/doc/svg_processor.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# Copyright 2015 Scott Bezek
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import re
|
||||
import xml
|
||||
from xml.dom import minidom
|
||||
|
||||
"""
|
||||
Processes SVG files generated by pcbnew to colorize and merge
|
||||
"""
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SvgProcessor(object):
|
||||
|
||||
def __init__(self, input_file):
|
||||
self.dom = minidom.parse(input_file)
|
||||
self.svg_node = self.dom.documentElement
|
||||
|
||||
def apply_color_transform(self, transform_function):
|
||||
# Set fill and stroke on all groups
|
||||
for group in self.svg_node.getElementsByTagName('g'):
|
||||
SvgProcessor._apply_transform(group, {
|
||||
'fill': transform_function,
|
||||
'stroke': transform_function,
|
||||
})
|
||||
|
||||
def import_groups(self, from_svg_processor):
|
||||
for child in from_svg_processor.svg_node.childNodes:
|
||||
if child.nodeType != child.ELEMENT_NODE or child.tagName != 'g':
|
||||
continue
|
||||
group = child
|
||||
output_node = self.dom.importNode(group, True)
|
||||
self.svg_node.appendChild(output_node)
|
||||
|
||||
def write(self, filename):
|
||||
with open(filename, 'wb') as output_file:
|
||||
self.svg_node.writexml(output_file)
|
||||
|
||||
def wrap_with_group(self, attrs):
|
||||
parent = self.svg_node
|
||||
wrapper = self.dom.createElement("g")
|
||||
for k,v in attrs.items():
|
||||
wrapper.setAttribute(k,v)
|
||||
|
||||
for child in parent.getElementsByTagName('g'):
|
||||
parent.removeChild(child)
|
||||
wrapper.appendChild(child)
|
||||
|
||||
parent.appendChild(wrapper)
|
||||
|
||||
@staticmethod
|
||||
def _apply_transform(node, values):
|
||||
original_style = node.attributes['style'].value
|
||||
for (k,v) in values.items():
|
||||
escaped_key = re.escape(k)
|
||||
m = re.search(r'\b' + escaped_key + r':(?P<value>[^;]*);', original_style)
|
||||
if m:
|
||||
transformed_value = v(m.group('value'))
|
||||
original_style = re.sub(
|
||||
r'\b' + escaped_key + r':[^;]*;',
|
||||
k + ':' + transformed_value + ';',
|
||||
original_style)
|
||||
node.attributes['style'] = original_style
|
||||
|
||||
Reference in New Issue
Block a user