r5358 - in trunk/eda: . lib lib/kicad-perl lib/kicad-perl/Kicad

alvieboy at docs.openmoko.org alvieboy at docs.openmoko.org
Sat Aug 1 12:08:00 CEST 2009


Author: alvieboy
Date: 2009-08-01 12:08:00 +0200 (Sat, 01 Aug 2009)
New Revision: 5358

Added:
   trunk/eda/lib/
   trunk/eda/lib/kicad-perl/
   trunk/eda/lib/kicad-perl/Changes
   trunk/eda/lib/kicad-perl/Kicad.pm
   trunk/eda/lib/kicad-perl/Kicad/
   trunk/eda/lib/kicad-perl/Kicad/Library.pm
   trunk/eda/lib/kicad-perl/Kicad/Netlist.pm
   trunk/eda/lib/kicad-perl/Kicad/Project.pm
   trunk/eda/lib/kicad-perl/Kicad/Schematic.pm
   trunk/eda/lib/kicad-perl/Makefile.PL
   trunk/eda/lib/kicad-perl/README
Log:
Initial import of Kicad perl library

Added: trunk/eda/lib/kicad-perl/Changes
===================================================================
--- trunk/eda/lib/kicad-perl/Changes	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Changes	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,6 @@
+2009/08/01 Alvaro Lopes <alvieboy at alvie.com>
+
+        Version 0.1
+
+	Initial version. There is still a lot to do. Need to document at least
+        API.

Added: trunk/eda/lib/kicad-perl/Kicad/Library.pm
===================================================================
--- trunk/eda/lib/kicad-perl/Kicad/Library.pm	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Kicad/Library.pm	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,140 @@
+#
+#    Kicad Library Parser
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+package Kicad::Library;
+
+use strict;
+use Carp;
+use warnings;
+use Data::Dumper;
+
+sub new
+{
+    my $self = {};
+    bless($self,shift);
+    return $self;
+}
+
+sub parse
+{
+    my ($self,$file) = @_;
+    
+    my $fh;
+    
+    if (ref($file) ne 'GLOB') {
+        open($fh,'<',$file) or croak "Cannot open $file: $!";
+    } else {
+        $fh = $file;
+    }
+    $self->parse_header($fh) or return undef;
+    $self->parse_body($fh);
+}
+
+sub parse_block
+{
+    my ($self,$fh,$line) = @_;
+
+    my $st = substr($line,1,1);
+    
+    ( $st eq 'C' && $self->parse_part_descr($fh,$line) ) ||
+        ( $st eq 'S' && $self->parse_sheet_descr($fh,$line) ) ||
+        ( $st eq 'D' && $self->parse_schema_descr($fh,$line) ) || die;
+    
+}
+
+sub parse_draw
+{
+    my ($self,$fh,$comp) = @_;
+    
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        my @key=qw/name num posx posy length direction name_text_size num_text_size unit convert electrical_type pin_type/;
+        if ($line=~/^X\s+(.*)/) {
+            my @val = split(/\s+/,$1);
+            my $i=0;
+            map { $comp->{'pins'}->{$val[1]}->{$_} = $val[$i++] } (@key);
+        }
+        if ($line=~/^ENDDRAW/) {
+            last;
+        }
+    }    
+}
+
+sub parse_def
+{
+    my ($self,$fh,$line,$name,$sym, at v) = @_;
+    
+    my $comp ={};
+    
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        if ($line=~/^ENDDEF/) {
+            last;
+        }
+        if ($line=~/^(F\d)\s+(.*)$/) {
+            # Reference and name
+            $comp->{$1}=$2;
+            next;
+        }
+        if ($line=~/^ALIAS\s+(.*)/) {
+            $comp->{'alias'} = $1;
+            next;
+        }
+        if ($line=~/^DRAW/) {
+            $self->parse_draw($fh,$comp);
+        }
+        
+        
+    }
+    $self->{'components'}->{$name} = $comp;
+    
+    # Add alias
+    if (defined ($comp->{'alias'})) {
+        foreach (split(/\s+/,$comp->{'alias'})) {
+            $self->{'components'}->{$_} = $comp;
+        }
+    }
+}
+
+sub find_component
+{
+    my ($self,$name) = @_;
+    #    print Dumper($self);
+    #print join(' ', keys(%{$self->{'components'}}));
+    return $self->{'components'}->{$name}
+}
+
+sub parse_body
+{
+    my ($self,$fh) = @_;
+    
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        if ($line=~/^DEF\s+([\S]+)\s+([\S]+)\s+(\d+)\s+(.*)$/)
+        {
+            $self->parse_def($fh,$line,$1,$2);
+        } else {
+            croak "Invalid line $line";
+        }
+    }
+}
+
+sub parse_header
+{
+    my ($self,$fh) = @_;
+    while (<$fh>) {
+        next if /^#/;
+        return 1 if (/^EESchema-LIBRARY/);
+    }
+    return undef;
+}
+
+1;

