<?php

// via
// ================================================================================================
function viaGetStyle($pcb, $via)
{
	for ($i=0; $i < sizeof($pcb->VIA_STYLE); $i++)
	{
		$style = $pcb->VIA_STYLE[$i];
		if ( ($style['shape'] == $via['shape']) and ($style['dia'] == $via['dia']) )
			return $style['name'];
	}

	return false;
}
function viaAddStyle($pcb, $via)
{
	for ($i=0; $i < sizeof($pcb->VIA_STYLE); $i++)
	{
		$style = $pcb->VIA_STYLE[$i];
		if ( ($style['shape'] == $via['shape']) and ($style['dia'] == $via['dia']) )
			return false;
	}

	$pcb->VIA_STYLE[$i]['shape'] = $via['shape'];
	$pcb->VIA_STYLE[$i]['dia'] = $via['dia'];
	$pcb->VIA_STYLE[$i]['name'] = "via".$i;

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

echo"generate via styles...<br>";
for($i=0; $i<=$pcb->HDR['net_points_count']['v']; $i++)
{
	$flags = hex($pcb->NETPOI[$i]['flags']['v']);
	$flags_hdr = substr($flags, 1, 1);

	if ( ($flags_hdr == "6") or ($flags_hdr == "7") ) {} else continue;

	$via['shape'] = $pcb->NETPOI[$i]['size']['v'];
	$via['dia'] = $via['shape'] / 2;

	if ($via_style = viaAddStyle($pcb, $via))
	{
		echo"net point [$i], added new via {shape=".$via['shape'].", dia=".$via['dia']."}, style=[$via_style]<br>";
	}

	$pcb->NETPOI[$i]['style']['v'] = viaGetStyle($pcb, $via);
}
echo"save via styles: ".sizeof($pcb->VIA_STYLE)."<br>";
	$pcad_lib =  "  (viaStyleDef \"(Default)\"\r\n";
	$pcad_lib .= "    (holeDiam 18)\r\n";
	$pcad_lib .= "    (StartRange 1)\r\n";
	$pcad_lib .= "    (EndRange 2)\r\n";
	$s = "Ellipse";
	$w = "40";
	$h = "40";
	$pcad_lib .= "    (viaShape (layerNumRef 1) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerNumRef 2) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerType Signal) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerType Plane) (viaShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "    (viaShape (layerType NonSignal) (viaShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "  )\r\n";
file_put_contents($out_file, $pcad_lib, FILE_APPEND | LOCK_EX);

$pcad_lib =  "";
for ($i=0; $i<sizeof($pcb->VIA_STYLE); $i++)
{
	$pcad_lib .= "  (viaStyleDef \"".$pcb->VIA_STYLE[$i]['name']."\"\r\n";
	$pcad_lib .= "    (holeDiam ".$pcb->VIA_STYLE[$i]['dia'].")\r\n";
	$pcad_lib .= "    (StartRange 1)\r\n";
	$pcad_lib .= "    (EndRange 2)\r\n";

	$s = "Ellipse";
	$w = $pcb->VIA_STYLE[$i]['shape'];
	$h = $pcb->VIA_STYLE[$i]['shape'];

	$pcad_lib .= "    (viaShape (layerNumRef 1) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerNumRef 2) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerType Signal) (viaShapeType $s) (shapeWidth $w) (shapeHeight $h) )\r\n";
	$pcad_lib .= "    (viaShape (layerType Plane) (viaShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "    (viaShape (layerType NonSignal) (viaShapeType NoConnect) (shapeWidth 0) (shapeHeight 0) )\r\n";
	$pcad_lib .= "  )\r\n";

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