Universal Robots+

Visit URCap Nodes using the TreeNode.traverse(newProgramVisitor() {}

Hello,

I have created a few childNodes for a parentNode and I want to visit some of them using this (TreeNode.traverse(new ProgramVisitor() { } )).

I have copied the code of Mr Jacob. Here is below:

TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);

// Traverse the program tree, using the ProgramNodeVisitor

root.traverse(new ProgramNodeVisitor() {

    // Override "visit" looking for "URCapProgramNode" nodes

    // Visit will be called for every URCapProgramNode in sub-tree

    @Override

    public void visit(URCapProgramNode programNode, int index, int depth) {

        // Check if the found program node implements the "MyCustomAPI" interface

        if(programNode.canGetAs(MyCustomAPI.class)) {

            // If it did, we found the right node

            // Now we can use "getAs" since we already tested with "canGetAs"

            // After "getAs" we can now call the interface methods 

            programNode.getAs(MyCustomAPI.class).addPoss(poss, firstPoss);

        }

    }

});

This gets in every childNode which implements MyCustomAPI. I want to get only in 3rd and 5th childNodes of the same parentNode.

Thanks in Advance,

Mehmet Bilgin

TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);

root.traverse(new ProgramNodeVisitor() {

@Override

public void visit(URCapProgramNode programNode, int index, int depth) {

    if(programNode.canGetAs(MyCustomAPI.class)) {

	if (index == 2 || index== 4){

        		programNode.getAs(MyCustomAPI.class).addPoss(poss, firstPoss);
	}

    }

}

});

Adding if (index == … did the trick for me. Keep in mind that the counting of the siblings starts with zero.

Thanks,