How can I use python to interact with a PropElements server?

Modified on Wed, 17 Jan at 1:32 PM

You can interact with PropElements scripting commands using the HydroCompPythonTools python library included in the ‘Samples’ folder in your installation (typically C:\Program Files (x86)\HydroComp\PropElements\Samples or C:\Program Files (x86)\PropElements\PropElements\Samples).

 

An example python script is shown below. This example demonstrates opening an existing PropElements file and iterating the propeller performance calculations for a range of RPMs. There is also a toggleable option (default off) to calculate the performance at each RPM for a range of propeller sizes. The results are pushed to a csv file for analysis.

# -*- coding: utf-8 -*-
"""
Created on Mon Jun  5 10:10:31 2023

@author: JA for HydroComp, Inc.

This script uses the HydroCompPythonTools  functions to open a PropElements Project, and run a simple script.

This has been tested in Windows with a PropElements Premium license running on the same machine.
Compatibility with other operating systems is unlikely.
"""

import HydroCompPythonTools as PropElements
import os

#Define the Directories...
ActiveDirectory = os.getcwd()

BuildPath = r"C:\Program Files (x86)\HydroComp\PropElements\PropElements2023.exe"
BuildRunScriptPath = r"C:\Program Files (x86)\HydroComp\PropElements\PropElements2023RunScript.exe"
ScriptPath = os.path.join(ActiveDirectory, r"Script.txt")
SessionOutputPath = os.path.join(ActiveDirectory, r"Output.txt")
SessionOutputLockPath = os.path.join(ActiveDirectory, r"Output.lock")
SummaryFile = os.path.join(ActiveDirectory,"OutputSummary.csv")

PropElements.SessionHandlePath = os.path.join(ActiveDirectory, r"SessionHandle.txt")
PropElements.SessionScriptPath = os.path.join(ActiveDirectory, r"SessionScript.txt")

#Start the PropElements session, which returns the unique handle for that session
SessionHandle = PropElements.StartSession(BuildPath)

#Open the example file
#This also sets PropElements's active directory where the file is located
FilePath = os.path.join(ActiveDirectory,"KP505DGE.hcpl")
PropElements.OpenFile(FilePath, BuildRunScriptPath, SessionHandle)

#Check if necesarry text files exist
if not os.path.exists(ScriptPath):
    with open(ScriptPath, 'w') as fp: 
        pass
if not os.path.exists(SessionOutputPath):
    with open(SessionOutputPath, 'w') as fp: 
        pass
if not os.path.exists(SummaryFile):
    with open(SummaryFile, 'w') as fp: 
        pass

rpmString = "CalcSettings.DesignRPM" #Scripting command to change
rpmSet = [700,725,750,775,800] #Values to iterate

CalcPropSizes = True # set false for only RPM
sizeString = "Propeller.ScaleData"
sizeSet = [0.90,0.95,1.00,1.05,1.15]

if os.path.exists(SummaryFile):
    with open(SummaryFile, "w") as f:
        f.truncate(0) #clear anything in the file
        if CalcPropSizes is True:
            f.write("RPM,Power,Thrust,Efficiency,Diameter\n")
        else:
            f.write("RPM,Power,Thrust,Efficiency\n")

for rpmValue in rpmSet:
    for sizeValue in sizeSet:
        with open(ScriptPath, "w") as f:
            f.write(rpmString + " " + str(rpmValue) + "\n")
            if CalcPropSizes is True:
                f.write(sizeString + " " + str(sizeValue) + "\n")
            f.write("Analysis.CalculatePerformance" + "\n")
            #f.write("App.SetActiveDirectory \"" + strActiveDirectory + "\"" + "\n")
            f.write("Output.Start \"" + SessionOutputPath + "\"" + "\n")
            f.write("CalcSettings.AddToOutput DesignRPM" + "\n")
            f.write("DesignPerformance.AddToOutput PowerDeliveredPerProp" + "\n")
            f.write("DesignPerformance.AddToOutput PropulsorThrust" + "\n")
            f.write("DesignPerformance.AddToOutput PropulsorEfficiency" + "\n")
            if CalcPropSizes is True:
                f.write("Propeller.AddToOutput Diameter" + "\n")
            f.write("Output.End")
        
        Comment = "RPM = " + str(rpmValue)
        ScriptSuccess = PropElements.RunScript(BuildRunScriptPath, ScriptPath, SessionHandle, SessionOutputPath, Comment)
        
        #Push session output to summary file
        if ScriptSuccess is True:
            f1 = open(SessionOutputPath, "r")
            f2 = open(SummaryFile, "a")
            LineToWrite = ""
            for line in f1.readlines():
                if len(line.split(" ")) > 1:
                    LineToWrite = LineToWrite + line.split(" ")[1] + ","
            LineToWrite = LineToWrite.rstrip(",") + "\n"
            f2.write(strLineToWrite)
            f1.close()
            f2.close()
        else:
            break #exit the loop, something is wrong with the scripts
        
        if CalcPropSizes is False:
            break #skip to next fltIterationValue
    
#Unload the session
PropElements.UnloadBuild(BuildRunScriptPath, SessionHandle)

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article