Read the node properties

Problem Description

In this example we will read the nodes data from the database.

Following values are extracted:

  • NR (node number)

  • INR (internal node number)

  • KFIX (degree of freedoms, bitwise encoded)

  • NCOD (additional bit code)

  • X (global X coordinate)

  • Y (global Y coordinate)

  • Z (global Z coordinate)

The nodes description can be found in CDBASE.CHM as shown in figure below:

../../_images/vba_read_nodes_13.png

The [INT] means that the data-type is integer. The data-type can easily be checked in the data-type list given in the example.

Problem Solution

The example can be found by following:

C:\<sofistik_installation>\2020\SOFiSTiK 2020\interfaces\examples\python\python_3.x\read_nodes

Here is a code example how to read the nodes from the CDB:

# 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 DLL (32bit or 64bit DLL)
if sofPlatform.find("32Bit") < 0:
   # Set environment variable for the DLL files
   print("Hint: 64bit DLLs are used")
   path = os.environ["Path"]

   # 64bit DLLs
   dllPath = r"C:\sofistik_installation\trunk\SOFiSTiK trunk\interfaces\64bit"
   dllPath += ";"

   # other necessary DLLs
   dllPath += r"C:\sofistik_installation\trunk\SOFiSTiK trunk"
   os.environ["Path"] = dllPath + ";" + path

   # Get the DLL functions
   myDLL = cdll.LoadLibrary("sof_cdb_w-70.dll")
   py_sof_cdb_get = cdll.LoadLibrary("sof_cdb_w-70.dll").sof_cdb_get
   py_sof_cdb_get.restype = c_int

   py_sof_cdb_kenq = cdll.LoadLibrary("sof_cdb_w-70.dll").sof_cdb_kenq_ex
else:
   # Set environment variable for the dll files
   print("Hint: 32bit DLLs are used")
   path = os.environ["Path"]

   # 32bit DLLs
   dllPath = r"C:\sofistik_installation\trunk\SOFiSTiK trunk\interfaces\32bit"
   os.environ["Path"] = dllPath + ";" + path

   # 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

# Connect to CDB
Index = c_int()
cdbIndex = 99

# Set the CDB Path
# e.g. fileName = "S:\\test\\read_nodes.cdb"
fileName = r"S:\test\read_nodes.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)

a = c_int()
ie = c_int(0)
datalen.value = sizeof(CNODE)
RecLen = c_int(sizeof(cnode))

"""
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, 20, 0, byref(cnode), byref(RecLen), 1)
   print("{:10d}{:10d}{:10d}{:10d}{:10.2f}{:10.2f}{:10.2f}".format(
      cnode.m_nr,      # node-number
      cnode.m_inr,     # internal node-number
      cnode.m_kfix,    # degree of freedoms
      cnode.m_ncod,    # additional bit code
      cnode.m_xyz[0],  # x coordinates
      cnode.m_xyz[1],  # y coordinates
      cnode.m_xyz[2])  # z coordinates
   )

   # Always read the length of record before sof_cdb_get is called
   RecLen = c_int(sizeof(cnode))

# 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")