239 lines
5.8 KiB
PHP
239 lines
5.8 KiB
PHP
|
<?php
|
||
|
/*
|
||
|
component-map.php
|
||
|
(c) andreika, 2017.
|
||
|
*/
|
||
|
|
||
|
if ($argc < 4)
|
||
|
{
|
||
|
echo "Use: component.php <componentsFile> <footprintsFile> <bomlistFile>\n";
|
||
|
die;
|
||
|
}
|
||
|
$componentsFile = $argv[1];
|
||
|
$footprintsFile = $argv[2];
|
||
|
$bomlistFile = $argv[3];
|
||
|
|
||
|
include("component-settings.php");
|
||
|
include("component-digikey.php");
|
||
|
include("component-common.php");
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
$components = read_csv($componentsFile);
|
||
|
$footprints = read_csv($footprintsFile);
|
||
|
$bomlist = read_csv($bomlistFile);
|
||
|
|
||
|
// fill associative LUT-arrays for footprints
|
||
|
$fps = array();
|
||
|
foreach ($footprints as $f)
|
||
|
{
|
||
|
if (!isset($fps[$f["d"]]))
|
||
|
$fps[$f["d"]] = array();
|
||
|
$fps[$f["d"]][] = $f;
|
||
|
}
|
||
|
|
||
|
// fill associative LUT-arrays for BOM
|
||
|
$bom = array();
|
||
|
foreach ($bomlist as $b)
|
||
|
{
|
||
|
$bds = preg_split("/[, ]+/", $b["d"]);
|
||
|
foreach ($bds as $bd)
|
||
|
{
|
||
|
if (!isset($bom[$bd]))
|
||
|
{
|
||
|
$bom[$bd] = $b;
|
||
|
$bom[$bd]["d"] = $bd;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// sort in "natural" style: C1,C2,..C10,C11...
|
||
|
function componentsSort($c1, $c2)
|
||
|
{
|
||
|
if (!array_key_exists("d", $c1) || !array_key_exists("d", $c2))
|
||
|
return -1;
|
||
|
return strnatcmp($c1["d"], $c2["d"]);
|
||
|
}
|
||
|
|
||
|
usort($components, "componentsSort");
|
||
|
|
||
|
//print_r($components);
|
||
|
//print_r($footprints);
|
||
|
//print_r($bom);
|
||
|
|
||
|
/////////////////////////////////////////
|
||
|
// start output
|
||
|
|
||
|
echo "// The following data is generated by 'src/component*.php' script (c) andreika, 2017\n";
|
||
|
echo "// componentsFile: \"$componentsFile\"\n";
|
||
|
echo "// footprintsFile: \" $footprintsFile\"\n";
|
||
|
echo "// bomlistFile: \" $bomlistFile\"\n\n";
|
||
|
echo "var components = [\n";
|
||
|
|
||
|
foreach ($components as $c)
|
||
|
{
|
||
|
if (!array_key_exists("d", $c))
|
||
|
continue;
|
||
|
if (!array_key_exists($c["f"], $fps))
|
||
|
continue;
|
||
|
|
||
|
// get linked footprint record
|
||
|
$ff = $fps[$c["f"]];
|
||
|
// get linked BOM record
|
||
|
$b = isset($bom[$c["d"]]) ? $bom[$c["d"]] : array();
|
||
|
|
||
|
// output all component footprints (multi-poly areas supported)
|
||
|
foreach ($ff as $f)
|
||
|
{
|
||
|
$coords = array();
|
||
|
$xy = array($c["x"], $c["y"]);
|
||
|
$shape = "poly";
|
||
|
// rectangle footprint shape
|
||
|
if ($f["t"] == "rect")
|
||
|
{
|
||
|
// add footprint position offset (optional)
|
||
|
$ox = 0; $oy = 0;
|
||
|
if (isset($f["ox"]))
|
||
|
$ox = $f["ox"];
|
||
|
if (isset($f["oy"]))
|
||
|
$oy = $f["oy"];
|
||
|
$w2 = $f["w"] / 2.0;
|
||
|
$h2 = $f["h"] / 2.0;
|
||
|
|
||
|
// footprint rotations
|
||
|
if ($c["r"] == 90)
|
||
|
{
|
||
|
$tmp = $h2;
|
||
|
$h2 = $w2;
|
||
|
$w2 = $tmp;
|
||
|
$tmp = $ox;
|
||
|
$ox = -$oy;
|
||
|
$oy = $tmp;
|
||
|
}
|
||
|
else if ($c["r"] == 180)
|
||
|
{
|
||
|
$ox = -$ox;
|
||
|
$oy = -$oy;
|
||
|
}
|
||
|
else if ($c["r"] == 270)
|
||
|
{
|
||
|
$tmp = $h2;
|
||
|
$h2 = $w2;
|
||
|
$w2 = $tmp;
|
||
|
$tmp = $ox;
|
||
|
$ox = $oy;
|
||
|
$oy = $tmp;
|
||
|
}
|
||
|
|
||
|
// flip for bottom layer
|
||
|
if ($c["l"] == 1)
|
||
|
{
|
||
|
$ox = -$ox;
|
||
|
$oy = -$oy;
|
||
|
}
|
||
|
$cc = array($xy[0] - $w2 + $ox, $xy[1] - $h2 + $oy, $xy[0] + $w2 + $ox, $xy[1] + $h2 + $oy);
|
||
|
// 4 corner verts
|
||
|
$coords[] = array($cc[0], $cc[1]);
|
||
|
$coords[] = array($cc[2], $cc[1]);
|
||
|
$coords[] = array($cc[2], $cc[3]);
|
||
|
$coords[] = array($cc[0], $cc[3]);
|
||
|
}
|
||
|
// circle shape (electrolytic capacitors etc.)
|
||
|
else if ($f["t"] == "circle")
|
||
|
{
|
||
|
// center & radius
|
||
|
$coords[] = $xy;
|
||
|
$coords[] = array($f["w"] / 2.0);
|
||
|
$shape = "circle";
|
||
|
}
|
||
|
|
||
|
// if multilayer footprint is set
|
||
|
$ml = isset($f["m"]) && $f["m"] != 0 ? $f["m"] : $c["l"] + 1;
|
||
|
$part = "";
|
||
|
$pdata = array();
|
||
|
// supplier
|
||
|
$supp = isset($b["s"]) ? $b["s"] : "";
|
||
|
// part number
|
||
|
$part = isset($b["p"]) ? $b["p"] : "";
|
||
|
|
||
|
// get supplier-specific data
|
||
|
if ($part != "")
|
||
|
{
|
||
|
if (preg_match("/.*?Digi-?key.*?/i", $supp))
|
||
|
{
|
||
|
if ($downloadDigikeyData)
|
||
|
$pdata = getDigikeyData($part);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// for each of 2 sides
|
||
|
for ($j = 1; $j <= 2; $j++)
|
||
|
{
|
||
|
// skip layers using footprint layer mask
|
||
|
if (($ml & $j) != $j)
|
||
|
continue;
|
||
|
// main fields
|
||
|
$value = isset($b["c"]) ? $b["c"] : $c["c"];
|
||
|
echo " { \"name\": \"".$c["d"]."\", \"value\": \"".$value."\", \"fp\": \"".$c["f"]."\", \"shape\": \"".$shape."\", \"coords\": \"";
|
||
|
// coords list
|
||
|
for ($i = 0; $i < count($coords); $i++)
|
||
|
{
|
||
|
if ($i > 0)
|
||
|
echo ",";
|
||
|
$xy = convertCoords($coords[$i], $j - 1);
|
||
|
echo $xy[0].(isset($xy[1]) ? ",".$xy[1] : "");
|
||
|
}
|
||
|
// description
|
||
|
echo "\",\"descr\": \"".(isset($b["i"]) ? $b["i"] : "")."\",";
|
||
|
// our remarks
|
||
|
echo "\"remark\": \"".(isset($b["k"]) ? $b["k"] : "")."\",";
|
||
|
|
||
|
// fill additional fields
|
||
|
$more = array();
|
||
|
// set footprint defaults first
|
||
|
if (isset($f["o"]) && preg_match_all("/([A-Za-z0-9]+):[ ]*([^,\"]+)/", $f["o"], $ret, PREG_SET_ORDER))
|
||
|
{
|
||
|
for ($i = 0; $i < count($ret); $i++)
|
||
|
{
|
||
|
$more[$ret[$i][1]] = $ret[$i][2];
|
||
|
}
|
||
|
}
|
||
|
// set bom defaults also
|
||
|
if (isset($b["ds"]))
|
||
|
{
|
||
|
$more["ds"] = $b["ds"];
|
||
|
}
|
||
|
|
||
|
// now, fill from downloaded part data
|
||
|
if ($part != "")
|
||
|
{
|
||
|
//print_r($data); die;
|
||
|
echo "\"part\": \"".($part)."\", \"supp\": \"".($supp)."\",";
|
||
|
if (count ($pdata) > 0)
|
||
|
{
|
||
|
foreach ($pdata as $pk=>$p)
|
||
|
{
|
||
|
$more[$pk] = $p;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// output gathered additional data...
|
||
|
if (count($more) > 0)
|
||
|
{
|
||
|
echo " \"more\": { ";
|
||
|
foreach ($more as $pk=>$p)
|
||
|
{
|
||
|
echo "\"$pk\": \"".($p)."\", ";
|
||
|
}
|
||
|
echo "}";
|
||
|
}
|
||
|
|
||
|
echo " },\n";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
echo "];\n";
|
||
|
|
||
|
?>
|