Root/b2/extract-symbols

1#!/usr/bin/perl
2#
3# extract-symbols - Extract symbol names from KiCad schematics
4#
5# Written 2012 by Werner Almesberger
6# Copyright 2012 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14
15sub usage
16{
17    print STDERR "usage: $0 file.sch\n";
18    exit(1);
19}
20
21
22&usage if $#ARGV != 0;
23
24push @f, $ARGV[0];
25($dir = $ARGV[0]) =~ s|[^/]*$||;
26$dir = "." if $dir eq "";
27
28while (@f) {
29    $file = pop @f;
30    $sheet = 0;
31    $comp = 0;
32    open(FILE, $file) || die "$file: $!";
33    while (<FILE>) {
34        if (/^\$Sheet/) {
35            $sheet = 1;
36            next;
37        }
38        if (/^\$EndSheet/) {
39            $sheet = 0;
40            next;
41        }
42        if (/^\$Comp/) {
43            $comp = 1;
44            undef $cref;
45            next;
46        }
47        if (/^\$EndComp/) {
48            $comp = 0;
49            next;
50        }
51        if (/^F1 "([^"]*)"/ && $sheet) {
52            if (-r "$1") {
53                push @f, $1;
54            } elsif (-r "$dir/$1") {
55                push @f, "$dir/$1";
56            } else {
57                die "don't know where to find $1";
58            }
59            next;
60        }
61        if (/^L\s+(\S+)\s+([^# \t\n]\S*)\s*$/ && $comp) {
62            die if defined $cref;
63            $cref = $2;
64            $sym = $1;
65            next;
66        }
67        if (/^U\s+1\s+/ && $comp) {
68            next unless defined $cref;
69            die "duplicate component reference \"$cref\" ($sym)" if
70                defined $sym{$cref};
71            $sym{$cref} = $sym;
72            undef $cref;
73            next;
74        }
75    }
76    close FILE;
77}
78
79for (sort keys %sym) {
80    print "$_ $sym{$_}\n" || die "print: $!";
81}
82close STDOUT || die "close: $!";
83

Archive Download this file

Branches:
master



interactive