Connector Scripting - help with bugs please

Connector Scripting - help with bugs please

Postby dgm5555 » Tue Feb 11, 2014 1:02 pm

I have two separate scripts.
The first creates connectors between all selected nodes.
The second is supposed to start at selected node(s), and add to the selection any other nodes which are connected to the originally selected nodes
The first script works reasonable well, but bugs are
1. Creates duplicate connectors between nodes if run more than once on a set (and I can't figure out why)
2. Should ideally select all nodes already twinned to the selected nodes before it is run, otherwise nodes get inconsistent links
3. I get a 'No signature of method' error with: node.setShape('LINE'),
4. I can't figure out how to change the line type (eg to dots) with the API

The second code partially works, but also has a couple of minor bugs
1. It doesn't correctly select all nodes in the group if source and target nodes have multiple connectors (the outcome varies depending on which node/nodes is originally selected, presumably because selectMultipleNodes toggles the selection if a node is 'selected' multiple times.
3. I can't get the check of the connector colour to work correctly - when decommented it results in random selection of the nodes.

Can anyone help with the bugs?
Thanks
David

Code: Select all
//Create a connector between all selected nodes
for (i = 0; i < c.selecteds.size()-1; i++) {      // interate through all selected nodes
    srcNode = c.selecteds[i]
    for (j = 1; j < c.selecteds.size(); j++) {      // interate through all selected nodes not already looped
        tgtNode = c.selecteds[j]
        hasNode = false
        srcNode.connectorsIn.each{
//            if (it.getColorCode()=='#C38EC7') {      // this colour is used to mark 'twins'
                if (tgtNode == it.getSource()) { hasNode = true }
//            }
        }
        srcNode.connectorsOut.each{
//            if (it.getColorCode()=='#C38EC7') {      // this colour is used to mark 'twins'
            if (tgtNode == it.getSource()) { hasNode = true }
//            }
        }

        // NB need to check if nodes already linked
        if (hasNode == false) {
            curCon = srcNode.addConnectorTo(tgtNode)
//            curCon.setShape('LINE')      // This doesn't seem to be working
//       curCon.setMiddleLabel('twin')
            curCon.setColorCode('#C38EC7')   //('#D2B9D3')   //('#fdeef4')
       curCon.setStartArrow(false)
            curCon.setEndArrow(false)
        }
    }
}



Code: Select all
curNodes = []
c.selecteds.each{
    curNodes.add(it)
}

// Select all linked 'twin' connectors
curSelCnt = 0
while (curSelCnt < c.selecteds.size()) {
    curSelCnt = c.selecteds.size()
    c.selecteds.each{
        it.connectorsIn.each{
//            if (it.getColorCode()=='#C38EC7') {      // this colour is used to mark 'twins'
                curNodes.add(it.getSource())
//            }
        }
        it.connectorsOut.each{
//            if (it.getColorCode()=='#C38EC7') {
                curNodes.add(it.getTarget())
//            }
        }
    }
}

// unfold the nodes so all 'twins' are visible (selection fails if node not visible).
curNodes.each{
    unfoldParents(it.parent)
}

c.selectMultipleNodes(curNodes)

void unfoldParents(def n){    // this function by jokro
    if (n!=map.root){
        n.folded=false
        unfoldParents(n.parent)
    }
}

dgm5555
 
Posts: 4
Joined: Sun Feb 02, 2014 9:19 pm

Re: Connector Scripting - help with bugs please

Postby boercher » Wed Feb 26, 2014 12:34 am

First, you should switch to Groovy like iteration methods, see the new wiki paragraph on iteration.

Second, declare variables via def since all undeclared variables are implicitely global:

def curCon = srcNode.addConnectorTo(tgtNode)

1.
Code: Select all
def selected = c.selecteds
selected.each { srcNode ->
    selected.each { tgtNode ->
        // there will be two connectors between two nodes, one forth, one back
        if (srcNode != tgtNode && srcNode.connectorsOut.find { it.target == tgtNode } == null) {
            def curCon = srcNode.addConnectorTo(tgtNode)
            curCon.setShape('LINE')      // This doesn't seem to be working
            curCon.setMiddleLabel('twin')
            curCon.setColorCode('#C38EC7')   //('#D2B9D3')   //('#fdeef4')
            curCon.setStartArrow(false)
            curCon.setEndArrow(true)
        }
    }
}


2.
Code: Select all
// a set is unique
def toSelect = new HashSet(c.selecteds)
toSelect.each {
    toSelect.addAll(it.connectorsOut.collect { it.target })
}
c.selectMultipleNodes(toSelect.toList())


I hope that the code is easy enough to understand.

Regards, Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron