<?php

// multilayer begin
$pcad_pcb  = "  (multiLayer\r\n";

// - via
for($i=0; $i<=$pcb->HDR['net_points_count']['v']; $i++)
{
	$net1 = $pcb->NETPOI[$i]['net_id']['v'];
	$net = getNetByPadOfs($pcb, $net1);
	$netref = "";
	if ($net) $netref = "(netNameRef \"$net\") ";

	$flags = hex($pcb->NETPOI[$i]['flags']['v']);
	$flags_hdr = substr($flags, 1, 1);

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

	$st = $pcb->NETPOI[$i]['style']['v'];
	$x = $pcb->NETPOI[$i]['pos_x']['v'];
	$y = $pcb->NETPOI[$i]['pos_y']['v'];

	$pcad_pcb .= "    (via (viaStyleRef \"$st\") (pt $y $x) $netref)\r\n";
}
//file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);

// - 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'] . "_1";
	$x = $C['pos_x']['v'];
	$y = $C['pos_y']['v'];

	$delta = $C['rotate']['v'] - $C['style_rotate']['v'];
	$r = (2 - $delta);

	$comment = "";
//	$comment = ";; styleR=[".$C['style_rotate']['v']."], compR=".$C['rotate']['v'].", calcR=$r";

	$pcad_pcb .= "    (pattern (patternRef \"$pattern\") (refDesRef \"$name1\") (pt $y $x) (rotation ".($r*90).") (patternGraphicsNameRef \"Primary\")\r\n";
	$pcad_pcb .= "      (patternGraphicsRef $comment\r\n";
	$pcad_pcb .= "        (patternGraphicsNameRef \"Primary\")\r\n";

	$tr = ($C['rotate']['v'] +3) %4;
	if (($tr == 1) or ($tr == 3)) $tr = ($tr +2) %4;

// can be experimenting
if ($C['pads_count']['v'] > 12)
	$pcad_pcb .= "        (attr \"RefDes\" \"$name1\" (rotation ".($tr*90).") (isVisible True) (justify Center) (textStyleRef \"(DefaultTTF)\") )\r\n";

	$pcad_pcb .= "      )\r\n";
	$pcad_pcb .= "    )\r\n";

//file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);
}

// multilayer end.
$pcad_pcb .= "  )\r\n";
file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);

