Configure the project#
The examples were made and tested by using Visual Studio Tools for ‘’Python 3.6’’. The usage for Python 2.x versions requires maybe modifications. An already defined project can be found by following:
C:\<sofistik_installation>\2024\SOFiSTiK 2024\interfaces\examples\python\connect_to_cdb
Set up DLL functions#
Important
add_dll_directory()
in Connect to CDB.
It is not required to set the DLL path to the %PATH%
environment variable
since add_dll_directory()
is provided.To use the functions from the DLL file, ctypes function library is used. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
Some code samples reference the ctypes c_int
type. This type is an alias for the c_long
type on 32-bit systems. So, you should not be confused if c_long
is printed if you would
expect c_int
, they are actually the same type.
from ctypes import
Here is a small example how to use the sof_cdb_get()
function:
# Get the DLL functions
myDLL = cdll.LoadLibrary("sof_cdb_w-2024.dll")
py_sof_cdb_get = cdll.LoadLibrary("sof_cdb_w-2024.dll").sof_cdb_get
py_sof_cdb_get.restype = c_int
Hint
In this example the 64-bit DLL is used.
Set up the Environment Variable [PATH]#
Important
In case you are using a python version 3.8 or later, then it is not required to use the environment variable PATH
.
Use os.add_dll_directory()
instead.
For more details see https://docs.python.org/3/whatsnew/3.8.html#ctypes.
Python 3.7 or a previous version#
It is also necessary to define the environment path:
# Set environment variable for the dll files
print ("Hint: 64bit DLLs are used")
path = os.environ["Path"]
# 64bit DLLs
dllPath = r"C:\<sofistik_installation>\2024\SOFiSTiK 2024\interfaces\64bit"
dllPath += ";"
# other necessary DLLs
dllPath += r"C:\<sofistik_installation>\2024\SOFiSTiK 2024"
os.environ["Path"] = dllPath + ";" + path
To check the Python platform architecture, use this code example:
# Check the python platform (32bit or 64bit)
print ("Python architecture=", platform.architecture())
sofPlatform = str(platform.architecture())
The environment path must be defined before DLL functions are called. In this example environment path is set to:
C:\<sofistik_installation>\2024\SOFiSTiK 2024\interfaces\64bit
C:\<sofistik_installation>\2024\SOFiSTiK 2024
Python 3.8 or a later version#
Since version 3.8 ctypes provides a new function for loading the DLLs, for more details see https://docs.python.org/3/whatsnew/3.8.html#ctypes.
See example that shows how to call the function (for 64-bit):
# Set environment variable for the dll files
print ("Hint: 64bit DLLs are used")
os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024\interfaces\64bit")
os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024")