<?php

// component
// ================================================================================================
echo"save components: ".$pcb->HDR['comp_count_real']['v']."<br>";
$pcad_lib  = "";
for($i=0; $i<sizeof($pcb->PATTERN_STYLE); $i++)
{
	$PTR = $pcb->PATTERN_STYLE[$i];

	$pcad_lib .= "  (compDef \"".$PTR['name']."_1\"\r\n";
	$pcad_lib .= "    (originalName \"".$PTR['name']."\")\r\n";
	$pcad_lib .= "    (compHeader\r\n";
	$pcad_lib .= "      (sourceLibrary \"\")\r\n";
	$pcad_lib .= "      (numPins ".$PTR['pads_count'].")\r\n";
	$pcad_lib .= "      (numParts 1)\r\n";
	$pcad_lib .= "      (alts (ieeeAlt True) (deMorganAlt False))\r\n";
	$pcad_lib .= "      (refDesPrefix \"U\")\r\n";
	$pcad_lib .= "    )\r\n";

	for ($j=1; $j<=$PTR['pads_count']; $j++)
	{
		$pad_des = $PTR['pad'.$j.'_des'];
		$pcad_lib .= "    (compPin \"$pad_des\" (pinName \"$pad_des\") (partNum 1) (symPinNum 1) (gateEq 1) (pinEq 0) (pinType Input) )\r\n";
	}

	$pcad_lib .= "    (attachedPattern (patternNum 1) (patternName \"".$PTR['name']."\")\r\n";
	$pcad_lib .= "      (numPads ".$PTR['pads_count'].")\r\n";
	$pcad_lib .= "      (padPinMap\r\n";

	for ($j=1; $j<=$PTR['pads_count']; $j++)
	{
		$pad_des = $PTR['pad'.$j.'_des'];
		$pcad_lib .= "        (padNum $j) (compPinRef \"$pad_des\")\r\n";
	}

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

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