Skip to content

Commit

Permalink
Better behavior when using different page sizes
Browse files Browse the repository at this point in the history
- Specially in the red_green mode, it failed.
- Now we inform it.
  • Loading branch information
set-soft committed Mar 29, 2023
1 parent b7edb83 commit 00661fe
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
41 changes: 35 additions & 6 deletions kicad-diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
from pcbnew import LoadBoard, PLOT_CONTROLLER, FromMM, PLOT_FORMAT_PDF, Edge_Cuts, GetBuildVersion, ToMM
import pcbnew
import re
import shlex
from shutil import rmtree, which, copy2
from struct import unpack
from subprocess import call, PIPE, run, STDOUT, CalledProcessError
from sys import exit
from tempfile import mkdtemp
Expand Down Expand Up @@ -105,6 +107,7 @@ def SetExcludeEdgeLayer(po, exclude_edge_layer, layer):
include.addLayer(layer)
po.SetPlotOnAllLayersSelection(include)


def WriteBBox(board, hash_dir):
fname = '{}{}bbox.csv'.format(hash_dir, sep)
if isfile(fname):
Expand All @@ -118,6 +121,7 @@ def WriteBBox(board, hash_dir):
f.write(','.join(tuple(map(str, vals))))
return vals


def GenPCBImages(file, file_hash, hash_dir, file_no_ext, layer_names, wanted_layers):
# Setup the KiCad plotter
board = LoadBoard(file)
Expand Down Expand Up @@ -276,7 +280,7 @@ def GenImages(file, file_hash, all):


def run_command(command):
logger.debug('Executing: '+str(command))
logger.debug('Executing: '+shlex.join(command))
try:
res = run(command, check=True, stdout=PIPE, stderr=STDOUT).stdout.decode()
except CalledProcessError as e:
Expand Down Expand Up @@ -340,10 +344,28 @@ def adapt_name(name_layer):
return name_layer


def png_size(file):
with open(file, 'rb') as f:
s = f.read(32)
assert s[:8] == b'\x89PNG\r\n\x1a\n' and (s[12:16] == b'IHDR')
w, h = unpack('>LL', s[16:24])
return int(w), int(h)


def create_diff_stereo(old_name, new_name, diff_name, font_size, layer, resolution, name_layer, only_different):
wn, hn = png_size(new_name)
wo, ho = png_size(old_name)
if wn != wo or hn != ho:
extent = ' -extent {}x{}'.format(max(wn, wo), max(hn, ho))
extra_name = ' [diff page size]'
else:
extra_name = extent = ''
text = ' -font helvetica -pointsize '+font_size+' -draw "text 10,'+font_size+' \''+adapt_name(name_layer)+'\'" '
command = ['bash', '-c', '( convert "'+new_name+'" miff:- ; convert "'+old_name+'" miff:- ) | ' +
r'convert - \( -clone 0-1 -compose darken -composite \) '+text+' -channel RGB -combine "'+diff_name+'"']
command = ['bash', '-c',
'( convert "'+new_name+'"'+extent+' miff:- ;' +
' convert "'+old_name+'"'+extent+' miff:- ) | ' +
r'convert - \( -clone 0-1 -compose darken -composite \) ' +
text+extra_name+' -channel RGB -combine "'+diff_name+'"']
run_command(command)
include = True
if only_different:
Expand All @@ -355,6 +377,13 @@ def create_diff_stereo(old_name, new_name, diff_name, font_size, layer, resoluti


def create_diff_stat(old_name, new_name, diff_name, font_size, layer, resolution, name_layer, only_different):
wn, hn = png_size(new_name)
wo, ho = png_size(old_name)
if wn != wo or hn != ho:
extent = ' -extent {}x{}'.format(max(wn, wo), max(hn, ho))
extra_name = ' [diff page size]'
else:
extra_name = extent = ''
# Compare both
cmd = ['compare',
# Tolerate 5 % error in color (configurable)
Expand All @@ -365,15 +394,15 @@ def create_diff_stat(old_name, new_name, diff_name, font_size, layer, resolution
old_name,
'-colorspace', 'RGB',
diff_name]
logger.debug('Executing: '+str(cmd))
logger.debug('Executing: '+shlex.join(cmd))
res = run(cmd, stdout=PIPE, stderr=STDOUT)
errors = int(res.stdout.decode())
logger.debug('AE for {}: {}'.format(layer, errors))
if args.threshold and errors > args.threshold:
logger.error('Difference for `{}` is not acceptable ({} > {})'.format(name_layer, errors, args.threshold))
exit(DIFF_TOO_BIG)
cmd = ['convert', diff_name, '-font', 'helvetica', '-pointsize', font_size, '-draw',
'text 10,'+font_size+" '"+adapt_name(name_layer)+"'", diff_name]
'text 10,'+font_size+" '"+adapt_name(name_layer)+extra_name+"'", diff_name]
logger.debug('Executing: '+str(cmd))
call(cmd)
return not only_different or (only_different and errors != 0)
Expand Down Expand Up @@ -761,7 +790,7 @@ def get_layer(line):
exit(0)
layers_new, bbox_new = GenImages(new_file, new_file_hash, args.all_pages)

zero_size = (0,0,0,0)
zero_size = (0, 0, 0, 0)
changed = bbox_old != bbox_new and bbox_old != zero_size and bbox_new != zero_size
output_pdf = DiffImages(old_file_hash, new_file_hash, layers_old, layers_new, args.only_different, changed)

Expand Down
19 changes: 19 additions & 0 deletions tests/cases/6/a/6.kicad_sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(kicad_sch (version 20211123) (generator eeschema)
(uuid 44ba6fc2-2aff-4fba-b7f6-346e269eb0f1)
(paper "A4")
(lib_symbols
)
(text "Different page size" (at 87.63 82.55 0)
(effects (font (size 5 5)) (justify left bottom))
(uuid dd26a243-2105-4229-b371-2bc5006e18ac)
)
(sheet_instances
(path "/" (page "1"))
)
)
19 changes: 19 additions & 0 deletions tests/cases/6/b/6.kicad_sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(kicad_sch (version 20211123) (generator eeschema)
(uuid 44ba6fc2-2aff-4fba-b7f6-346e269eb0f1)
(paper "A3")
(lib_symbols
)
(text "Different page size" (at 87.63 82.55 0)
(effects (font (size 5 5)) (justify left bottom))
(uuid dd26a243-2105-4229-b371-2bc5006e18ac)
)
(sheet_instances
(path "/" (page "1"))
)
)
Binary file added tests/cases/6/ref_sch/diff-Schematic0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion tests/test_sch_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Tests for PCB diffs
For debug information use:
pytest-3 --log-cli-level debug
pytest-3 --log-cli-level debug --test_dir=pp -k TEST
"""

Expand Down Expand Up @@ -43,3 +43,10 @@ def test_sch_diff_sheets_1(test_dir):
ctx.run(['--all_pages'])
ctx.compare_out_pngs()
ctx.clean_up()


def test_sch_diff_size_1(test_dir):
ctx = context.TestContextSCH(test_dir, 6)
ctx.run()
ctx.compare_out_pngs()
ctx.clean_up()

0 comments on commit 00661fe

Please sign in to comment.