<?php

// pad
// ================================================================================================
function padGetStyle($pcb, $pad)
{
	for ($i=0; $i < sizeof($pcb->PAD_STYLE); $i++)
	{
		$style = $pcb->PAD_STYLE[$i];
		if ( 
			($style['shape'] == $pad['shape']) 
			and ($style['width'] == $pad['width'])
			and ($style['height'] == $pad['height'])
			and ($style['hole_dia'] == $pad['hole_dia'])
			and ($style['flags'] == $pad['flags'])
		)
			return $style['name'];
	}

	return false;
}
function padAddStyle($pcb, $pad)
{
	$st = padGetStyle($pcb, $pad);
	if ($st)
		return false;

	$i = sizeof($pcb->PAD_STYLE);
	$pcb->PAD_STYLE[$i]['shape'] = $pad['shape'];
	$pcb->PAD_STYLE[$i]['width'] = $pad['width'];
	$pcb->PAD_STYLE[$i]['height'] = $pad['height'];
	$pcb->PAD_STYLE[$i]['hole_dia'] = $pad['hole_dia'];
	$pcb->PAD_STYLE[$i]['flags'] = $pad['flags'];
	$pcb->PAD_STYLE[$i]['name'] = "pad".$i;

	return $pcb->PAD_STYLE[$i]['name'];
}

echo"<br>";
echo"generate pad styles...<br>";
for($i=0; $i<=$pcb->HDR['comp_count_real']['v']; $i++)
{
	$C = $pcb->COMP[$i];

	for ($j=1; $j<=$C['pads_count']['v']; $j++)
	{
		$pad['shape'] = $C['pad'.$j.'_shape']['v'];
		$pad['width'] = $C['pad'.$j.'_width']['v'];
		$pad['height'] = $C['pad'.$j.'_height']['v'];
		$pad['hole_dia'] = $C['pad'.$j.'_hole_dia']['v'];
		$pad['flags'] = $C['pad'.$j.'_flags']['v'];

		if ($pad_style = padAddStyle($pcb, $pad))
		{
			echo"comp [$i], pad [$j], added new pad {";
			echo"shape=".$pad['shape']."";
			echo", hole_dia=".$pad['hole_dia']."";
			echo", size=".$pad['width']."x".$pad['height']."";
			echo", flags=".$pad['flags']."";
			echo"}, style=[$pad_style]<br>";
		}

		$pcb->COMP[$i]['pad'.$j.'_style']['v'] = padGetStyle($pcb, $pad);
	}
}
echo"save pad styles: ".sizeof($pcb->PAD_STYLE)."<br>";
$pcad_lib =  "";
for ($i=0; $i<sizeof($pcb->PAD_STYLE); $i++)
{
	$pcad_lib .= "  (padStyleDef \"".$pcb->PAD_STYLE[$i]['name']."\"\r\n";
	$pcad_lib .= "    (holeDiam ".$pcb->PAD_STYLE[$i]['hole_dia'].")\r\n";
	$pcad_lib .= "    (StartRange 1)\r\n";
	$pcad_lib .= "    (EndRange 2)\r\n";

	$s1 = "Ellipse"; if ($pcb->PAD_STYLE[$i]['shape'] == 2) $s1 = "Rect";
	$w1 = $pcb->PAD_STYLE[$i]['width'];
	$h1 = $pcb->PAD_STYLE[$i]['height'];

	$w2 = $w1;
	$h2 = $w1;
	$s2 = $s1;
	if (!$pcb->PAD_STYLE[$i]['hole_dia'])
	{
		$w2 = "0";
		$h2 = "0";
		$s2 = "NoConnect";
	}
	$pcad_lib .= "    (padShape (layerNumRef 1) (padShapeType $s1) (shapeWidth $w1) (shapeHeight $h1) )\r\n";
	$pcad_lib .= "    (padShape (layerNumRef 2) (padShapeType $s2) (shapeWidth $w2) (shapeHeight $h2) )\r\n";
	$pcad_lib .= "    (padShape (layerType Signal) (padShapeType $s2) (shapeWidth $w2) (shapeHeight $h2) )\r\n";
	$pcad_lib .= "    (padShape (layerType Plane) (padShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "    (padShape (layerType NonSignal) (padShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "  )\r\n";

}
file_put_contents($out_file, $pcad_lib, FILE_APPEND | LOCK_EX);
