
import com.ptc.cipjava.*;

import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.pfc.pfcComponentFeat.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcBase.*;
import com.ptc.pfc.pfcDimension.*;
import com.ptc.pfc.pfcGeometry.*;
import com.ptc.pfc.pfcLayer.*;
import com.ptc.pfc.pfcExceptions.*;

import java.io.*;
import java.util.*;
import java.text.*;

public class Test_BOM {

	static Session curSession;
	static final String MSGFILE = "bom.txt";

	public static void start() {

		try {
			curSession = pfcGlobal.GetProESession();
			UICommand inputCommand = curSession.UICreateCommand("Test BOM", new Test_BOM_Listener(curSession));
			curSession.UIAddButton(inputCommand,
									"Applications",
									"Applications.psh_util_pproc",
									"Test_BOM",
									"Test_BOM_2",
									MSGFILE);
		}
		catch(jxthrowable x) {
			System.out.println("Exception:"+x);
		}
	}

	public static void stop() {}
}

class Test_BOM_Listener extends DefaultUICommandActionListener {

	static Session session;
	static Model curmodel;
	static Assembly assy;
	static Models models;
	static Vector [] modelData;
	static String CREATION_PARAM = "JLINK_TRACKER_CREATION_PARAM";
	static String ACCESS_PARAM = "JLINK_TRACKER_ACCESS_PARAM", eol="\n", eol2="\n\n";

	public Test_BOM_Listener(Session sess) {
		session = sess;
	}

	public void OnCommand () {

		try {
	 		SimpleDateFormat df = new SimpleDateFormat ("MMddyy", Locale.getDefault());
			Date tm = new Date();
			String DateStg = df.format(tm);

			session=pfcGlobal.GetProESession();
			curmodel=session.GetCurrentModel();
			String full_name = curmodel.GetFullName();

			models = session.ListModels();

			int modelCount = models.getarraysize();

			modelData = new Vector [modelCount];

			for (int i = 0; i< modelCount; i++) {
				getModelInfo(i);
			}

			String inf_name = full_name + "." + DateStg + ".inf";

			try {
				PrintStream vf = new PrintStream(new FileOutputStream(inf_name));

				for (int i =0; i<modelCount; i++) {
					vf.println( modelData[i] );
				}
				vf.flush();
				vf.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf_name + " file.");
			}
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
	}

	public void getModelInfo(int index) throws jxthrowable {
		Vector data_i;
		Model model_i;

		ModelType type;
		ModelDescriptor desc;
		Integer versionStamp;
		String get_RL, get_REV, get_VER, GenName, InstName, typeString;
		String name, mtypeString, path, fname, ftypeString, fstatusString;
		String itemtypeString, feattypeString;

		model_i = models.get(index);
		data_i = new Vector (9);

		name = model_i.GetFullName();
		name = "Model Name: " + name + eol;
		data_i.addElement(name);

		get_RL = model_i.GetReleaseLevel();
		get_RL = "Release Level: " + get_RL + eol;
		data_i.addElement(get_RL);

		get_REV = model_i.GetRevision();
		get_REV = "Revision Level: " + get_REV + eol;
		data_i.addElement(get_REV);

		get_VER = model_i.GetVersion();
		get_VER = "Version: " + get_VER + eol;
		data_i.addElement(get_VER);

		type = model_i.GetType();

		mtypeString = pfctypeclass.pfcModelType[type.getValue()];
		mtypeString = "Model Type: " + mtypeString + eol;
		data_i.addElement(mtypeString);

		itemtypeString = pfctypeclass.pfcModelItemType[type.getValue()];
		itemtypeString = "Model Item Type: " + itemtypeString + eol;
		data_i.addElement(itemtypeString);

		feattypeString = pfctypeclass.pfcFeatureType[type.getValue()];
		feattypeString = "Feature Item Type: " + feattypeString + eol;
		data_i.addElement(feattypeString);

		InstName = model_i.GetInstanceName();
		InstName = "Instance Name: " + InstName + eol;
		data_i.addElement(InstName);

		GenName = model_i.GetGenericName();
		GenName = "Generic Name: " + GenName + eol;
		data_i.addElement(GenName);

		modelData[index] = data_i;

		return;
	}
}
