4.4.3. Code Listing

Below is the complete code listing for client_ex.py

# client_ex.py
# A simple example of a Fluent As A Server client application
import os
import io
import CORBA
import AAS_CORBA    #import the AAS_CORBA module compiled from CoFluentUnit.idl
from functools import partial

# Request that the user supply an aaS_FluentId.txt file with connection
# details for a Fluent session
iorstring=''

while iorstring=='':
    filename=str(raw_input('Please provide an aaS_FluentId.txt file:\n> '))
    try:
        filehdl=open(filename,'r')
    except IOError:
        print(filename+" doesn't appear to exist!")
        continue

    if filehdl.read(3)!="IOR":   #if the file doesn't begin with IOR
        print(filename+" doesn't appear to contain a valid IOR string!")
        continue
    else:
        filehdl.seek(0)
        iorstring=filehdl.read()
    filehdl.close()

# Connect to the Fluent session and open the scheme interface
print "\nConnecting to Fluent sesssion:\n" + iorstring +"\n"
orb=CORBA.ORB_init()
fluent_session=orb.string_to_object(iorstring)
scheme_interface=fluent_session.getSchemeControllerInstance()

# Request the name of a local case file and upload it to the Fluent server
# machine and read it into Fluent. For simplicity in this example we assume
# here that it is a valid case file for the running session).
casefile=''
while not os.path.exists(casefile):
    casefile=str(raw_input('Please provide a case file to load:\n> '))

with io.FileIO(casefile) as f:
    fn_remote=os.path.basename(f.name)
    print 'Uploading file ' + fn_remote + '\n'
    f_buf_r=io.BufferedReader(f)
    scheme_interface.uploadFileFromBuffer(fn_remote,f_buf_r.read())
    fluent_session.loadCase(fn_remote)

# Once the case file is loaded, the user may enter conventional TUI
# commands to perform simulation, write files, etc
while True:
    try:
        command=raw_input("Please enter a TUI command (END-COSIM to finish):\n> ")
    except:
        break
    if command=='END-COSIM':
        break
    output=scheme_interface.doMenuCommandToString(command)
    print output

# Ask the user for any remote files they would like to retrieve, download
# them into a buffer and write them to local files
files=scheme_interface.doMenuCommandToString('ls')
for fn_retrieve in iter(partial(raw_input, 'Filename to retrieve (CR when done): '), ''):
    f_local=io.FileIO(fn_retrieve,'w')
    f_buf_w=io.BufferedWriter(f_local)
    f_buf_w.write(scheme_interface.downloadFileToBuffer(fn_retrieve))
    f_buf_w.flush()
    f_buf_w.close()

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