Skip to content

Scripts to Manage Control Plans in Revisions

These scripts are used in managing a control plan when it is to be modified by splitting in to multiple, area specific control plans to be edited in different revisions and, after the revisions have been consolidated back into the master model, to merge the individual plans back into a single control plan.

Script to Split a Control Plan

#####################################################################################
#####################################################################################
# Disclaimer of Warranty: TO THE EXTENT PERMITTED BY APPLICABLE LAW THE AIMSUN
# SCRIPTS, INCLUDING BUT NOT LIMITED TO ALL DATA, TOOLS AND CALCULATIONS THEREIN
# ARE PROVIDED "AS IS" AND WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND BY
# EITHER AIMSUN OR ANYONE ELSE WHO HAS BEEN INVOLVED IN THE CREATION, PRODUCTION OR
# DELIVERY OF THE AIMSUN SCRIPTS, INCLUDING BUT NOT LIMITED TO ANY EXPRESS OR
# IMPLIED WARRANTY OF MERCHANTABILITY, ACCURACY, QUIET ENJOYMENT,
# NONINFRINGEMENT OR FITNESS FOR A PARTICULAR PURPOSE. NO COVENANTS, WARRANTIES
# OR INDEMNITIES OF ANY KIND ARE GRANTED BY AIMSUN TO YOU THE USER. SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
#####################################################################################
#####################################################################################

################################################################################### 
###                       
###     Start main body -
###                                
###################################################################################


def main():

    sourceControlPlans = []

    # Check this is being run from  the right type of object
    if target==None or target.isA(model.getType("GKControlPlan"))==False:
        print "must be run from a control plan or plans"

    # Assemble a list of selected control plans
    for item in selection:
        if item.isA(model.getType("GKControlPlan")):
            sourceControlPlans.append(item)

    # get the relevant objects
    problemNet = returnProblemNetFromDialog("Retrieve problem net")
    nodeIdsInProblemNet = returnDictNodesInPolygon(problemNet).keys()
    polygonLayer = returnPolygonLayerFromDialog("Retrieve polygon layer")
    polygons = returnPolygonsForLayer(polygonLayer)
    dictPolygonNameNodeIds = {}
    listNodeIdsInPolygons = []

    #Split into new plans using polygons
    for polygon in polygons:
        #Try to find the polygon name
        polygonName = str(polygon.getName())
        if polygonName == "":
            polygonName = str(polygon.getExternalId())
        if polygonName == "":
            polygonName = str(polygon.getId())
        if polygonName in dictPolygonNameNodeIds:
            print "repeated polygon name - Error"
            return

        listNodeIdsInPolygon = returnDictNodesInPolygon(polygon).keys()

        #Get the nodes in the polygon
        dictPolygonNameNodeIds[polygonName] = []
        for nodeId in listNodeIdsInPolygon:
            if nodeId not in listNodeIdsInPolygons:
                dictPolygonNameNodeIds[polygonName].append(nodeId)
                listNodeIdsInPolygons.append(nodeId)

    polygonName = "OUTSIDE"
    for nodeId in nodeIdsInProblemNet:
        if nodeId not in listNodeIdsInPolygons:
            if polygonName not in dictPolygonNameNodeIds:
                dictPolygonNameNodeIds[polygonName]=[]
            dictPolygonNameNodeIds[polygonName].append(nodeId)
            print "nodeId outside" + str(nodeId)

    # Copy the nodes to the polygons
    for sourceControlPlan in sourceControlPlans:
        sourceControlPlanName = str(sourceControlPlan.getName())
        for polygonName in sorted(dictPolygonNameNodeIds.keys()):
            listNodesIds = dictPolygonNameNodeIds[polygonName]
            targetControlPlanName = sourceControlPlanName + "_" + polygonName
            targetControlPlan = getControlPlan( model, targetControlPlanName )
            targetControlPlan.setDataValue(parentControlPlanIdCol, QVariant(sourceControlPlan.getId()))

            copyControlJunctionsFromSourceControlPlanToTargetControlPlan(sourceControlPlan, targetControlPlan, listNodesIds)
    return



## Entry Point
## Call the main function for this script
main()

print "Done"

Script to Recombine Control Plans

##################################################################################  
###                       
###     Start main body -
###                               
##################################################################################

