Top Banner
Predictive Engineering FEMAP API Tutorial by [email protected] Programming API for FEMAP An Introduction to The How’s and Why’s
22

Programming API for FEMAP

Dec 31, 2016

Download

Documents

dinhliem
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Programming API for FEMAP

An Introduction toThe How’s and Why’s

Page 2: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

• The FEMAP API is an OLC/COM based programming interface and is objectoriented programming. If you have never programmed in an object orientedcode, it can seem quite different and foreign.

• API means “Application Programming Interface”. It is important to understandthat the API script you write is not part of FEMAP, but is a stand alone programthat 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 notneed to be compiled.

• This tutorial will focus exclusively on using WinWrap via the FEMAP API window.

What is an API?

Page 3: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

• This is the optional FEMAPAPI editing window.

• Although the window appearsto be part of your FEMAPsession, it is not. It is merelya code editing tool.

Page 4: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

What is Object Oriented Programming?• Traditional programming is usually seen as being a set of functions, or simply

as a list of instructions.• Object Oriented Programming (or OOP) can be seen as a group of Objects

that cooperate with each other. Each of the objects have their own distinct setof capabilities.

• OOP programming is quite complex and includes topics such as inheritance,encapsulation, among others. These more complex ideas are not immediatelynecessary, and will not be discussed. In fact, the FEMAP API has made itpretty much unnecessary to ever have to learn these concepts.

Page 5: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Organization

FEMAPVisual Basic Code

API

It is helpful to think of each of the entities as being separate.• Your Visual Basic code acts like a traditional code, i.e. as a

set of instructions.• The VB code makes requests of the API, which then acts

upon those requests either by retrieving from and puttingthings into the FEMAP database.

• FEMAP is a database, which only holds and displays data.

Page 6: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

The FEMAP API Objects• The objects found in the FEMAP API fall into two categories:

• The FEMAP Application Object• Stand Alone Objects

• Generally speaking, these objects act have the following properties:• The FEMAP Application Object has all the properties needed to create

things. It is the object that will be used to create geometry, measurethings, mesh geometry, delete entities, etc.

• The Stand Alone Objects are used to manipulate existing entities.

Page 7: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Anatomy of a Simple APINow we’ll walk through a simple API.All this API does is move a set ofselected nodes 10 units in the xdirection. Yes, there is a function thatwill do this directly without an API, butwe are starting simple. The entirescript is shown on the left. We willwalk through each step in this API.

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Page 8: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

What we want to do will also requirethe help of another object, called theSet object. entitySet now has all theproperties inherent in the Set object.

Next we create an object called femap.We then set this object equal to thecurrent femap session. Essentiallywhat this does is create the FEMAPApplication Object and appropriatelycalls it femap. So the object femap nowhas all the properties of the FEMAPApplication Object.

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Defining an ObjectSub Main at the top of the script signifiesthat what follows is the main program.

Page 9: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Entity TypesEach entity in the FEMAPAPI is identified by a nameand an number. The entitycan be referred to byeither. In the precedingpiece of code where I referto the node entity as thenumber 7, I could alsohave referred to it asFT_NODE. Either way theAPI will know to whichentity type you arereferring.

Page 10: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Data TypesVisual Basic requires the programmer to declare all variables before theyare used as well as what type of data they will be. The six data types areshown below. WinWrap corresponds to the Visual Basic 6 data types.

Page 11: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Dimensioning Variables

Next we declare a 3 dimensional array,composed of 8-byte real numbers calledvecMove. This array will represent thevector along which the translation will takeplace. We then specify each value in thearray.

We also declare a variable calledentityType as a 4-byte integer and assignit a value.

Page 12: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Using the Capabilities of an Object

Next, we will declare a string, andgive it a value.

Now what we want to do is collectfrom the user, what nodes theywould like moved.The Set object has a handycapability that allows us to do thiscalled select.

Page 13: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

rc = object.capability(requirements, output)

The syntax in the above statement is standard. The object is what is being “asked” toact. The capability is what the object is being asked to do. The requirements arewhat the object needs in order to execute the capability. The output is what theobject 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 anumber of object success states. The rc status is explained in more detail in asubsequent slide. The reality is that it is an extra - sort of a bonus feature to help indebugging or controlling sequence execution.

Object Syntax

Page 14: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

How this all works is best explained by a moreconcrete example. Think of an object as a person, aperson who will do things that you ask. Will call thisperson “Mike”. Say we want “Mike” to go to the storeand buy an apple. In order for “Mike” to do this, weneed to to provide him with a car and money. For thiscapability, “Mike” will produce an output: an apple. Thestatement would look like this:

rc = Mike.GetApple(Money, Car, Apple)

Objects that Produce an Output

OBJECT CAPABILITY REQUIREMENTS OUTPUT

