Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Femap API for Length

Status
Not open for further replies.

sdanderson

Aerospace
Jun 20, 2013
4
I created a simple truss problem in FEMAP and I am trying to extrapolate the data from the FEMAP model using the API feature. I have been trying to pull the length of each element into an excel sheet, the method is Length(dlength) associated with the element. I am struggling to get any code working, does anyone have any suggestions regarding this or a sample code that you were able to get to work. Thanks.
 
Replies continue below

Recommended for you

Below is the code that will do this. This just exports the length of all elements in the model. You can use the .select method if you want to select only certain elements. Note for anything but line elements (Quads, etc.) the length will be zero.

Chris Teague | Sr. Applications Engineer |
Saratech, Inc. | 23201 Lake Center Drive, Suite 101 | Lake Forest, CA 92630 | USA |


Sub Main
'Sample code to export element lengths to Excel for all elements in the model
'Chris Teague
'Saratech - 1-Jul-13

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

Dim excelApp As Excel.Application
Set excelApp = New Excel.Application

Dim feElem As femap.Elem
Set feElem = App.feElem
Dim barSet As femap.Set
Set barSet = App.feSet

Dim counter As Long
Dim dLength As Double

Dim excelWkb As Excel.Workbook
Set excelWkb = excelApp.Workbooks.Add

Dim excelWks As Excel.Worksheet
Set excelWks = excelWkb.Worksheets(1)

excelWks.Cells(1,1) = "Element ID"
excelWks.Cells(1,2) = "Length"

feElem.Reset()

counter=2
'Loop through all elements to output the ID and length
While feElem.Next()
excelWks.Cells(counter, 1) = feElem.ID
feElem.Length(dLength)
excelWks.Cells(counter, 2) = dLength
counter = counter + 1
Wend

'Cell Formating
excelWks.Range("A1", "B1").Font.Bold = True
excelWks.Range("A1", "B1").Font.ThemeColor = xlThemeColorDark1
excelWks.Range("A1", "B1").Interior.ThemeColor = xlThemeColorLight2
excelWks.Columns("A:I").EntireColumn.AutoFit

'Make Excel Visible when we are done
excelApp.Visible = True

End Sub
 
Chris,
Is Saratech offering an API class in Huntsville, AL anytime soon?

"On the human scale, the laws of Newtonian Physics are non-negotiable"
 
@dwallace1971, There are upcoming Femap API (301) classes coming up in Huntsville. They should be posted on our website shortly, and I'll let you know as soon as the dates are available.
 
We do have a Femap API class coming up on August 12-14 2013 in Mission Viejo, CA if you need something soon.
 
Thanks, Chris. I'll look into the class in CA.

"On the human scale, the laws of Newtonian Physics are non-negotiable"
 
@dwallace1971, The next Femap API (301) class coming up in Huntsville is on Sept. 18, 19, 20, 2013. Feel free to use the contact links on our website if you want more information.
 
Thanks for the sample coding. However I am receiving the following error messages from FEMAP 10.3.1:
Measure Distance Between Nodes
Help Index
View Options
Play (F5)
API Error (Line 9): Expecting a valid data type (eg. Integer).
Dim excelApp As -->Excel.Application
API Error (Line 10): Not an object reference.
Set excelApp -->= New Excel.Application
API Error (Line 20): Expecting a valid data type (eg. Integer).
Dim excelWkb As -->Excel.Workbook
API Error (Line 21): Not an object reference.
Set excelWkb -->= excelApp.Workbooks.Add
API Error (Line 23): Expecting a valid data type (eg. Integer).
Dim excelWks As -->Excel.Worksheet
API Error (Line 24): Not an object reference.
Set excelWks -->= excelWkb.Worksheets(1)
API Error (Line 26): Unexpected text.
excelWks-->.Cells(1,1) = "Element ID"
API Error (Line 27): Unexpected text.
excelWks-->.Cells(1,2) = "Length"
API Error (Line 34): Unexpected text.
excelWks-->.Cells(counter, 1) = feElem.ID
API Error (Line 36): Unexpected text.
excelWks-->.Cells(counter, 2) = dLength
API Error (Line 41): Unexpected text.
excelWks-->.Range("A1", "B1").Font.Bold = True
API Error (Line 42): Unexpected text.
excelWks-->.Range("A1", "B1").Font.ThemeColor = xlThemeColorDark1
API Error (Line 43): Unexpected text.
excelWks-->.Range("A1", "B1").Interior.ThemeColor = xlThemeColorLight2
API Error (Line 44): Unexpected text.
excelWks-->.Columns("A:I").EntireColumn.AutoFit
API Error (Line 47): Unexpected text.
excelApp-->.Visible = True
 
Steve,

For references to external programs, you will need to set that up one time per API program. So right click in the Femap API window with that program showing, and choose "References...". Scroll down to Microsoft Excel, click the check box, and then select OK. Run the program again and you should be fine.
 
Thanks cpt09135. I eventually figured it out and wrote this code in Excel's VB Developer:

Private Sub LoadNodalData()

Dim femap As Object
Set femap = GetObject(, "femap.model")

'Dimension these variables so that they can take the form of the object commands
Dim nd As Object
Dim mt As Object
Dim pr As Object
Dim el As Object
Dim pt As Object
Dim cv As Object

'Dimensions for frame outputs
Dim rc As Long
Dim sP As Long
Dim eP As Long
Dim EndPoints As Variant
Dim i As Long
Dim j As Long

