Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Hi All. I have a requirement to store parts while traversing through a multi level BOM structure. So far I have been able to recursively print all the child parts but having a problem add all parts to a list or map ( only adds values of 1st level BOM but not other levels).
Here is the code I am using;
public static List<WTPart> getChildParts(WTPart parentPart) throws WTException {
ArrayList<WTPart> info = new ArrayList<WTPart>();
Map<String, List<String>> map = new HashMap<String,List<String>>();
QueryResult queryResult= WTPartHelper.service.getUsesWTParts(parentPart, new LatestConfigSpec());
// int structure = 1;
WTPart part=null;
if (queryResult != null){
while(queryResult.hasMoreElements()){
Persistable[] persistable=(Persistable[])queryResult.nextElement();
part=(WTPart)persistable[1];
printMessageToConsole(part.getNumber());
info.add(part);
//WTPartUsageLink partLink=(WTPartUsageLink)persistable[0];
getChildParts(part);
}
}
return info;
}
I am also trying to get the BOM Structure level. Is there anyway to get this using this piece of code?
Solved! Go to Solution.
Hi @WM_9965332
You need to write own function recursively.
Example:
// root function
public static void ProjectsTest2(HTTPRequest req, HTTPResponse resp) throws WTException
{
WTPart wtp = UtilWTPart.getPartLatestIteration("0000000017"); // own function to search wtp
List<WTPart> collectedWTPStructure = new ArrayList<>(); // list of all wtparts from all levels
getAllMultiLevelStructure(wtp,1,collectedWTPStructure); // call recursive funtion
}
// recursive function
private static void getAllMultiLevelStructure(WTPart parent, int level, List<WTPart> structureParts) throws WTException
{
QueryResult queryResult = WTPartHelper.service.getUsesWTParts(parent, new LatestConfigSpec());
while (queryResult.hasMoreElements())
{
Persistable[] subWTPObj = (Persistable[]) queryResult.nextElement();
if (subWTPObj[1] instanceof WTPart)
{
WTPart subWTP = (WTPart) subWTPObj[1];
if (!structureParts.contains(subWTP))
{
structureParts.add(subWTP);
}
getAllMultiLevelStructure(subWTP, level++, structureParts); //recursive call until the wtpart does not have children
}
}
}
PetrH
Hi @WM_9965332
You need to write own function recursively.
Example:
// root function
public static void ProjectsTest2(HTTPRequest req, HTTPResponse resp) throws WTException
{
WTPart wtp = UtilWTPart.getPartLatestIteration("0000000017"); // own function to search wtp
List<WTPart> collectedWTPStructure = new ArrayList<>(); // list of all wtparts from all levels
getAllMultiLevelStructure(wtp,1,collectedWTPStructure); // call recursive funtion
}
// recursive function
private static void getAllMultiLevelStructure(WTPart parent, int level, List<WTPart> structureParts) throws WTException
{
QueryResult queryResult = WTPartHelper.service.getUsesWTParts(parent, new LatestConfigSpec());
while (queryResult.hasMoreElements())
{
Persistable[] subWTPObj = (Persistable[]) queryResult.nextElement();
if (subWTPObj[1] instanceof WTPart)
{
WTPart subWTP = (WTPart) subWTPObj[1];
if (!structureParts.contains(subWTP))
{
structureParts.add(subWTP);
}
getAllMultiLevelStructure(subWTP, level++, structureParts); //recursive call until the wtpart does not have children
}
}
}
PetrH
Thanks for the reply @HelesicPetr . One thing I've found is that the parts do not come in order. I'm trying to recreate the multi level BOM report and I need the parts and level to show in a way that the user can interpret which parts are parents and which ones are children
Hi @WM_9965332
it is just example how to use recursive call.
If you need to collect all BOM level information then you need to use different type of variable to save the information.
In my example I just used a List with WTPart. You can see that I used a level number to show you how to get that information. but the example does not use it.
You can.
If you need to know the information as a parent-child and so on you need to use better solution to save that information form multilevel structure
for example, use the UsageLink instead of WTPart in the List<> (subWTPObj[0] instanceof WTPartUsageLink)
or
use a Map<WTPartUsageLink, int> dataMap to collect all usageLinks and save a bom level
or
create own Class where you can store variables as parentWTPart, childWTPArt, usageParameters and so on.
Then you put new own class for each usageLink to a list to process it in the end of collation from all BOM levels.
PetrH