General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

Python and GHS
(Requires GHS with GHSCOM)

Do you know that the GHSCOM module allows Python and GHS to work together? PyWin32 is a set of extensions for Python that provides access to many of the Windows APIs, such as registering Microsoft COM objects.

GHSCOM has long offered the ability to communicate GHS calculations to other programs such as Microsoft Office, Rhino, AutoCAD, and even custom C/C++ programs. The primary communication is done by commands sent to GHS and retrieval of GHS variable values.

Here is an example of how simple a basic lash-up of Python and GHS could be:
# use PyWin32 to access GHSCOM
import win32com.client
ghs = win32com.client.Dispatch("GHSCOM.Application")

# the usual GHS wizardry ensues
ghs.Command("READ FV")
ghs.Command("WEIGHT 250 0 0 8")
ghs.Command("SOLVE")

# transfer GHS variables to python variables!
gfname=ghs.GetVariableString("GF")
gm=ghs.GetVariableValue("GMT")

# and print them
print('GF: ',gfname)
print('GM: ',gm)

# kill the COM object
del ghs

Since this relies on PyWin32 you may need to install that:
pip install pywin32

Perhaps the main advantage of Python is the ready availability of numerous library files. One of our novice Python users put together the following using numpy and matplotlib. It is a contour plot of the variation of the BGN system variable with heel and trim. The script file is available here.



After working with Python for a short while, you might find yourself appreciating even more the way GHS allows you to manipulate strings and incorporate variable values nearly anywhere in a run file. Fortunately, if you really need the power of Python, you don't have to give up GHS.

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs