<?php

$pcad_net  = "";

// install components
for($i=1; $i<=$pcb->HDR['comp_count_real']['v']; $i++)
{
	$C = $pcb->COMP[$i];

	$name1 = ValidateRef($C['name1_str']['v']);
	$name2 = ValidateRef($C['name2_str']['v']);
	$pattern = $C['style']['v'];
	$pcad_net .= "  (compInst \"$name1\"\r\n";
	$pcad_net .= "    (compRef \"".$pattern."_1\")\r\n";
	$pcad_net .= "    (originalName \"$pattern\")\r\n";
	$pcad_net .= "    (compValue \"$name2\")\r\n";
	$pcad_net .= "  )\r\n";
}
file_put_contents($out_file, $pcad_net, FILE_APPEND | LOCK_EX);



echo"generate net list...<br>";
$net_lists = array();
for($i=1; $i<=$pcb->HDR['comp_count_real']['v']; $i++)
{
	$C = $pcb->COMP[$i];
	for ($j=1; $j<=$C['pads_count']['v']; $j++)
	{
		$net = $C['pad'.$j.'_net']['v'];
if (!trim($net)) continue;

		if (!in_array($net, $net_list))
		{
			$net_list[] = $net; 
		}
	}
}
echo"nets from components: ".sizeof($net_list)."<br>";
//for($n=0; $n<sizeof($net_list); $n++) echo"(".$net_list[$n].")"; echo"<br>";

$pcad_net  = "";
for($n=0; $n<sizeof($net_list); $n++)
{
	$net = $net_list[$n];
	$pcad_net .= "  (net \"".$net."\"\r\n";

	for($i=1; $i<=$pcb->HDR['comp_count_real']['v']; $i++)
	{
		$C = $pcb->COMP[$i];
		for ($j=1; $j<=$C['pads_count']['v']; $j++)
		{
			$pad_net = $C['pad'.$j.'_net']['v'];
			$pad_des = $C['pad'.$j.'_des']['v'];
			if ($net == $pad_net)
			{
				$pcad_net .= "    (node \"".ValidateRef($C['name1_str']['v'])."\" \"$pad_des\")\r\n";
			}
		}
	}
//*/
	$pcad_net .= "  )\r\n";

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


echo"generate netclass...<br>";
$pcad_net  = "  (netClass \"DefaultNetClass\"\r\n";
for($n=0; $n<sizeof($net_list); $n++)
{
	$pcad_net .= "    (netNameRef \"".$net_list[$n]."\")\r\n";
}
$pcad_net .= "    (attr \"Width\" \"10\" (textStyleRef \"(Default)\"))\r\n";
$pcad_net .= "    (attr \"Clearance\" \"10\" (textStyleRef \"(Default)\"))\r\n";
$pcad_net .= "    (attr \"MaxWidth\" \"1000\" (textStyleRef \"(Default)\"))\r\n";
$pcad_net .= "    (attr \"MinWidth\" \"1\" (textStyleRef \"(Default)\"))\r\n";
$pcad_net .= "    (attr \"USE_LAYERS\" \"TOP, BOTTOM\" (textStyleRef \"(Default)\"))\r\n";
$pcad_net .= "  )\r\n";
//file_put_contents($out_file, $pcad_net, FILE_APPEND | LOCK_EX);

