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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Windchill Java API to traverse Product structure

MartyRoss
1-Newbie

Windchill Java API to traverse Product structure

... just cracked open the Windchill Javadocs. It's not clear where to start.

Want to navigate a part structure in Java (i.e. get WTPart, follow WTPartUsageLink, ...)

Can anyone suggest which APIs to begin looking at / to use?

At first, wt.part.StandardWTPartService looked good, but I can't figure out how to find & load the initial "WTPart" or "WTPartMaster".

Alternatively, we could achieve our goals using tasks with XSLT, but the perception here is using Java API will be more efficient - thoughts?

9 REPLIES 9

Dear Marty,

You are in the right area, but you should use this call, recursively

WTPartHelper.service.getUsesWTParts(myPart, currConfigSpec);

(it will call the StandardWTPartService but will work both client and server side, you should never use Standard services always via the Helper)

Note you need a ConfigSpec; this is the object that resolves the children, the PartUsageLink links the WTPart to a WTPartMaster, the config spec is used to determine which revision of the part is valid in this context .e.g Effectivity, Latest, Baseline etc. However a typical ConfigSpec is for example "Latest" which is the easiest to create

ConfigSpec configSpec = new LatestConfigSpec();

The Java API will be effecient as it is the lowest call level but as this is making multiple queries large structures can take time to navigate, a well optimised db will help. You can use XLST to render the results but it is not a replacement for a data service to get the data structure you need to render.

Have fun,
Simon


akochhar
6-Contributor
(To:ptc-568593)

Hello,

I had a code to traverse a BOM structure and also to get related CAD document.

It is just a smaple code you can modify... and there are also many different API to do this.

 

package com.Custom_Test;

import wt.epm.EPMDocument;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
import wt.vc.VersionReference;

public class Test_Class {

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

Test_Class testClass = new Test_Class();
testClass.getPart();

}

public void getPart() throws WTException
{

System.out.println("it is working");
ReferenceFactory factory = new ReferenceFactory();
VersionReference objRef = (VersionReference) factory.getReference("VR:wt.part.WTPart:83766");
System.out.println("the reference is "+objRef);
WTPart part = (WTPart) objRef.getObject();
QueryResult getChildParts=WTPartHelper.service.getUsesWTPartMasters(part);

while(getChildParts.hasMoreElements())

{
WTPartUsageLink usageLink =(WTPartUsageLink) getChildParts.nextElement();
WTPartMaster childPartMaster= (WTPartMaster) usageLink.getRoleBObjectRef().getObject();
QueryResult childPartResult= VersionControlHelper.service.allIterationsOf(childPartMaster);
while(childPartResult.hasMoreElements())
{
WTPart childPart=(WTPart) childPartResult.nextElement();
System.out.println("the part is "+childPart);
getEPMDocument(childPart);
}

}


}

public void getEPMDocument(WTPart part) throws WTException
{
QueryResult result=WTPartHelper.service.getDescribedByDocuments(part);
while(result.hasMoreElements())
{
EPMDocument epmDocument = (EPMDocument) result.nextElement();
System.out.println("the epmdocument name is "+epmDocument.getCADName());
}
}


}

Marty,

I know it's not directly using the Java API, but it might be worth your while to investigate the Query-Tree webject. It can return multiple levels at once and all you have to do is read the output group.

-Thomas R. Busch
Sr. Software Developer
Stryker Instruments
(269) 323-7700 x4014
tom.busch@stryker.com<">mailto:tom.busch@stryker.com>

Hello,

I responding to an old post again, but it pertains to my task.

My current task is to generate a basline for every wtpart (that has children) of a resulting objects of a change notice.

Otherwise save the history of all new Revs in a baseline.

I have the code to gather resulting objects. The create baselinecode that I found lookspretty easy. Of course till I have to use it.

The problem I am having is getting all of tghe children of wtpart tree. I am using the code below & suggusted my this post.


LatestConfigSpec config_spec = null;
config_spec = new LatestConfigSpec();

WTPart wtpm = ((wt.part.WTPart) changeable2); // resulting object


QueryResult queryresult = WTPartHelper.service.getUsesWTParts(wtpm, config_spec);

But its only reporting one level deep on a multilevel wtpart structure.

I more or less need the methods used whenweusebaseline "Collect Related Objects" icon.

I have read into the jsp for the collectItems, etc, but reading wc jsp/tags/etc always drives me nuts.

