
import com.ptc.cipjava.jxthrowable;

import com.ptc.pfc.pfcGlobal.pfcGlobal;
import com.ptc.pfc.pfcSession.Session;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcSolid.Solid;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcComponentFeat.*;
import com.ptc.pfc.pfcFamily.FamilyTableRow;

public class pfcComponentFeatExamples {

	public static void replaceBolts (com.ptc.pfc.pfcAssembly.Assembly assembly)
	{
		Session session = null; 	// The Pro/E session object
		Solid bolt = null; 			// The bolt solid model
		FamilyTableRow row = null; 	// The family table row corresponding
									// to the new instance
		Solid newBolt; 				// The new bolt instance
		Features components; 		// List of components in the assembly
		ComponentFeat component;
		ModelDescriptor desc; 		// Component model descriptor
		CompModelReplace replace;
		FeatureOperations replaceOps;

		String oldInstance = "PHILLIPS7_8";
		String newInstance = "SLOT7_8";

		try {
			session = pfcGlobal.GetProESession();

			bolt = (Solid)session.GetModel ("BOLT", ModelType.MDL_PART);
		}
		catch (jxthrowable x)
		{
			System.out.println ("Caught exception: "+x);
			x.printStackTrace();
			return;
		}

		try {
			row = bolt.GetRow (newInstance);

			newBolt = (Solid)row.CreateInstance();


			replaceOps = FeatureOperations.create();

			components = assembly.ListFeaturesByType (Boolean.FALSE,
					FeatureType.FEATTYPE_COMPONENT);

			for (int ii = 0; ii < components.getarraysize(); ii++)
			{
				component = (ComponentFeat)components.get(ii);

				desc = component.GetModelDescr();

				if (desc.GetInstanceName().equals(oldInstance))
				{
					replace = component.CreateReplaceOp (newBolt);

					replaceOps.insert(0, replace);
				}
			}

			assembly.ExecuteFeatureOps (replaceOps, null);
		}
		catch (jxthrowable x)
		{
			System.out.println ("Caught exception: "+x);
			x.printStackTrace();
			return;
		}
	return;
	}
}
