<?php

// pattern
// ================================================================================================
function patternGetStyle($pcb, $pattern, $ind)
{
	for ($i=0; $i < sizeof($pcb->PATTERN_STYLE); $i++)
	{
		$style = $pcb->PATTERN_STYLE[$i];

		// 1. name
		$pattern_name_bool = strtoupper($style['pattern']) == strtoupper($pattern['pattern']);

		// 2. pads count
		if ($pattern_name_bool)
		{
			$pads_count_bool = $style['pads_count'] == $pattern['pads_count'];
			if (!$pads_count_bool) 
				return false;

			// 3. pad styles
			for ($j=1; $j<=$pattern['pads_count']; $j++)
			{
				$pad_style_bool = $style['pad'.$j.'_style'] == $pattern['pad'.$j.'_style'];
				if (!$pad_style_bool) 
					break;
			}

			if (!$pad_style_bool) 
				continue;

			return $style;
		}
	}

	return false;
}
function patternCheckName($pcb, $name)
{
	for ($i=0; $i < sizeof($pcb->PATTERN_STYLE); $i++)
	{
		$style = $pcb->PATTERN_STYLE[$i];
		if ($style['name'] == $name)
			return $name."_".$i;
	}
	return $name;
}
function patternAddStyle($pcb, $pattern, $ind)
{
	$st = patternGetStyle($pcb, $pattern, $ind);
	if ($st['name'])
		return false;

	$i = sizeof($pcb->PATTERN_STYLE);
	$pcb->PATTERN_STYLE[$i] = $pattern;                               
	$pcb->PATTERN_STYLE[$i]['name'] = patternCheckName($pcb, strtoupper($pattern['pattern']));
//	$pcb->PATTERN_STYLE[$i]['rotate'] = strtoupper($pattern['rotate']);

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

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

	$pattern = array();
	$pattern['pattern'] = $C['pattern']['v'];
	$pattern['rotate'] = $C['rotate']['v'];
	$pattern['pads_count'] = $C['pads_count']['v'];

	for ($j=1; $j<=$pattern['pads_count']; $j++)
	{
		$pattern['pad'.$j.'_num'] = $j;
		$pattern['pad'.$j.'_style'] = $C['pad'.$j.'_style']['v'];
		$pattern['pad'.$j.'_rotate'] = $C['pad'.$j.'_rotate']['v'];
		$pattern['pad'.$j.'_pos_x'] = $C['pos_x']['v'] - $C['pad'.$j.'_pos_x']['v'];
		$pattern['pad'.$j.'_pos_y'] = $C['pos_y']['v'] - $C['pad'.$j.'_pos_y']['v'];
		$pattern['pad'.$j.'_des'] = $C['pad'.$j.'_des']['v'];
	}

	$pattern['lines_count'] = $C['lines_count']['v'];
	for ($j=1; $j<=$pattern['lines_count']; $j++)
	{
		$pattern['line'.$j.'_pos_x'] = $C['pos_x']['v'] - $C['line'.$j.'_pos_x']['v'];
		$pattern['line'.$j.'_pos_y'] = $C['pos_y']['v'] - $C['line'.$j.'_pos_y']['v'];
		$pattern['line'.$j.'_size'] = "8.0";               
		$pattern['line'.$j.'_flags'] = $C['line'.$j.'_flags']['v'];
	}

	if ($pattern_style = patternAddStyle($pcb, $pattern, $i))
	{
		echo"comp [$i], added NEW pattern {";
		echo"name=".$pattern['pattern']."";
		echo", pads=".$pattern['pads_count']."";
		echo", lines=".$pattern['lines_count']."";
		echo", style_rotate=".$pattern['rotate']."";
		echo"}, style=[$pattern_style]<br>";
	}

	// save pattern style to component
	$st = patternGetStyle($pcb, $pattern, $i);
	$pcb->COMP[$i]['style']['v'] = $st['name'];
	$pcb->COMP[$i]['style_rotate']['v'] = $st['rotate'];
}

echo"save patterns: ".sizeof($pcb->PATTERN_STYLE)."<br>";
$pcad_lib  = "";
for($i=0; $i<sizeof($pcb->PATTERN_STYLE); $i++)
{
	$PTR = $pcb->PATTERN_STYLE[$i];

	// begin library pattern
	$pcad_lib .= "  (patternDefExtended \"".$PTR['name']."_1\"\r\n";
	$pcad_lib .= "  (originalName \"".$PTR['name']."\")\r\n";
	$pcad_lib .= "    (patternGraphicsNameRef \"Primary\")\r\n";
	$pcad_lib .= "    (patternGraphicsDef \r\n";
	$pcad_lib .= "      (patternGraphicsNameDef \"Primary\")\r\n";

	// multilayer, pads
	$pcad_lib .= "      (multiLayer \r\n";
	for ($j=1; $j<=$PTR['pads_count']; $j++)
	{
		$rot = ($PTR['pad'.$j.'_rotate'] * 90);
		$x = $PTR['pad'.$j.'_pos_x'];
		$y = $PTR['pad'.$j.'_pos_y'];

		$pcad_lib .= "        (pad (padNum $j) (padStyleRef \"".$PTR['pad'.$j.'_style']."\") (pt $y $x) (rotation $rot) (defaultPinDes \"".$PTR['pad'.$j.'_des']."\") )\r\n";
	}
	$pcad_lib .= "      )\r\n";

	// layer 6 - top silk
	$pcad_lib .= "      (layerContents  (layerNumRef  6)\r\n";
	for ($j=1; $j<$PTR['lines_count']; $j++)
	{
		$flags2 = $PTR['line'.($j+1).'_flags'];
		if (!$flags2) continue; // next point flags = 0, begin other line

		$x1 = $PTR['line'.$j.'_pos_x'];
		$y1 = $PTR['line'.$j.'_pos_y'];

		$x2 = $PTR['line'.($j+1).'_pos_x'];
		$y2 = $PTR['line'.($j+1).'_pos_y'];

		$w = $PTR['line'.$j.'_size'];

if ( ($x1 == $x2) and ($y1 == $y2) ) {} else // HACK !!!
		$pcad_lib .= "        (line (pt $y2 $x2) (pt $y1 $x1) (width $w) )\r\n";
	}
//	$rot = ($PTR['pad'.$j.'_rotate'] * 90);     //(rotation $rot) 
	$pcad_lib .= "        (attr \"RefDes\" \"\" (isVisible False) (justify Center) (textStyleRef \"(DefaultTTF)\") )\r\n";
	$pcad_lib .= "        (attr \"Type\" \"\" (isVisible False) (justify Center) (textStyleRef \"(DefaultTTF)\") )\r\n";
	$pcad_lib .= "        (attr \"Value\" \"\" (isVisible False) (justify Center) (textStyleRef \"(DefaultTTF)\") )\r\n";
	$pcad_lib .= "      )\r\n";

	// end
	$pcad_lib .= "    )\r\n"; // patternGraphicsDef
	$pcad_lib .= "  )\r\n"; // patternDefExtended

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