Decode text from CDB#
See also
Python - Examples
Problem Description#
Decode the loadcase titles from integer values.
Problem Solution#
The Python project example can be found by following:
C:\<sofistik_installation>\2024\SOFiSTiK 2024\interfaces\examples\python\python_3.x\number2text
The loadcases information is stored in the record LC_CTRL 012/LC:?
. The loadcase title
is stored into RTEX
array.
@25: RTEX [str*32] |Designation of loadcase | Bezeichnung des Lastfalls
From SP 2020-4 or later, we provide in sof_cdb_w-2024.dll
the
C sof_lib_cs2ps(pckcode*, char*, int)
function for decoding the integer values to text. Where pckcode
is following typedef:
typedef unsigned int pckcode;
See below an example how to use the function via python:
# +============================================================================+
# | Company: SOFiSTiK AG |
# | Version: SOFiSTIK 2024 |
# +============================================================================+
################################################################################
# ABOUT:
# In this example we will show how to use the sof_lib_ps2cs() function
# sof_lib_ps2cs() converts integer values to string
# import all types from sofistik_daten.py, original file can be found by following
# --> examples/python/sofistik_daten.py
from sofistik_daten import *
import os # for the environment variable necessary
import platform # checks the python platform
from ctypes import * # read the functions from the cdb
# This example has been tested with Python 3.7 (64-bit)
print("The path variable=", os.environ["Path"])
# Check the python platform (32bit or 64bit)
print("Python architecture=", platform.architecture())
sofPlatform = str(platform.architecture())
# Get the DLLs (32bit or 64bit DLL)
if sofPlatform.find("32Bit") < 0:
# Set environment variable for the dll files
print ("Hint: 64bit DLLs are used")
# Set DLL dir path - new in PY 3.8 for ctypes
# See: https://docs.python.org/3/whatsnew/3.8.html#ctypes
#os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024\interfaces\64bit")
#os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024")
os.add_dll_directory(r"S:\sof\2024\exe\trunk\_build.fea-gui\release_with_symbols\x64\bin")
# 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
py_sof_cdb_kenq = cdll.LoadLibrary("sof_cdb_w-2024.dll").sof_cdb_kenq_ex
py_sof_lib_ps2cs = cdll.LoadLibrary("sof_cdb_w-2024.dll").sof_lib_ps2cs
py_sof_lib_cs2ps = cdll.LoadLibrary("sof_cdb_w-2024.dll").sof_lib_cs2ps
else:
# Set environment variable for the DLL files
print ("Hint: 32bit DLLs are used")
# Set DLL dir path - new in PY 3.8 for ctypes
# See: https://docs.python.org/3/whatsnew/3.8.html#ctypes
os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024\interfaces\32bit")
os.add_dll_directory(r"C:\sofistik_installation\2024\SOFiSTiK 2024")
# Get the DLL functions
myDLL = cdll.LoadLibrary("cdb_w31.dll")
py_sof_cdb_get = cdll.LoadLibrary("cdb_w31.dll").sof_cdb_get
py_sof_cdb_get.restype = c_int
py_sof_cdb_kenq = cdll.LoadLibrary("cdb_w31.dll").sof_cdb_kenq_ex
py_sof_lib_ps2cs = cdll.LoadLibrary("cdb_w31.dll").sof_lib_ps2cs
py_sof_lib_cs2ps = cdll.LoadLibrary("cdb_w31.dll").sof_lib_cs2ps
# Connect to CDB
Index = c_int()
cdbIndex = 99
# Set the CDB Path
# e.g. fileName = "S:\\test\\read_nodes.cdb"
fileName = r"S:\test\155852\155852.cdb"
# important: Unicode call!
Index.value = myDLL.sof_cdb_init(fileName.encode('utf-8'), cdbIndex)
cdbStat = c_int() # get the CDB status
cdbStat.value = myDLL.sof_cdb_status(Index.value)
# Print the Status of the CDB
print("CDB Status:", cdbStat.value)
pos = c_int(0)
datalen = c_int(0)
ie = c_int(0)
datalen.value = sizeof(CLC_CTRL)
RecLen = c_int(sizeof(clc_ctrl))
# 17 array elements × 4 + include 0 (17*4+1)
s = create_string_buffer(17*4+1)
"""
do while ie == 0, see cdbase.chm, Returnvalue.
= 0 -> No error
= 1 -> Item is longer than Data
= 2 -> End of file reached
= 3 -> Key does not exist
"""
while ie.value < 2:
ie.value = py_sof_cdb_get(Index, 12, 1, byref(clc_ctrl), byref(RecLen), 1)
if ie.value < 2:
myDLL.sof_lib_ps2cs(byref(clc_ctrl.m_rtex), byref(s), sizeof(s))
print(s.value)
# Always read the length of record before sof_cdb_get is called
RecLen = c_int(sizeof(clc_ctrl))
# Close the CDB, 0 - will close all the files
myDLL.sof_cdb_close(0)
# Print again the status of the CDB, if status = 0 -> CDB Closed successfully
cdbStat.value = myDLL.sof_cdb_status(Index.value)
if cdbStat.value == 0:
print("CDB closed successfully, status = 0")