C#: 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_1.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

A code example for C#:

static void Main(string[] args)
{
   int index = 0;
   int status = 0;
   // Define the path of the dlls
   string directory1 = @"C:\Program Files\SOFiSTiK\2022\SOFiSTiK 2022\interfaces\64bit";
   string directory2 = @"C:\Program Files\SOFiSTiK\2022\SOFiSTiK 2022";

   // Get the path
   string path = Environment.GetEnvironmentVariable("path");

   // Set the new path environment variable + SOFiSTiK dlls path
   path = directory1 + ";" + directory2 + ";" + path;

   // Set the path variable (to read the data from CDB)
   System.Environment.SetEnvironmentVariable("path", path);


   // Connect to CDB, int sof_cdb_init  ( char* FileName, int Index);
   // Always use index 99, for more details see cdbase.chm
   index = sof_cdb_init(@"S:\test\read_nodes.cdb", 99);
   if (index < 0)
   {
      Console.WriteLine("ERROR: Index = " + index + " < 0 - see clib1.h for meaning of error code");
      return;
   }
   else if (index == 0)
   {
      Console.WriteLine("ERROR: Index = " + index + " - The file is not a database");
      return;
   }

   // Check if sof_cdb_flush is working
   status = sof_cdb_status(index);

   // print index and status
   Console.WriteLine("Index: " + index);
   Console.WriteLine("Status: " + status);
   Console.WriteLine();

   // data as cs_node
   cs_node data;

   // get the length of the structure
   int datalen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(cs_node));

   Console.WriteLine(string.Format("{0,-14}", "NR") +
         string.Format("{0,-14}", "INR") +
         string.Format("{0,-14}", "KFIX") +
         string.Format("{0,-14}", "NCOD") +
         string.Format("{0,-14}", "X") +
         string.Format("{0,-14}", "Y") +
         string.Format("{0,-14}", "Z"));

   // the kwh = 20, kwl = 0, pos = 1
   while (sof_cdb_get(index, 20, 0, &data, ref datalen, 1) == 0)
   {
      Console.WriteLine(string.Format("{0,-14}", data.m_nr) +
            string.Format("{0,-14}", data.m_inr) +
            string.Format("{0,-14}", data.m_kfix) +
            string.Format("{0,-14}", data.m_ncod) +
            string.Format("{0,-14}", data.m_xyz[0]) +
            string.Format("{0,-14}", data.m_xyz[1]) +
            string.Format("{0,-14}", data.m_xyz[2]));

      // check again for the length
      datalen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(cs_node));
   }

   // use sof_cdb_flush() and sof_cdb_close()
   sof_cdb_flush(index);

   // close the CDB
   sof_cdb_close(0);

   // Output the status after closing the CDB
   Console.WriteLine();

   if (sof_cdb_status(index) == 0)
      Console.WriteLine("CDB Status = 0, CDB closed succesfully");
   else
      Console.WriteLine("CDB Status <> 0, the CDB doesn't closed successfully");

   Console.Write("Press <ENTER> key to close the application...");
   Console.ReadKey();
}

The example can be found by following:

C:\<sofistik_installation>\2022\SOFiSTiK 2022\interfaces\examples\c#\read_nodes