def main():

    # Check this is being run from  the right type of object
    if parentControlPlanIdCol==None:
        print "not found column GKControlPlan::ParentControlPlanId"
        return
    dictOriginalControlPlanIdListSplitControlPlans = {}
    if target==None or target.isA(model.getType("GKControlPlan"))==False:
        print "must be run from a control plan or plans"

    # Assemble a list of control plans
    for item in selection:
        if item.isA(model.getType("GKControlPlan")):
            originalControlPlanId = item.getDataValueInt(parentControlPlanIdCol)
            if originalControlPlanId > 0:
                if originalControlPlanId not in dictOriginalControlPlanIdListSplitControlPlans:
                    dictOriginalControlPlanIdListSplitControlPlans[originalControlPlanId]=[]
                dictOriginalControlPlanIdListSplitControlPlans[originalControlPlanId].append(item)

    for originalControlPlanId in dictOriginalControlPlanIdListSplitControlPlans.keys():
        originalControlPlanName = str(originalControlPlanId)
        originalControlPlan = model.getCatalog().find(originalControlPlanId)
        if originalControlPlan!=None:
            originalControlPlanName = str(originalControlPlan.getName())
        if originalControlPlan==None or CREATE_NEW_CONTROL_PLAN:
            originalControlPlanName = originalControlPlanName + "_COMBINED"
            originalControlPlan = getControlPlan( model, originalControlPlanName

        # Copy the junction controls
        for splitControlPlan in dictOriginalControlPlanIdListSplitControlPlans[originalControlPlanId]:
            nodeIdsWithControl = splitControlPlan.getControlJunctions().keys()
            copyControlJunctionsFromSourceControlPlanToTargetControlPlan(splitControlPlan, originalControlPlan, nodeIdsWithControl)

    return


# Entry Point
## Call the main function for this script
main()

print "Done"

Utility Functions

The utility functions are used by the control plan copy scripts.

#################################################################################
###                         
###     Define Utility Functions
###                                
#################################################################################

def getControlPlan( model, name ):
    #find control plan in model
    controlPlanType = model.getType( "GKControlPlan" )
    controlPlan = model.getCatalog().findByName(str(name), controlPlanType)
    if controlPlan!=None:
        return controlPlan
    else:
        cmd = GKControlPlanNewCmd()
        model.getCommander().addCommand(cmd)
        controlPlan = cmd.createdObject()
        controlPlan.setName( name )
        controlPlan.setExternalId( name )
        return controlPlan

def returnProblemNetFromDialog(windowTitle):

    print "Select " + windowTitle + " problemNet"

    dialog = GAnyObjectChooserEditor()
    dialog.setWindowTitle("Select " + windowTitle + " problemNet")
    #dialog.setName("Select " + windowTitle + " Centroids")
    chooser = dialog.getChooser()
    chooser.setWindowTitle("Select " + windowTitle + " problemNet")
    #chooser.setName("Select " + windowTitle + " Centroids")
    chooser.setType(model, model.getType("GKProblemNet"), GAnyObjectChooser.ChooserMode.eOneObject)
    acceptedRejected = dialog.execDialog()
    if acceptedRejected == 1:
        layer = chooser.getObject()
        if layer!=None:
            print "returning problem net by chooser"
            return layer

    print "no problem net"
    return None

def returnPolygonLayerFromDialog(windowTitle):

    print "Select " + windowTitle + " polygon layer"

    dialog = GAnyObjectChooserEditor()
    dialog.setWindowTitle("Select " + windowTitle + " polygon layer")
    #dialog.setName("Select " + windowTitle + " Centroids")
    chooser = dialog.getChooser()
    chooser.setWindowTitle("Select " + windowTitle + " polygon layer")
    #chooser.setName("Select " + windowTitle + " Centroids")
    chooser.setType(model, model.getType("GKLayer"), GAnyObjectChooser.ChooserMode.eOneObject)
    acceptedRejected = dialog.execDialog()
    if acceptedRejected == 1:
        layer = chooser.getObject()
        if layer!=None:
            print "returning problem net by chooser"
            return layer

    print "no problem net"
    return None

def copyControlJunctionsFromSourceControlPlanToTargetControlPlan(sourceControlPlan, targetControlPlan, listNodesIds):

    nodeIdsWithControl = targetControlPlan.getControlJunctions().keys()
    for nodeId in nodeIdsWithControl:
        targetControlPlan.removeControlJunction(nodeId)

    for nodeId in listNodesIds:

        sourceControlJunction = sourceControlPlan.getControlJunction(nodeId)

        targetControlPlan.removeControlJunction(nodeId)

        if sourceControlJunction!=None:
            targetControlPlan.addControlJunction(nodeId, sourceControlJunction.clone())

    return

def returnPolygonsForLayer(layer):

    listPolygons = []

    for geoObject in layer.getObjects():
        if geoObject.isA(model.getType("GKPolygon")):
            listPolygons.append(geoObject)

    return listPolygons

def returnDictNodesInPolygon(polygon):

    dictNodeIdNodes = {}

    if polygon==None:
        for node in model.getCatalog().getObjectsByType(model.getType("GKNode")).values():
            dictNodeIdNodes[node.getId()]=node
        return dictNodeIdNodes

    for nodeOrSection in polygon.classifyObjects(polygon.eOnlyNodesAndSections):
        if nodeOrSection.isA(model.getType("GKNode")):
            dictNodeIdNodes[nodeOrSection.getId()]=nodeOrSection

    return dictNodeIdNodes