Page 15: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Objects that Produce NO OutputNow suppose we want “Mike” to wash the dishesin the kitchen. We need to provide him with thedishes, soap, a sponge, and a sink. After he isdone he will produce NO output for us becausewe haven’t asked him to bring us anything. All wehave done is ask him to go off and do something.The statement looks much like the previous one.

Sometimes we ask objects to organize things. Sometimes we will ask them tocreate or move things. The only time objects will have output is if we ask them tobring us something specific. This most likely seems fairly abstract, but once yousee how it actually works you will see that it is very intuitive.

rc = Mike.DoDishes(Dishes, Sponge, Soap, Sink)

OBJECT CAPABILITY REQUIREMENTS NO OUTPUT

Page 16: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

The syntax of the entitySet.Select objectcapability follows the standard syntax.For this object there is no output, onlyrequirements. This is because we arenot “asking” the object for anythingconcrete (like a value), we are askingthe the object to place certain entitiesinto a set. The effect is having ourdesired entities added to the entitySet.(Later, we will use an object that willproduce an actual output, a requireddistance.)

rc = entitySet.Select(entityType,True,messageString)

OBJECTWhat the OBJECT needs in orderto execute the CAPABILITY.

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

CAPABILITY

Page 17: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

rc = entitySet.Select(entityType,True,messageString)

OBJECT CAPABILITY

A Set object is used to store a set of entities, i.e. a list of nodes. The select capabilitydisplays the above shown dialogue box so the user can select which nodes they areinterested in. After the user selects these nodes, they are added to the set calledentitySet.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

What the OBJECT needs to usethe CAPABILITY.

Page 18: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Return CodesOften statements like the following are foundin API’s:

rc = object.capability(requirements,output)

The rc stands for return code. After theobject executes it’s capability, it returns acode that corresponds to it’s success inexecuting the capability. If the object issuccessful, a -1 is returned. If it is notsuccessful, something else will be returneddepending upon what went wrong. All thereturn codes are found in the table on theright.

FEMAP Return Codes

Page 19: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

One More Type of Object SyntaxSub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Certain object capabilities require noinput and do not provide output in theconvectional way.Such is the case with the object.IDstatement.

Instead this syntax returns the desiredvalue to the variable on the left handside of the equal sign. In this case setIDwill take on the ID number of the objectentitySet. A single program can havemultiple set objects defined, eachcontaining their own data. Each of thesesets would have a specific ID todifferentiate them.

Page 20: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Retrieving the Length of the Move VectorSub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

We will now use a capability of the FEMAPApplication Object to find the magnitude ofthe nodal move we will be requesting.

rc = femap.feVectorLength(vecMove,vecLength)

CAPABILITY INPUT OUTPUT

What we are asking of the FEMAPApplication Object is for it to take ourvector, called vecMove, and tell us howlong it is. What the object gives us, is a newvalue for vecLength. If the operation issuccessful, rc will be given the value of –1.

Page 21: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

Moving the Nodes

And last, but certainly not least, we willrequest that the FEMAP ApplicationObject moves our nodes.This capability, called feMoveBy, hasthe following requirements:• what type of entity it is moving,• what set contains the ID’s of the

entities to move,• whether or not this is a radial

translation,• the length of the translation,• and a vector specifying the direction

of the translation.

Sub Main

Dim femap As femap.modelSet femap = GetObject(,"femap.model")

Dim entitySet As ObjectSet entitySet = femap.feSet

Dim vecMove(3) As DoublevecMove(0) = 10vecMove(1) = 0vecMove(2) = 0

Dim entityType as longentityType = 7

Dim messageString as StringmessageString = “Please Select the Nodes You Would Like To Move”

rc = entitySet.Select(entityType,True,messageString)

Dim setID As LongsetID = entitySet.ID

Dim vecLength As Double

rc = femap.feVectorLength(vecMove,vecLength)

rc = femap.feMoveBy( entityType, setID, False, vecLength, vecMove)

End Sub

Page 22: Programming API for FEMAP

Predictive Engineering FEMAP API Tutorial by [email protected]

What is most interesting about the script we just explored, is that it only does onething: it moves the nodes. Everything else found in script exists only to provide thelast command with the information it needs to make the move. This is fairlycommon. Often much of the API script is devoted to retrieving things from thedatabase, 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, organizedthem into a set, and then requested that the FEMAP Application Object movethem.The previous example is a simple one that uses very little logic. There are no for orwhile loops and no if else statements, but all of the standard logic statements areavailable and are used all the time. Anyone with basic programming skills shouldbe able to utilize them as they would in any other language.You should now understand the basics needed to read and understand basic API’s.The only way to become a PRO at writing them is to sit down and do it. In no timeyou will find that the structure and capabilities are extremely powerful. You will alsofind that you will never again need to scratch your head and say, “I wish theFEMAP programmers would have included this feature.”

Conclusion