Skip to main content
1-Visitor
September 12, 2017
Solved

Extract all the part Id in a session using Jlink

  • September 12, 2017
  • 5 replies
  • 3590 views

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

 

Best answer by RandyJones

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();
		}

	}

}

5 replies

20-Turquoise
September 12, 2017

You are going to need a recursive function to solve this.

ddhini1-VisitorAuthor
1-Visitor
September 13, 2017

joness ,

Hope you undestand what I need .can you please help me 

20-Turquoise
September 13, 2017

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();
		}

	}

}