CDP 4 User Manual

Scripting Engine

Plugin: CDP4Scripting

Introduction

Multiple computations can be done in CDP4™ using scripts. The module Scripting Engine offer you a development environment specially for that purpose. It is for example possible to manipulate an Engineering Model Setup to calculate its total mass.

Usage

The scripting engine module can be found in the view ribbon. It's possible to open several script files or create a new script. Every script panel contains its own text editor, output console, input console and a panel to see the actual local variables. A script panel can be associated to a session , it's notably compulsory to retrieve data from CDP4™.

img

Commands

The scripting engine contains commands that will help you to interact with it, to clean your development environment or to get data from CDP4™ for example. Besides some CDP4™ classes and methods are available in the script.

A command has to be used as follow Command.CommandName(parameters)

These are the main commands that will help you to get started.

Supported language

Python 2.7

Lua 5.x

Remarks: so far it's not possible to get data from CDP4™ in a Lua script.

Samples

Remarks: Those scripts rely on data of our models. Depending on the architecture of yours, the scripts could not work. The aim of those samples is to show how to use the scripting engine.

Computation of the total mass of an engineering model

""" 
engineeringModel: short name of the engineering model
iterationNumber: number of the iteration 
"""
def totalMassIteration(engineeringModel, iterationNumber) :
    Command.Clear()
    # Retrieve the iteration
    iteration = Command.GetEngineeringModelIteration(engineeringModel, iterationNumber)

    mass = 0
    # Browse all the element definitions of the iteration
    for elementDefinition in iteration.Element :
        print "\n----------------------------\nelement definition : ", elementDefinition.UserFriendlyShortName
        # Browse all the parameters of the element definition
        for parameter in elementDefinition.Parameter :
            print "\nparameter : ", parameter.UserFriendlyShortName
            if parameter.StateDependence is None :
                print "StateDependence : null"
            else :
                print "StateDependence : ", parameter.StateDependence
            print "IsOptionDependent : ", parameter.IsOptionDependent
            # if the parameter is not type of mass we go directly to the next one
            if parameter.UserFriendlyShortName != elementDefinition.UserFriendlyShortName + ".m" :
                continue
            # Add the mass of the parameter to the total mass
            for valueSet in parameter.ValueSet :
                print "published value : ", valueSet.Published[0]               
                if valueSet.Published[0] != "" and "-" not in valueSet.Published[0] :
                    val = float(str(valueSet.Published[0]))
                    print "%f added to the total mass" %val
                    mass += val

    print '\nmass = %f kg' %mass
    return                  

#Command.Clear()
totalMassIteration("LOFT", 1)

Remarks: some additional information such as the stateDependence are shown to show you how easy it is to get data from an object.

Display the nested elements of an iteration

# function to display the nested elements
def printNestedElements(engineeringModelShortName, iterationNumber, domainOfExpertiseShortName, optionShortName) :
    Command.Clear()

    print "Engineering Model: ", engineeringModelShortName
    print "Iteration: ", iterationNumber
    print "option: ", optionShortName
    print "domain of expertise: ", domainOfExpertiseShortName, "\n"

    # Retrieve the engineeringModel
    engineeringModel = Command.GetEngineeringModel(engineeringModelShortName)

    # Retrieve the iteration
    iteration = Command.GetEngineeringModelIteration(engineeringModelShortName, iterationNumber)

    # Retrieve the DomainOfExpertise of the EngineeringModelSetup
    domainOfExpertise = None
    for engineeringModelDomainOfExpertise in engineeringModel.EngineeringModelSetup.ActiveDomain :
        if domainOfExpertiseShortName == engineeringModelDomainOfExpertise.ShortName :
            domainOfExpertise = engineeringModelDomainOfExpertise
            break

    # Retrieve the first option of the iteration
    option = None
    for iterationOption in iteration.Option :
        if iterationOption.ShortName == optionShortName :
            option = iterationOption            
            break           

    # generate the nested elements for the option and the domain of expertise chosen
    nestedElements = None
    if domainOfExpertise is not None and  option is not None :
        nestedElements = Command.NestedElementTreeGenerator.Generate(option, domainOfExpertise)
    else :
        return "The domain of expertise or the option do not exist"

    # Browse the nested elements
    rootNestedElement = None;
    for nestedElement in nestedElements :
        if nestedElement.IsRootElement == True :
            rootNestedElement = nestedElement
        browseNestedElement(nestedElement)          

    # display the root nested element
    print "-------------------------------------------------------"
    if rootNestedElement is not None :
        print "\nrootNestedElement: ", rootNestedElement.ShortName
    else :
        print "\nNo root element found"


# function to display some information of a nested element
def browseNestedElement(nestedElement) :
    print "-------------------------------------------------------"
    print "\nnested element: ", nestedElement.ShortName

    print "\nelements usages "
    for elementUsage in nestedElement.ElementUsage :
        print "\t-", elementUsage.Name

    print "\nnested parameters "
    for nestedParameter in nestedElement.NestedParameter :
        print "\t- '",nestedParameter.UserFriendlyName,"' with the associated parameter '",nestedParameter.AssociatedParameter.UserFriendlyName,"'"


printNestedElements("LOFT", 1, "INS", "default")

Last modified 11 months ago.

^ Top