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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

getUsedByWTParts return latest iteration of all revision. How do I get only latest iteration of the latest revision

aacciano-2
10-Marble

getUsedByWTParts return latest iteration of all revision. How do I get only latest iteration of the latest revision

Hello.

I am trying to get the parent parts of a WTPart, using the WTPartHelper.service.getUsedByWTParts.

Here is what I have used:

package ext;

 

import wt.fc.ObjectIdentifier;

import wt.fc.PersistenceHelper;

import wt.fc.QueryResult;

import wt.part.WTPart;

import wt.part.WTPartHelper;

import wt.part.WTPartMaster;

import wt.util.WTException;

public class UsedBy {

 

public static void main(String[] args) throws WTException {

// TODO Auto-generated method stub

ObjectIdentifier oid = ObjectIdentifier.newObjectIdentifier("wt.part.WTPart:780838610");

WTPart part = (WTPart)PersistenceHelper.manager.refresh(oid);

WTPartMaster prtMaster = (WTPartMaster) part.getMaster();

System.out.println("Parent Part Number: " + prtMaster.getNumber());

QueryResult qr = WTPartHelper.service.getUsedByWTParts(prtMaster);

System.out.println("qr size is: " + qr.size());

if (qr.size() == 0) {

System.out.println("QueryResult is null");

else {

for ( int i=0; i<qr.size(); i++) {

WTPart usedPart = (WTPart) qr.nextElement();

System.out.println("Part Parent is: " + usedPart.getIdentity());

Here is what I get:

Here is in Windchill what I have:


How do I get the latest part version?

Thanks for your help.

Antonio

1 ACCEPTED SOLUTION

Accepted Solutions

Antonio,



Use below code and let me know if works

Thanks

Shreyas

package ext.sra;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Set;

import wt.fc.ObjectIdentifier;

import wt.fc.PersistenceHelper;

import wt.fc.QueryResult;

import wt.part.WTPart;

import wt.part.WTPartHelper;

import wt.part.WTPartMaster;

import wt.util.WTException;

public class LatestWhereUSed {

  public static void main(String args[]) throws WTException {

  // TODO Auto-s[]generated method stub

  ObjectIdentifier oid = ObjectIdentifier

  .newObjectIdentifier("wt.part.WTPart:195983493");

  WTPart part = (WTPart) PersistenceHelper.manager.refresh(oid);

  WTPartMaster prtMaster = (WTPartMaster) part.getMaster();

  System.out.println("Parent Part Number: " + prtMaster.getNumber());

  QueryResult qr = WTPartHelper.service.getUsedByWTParts(prtMaster);

  System.out.println("qr size is: " + qr.size());

  if (qr.size() == 0) {

  } else {

  HashMap<String, WTPartMaster> allMasters=new HashMap<String, WTPartMaster>();

  for (int i = 0; i < qr.size(); i++) {

  WTPart usedPart = (WTPart) qr.nextElement();

  allMasters.put(usedPart.getNumber(),(WTPartMaster) usedPart.getMaster());

  System.out.println("Part Parent is: " + usedPart.getIdentity());

  }

  System.out.println(allMasters);

  Set<String> ketset = allMasters.keySet();

  

  for(Iterator<String> kItr = ketset.iterator();kItr.hasNext();){

  WTPartMaster master= allMasters.get( kItr.next());

  WTPart usedPart=getLatest(master);

  System.out.println("Latest ***Part Parent is: " + usedPart.getIdentity());

  

  }

  }

  }

  public static WTPart getLatest(WTPartMaster doc) throws WTException {

  WTPart part = null;

  QueryResult allIteration = wt.vc.VersionControlHelper.service.allIterationsOf(doc);

  if (allIteration != null && allIteration.hasMoreElements())

  part = (WTPart) allIteration.nextElement();

  return part;

  }

}

View solution in original post

2 REPLIES 2

Antonio,



Use below code and let me know if works

Thanks

Shreyas

package ext.sra;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Set;

import wt.fc.ObjectIdentifier;

import wt.fc.PersistenceHelper;

import wt.fc.QueryResult;

import wt.part.WTPart;

import wt.part.WTPartHelper;

import wt.part.WTPartMaster;

import wt.util.WTException;

public class LatestWhereUSed {

  public static void main(String args[]) throws WTException {

  // TODO Auto-s[]generated method stub

  ObjectIdentifier oid = ObjectIdentifier

  .newObjectIdentifier("wt.part.WTPart:195983493");

  WTPart part = (WTPart) PersistenceHelper.manager.refresh(oid);

  WTPartMaster prtMaster = (WTPartMaster) part.getMaster();

  System.out.println("Parent Part Number: " + prtMaster.getNumber());

  QueryResult qr = WTPartHelper.service.getUsedByWTParts(prtMaster);

  System.out.println("qr size is: " + qr.size());

  if (qr.size() == 0) {

  } else {

  HashMap<String, WTPartMaster> allMasters=new HashMap<String, WTPartMaster>();

  for (int i = 0; i < qr.size(); i++) {

  WTPart usedPart = (WTPart) qr.nextElement();

  allMasters.put(usedPart.getNumber(),(WTPartMaster) usedPart.getMaster());

  System.out.println("Part Parent is: " + usedPart.getIdentity());

  }

  System.out.println(allMasters);

  Set<String> ketset = allMasters.keySet();

  

  for(Iterator<String> kItr = ketset.iterator();kItr.hasNext();){

  WTPartMaster master= allMasters.get( kItr.next());

  WTPart usedPart=getLatest(master);

  System.out.println("Latest ***Part Parent is: " + usedPart.getIdentity());

  

  }

  }

  }

  public static WTPart getLatest(WTPartMaster doc) throws WTException {

  WTPart part = null;

  QueryResult allIteration = wt.vc.VersionControlHelper.service.allIterationsOf(doc);

  if (allIteration != null && allIteration.hasMoreElements())

  part = (WTPart) allIteration.nextElement();

  return part;

  }

}

Shreyas,

Thank you so much!!

Very smart way to use the allIterationsOf() and to actually create an Hasmap with <Number, WTPartMaster> (not so useful using the WTPart like I did)!

I am just learning coding, but you help me understand a little better that the solution is easy and there, I just need to stop it for moment, step away from coding and look at the problem from another prospective .

Again,

Thank you very much for your help!

Antonio

Top Tags