Cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

API - Find Free Edges using C#

Genius
Genius

In other posts on this forum I found the following femap script to obtain a set of free edges:

    Dim App As femap.model
    Set App = feFemap()

    'put all elements into set
    Dim elemSet As femap.Set
    Set elemSet = App.feSet
	elemSet.AddAll(FT_ELEM)

	'get free edges
	Dim nfreecount As Long
	Dim nfreedata As Variant
	App.feElementFreeEdge(elemSet.ID,False,False,nfreecount,nfreedata)

	'retrieve element ids into new array
	Dim i As Integer
	Dim freeElements() As Long
	ReDim freeElements(nfreecount)
	For i = 0 To nfreecount-1
		freeElements(i) = nfreedata(i*3)
	Next

etc.

I'm trying to convert it into a C# script but the debugger keeps on nagging that a Method Name is Expected at:  "nfreedata((i * 3));"

 

My C# code:

model femapModel;
            try
            {
                femapModel = (model)Marshal.GetActiveObject("femap.model");
            }
            catch (Exception)
            {
                femapModel = null;
                MessageBox.Show("No model active, exiting application");
                Environment.Exit(1); //Exits if there is no active model
            }

            var eSet = femapModel.feSet;

            femapModel.feElementFreeEdge(eSet.ID, false, false, out var nfreecount, out object nfreedata);

            var i = 0;
            long[] freeElements = null;
            freeElements = new long[nfreecount + 1];

            for (i = 0; i <= nfreecount - 1; i++)
            {
                freeElements[i] = nfreedata((i * 3));
            }

nfreedata.gif

 

What am I doing wrong, and more importantly how can I resolve it?

Maarten Elings | Engineer
3 REPLIES 3

Re: API - Find Free Edges using C#

Genius
Genius

as an 'improvement' i have replaced the "freeElements = new long[nfreecount + 1];" with "Array.Resize(ref freeElements, nfreecount);".

 

the problem lies that I use the object nfreedata as an method (obviously).

whats the best way to resolve this? and why does this work in VB-script and not in c#

 

current code:

model femapModel;
            try
            {
                femapModel = (model)Marshal.GetActiveObject("femap.model");
            }
            catch (Exception)
            {
                femapModel = null;
                MessageBox.Show("No model active, exiting application");
                Environment.Exit(1); //Exits if there is no active model
            }

            var eSet = femapModel.feSet;

            femapModel.feElementFreeEdge(eSet.ID, false, false, out var nfreecount, out object nfreedata);

            var i = 0;
            long[] freeElements = null;
            Array.Resize(ref freeElements, nfreecount); // more like the redim

            for (i = 0; i <= nfreecount - 1; i++)
            {
                freeElements[i] = nfreedata((i * 3));
            }
Maarten Elings | Engineer

Re: API - Find Free Edges using C#

Siemens Phenom Siemens Phenom
Siemens Phenom

Maarten,

 

This method returns an array of integers - see Help Section 3.14.1 feElementFreeEdge...

 

INT4.png

 

 

So, it looks like you need to convert your object to an array of integers.  A similar scenario is presented and explained here:

 

https://community.plm.automation.siemens.com/t5/CAE-Simulation-Knowledge-Base/Writing-the-FEMAP-API-...

 

Also, I did not see any elements added to your set. 

 

 

Below is what got it working for me:

 

 

            femap.model femapModel;
            try
            {
                femapModel = (femap.model)Marshal.GetActiveObject("femap.model");
            }
            catch (Exception)
            {
                femapModel = null;
                MessageBox.Show("No model active, exiting application");
                Environment.Exit(1); //Exits if there is no active model
            }

            var eSet = femapModel.feSet;

            eSet.AddAll(femap.zDataType.FT_ELEM);
            
            femapModel.feElementFreeEdge(eSet.ID, false, false, out var nfreecount, out object nfreedata);
            
            var i = 0;
            long[] freeElements = null;
            Array.Resize(ref freeElements, nfreecount); // more like the redim

            int[] nfreedata_converted = new int[nfreecount * 3];
            nfreedata_converted = (int[])nfreedata;

            for (i = 0; i <= nfreecount - 1; i++)
            {
                freeElements[i] = nfreedata_converted[(i * 3)];
            }

Re: API - Find Free Edges using C#

Genius
Genius

thanks, that works like a charm!

I always thought that either 'var' or 'object' could basically be used as anything, including arrays.

 

while cleaning up the code for my previous post I accidentaly deleted the "eSet.AddAll(zDataType.FT_ELEM);" bit.

 

Thank you so much for your help!

 

Hopefully to aid others, here is my complete C# conversion of Yarkos "Group edge elements" script (see this post: API-to-group-only-edge-elements

 

 

 

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

public void GroupFreeEdges()
        {
            model femapModel;
            try
            {
                femapModel = (model)Marshal.GetActiveObject("femap.model");
            }
            catch (Exception)
            {
                femapModel = null;
                MessageBox.Show("No model active, exiting application");
                Environment.Exit(1); //Exits if there is no active model
            }

            Set elemSet = femapModel.feSet;
            elemSet.AddAll(zDataType.FT_ELEM);

            //Get free edges
            femapModel.feElementFreeEdge(elemSet.ID, false, false, out var nfreecount, out var nfreedata);

            //retrieve element ID's into new array
            int i = 0;
            long[] freeElements = null;
            Array.Resize(ref freeElements, nfreecount);

            int[] nfreedata_converted = new int[nfreecount * 3];
            nfreedata_converted = (int[])nfreedata;

            for (i = 0; i <= nfreecount - 1; i++)
            {
                freeElements[i] = nfreedata_converted[(i * 3)];
            }

            Set fset = femapModel.feSet;
            fset.AddArray(nfreecount, freeElements);

            //Create the free-edge group
            Group fgroup = femapModel.feGroup;
            fgroup.title = "Free edge elements";
            fgroup.SetAdd(zDataType.FT_ELEM, fset.ID);
            fgroup.Put(fgroup.NextEmptyID());

            //Update view to show group
            femapModel.feViewRegenerate(0);
        }

 

 

 

 

Maarten Elings | Engineer