API Tutorial
API Tutorial
An Introduction to
The Hows and Whys
What is an API?
that the API script you write is not part of FEMAP, but is a stand alone program
that is interacting with FEMAP.
There are a number of codes that can call FEMAP through the API: Visual Basic,
VBA (Excel, Word, Access, ... ), C, or C++.
The most commonly used codes used are Visual Basic, VBA, and WinWrap.
WinWrap is a flavor of Visual Basic that is included with FEMAP. In the FEMAP
interface, WinWrap is noncompilable, for this reason many choose not to use it,
but it is a very convenient way to program if your specific application does not
need to be compiled.
This tutorial will focus exclusively on using WinWrap via the FEMAP API window.
Organization
API
FEMAP
The objects found in the FEMAP API fall into two categories:
The FEMAP Application Object has all the properties needed to create
things. It is the object that will be used to create geometry, measure
things, mesh geometry, delete entities, etc.
Defining an Object
Sub Main
Dim femap As femap.model
Set femap = GetObject(,"femap.model")
Dim entitySet As Object
Set entitySet = femap.feSet
Dim vecMove(3) As Double
vecMove(0) = 10
vecMove(1) = 0
vecMove(2) = 0
Dim entityType as long
entityType = 7
Dim messageString as String
messageString = Please Select the Nodes You Would Like To Move
rc = entitySet.Select(entityType,True,messageString)
Dim setID As Long
setID = entitySet.ID
Dim vecLength As Double
rc = femap.feVectorLength(vecMove,vecLength)
rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)
End Sub
Entity Types
Each entity in the FEMAP
API is identified by a name
and an number. The entity
can be referred to by
either. In the preceding
piece of code where I refer
to the node entity as the
number 7, I could also
have referred to it as
FT_NODE. Either way the
API will know to which
entity type you are
referring.
Data Types
Visual Basic requires the programmer to declare all variables before they
are used as well as what type of data they will be. The six data types are
shown below. WinWrap corresponds to the Visual Basic 6 data types.
Dimensioning Variables
Sub Main
Dim femap As femap.model
Set femap = GetObject(,"femap.model")
Dim entitySet As Object
Set entitySet = femap.feSet
rc = entitySet.Select(entityType,True,messageString)
Dim setID As Long
setID = entitySet.ID
Dim vecLength As Double
rc = femap.feVectorLength(vecMove,vecLength)
rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)
End Sub
Object Syntax
rc = object.capability(requirements, output)
The syntax in the above statement is standard. The object is what is being asked to
act. The capability is what the object is being asked to do. The requirements are
what the object needs in order to execute the capability. The output is what the
object will produce, although often times capabilities will have no output.
The term rc is the return code and will generate a specific value depending on a
number of object success states. The rc status is explained in more detail in a
subsequent slide. The reality is that it is an extra - sort of a bonus feature to help in
debugging or controlling sequence execution.
CAPABILITY
REQUIREMENTS
OUTPUT
CAPABILITY
REQUIREMENTS
NO OUTPUT
CAPABILITY
OBJECT
Sub Main
Dim femap As femap.model
Set femap = GetObject(,"femap.model")
rc = entitySet.Select(entityType,True,messageString)
OBJECT
CAPABILITY
rc = entitySet.Select(entityType,True,messageString)
A Set object is used to store a set of entities, i.e. a list of nodes. The select capability
displays the above shown dialogue box so the user can select which nodes they are
interested in. After the user selects these nodes, they are added to the set called
entitySet.
In order to do this, the Set object needs:
it needs to know what type of entity to ask for: entityType, which has already been
set to 7; this number corresponds to the node entity,
the True statement tells the object to clear the set of any old entities that may be in it,
and a message to give the user: messageString
Return Codes
Often statements like the following are found
in APIs:
FEMAP Return Codes
rc = object.capability(requirements,output)
The rc stands for return code. After the
object executes its capability, it returns a
code that corresponds to its success in
executing the capability. If the object is
successful, a -1 is returned. If it is not
successful, something else will be returned
depending upon what went wrong. All the
return codes are found in the table on the
right.
INPUT
OUTPUT
End Sub
Conclusion
What is most interesting about the script we just explored, is that it only does one
thing: it moves the nodes. Everything else found in script exists only to provide the
last command with the information it needs to make the move. This is fairly
common. Often much of the API script is devoted to retrieving things from the
database, interpreting them, changing them, and then finally inserting them back in.
In our case, we retrieved the node numbers of that were to be moved, organized
them into a set, and then requested that the FEMAP Application Object move
them.
The previous example is a simple one that uses very little logic. There are no for or
while loops and no if else statements, but all of the standard logic statements are
available and are used all the time. Anyone with basic programming skills should
be able to utilize them as they would in any other language.
You should now understand the basics needed to read and understand basic APIs.
The only way to become a PRO at writing them is to sit down and do it. In no time
you will find that the structure and capabilities are extremely powerful. You will also
find that you will never again need to scratch your head and say, I wish the
FEMAP programmers would have included this feature.