<?php

// generate lines
// ================================================================================================
for ($i=0; $i<20; $i++)
  $LAYER[$i] = array();

for($i=1; $i<$pcb->HDR['net_points_count']['v']; $i++)
{
	$flags1 = hex($pcb->NETPOI[$i]['flags']['v']);
	$flags1_layer = substr($flags1, 0, 1); // 0-bot, 1-top
	$flags1_head = substr($flags1, 1, 1); // 0-start line, 1-continue, 6,7 via
	$net1 = $pcb->NETPOI[$i]['net_id']['v'];
	$x1 = $pcb->NETPOI[$i]['pos_x']['v'];
	$y1 = $pcb->NETPOI[$i]['pos_y']['v'];
	$size = $pcb->NETPOI[$i]['size']['v'];


	$flags2 = hex($pcb->NETPOI[($i+1)]['flags']['v']);
	$flags2_layer = substr($flags2, 0, 1); // 0-bot, 1-top
	$flags2_head = substr($flags2, 1, 1); // 0-start line, 1-continue, 6,7 via
	$net2 = $pcb->NETPOI[($i+1)]['net_id']['v'];
	$x2 = $pcb->NETPOI[($i+1)]['pos_x']['v'];
	$y2 = $pcb->NETPOI[($i+1)]['pos_y']['v'];

	if ($net1 != $net2) continue; // next point is from other net
	if ($flags2_head == 0) continue; // next point is start
	if ($flags2_head == 8) continue; // next point is start
	if ($flags2_layer != $flags1_layer) continue; // next point at another layer, via is here
	if (($flags1_layer == $flags2_layer) and ($flags2_head == 6)) continue;



//if ($net1 != 16521) continue; // ISA RES
//if ($net1 != 78) continue; // GND
//if ($net1 != 90) continue; // VCC
//if ($net1 != 66) continue; // VCC



	$line['id1'] = $i;
	$line['x1'] = $x1;
	$line['y1'] = $y1;
	$line['id2'] = $i+1;
	$line['x2'] = $x2;
	$line['y2'] = $y2;
	$line['net'] = getNetByPadOfs($pcb, $net1);
	$line['size'] = $size;

	$LAYER[$flags1_layer][] = $line;
}

for($i=1; $i<$pcb->HDR['brd_points_count']['v']; $i++)
{
	$flags1 = hex($pcb->BRDPOI[$i]['flags']['v']);
	$flags1_head = substr($flags1, 1, 1); // 0-start line, 1-continue, 6,7 via
	$x1 = $pcb->BRDPOI[$i]['pos_x']['v'];
	$y1 = $pcb->BRDPOI[$i]['pos_y']['v'];

	$flags2 = hex($pcb->BRDPOI[($i+1)]['flags']['v']);
	$flags2_head = substr($flags2, 1, 1); // 0-start line, 1-continue, 6,7 via
	$x2 = $pcb->BRDPOI[($i+1)]['pos_x']['v'];
	$y2 = $pcb->BRDPOI[($i+1)]['pos_y']['v'];

	if ($flags2_head == 0) continue; // next point is start

	$line['x1'] = $x1;
	$line['y1'] = $y1;
	$line['x2'] = $x2;
	$line['y2'] = $y2;
	$line['size'] = "8";

	$LAYER[3][] = $line;
}

function createLayerContent($pcb, $LAYER, $orcad_layer)
{
	$pcad_pcb = "";
	for($i=1; $i<sizeof($LAYER[$orcad_layer]); $i++) // 1 - orcad top
	{      
		$line = $LAYER[$orcad_layer][$i];
		$x1 = $line['x1'];
		$y1 = $line['y1'];
		$x2 = $line['x2'];
		$y2 = $line['y2'];
		$size = $line['size'];

		$net = $line['net'];
		$netref = "";
		if ($net) 
			$netref="(netNameRef \"$net\") ";

		$pcad_pcb .= "    (line (pt $y1 $x1) (pt $y2 $x2) (width $size) $netref)";  
//		$pcad_pcb .= " ;; [".$line['id1']."]-[".$line['id2']."]";
		$pcad_pcb .= "\r\n";
	}

	for($i=1; $i<=$pcb->HDR['texts_count']['v']; $i++)
	{
		$T = $pcb->TXT[$i];
		if ($T['layer']['v'] != $orcad_layer) continue; // 0-bot, 1-top

		$style = "(DefaultTTF)";
		$style = $T['style']['v'];
		$x = $T['pos_x']['v'];
		$y = $T['pos_y']['v'];
		$s = $T['str']['v'];
		$r = ($T['rotate']['v']);
//		$r = $r - 1; // rotate -90
		if ($r<0) $r = 4 + $r;
		if ($orcad_layer == 1) // HACK FOR TOP LAYER ONLY !!!
		{
			if ($r==3) $r = 1; 
		}
		$r = $r * 90;

		$flip = "";
		$flags = hex($T['flags']['v']);
		$mirrored = substr($flags, 0, 1); // 1 - not mirrored, 0 - mirrored (why ???)
		$visible = substr($flags, 1, 1); // 1 - visible, 0 - not visible
		if ($mirrored == "0") $flip = "(isFlipped True) ";
		$pcad_pcb .= "    (text (pt $y $x) \"$s\" (textStyleRef \"$style\") (rotation $r) $flip(justify Center) $ext)\r\n";
	}

	return $pcad_pcb;
}


// layer1, pcad top
// ================================================================================================
$pcad_pcb  = "  (layerContents (layerNumRef 1)\r\n";
$pcad_pcb .= createLayerContent($pcb, $LAYER, 1);
$pcad_pcb .= "  )\r\n";
file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);

// layer2, pcad bottom
// ================================================================================================
$pcad_pcb  = "  (layerContents (layerNumRef 2)\r\n";
$pcad_pcb .= createLayerContent($pcb, $LAYER, 0);
$pcad_pcb .= "  )\r\n";
file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);

// board
// ================================================================================================
$pcad_pcb  = "  (layerContents (layerNumRef 3)\r\n";
// - lines
for($i=1; $i<sizeof($LAYER[3]); $i++)
{
	$line = $LAYER[3][$i];
	$x1 = $line['x1'];
	$y1 = $line['y1'];
	$x2 = $line['x2'];
	$y2 = $line['y2'];
	$net = $line['net'];
	$size = $line['size'];

	$pcad_pcb .= "    (line (pt $y1 $x1) (pt $y2 $x2) (width $size) )\r\n";
}
$pcad_pcb .= "  )\r\n";
file_put_contents($out_file, $pcad_pcb, FILE_APPEND | LOCK_EX);

