cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Extract all the part Id in a session using Jlink

ddhini
15-Moonstone

Extract all the part Id in a session using Jlink

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

 

ACCEPTED SOLUTION

Accepted Solutions
RandyJones
19-Tanzanite
(To:ddhini)

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

	}

}

View solution in original post

5 REPLIES 5
RandyJones
19-Tanzanite
(To:ddhini)

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

ddhini
15-Moonstone
(To:RandyJones)

joness ,

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

RandyJones
19-Tanzanite
(To:ddhini)

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

	}

}
ddhini
15-Moonstone
(To:RandyJones)

RandyJones,

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.

RandyJones
19-Tanzanite
(To:ddhini)


@ddhini wrote:

RandyJones,

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.

Announcements
NEW Creo+ Topics: Real-time Collaboration


Top Tags