'Element outputs
Dim sc As Long
Dim dAA As Double
Dim dAB As Double
Dim Area As Variant

'Force, Displacement, Stress, ETC Output variables and calls
Dim s As Object
Dim v As Object
Dim e As Object
Dim ov As femap.Output

Dim pc As Long
Dim ppc As Long

Dim Col As Long
Dim Row As Long

Dim minID As Long
Dim maxID As Long
Dim minVAL As Double
Dim maxVAL As Double

Dim Count As Long
Dim ID As Variant
Dim IDen As Long
Dim Title As Variant

Dim ouSetID As Long
Dim ouVec As femap.Output
Set ouVec = femap.feOutput
Dim CurrentData As Double

'Simplifying the application call names
Set nd = femap.feNode
Set mt = femap.feMatl
Set pr = femap.feProp
Set el = femap.feElem
Set pt = femap.fePoint
Set cv = femap.feCurve
Set e = femap.feSet
Set ov = femap.feOutput

'Clear Contents
Range(Selection, Selection.SpecialCells(xlLastCell)).Select
With Selection
.Clear
End With

'Getting Material, Property, Element Data
'The numbers in the () represent property type, i.e: 1 corresponds to Property 1 or 1 _
corresponds to Material 1 listed in the Property or Material Tree.

'The pt is starting at ID 0, since there is no ID 0 it will go to 1.
mt.Get (1)
pr.Get (1)
pt.Get (0)


'Creating Title Bar
Range("A1:H1").Select
With Selection
.MergeCells = True
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With

Worksheets(1).Range("A1:H1").Value = "Truss Problem"

'Declaring starting row
Row = 5

'Formatting the Header
Rows(Row).Select
With Selection
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With

Worksheets(1).Cells(Row, 1).Value = "Point ID"
Worksheets(1).Cells(Row, 2).Value = "X"
Worksheets(1).Cells(Row, 3).Value = "Y"
Worksheets(1).Cells(Row, 4).Value = "Z"
Worksheets(1).Cells(Row, 5).Value = "Material"
Worksheets(1).Cells(Row, 6).Value = "M. of Elasticity"

While pt.Next
Row = Row + 1

'Formating CoorCells
Range(Cells(Row, 2), Cells(Row, 4)).Select
With Selection
.HorizontalAlignment = xlCenter
End With

Worksheets(1).Cells(Row, 1).Value = pt.ID
Worksheets(1).Cells(Row, 2).Value = pt.x
Worksheets(1).Cells(Row, 3).Value = pt.y
Worksheets(1).Cells(Row, 4).Value = pt.Z
Worksheets(1).Cells(Row, 5).Value = mt.Title
Worksheets(1).Cells(Row, 6).Value = mt.Ex
Wend

'Row operation, so that the worksheet can accomidate any size truss
Row = Row + 4

'Formatting the Header
Rows(Row).Select
With Selection
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With

Worksheets(1).Cells(Row, 1).Value = "Member ID"
Worksheets(1).Cells(Row, 2).Value = "Length"
Worksheets(1).Cells(Row, 3).Value = "Start Point"
Worksheets(1).Cells(Row, 4).Value = "End Point"
Worksheets(1).Cells(Row, 5).Value = "Area"

'Loop for each element

i = 1
While cv.Next
el.Next
i = i + 1

'In Loop, to get start and end points
rc = cv.Get(i)
rc = cv.EndPoints(sP, eP)

'In Loop, to get start and end area
sc = el.Get(i)
sc = el.Area(dAA, dAB)

Row = Row + 1

'Formatting the Length and Area
Cells(Row, 2).Select
With Selection
.NumberFormat = "0.000"
.HorizontalAlignment = xlCenter
End With
Cells(Row, 5).Select
With Selection
.NumberFormat = "0.000"
.HorizontalAlignment = xlCenter
End With

'Printing in designated row and column
Worksheets(1).Cells(Row, 1).Value = cv.ID
Worksheets(1).Cells(Row, 2).Value = cv.length
Worksheets(1).Cells(Row, 3).Value = sP
Worksheets(1).Cells(Row, 4).Value = eP
Worksheets(1).Cells(Row, 5).Value = dAA


Wend

'Force/Displacement Outputs

i = Row + 6
j = Row + 4

Worksheets(1).Cells(j, 1) = "Output Set ID"
Worksheets(1).Cells(j, 2) = "Element ID"

rc = femap.feSelectOutput("Select", 0, FOT_ANY, FOC_ANY, FT_ELEM, False, s, v)
sc = e.Select(FT_ELEM, True, "select")

While s.Next
v.Reset
Col = 3

While v.Next
e.Reset
pc = ov.GetFromSet(s.CurrentID, v.CurrentID)

ppc = ov.GetTitleIDList(False, v.CurrentID, v.CurrentID, Count, ID, Title)
Row = i

While e.Next()
Worksheets(1).Cells(Row, 1).Value = s.CurrentID
Worksheets(1).Cells(Row, 2).Value = e.CurrentID

ouVec.SetID = s.CurrentID
rc = ouVec.Get(v.CurrentID)
CurrentData = ouVec.Value(e.CurrentID)
Worksheets(1).Cells(Row, Col) = CurrentData

Cells(Row, Col).Select
With Selection
.NumberFormat = "0.000"
.HorizontalAlignment = xlRight
End With

Row = Row + 1
Wend

Worksheets(1).Cells(j, Col).Value = Title
Worksheets(1).Cells(j + 1, Col).Value = ID
Col = Col + 1

Wend
i = Row
Wend


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor