Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
I need to extract all the component Id's inside the model .
//Arraylist to stores the ID ,NAME
ArrayList <String> std1=new ArrayList<String>();
//GET SESSION Session session=pfcGlobal.GetProESession(); //GET MODEL Model mod=session.GetCurrentModel(); //Set Model=Null Model model = null; //GET ASSEMBLY Assembly asm = (Assembly) session.GetActiveModel(); //Get the Feautres in an Assembly Features components = asm.ListFeaturesByType(null, FeatureType.FEATTYPE_COMPONENT); //CREATE MODELS Models models = Models.create(); //MEMBERS OF COMPONENT for(int i = 0; i < components.getarraysize(); i++) { //GET COMPONENT ComponentFeat component = (ComponentFeat) components.get(i); //MODELDESCRIPTOR TO LOAD MODEL ModelDescriptor md = component.GetModelDescr(); //LOAD MODEL model = session.GetModelFromDescr(md); //EXTRACT THE NAME AND ID String nameid=""+model.GetFullName()+" - "+ component.GetId()+" "; //ADD THE ID AND NAME TO LIST std1.add(nameid); //if model is assembly then again iterate }
The above code extract all the components id into the list.The code extract only the first level components.Its not able to go for all level.I want to extract all the level components Id into my list .please Help me to solve this.Thanks in advance
Solved! Go to Solution.
Here is a class that is using a recursive function:
public class FindActiveModelSessionIds { //Arraylist to stores the ID ,NAME private ArrayList<String> std1 = new ArrayList<String>();//will have duplicates private HashSet<String> sessionIdsSet = new HashSet<String>();//eliminates duplicates public FindActiveModelSessionIds() { try { Session session = pfcGlobal.GetProESession();//get session Model model = session.GetActiveModel();//get active model recursiveTraverseBOM(model, session); System.out.println("SessionIDS Array = " + std1); System.out.println("SessionIDS HashSet = " + sessionIdsSet); } catch (jxthrowable e) { // TODO Auto-generated catch block e.printStackTrace(); } //GET MODEL } private void recursiveTraverseBOM(Model model, Session session) { try { String nameid = "" + model.GetFullName() + " - " + model.GetRelationId() + " "; //ADD THE ID AND NAME TO LIST std1.add(nameid);//could have duplicated entries if same component is used multiple times or in multiple levels sessionIdsSet.add(nameid);//no duplicate entries if(model.GetType().equals(ModelType.MDL_ASSEMBLY)) { //We have an assembly Features components = ((Solid) model).ListFeaturesByType(null, FeatureType.FEATTYPE_COMPONENT); //MEMBERS OF COMPONENT for(int i = 0; i < components.getarraysize(); i++) { ComponentFeat component = (ComponentFeat) components.get(i);//next component ModelDescriptor md = component.GetModelDescr(); Model componentModel = session.GetModelFromDescr(md);//get the model recursiveTraverseBOM(componentModel, session); } } } catch (jxthrowable e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
You are going to need a recursive function to solve this.
joness ,
Hope you undestand what I need .can you please help me
Here is a class that is using a recursive function:
public class FindActiveModelSessionIds { //Arraylist to stores the ID ,NAME private ArrayList<String> std1 = new ArrayList<String>();//will have duplicates private HashSet<String> sessionIdsSet = new HashSet<String>();//eliminates duplicates public FindActiveModelSessionIds() { try { Session session = pfcGlobal.GetProESession();//get session Model model = session.GetActiveModel();//get active model recursiveTraverseBOM(model, session); System.out.println("SessionIDS Array = " + std1); System.out.println("SessionIDS HashSet = " + sessionIdsSet); } catch (jxthrowable e) { // TODO Auto-generated catch block e.printStackTrace(); } //GET MODEL } private void recursiveTraverseBOM(Model model, Session session) { try { String nameid = "" + model.GetFullName() + " - " + model.GetRelationId() + " "; //ADD THE ID AND NAME TO LIST std1.add(nameid);//could have duplicated entries if same component is used multiple times or in multiple levels sessionIdsSet.add(nameid);//no duplicate entries if(model.GetType().equals(ModelType.MDL_ASSEMBLY)) { //We have an assembly Features components = ((Solid) model).ListFeaturesByType(null, FeatureType.FEATTYPE_COMPONENT); //MEMBERS OF COMPONENT for(int i = 0; i < components.getarraysize(); i++) { ComponentFeat component = (ComponentFeat) components.get(i);//next component ModelDescriptor md = component.GetModelDescr(); Model componentModel = session.GetModelFromDescr(md);//get the model recursiveTraverseBOM(componentModel, session); } } } catch (jxthrowable e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Thanks for your solution .Its worked fine.
By using Component.GetId() we can access all the component ids inside the Model.There is no duplicate in ID.Each have its unique ID.
Thanks Randy.
@ddhini wrote:
Thanks for your solution .Its worked fine.
By using Component.GetId() we can access all the component ids inside the Model.There is no duplicate in ID.Each have its unique ID.
You will have duplicate id's in the ArrayList if you have the same component assembled multiple times or in different levels (subassemblies). Using a HashSet eliminates this.