Root/genkicat/expand-pintype

1#!/usr/bin/perl
2#
3# expand-pintype - Expand pin types of symbols in a Kicad library
4#
5# Copyright 2010, 2012 by Werner Almesberger
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12
13#
14# usage:
15#
16# expand-pintype input.lib
17# expand-pintype input.lib output.lib
18#
19
20
21$GAP = 50; # gap between pin and type, in mil
22$CHAR_WIDTH = 60; # default character width, in mil
23$NAME_WIDTH = 15; # name field width in ASCII mode, in characters
24
25
26%map = (
27    "I" => "Input",
28    "O" => "Output",
29    "B" => "BiDi",
30    "T" => "3-state",
31    "P" => "Passive",
32    "U" => "Unspec",
33    "W" => "Pwr In",
34    "w" => "Pwr Out",
35    "C" => "OC",
36    "E" => "OE",
37    "N" => "NC",
38);
39
40if (@ARGV < 2) {
41    $out = 0;
42} elsif (@ARGV == 2) {
43    $file = pop @ARGV;
44    $out = 1;
45    open(FILE, ">$file") || die "$file: $!";
46} else {
47    print STDERR "usage: expand-pintype input.lib [output.lib]\n";
48    exit(1);
49}
50
51while (<>) {
52    if ($out) {
53    # make name differ so that KiCad's cache doesn't get confused
54        s/^DEF\s+~?/$&X/;
55        s/^F1\s+"+/$&X/;
56        print FILE || die;
57    }
58    next unless /^X/;
59    @a = split(/\s+/);
60    ($name, $pin, $x, $y, $dir, $unit, $pt) = @a[1, 2, 3, 4, 6, 9, 11];
61    $type = $map{$pt};
62    $type = "???" unless defined $type;
63    if ($out) {
64        $off = $GAP+(length $type)*$CHAR_WIDTH/2;
65        if ($dir eq "U") {
66            ($a, $y) = (90, $y-$off);
67        } elsif ($dir eq "D") {
68            ($a, $y) = (90, $y+$off);
69        } elsif ($dir eq "R") {
70                ($a, $x) = (0, $x-$off);
71        } else {
72            ($a, $x) = (0, $x+$off);
73        }
74        $type =~ y/ /~/;
75        print FILE sprintf("T %d %d %d 60 0 %d 0 %s Normal 0\n",
76            $a*10, $x, $y, $unit, $type);
77    } else {
78        $s = "$name ($pin)";
79        $f = $NAME_WIDTH-length $s;
80        $f = "-" x ($f > 0 ? $f : 0);
81        print "$s $f $type\n";
82    }
83}
84if ($out) {
85    close FILE || die;
86}
87

Archive Download this file

Branches:
master



interactive