Added: trunk/eda/lib/kicad-perl/Kicad/Netlist.pm
===================================================================
--- trunk/eda/lib/kicad-perl/Kicad/Netlist.pm	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Kicad/Netlist.pm	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,175 @@
+#
+#    Kicad Netlist Parser
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+package Kicad::Netlist;
+
+use strict;
+use Carp;
+use warnings;
+
+sub new
+{
+    my $self = {};
+    bless($self,shift);
+    $self->parse($_[0]) if defined ($_[0]);
+    return $self;
+}
+
+sub parse
+{
+    my ($self,$file) = @_;
+    
+    my $fh;
+    
+    if (ref($file) ne 'GLOB') {
+        open($fh,'<',$file) or croak "Cannot open $file: $!";
+    } else {
+        $fh = $file;
+    }
+    $self->parse_header($fh) or return undef;
+    $self->parse_body($fh);
+}
+
+sub get_components
+{
+    my ($self) =@_;
+    return $self->{'components'};
+}
+
+sub parse_component
+{
+    my ($self,$fh,$cdef) = @_;
+    my @v = split(/\s+/,$cdef);
+    my $comp = { 'pins' => {} };
+    my $i=0;
+    map { $comp->{$_} = $v[$i++] } (qw/id footprint name value library/);
+    $comp->{'library'} =~ s/\{Lib=([^}]+)\}/$1/e;
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        if ($line=~/^\s+\(\s+(\S+)\s+(\S+)\s+\)$/) {
+            # Pin entry
+            $comp->{'pins'}->{$1}=$2;
+        }
+        if ($line=~/^\s+\)/) {
+            last;
+        }
+    }
+    $self->{components}->{$comp->{name}}=$comp;
+}
+
+sub parse_footprints
+{
+    my ($self,$fh) = @_;
+    while (my $line=<$fh>) {
+        chomp $line;
+        # Nothing to do yet
+        if ($line=~/^\}/) {
+            last;
+        }
+    }
+}
+
+sub parse_nets
+{
+    my ($self,$fh) = @_;
+    
+    my %nets;
+    my $current_net;
+    
+    while (my $line=<$fh>) {
+        chomp $line;
+        #Net 2 "" ""
+        # R1519 2
+        # TP1511 1
+        # U1501 R25
+
+        if ($line=~/^Net\s+(\S*)\s+(\S+)\s+(\S+)$/) {
+            croak "NET $1 already exists!!!" if exists $nets{$1};
+            $current_net = $1;
+            $nets{$current_net}->{'name_full'} = $2;
+            $nets{$current_net}->{'name'} = $3;
+            next;
+        }
+        if ($line=~/^\}/) {
+            last;
+        }
+        croak "Net definition without net" unless defined $current_net;
+        $line=~s/^\s+//;
+        my ($component,$pin) = split(/\s/,$line);
+        
+        # Find component instance
+        
+        croak "Cannot find component instance $component" unless defined $self->find_component_instance($component);
+        $nets{$current_net}->{'nodes'} ||= [];
+        
+        push(@{$nets{$current_net}->{'nodes'}}, {'comp' => $component, 'pin' => $pin});
+    }
+    $self->{'nets'} = {%nets};
+}
+
+sub get_nets
+{
+    my ($self) = @_;
+    return $self->{'nets'};
+}
+
+sub find_component_instance
+{
+    my ($self,$name) = @_;
+    return $self->{'components'}->{$name};
+}
+
+sub parse_netlist
+{
+    my ($self,$fh) = @_;
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        if ($line=~/\s+\(\s+(.*)$/) {
+            $self->parse_component($fh,$1);
+            next;
+        } 
+        if ($line=~/^\*/) {
+            $self->parse_footprints($fh);
+            next;
+        }
+        if ($line=~/^\{/) {
+            $self->parse_nets($fh);
+        }
+    }
+}
+
+sub parse_body
+{
+    my ($self,$fh) = @_;
+    
+    while (my $line=<$fh>) {
+        chomp $line;
+        next if $line=~/^#/;
+        if ($line=~/^\(/)
+        {
+            $self->parse_netlist($fh);
+        } else {
+            croak "Invalid line $line";
+        }
+    }
+}
+
+sub parse_header
+{
+    my ($self,$fh) = @_;
+    while (<$fh>) {
+        return 1 if (/^# EESchema Netlist/);
+    }
+    croak "Invalid netlist";
+    return undef;
+}
+
+1;

Added: trunk/eda/lib/kicad-perl/Kicad/Project.pm
===================================================================
--- trunk/eda/lib/kicad-perl/Kicad/Project.pm	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Kicad/Project.pm	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,92 @@
+#
+#    Kicad Project
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+package Kicad::Project;
+
+use Kicad::Library;
+
+use strict;
+use Carp;
+use warnings;
+
+my @paths = qw,/usr/share/kicad/library /usr/local/share/kicad/library,;
+
+sub new
+{
+    my $self = { 'libs' => [] };
+    bless($self,shift);
+    $self->parse($_[0]) if defined ($_[0]);
+    return $self;
+}
+
+sub add_library
+{
+    my ($self,$lib) = @_;
+    my $libinstance = new Kicad::Library;
+    $libinstance->parse($lib);
+    push(@{$self->{'libs'}},$libinstance);
+}
+
+sub find_component_in_libraries
+{
+    my ($self,$cname) = @_;
+    for my $lib (@{$self->{'libs'}}) {
+        my $c = $lib->find_component($cname);
+        return $c if defined $c;
+    }
+    return undef;
+}
+
+sub parse
+{
+    my ($self,$file) = @_;
+    my $fh;
+
+    if (ref($file) ne 'GLOB') {
+        open($fh,'<',$file) or croak "Cannot open $file: $!";
+    } else {
+        $fh = $file;
+    }
+
+    # scan for [eeschema/libraries]
+    while (my $line=<$fh>) {
+        chomp $line;
+        if ($line=~/^\[eeschema\/libraries\]/) {
+            $self->parse_libraries($fh);
+        }
+    }
+}
+
+sub load_lib
+{
+    my ($self,$libname) = @_;
+    $libname =~ s/\.lib$//;
+    for my $path(@paths,'.','..') {
+        my $name = "${path}/${libname}.lib";
+        if (-f $name) {
+            $self->add_library($name);
+            return;
+        }
+    }
+    croak "Cannot load library $libname";
+}
+
+sub parse_libraries
+{
+    my ($self,$fh) = @_;
+    while (my $line=<$fh>) {
+        chomp $line;
+        if ($line=~/^LibName\d+=(.*)$/) {
+            $self->load_lib($1);
+            next;
+        } 
+        last;
+    }
+}
+
+1;

Added: trunk/eda/lib/kicad-perl/Kicad/Schematic.pm
===================================================================
--- trunk/eda/lib/kicad-perl/Kicad/Schematic.pm	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Kicad/Schematic.pm	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,96 @@
+#
+#    Kicad Schematic Parser
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+package Kicad::Schematic;
+
+use strict;
+
+sub new
+{
+    my $self = {};
+    bless($self,shift);
+    return $self;
+}
+
+sub parse
+{
+    my ($self,$file) = @_;
+    
+    my $fh;
+    
+    if (ref($file) ne 'GLOB') {
+        open($fh,'<',$file) or croak "Cannot open $file: $!";
+    } else {
+        $fh = $file;
+    }
+    $self->parse_header($fh) or return undef;
+    $self->parse_body($fh);
+}
+
+sub parse_block
+{
+    my ($self,$fh,$line) = @_;
+
+    my $st = substr($line,1,1);
+    
+    ( $st eq 'C' && $self->parse_part_descr($fh,$line) ) ||
+        ( $st eq 'S' && $self->parse_sheet_descr($fh,$line) ) ||
+        ( $st eq 'D' && $self->parse_schema_descr($fh,$line) ) || die;
+    
+}
+
+sub parse_connection_descr
+{
+    my ($self,$fh,$line) = @_;
+    
+    my @v = split(/\s+/,$line);
+    print STDERR "Connection $v[1] at $v[2],$v[3]\n";
+}
+
+sub parse_schema_descr
+{
+    my ($self,$fh,$line) = @_;
+    while(<$fh>) {
+        chomp;
+        return 1 if /\$EndDescr/;
+    }
+    die "Invalid descr";
+    return undef;
+}
+sub parse_body
+{
+    my ($self,$fh) = @_;
+    
+    while (my $line =<$fh>) {
+        chomp $line;
+        my $st = substr($line,0,1);
+        print STDERR "'$st' ($line)\n";
+        ( $st eq '$' && $self->parse_block($fh,$line) ) ||
+            ( $st eq 'T' && $self->parse_text_descr($fh,$line) ) ||
+            ( $st eq 'L' && $self->parse_part_descr($fh,$line) ) ||
+            ( $st eq 'W' && $self->parse_segment_descr($fh,$line) ) ||
+            ( $st eq 'E' && $self->parse_record_descr($fh,$line) ) ||
+            ( $st eq 'P' && $self->parse_polyline_descr($fh,$line) ) ||
+            ( $st eq 'C' && $self->parse_connection_descr($fh,$line) ) ||
+            ( $st eq 'C' && $self->parse_noconnection_descr($fh,$line) || die "Invalid start line $st\n");
+
+    }
+}
+
+sub parse_header
+{
+    my ($self,$fh) = @_;
+    while (<$fh>) {
+        print;
+
+        return 1 if (/^EELAYER\sEND/);
+    }
+    return undef;
+}
+
+1;

Added: trunk/eda/lib/kicad-perl/Kicad.pm
===================================================================
--- trunk/eda/lib/kicad-perl/Kicad.pm	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Kicad.pm	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,13 @@
+#
+#    Kicad Perl library
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+package Kicad;
+
+our $VERSION = '0.1';
+
+1;

Added: trunk/eda/lib/kicad-perl/Makefile.PL
===================================================================
--- trunk/eda/lib/kicad-perl/Makefile.PL	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/Makefile.PL	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,14 @@
+#
+#    Kicad library Makefile
+#    Copyright (C) 2009 Alvaro Lopes <alvieboy at alvie.com>
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the same terms as Perl itself.
+#
+
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+              NAME         => 'Kicad',
+              VERSION_FROM => 'Kicad.pm'
+);

Added: trunk/eda/lib/kicad-perl/README
===================================================================
--- trunk/eda/lib/kicad-perl/README	                        (rev 0)
+++ trunk/eda/lib/kicad-perl/README	2009-08-01 10:08:00 UTC (rev 5358)
@@ -0,0 +1,21 @@
+ABOUT THIS LIBRARY
+
+  This is a perl parser for several Kicad file formats.
+
+  For information about Kicad, please visit http://kicad.sourceforge.net
+
+BUILDING AND INSTALLING
+
+  Just type:
+  
+    perl Makefile.PL
+    make
+    make install
+
+COPYRIGHT
+
+  Copyright 2009 Alvaro Lopes <alvieboy at alvie.com>
+  
+  This library is free software; you can redistribute it and/or
+  modify it under the same terms as Perl itself.
+




More information about the commitlog mailing list