I thoughtI ran it down to .getUsesWTParts, but obviously not, since the collectItems collectsall children, where the .getUsesWTParts seems to only get 1 level deep. Unless I am using or reportingit improperly.

I have started throwing code together for:


WTPartUsageLink[] getUsedBy( WTPart wtpart )


WTPartUsageLink[] getUsedByLinks( WTPart wtpart )

But have not set it to report yet & I figure I should post this before its time to call it a night.

Any ideas, snippets would be helpful.

L Jett
cadjett@aol.com;datajett@aol.com

I seen one of my issues in the previous post.

getUsedBy & getUsedByLinks, show whereused & opposite of what I want. Dont need that route.

L Jett

Back to task now.

Trying:

wt.fc.Persistable apersistable[][][] = WTPartHelper.service.getUsesWTParts(new WTArrayList(list), configSpec);

In hopes to get multi level parent/child BOM Tree type list.

L Jett



In Reply to Lawrence Jett:



The problem I am having is getting all of tghe children of wtpart tree. I am using the code below & suggusted my this post.


LatestConfigSpec config_spec = null;
config_spec = new LatestConfigSpec();

WTPart wtpm = ((wt.part.WTPart) changeable2); // resulting object


QueryResult queryresult = WTPartHelper.service.getUsesWTParts(wtpm, config_spec);




Yes, getUsesWTParts goes one level deep. To go through the whole structure, you then use it on each part that it returned from the level above. In it's simplest form it would be like this:


while( queryresult.hasMoreElements() ) {

	Object obj = queryresult.nextElement();

	QueryResult qrLvl2 = WTPartHelper.service.getUsesWTParts( (WTPart)obj, config_spec);

	while( qrLvl2.hasMoreElements() ){ 

	...

	}

}

Do you know the "recursivity" approach in coding ?

If so, then use it with the service : WTPartHelper.service.getUsesWTParts, and that's it!

Otherwise, Lisp and/or Scheme (functional coding) are good languages to practice.

I hope your planes will not be turning around in an infinity loop ... 😉



- Nicolas Weydert -




I think I tried the route of query the children on children, but I think it only went to 3rd level then. I have structures that are 5 or more deeper.

but I did borrow loop type code from DPGenericpart.class

LatestConfigSpec configSpec = new LatestConfigSpec();
ArrayList arraylist = new ArrayList();
arraylist = listSortedChildrenInfo(wtpm, configSpec);

public ArrayList listSortedChildrenInfo(WTPart wtpart, ConfigSpec configspec)
throws WTPropertyVetoException, WTException
{
ArrayList arraylist = new ArrayList();
Persistable apersistable[][] = my_getUsesWTParts(wtpart, configspec);
if(apersistable.length > 0)
{
for(int j = 0; j < apersistable.length; j++)
{
WTPart wtChild2 = (WTPart)apersistable[j][1];
ArrayList arraylist1 = listSortedChildrenInfo(wtChild2, configspec);
arraylist.add(arraylist1);
}
}
return arraylist;
}


static Persistable[][] my_getUsesWTParts(WTPart wtpart, ConfigSpec configspec)
throws WTException
{
ArrayList arraylist = new ArrayList();
QueryResult qrMY = WTPartHelper.service.getUsesWTParts(wtpart, configspec);
do
{
if(!qrMY.hasMoreElements())
break;
Persistable apersistable[] = (Persistable[])(Persistable[])qrMY.nextElement();
if(apersistable[1] instanceof WTPart)
{
WTPartUsageLink wtpartusagelink = (WTPartUsageLink)apersistable[0];
if(!wtpartusagelink.dissatisfiesFilterCriteria())
arraylist.add(apersistable);
}
} while(true);
return (Persistable[][])(Persistable[][])arraylist.toArray(new Persistable[arraylist.size()][]);
}

I just thought the 3d array looked closer to obtaining what I need:


wt.fc.Persistable apersistable[][][] = WTPartHelper.service.getUsesWTParts(new WTArrayList(arraylistIn), configSpec);

But it wants a list of wtparts, where I only want 1 wtpart at a time,so I have to adjust for that.

I then have to take the hashmap & run itthrough a baseline creation method, which is my main task.

All from CN Complete transition robot code. No, not all code there, just the PBO sent to ext class.

Any code snippets on wtpart tree that will go into baseline creation method would be appreciated.

Thanks
L Jett
cadjett@aol.com;datajett@aol.com

Top Tags