#!/usr/bin/perl # -*- perl -*- # Copyright 2017 DJ Delorie # Distributed under the GPL Version 2 or, at your choice, any later version $mapfile = shift; $infile = shift; $outfile = shift; open(MAP, $mapfile) || die(); $package = undef; sub pins_for { my ($refdes, $pin_in) = @_; $pin_out = $pinmap{$package_for{$refdes}}{$pin_in}; if (defined $pin_out) { return split(/,/, $pin_out); } else { return $pin_in; } } while () { chomp; next if /^#/; next unless /\S/; if (defined $package) { # pin NN -> NN # attribute = value if (/pin\s+(\S+)\s*=\s*(\S+)/) { $pinmap{$package}{$1} = $2; } elsif (/(\S+)\s*=\s*(.*)/) { $attr{$package}{$1} = $2; } } else { # refdes = package if (/(\S+)\s*=\s*(\S+)/) { $package_for{$1} = $2; } } if (/\[(\S+)\]/) { $package = $1; } } close (MAP); open(IN, $infile); open(OUT, ">$outfile"); $attrlines = ''; while () { # Netlist(Add,unnamed_net2,D1-2) if (/Netlist\(Add,(\S+),(\S+)-(\S+)\)/) { $net = $1; $refdes = $2; $pin_in = $3; @pin_out = &pins_for($refdes, $pin_in); $_ = ''; for $pin (@pin_out) { $_ .= "Netlist(Add,$net,$refdes-$pin)\n"; } } # ElementList(Need,"C5","0603dj","1uF") if (/ElementList\(Need,"(.*)","(.*)","(.*)"\)/) { $refdes = $1; $footprint = $2; $value = $3; $newfp = $attr{$package_for{$refdes}}{"footprint"}; if (defined $newfp) { $footprint = $newfp; } $_ = "ElementList(Need,\"$refdes\",\"$footprint\",\"$value\")\n"; # ElementSetAttr("IN_LEFT","manufacturer","unknown") for $a (sort keys %{$attr{$package_for{$refdes}}}) { next if $a eq "footprint"; $v = $attr{$package_for{$refdes}}{$a}; $attrlines .= "ElementSetAttr(\"$refdes\",\"$a\",\"$v\")\n"; } } # ElementSetAttr("IN_LEFT","manufacturer","unknown") if (/ElementSetAttr\("(.*)","(.*)","(.*)"\)/) { $refdes = $1; $a = $2; $v = $3; if (defined $attr{$package_for{$refdes}}{$a}) { $_ = ''; } } # ChangePinName("D1", 3, "+") if (/ChangePinName\("(.*)",\s*(\S+),\s*"(.*)"\)/) { $refdes = $1; $pin = $2; $name = $3; $newpin = $pinmap{$package_for{$refdes}}{$pin}; @newpins = &pins_for($refdes, $pin); $_ = ''; for $pin (@newpins) { $_ .= "ChangePinName(\"$refdes\", $pin, \"$name\")\n"; } } print OUT; print OUT $attrlines; $attrlines = ''; }