cancel
Showing results for 
Search instead for 
Did you mean: 

Writing the FEMAP API in C#

Siemens Experimenter Siemens Experimenter
Siemens Experimenter

While many users begin experimenting with the FEMAP API within the API Programming window available in FEMAP, the API can also be written in most programming languages, as long as you can transfer over the type library. Bringing the FEMAP type library into C# is very simple; within your Visual Studio window go to Project, Add Reference… then in that dialog window go to COM on the sidebar. Within this list you should be able to find your version of FEMAP and add it, if not go to browse and find “femap.tlb” within the highest level folder of your FEMAP installation. This will allow you to access FEMAP objects and methods using C#.

To initialize the program you first need to be able to fetch the active model. To do this you must start with the code below. To use the Marshal object that is seen below you must first declare the System.Runtime.InteropServices namespace at the beginning of your program.

 

femap.model pModel;
try
{
pModel = (femap.model)Marshal.GetActiveObject("femap.model");
}
catch(Exception)
{
pModel = null;
System.Environment.Exit(1);//Exits if there is no active model
}

 

You do not always need to fetch an active application of FEMAP in your program. Instead, you could create a new instance of FEMAP by using the code below.

 

pModel = new femap.model();
pModel.feAppVisible(true);//Makes instance visible

 

One of the more difficult issues when using programming languages other than Visual Basic with FEMAP is the lack of a native Variant type. Within C# they have the object type, which is very similar to a Variant. The major advantage of this object type is how easy it is to convert between different types. The code below shows how to properly convert an object to a double array using the C# syntax.

 

double[] XYZ = new double[nodeCount * 3];
XYZ = (double[])nodeXYZ;

 

Attached code is a C# example that finds the displacement in each direction for selected nodes, and then prints the values to FEMAP’s built in data table. This code shows how to properly use the object type in C# to access the full capabilities of the FEMAP API. One major advantage of Visual C# using Visual Studio is that it automatically builds an executable file from program and stores it in “Documents\Visual Studio\projects\PROJECT_NAME\PROJECT_NAME\bin\Debug”. This allows you to send your program to someone else without them having C# installed on their computer.

Comments
Dreamer
Dreamer

Hi,

I'm having a little trouble exiting my application in C# with out it a warning that my application crashed.  I'd like to see how you exit your C# API application, but I don't see any attachment to download your code.  Can you please post your code and/or Visual Studio solution?

 

My code is below:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using femap;
using System.Runtime.InteropServices;
using System.IO;

namespace ConsoleTestOctagonalBench
{
    class Program
    {
        static void Main(string[] args)
        {

            model App;
            App = new model();

            //  Announce myself
            Console.WriteLine("Trying to connect to active Femap session...");

            try
            {
                App = (femap.model)Marshal.GetActiveObject("femap.model");
            }
            catch (Exception)
            {
                App = null;
                System.Environment.Exit(1);//Exits if there is no active model
            }

            //  Wrap it up
            Console.WriteLine("...Completed");
            string returnGarbage;
            returnGarbage = Console.ReadLine();

        }
    }
}

This occurs when I press <Enter> to end my app.  Femap itself continues, so I'm not sure I understand why it said it stopped working.This occurs when I press <Enter> to end my app. Femap itself continues, so I'm not sure I understand why it said it stopped working.Out of curiousity, I selected "Debug the program".  It did not take me to my code.  Is the DLL that crashed (ntdll.dll) a clue?Out of curiousity, I selected "Debug the program". It did not take me to my code. Is the DLL that crashed (ntdll.dll) a clue?

Dreamer
Dreamer

Just a wild guess, but I think this solves the problem:

 

            Marshal.ReleaseComObject(App);
Contributors