Showing results for 
Search instead for 
Do you mean 

Writing the FEMAP API in C++

by Siemens Experimenter Siemens Experimenter on ‎12-07-2016 03:28 PM

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 fairly simple, all you need to do is #import “FEMAP_ROOT\femap.tlb” name_guids at the beginning of your code.  You will also need to include the atlsafe.h library in order to properly call the FEMAP object. After including these things you will be able to access the FEMAP API objects and methods within C++.

 

#include <atlsafe.h>
#import "C:\FEMAPv1132\femap.tlb" named_guids

 

Then, to be able to access the active FEMAP window you must fetch it within your C++ code. To do this you must follow insert code that is shown below and use GetActiveObject. When doing this you must make sure to use CoInitialize so that you can initialize the COM library needed to call FEMAP.

 

CoInitialize(NULL);
HRESULT hr;
femap::ImodelPtr pModel;
hr = pModel.GetActiveObject("femap.model");
if (hr != S_OK)
{
	return 0;//Ends the program if FEMAP isn’t found
}

 

If you would instead like to create new instance of FEMAP instead of using an active one, all you need to do is replace GetActiveObject with CreateInstance. It is also advised to follow with feAppVisible(true) to make the FEMAP instance visible on your machine.

 

pModel.CreateInstance("femap.model");
pModel->feAppVisible(true);

 

As seen above FEMAP methods are not called in C++ using period notation as the objects are all declared as pointers. To access objects methods you must use “->”, these will access their FEMAP methods. If you instead would like to access their COM methods, like GetActiveObject, you would use period notation.

Compared to other programming languages that use the FEMAP API, C++ is very different. First of all when passing objects that you are expecting output for, you must pass them by reference. To pass an object by reference all you need to do is include an “&” before the object when including it in the function. For Example:

 

pModel->feAppGetActiveView(&viewID)

 

Another major difference is that C++ will not allow you to pass a FEMAP object through native FEMAP functions (if you make your own function you can pass FEMAP objects). To get around this you must use a CComDispatchDriver object, the Dispatch object will take place of the object and will allow you to access its methods through the invoke function.  The easy way to use Dispatches in FEMAP is to declare the object, then use GetProperty to get the objects ID, then use FEMAP’s Get method to transfer the Dispatch to the object you declared. For Example:

 

CComDispatchDriver pDispatch;
CComVariant objectID;
femap::ILoadGeomPtr pLoadGeom;
pLoadGeom = pModel->feLoadGeom;
pLoadSet->NextLoad(&pDispatch); pDispatch.GetPropertyByName(L"ID", &objectID); pLoadGeom->Get(objectID.intVal);

 

Finally, C++’s variants are not as simple to use as the variants in Visual Basic. In C++ it is recommended to use the CComVariant objects when interacting with the FEMAP API. The thing to keep in mind when using CComVariants is that they are just a wrapper for their data, so you need to certain methods to transfer data to and from them. For example to get an array of values out of CComVariant you have to use the built in parray method and transfer it to a recommended CComSafeArray object. Then to transfer data between the SafeArray you must use their GetAt and SetAt methods. Finally to get the data back to the CComVariant it must first be passed through a VARIANT object. For Example:

 

CComVariant pVariant;
CComSafeArray<double> pSafeArray;
VARIANT *pHold = NULL;

pModel->feElemCountOnProp(0, &pVariant, &pTest);
pSafeArray = pVariant.parray;
i = pSafeArray.GetAt(5);
pSafeArray.SetAt(5, i * 2);
pVariant.Clear();
pHold = new CComVariant(pSafeArray);
pVariant.Attach(pHold);

 

 

Attached is a code example that takes the maximum from each output vector across each output set, then prints the value along with the corresponding set to the data table. This code has examples of everything that I have gone over in this article along with other basic C++ syntax. Finally, there is also an executable attached, in C++ you can get an exe file for your code by going to Build, Compile. The exe file will then be stored in “Documents\VisualStudio\projects\PROJECT_NAME\Debug”. An exe file will allow you to send your program to anyone without them needing C++ on their computer.

Contributors