From 7717e7fc3c6640548a1715cea30429a1164aef21 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 23 May 2012 23:24:38 -0300 Subject: [PATCH] b2/extract-symbols: extract cref-symbol map from KiCad schematics --- b2/extract-symbols | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 b2/extract-symbols diff --git a/b2/extract-symbols b/b2/extract-symbols new file mode 100755 index 0000000..11ebbe5 --- /dev/null +++ b/b2/extract-symbols @@ -0,0 +1,82 @@ +#!/usr/bin/perl +# +# extract-symbols - Extract symbol names from KiCad schematics +# +# Written 2012 by Werner Almesberger +# Copyright 2012 Werner Almesberger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + + +sub usage +{ + print STDERR "usage: $0 file.sch\n"; + exit(1); +} + + +&usage if $#ARGV != 0; + +push @f, $ARGV[0]; +($dir = $ARGV[0]) =~ s|/[^/]*$||; +$dir = "." if $dir eq ""; + +while (@f) { + $file = pop @f; + $sheet = 0; + $comp = 0; + open(FILE, $file) || die "$file: $!"; + while () { + if (/^\$Sheet/) { + $sheet = 1; + next; + } + if (/^\$EndSheet/) { + $sheet = 0; + next; + } + if (/^\$Comp/) { + $comp = 1; + undef $cref; + next; + } + if (/^\$EndComp/) { + $comp = 0; + next; + } + if (/^F1 "([^"]*)"/ && $sheet) { + if (-r "$1") { + push @f, $1; + } elsif (-r "$dir/$1") { + push @f, "$dir/$1"; + } else { + die "don't know where to find $1"; + } + next; + } + if (/^L\s+(\S+)\s+([^# \t\n]\S*)\s*$/ && $comp) { + die if defined $cref; + $cref = $2; + $sym = $1; + next; + } + if (/^U\s+1\s+/ && $comp) { + next unless defined $cref; + die "duplicate component reference \"$cref\" ($sym)" if + defined $sym{$cref}; + $sym{$cref} = $sym; + undef $cref; + next; + } + } + close FILE; +} + +for (sort keys %sym) { + print "$_ $sym{$_}\n" || die "print: $!"; +} +close STDOUT || die "close: $!";