13.6.4. Writing the Script

The first thing that the script must do is import the Common Language Runtime object and load the C# libraries. This is shown below:

import clr
clr.AddReferenceToFileAndPath("bin//CookComputing.XmlRpcV2.dll")
clr.AddReferenceToFile("bin//EkmClientApi.dll")
clr.AddReferenceToFile("bin//log4net.dll")

Next, the script needs to import any classes needed. For example, this script only needs specific access to the ConnectionFactory class:

from com.ansys.ingress.client.api import ConnectionFactory

Finally, the script can open the connection and perform some task. The full script contained in OpenCloseConnection.py is provided below:

# Load EKM assemblies
import client_init

from com.ansys.ingress.client.api import ConnectionFactory

if __name__ == "__main__":
    conn = None
    try:
        # Change the username and password below as needed
        conn = ConnectionFactory.Open(
            "http://localhost:8080", # host
            "ekmadmin",              # username
            "Adminuser123!")         # password
        print ">>>>> Connection " + conn.GetName() + " opened"
    finally:
        if conn:
            conn.Close()
            print ">>>>> Connection " + conn.GetName() + " closed"

The following is the sample output of this script:

F:\scripts>ipy OpenCloseConnection.py
>>>>> Connection Default@lebmmoales2 opened
>>>>> Connection Default@lebmmoales2 closed

Release 18.2 - © ANSYS, Inc. All rights reserved.