Cancel
Showing results for 
Search instead for 
Did you mean: 

Can't Export Data into a CSV file using this API. CSV is just empty.

Creator
Creator

I have been trying for a while to setup my API to automatically populate all information regarding Cbush elements into a CSV file. However, I can get the required output on Femap but have not been able to successfully export the output.

 

example output: 
The coordinate of Node 329 is (10, 0, 0).
The coordinate of Node 211 is (14, 0, 0).
Load ID 46, Propety ID 7, Elem Type 6, Nodes 329, Nodes 211


The coordinate of Node 327 is (16, 2, 0).
The coordinate of Node 213 is (16, 6, 0).
Load ID 47, Propety ID 7, Elem Type 6, Nodes 327, Nodes 213

 

 

I have been trying to setup the code to populate Load ID, property ID, Elem Type, Node A and B, Node coordinates into a CSV. However, I can't even get any data exported at all and the CSV file is empty.

 

Can someone set me on the right track? Thank you.

 

The HTML Clipboard

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


Const MAX_NODES_PER_ELEM = 20
Dim fNode As Node
Dim fSet As femap.Set
Dim nodeID As Long
Dim xcoordinate As String
Dim ycoordinate As String
Dim zcoordinate As String
Dim nodeIDstr As String
Dim xyz(3) As Double
Dim vXYZ As Variant
Dim eset As femap.Set
Dim El As Object
Dim elSet As Object
Dim numElem As Long
Dim entID As Variant
Dim propID As Variant
Dim elemType As Variant
Dim topology As Variant
Dim Lyr As Variant
Dim color As Variant
Dim formulation As Variant
Dim orient As Variant
Dim offset As Variant
Dim release As Variant
Dim orientSET As Variant
Dim orientID As Variant
Dim nodes As Variant
Dim connectTYPE As Variant
Dim connectSEG As Variant
Dim Msg As String
Dim Data As femap.DataTable
Dim tc As Long
Dim oset As femap.OutputSet
Dim outputvector As femap.Output
Dim C0 As Variant
Dim C1 As Variant
Dim C2 As Variant
Dim C3 As Variant
Dim C4 As Variant
Dim C5 As Variant
Dim C6 As Variant
Dim newids As Long
Dim outputcount As Long
Dim locpath As String
Dim csvfile As String
Dim fname As String



Set fNode=App.feNode
Set fSet=App.feSet
Set eset = App.feSet
Set elSet = App.feSet
Set El = App.feElem
Set Data = App.feDataTable
Set oset= App.feOutputSet
Set outputvector = App.feOutput


Dim findElemType As Integer
findElemType = 6 


fname=""
App.feFileGetName ("Export Cbush Info to Excel", "Comma Seperated File", "*.csv",False,fname)
csvfile = Mid(fname,InStrRev(fname, "\") + 1 )
locpath = Left(fname, Len(fname)-Len(nastfile))


If Len(fname) = 0 Then
        App.feAppMessage(3, "Process cancelled.")
        GoTo jumping_out

End If

count = 0
rc = elSet.AddRule(findElemType, FGD_ELEM_BYTYPE)
outputcount= elSet.Count

App.feAppManagePanes("Data Table", 1)
Data.Lock(False)

''''''output count is the correct count of number of Cbush elements.
'App.feAppMessage(0,Str$(outputcount))


ReDim C0(outputcount) As Long
ReDim C1(outputcount) As Variant
ReDim C2(outputcount) As Double
ReDim C3(outputcount) As Double
ReDim C4(outputcount) As Double
ReDim C5(outputcount) As Double
ReDim C6(outputcount) As Double

''''''''''''''''''''''''
count2=0
If outputcount > count2 Then

rc = El.GetAllArray(elSet.ID, numElem, entID, propID, _
elemType, topology, Lyr, color, formulation,orient,offset, _
release, orientSET, orientID, nodes, connectTYPE, connectSEG )
'tc=rc



vXYZ = xyz(0)
El.type = 6
eset.AddRule(El.type,FGD_ELEM_BYTYPE)
fSet.AddSetRule(eset.ID,FGD_NODE_ONELEM)
nodeID=fSet.First


App.feAppMessage(FCL_NORMAL, _
" Number of Cbush elements: " + Str$(numElem))
For i = 0 To numElem-1
' Load ID, property, type, and nodes into String for display
Msg = "Load ID" + Str$(entID(i)) + ","+ " Propety ID"+Str$(propID(i)) + ", "+ _
    "Elem Type" + Str$(elemType(i))



    For u = 0 To MAX_NODES_PER_ELEM-1
If nodes(i*MAX_NODES_PER_ELEM + u) <> 0 Then

    vc=fNode.Get(nodeID)
    vc=App.feCoordOnNode(nodeID, vXYZ)
'tc=vc
    nodeID= nodes(i*MAX_NODES_PER_ELEM + u)
    ID = nodeID
    nodeID=fSet.Next
    x=vXYZ(0)
    y=vXYZ(1)
    z=vXYZ(2)
    nodeIDstr=ID
    xcoordinate=x
    ycoordinate=y
    zcoordinate=z

Msg = Msg + ", " +"Nodes" + Str$(nodes(i*MAX_NODES_PER_ELEM + u))

App.feAppMessage(0,"The coordinate of Node "+nodeIDstr+" is ("+xcoordinate+", "+ycoordinate+", "+zcoordinate+").")

End If




Next u

App.feAppMessage(FCM_NORMAL, Msg)
C0= count+1
C1=entID
count = count + 1

rc = Data.AddColumn(False, False, FT_ELEM, 0, "ID", FCT_DOUBLE, 5, count, "test", newids)


Next i


count2=count2+1




End If
rc= Data.Save(True, fname, FSF_CSV)




jumping_out:


End Sub

2 REPLIES

Re: Can't Export Data into a CSV file using this API. CSV is just empty.

Siemens Phenom Siemens Phenom
Siemens Phenom

oSam,

 

The ".AddColumn" method on your Data Table object is not valid.  If you step through your code and watch rc, you'll see a return code of 3 (rc = 3).  

 

rc=3.png

 

If you reference the Femap Help section "3.3.7 Femap Constants" you'll see that rc = 3 means "FE_INVALID".

 

RETURN3.png 

 

This is likely due to erros with your function paramters.  For example, "test" is not a valid variant that contains values.  I am not sure how  you plan to populate the data table with your given info.

 

Have you considered the alternative method using WinWrap function "Print"?  The modification to your API below utilizes this and prints successfully to a CSV.

 


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


Const MAX_NODES_PER_ELEM = 20
Dim fNode As Node
Dim fSet As femap.Set
Dim nodeID As Long
Dim xcoordinate As String
Dim ycoordinate As String
Dim zcoordinate As String
Dim nodeIDstr As String
Dim xyz(3) As Double
Dim vXYZ As Variant
Dim eset As femap.Set
Dim El As Object
Dim elSet As Object
Dim numElem As Long
Dim entID As Variant
Dim propID As Variant
Dim elemType As Variant
Dim topology As Variant
Dim Lyr As Variant
Dim color As Variant
Dim formulation As Variant
Dim orient As Variant
Dim offset As Variant
Dim release As Variant
Dim orientSET As Variant
Dim orientID As Variant
Dim nodes As Variant
Dim connectTYPE As Variant
Dim connectSEG As Variant
Dim Msg As String
Dim Data As femap.DataTable
Dim tc As Long
Dim oset As femap.OutputSet
Dim outputvector As femap.Output
Dim C0 As Variant
Dim C1 As Variant
Dim C2 As Variant
Dim C3 As Variant
Dim C4 As Variant
Dim C5 As Variant
Dim C6 As Variant
Dim newids As Long
Dim outputcount As Long
Dim locpath As String
Dim csvfile As String
Dim fname As String



Set fNode=App.feNode
Set fSet=App.feSet
Set eset = App.feSet
Set elSet = App.feSet
Set El = App.feElem
Set Data = App.feDataTable
Set oset= App.feOutputSet
Set outputvector = App.feOutput


Dim findElemType As Integer
findElemType = 6


fname=""
'Set 4th parameter = True to read file
App.feFileGetName ("Export Cbush Info to Excel", "Comma Seperated File", "*.csv",True,fname)
csvfile = Mid(fname,InStrRev(fname, "\") + 1 )
locpath = Left(fname, Len(fname)-Len(nastfile))

Open fname  For Output As #1

Close #1

Open fname  For Append As #1

If Len(fname) = 0 Then
        App.feAppMessage(3, "Process cancelled.")
        GoTo jumping_out

End If

count = 0
rc = elSet.AddRule(findElemType, FGD_ELEM_BYTYPE)
outputcount= elSet.Count

App.feAppManagePanes("Data Table", 1)
Data.Lock(False)

''''''output count is the correct count of number of Cbush elements.
'App.feAppMessage(0,Str$(outputcount))


ReDim C0(outputcount) As Long
ReDim C1(outputcount) As Variant
ReDim C2(outputcount) As Double
ReDim C3(outputcount) As Double
ReDim C4(outputcount) As Double
ReDim C5(outputcount) As Double
ReDim C6(outputcount) As Double

''''''''''''''''''''''''
count2=0
If outputcount > count2 Then

rc = El.GetAllArray(elSet.ID, numElem, entID, propID, _
elemType, topology, Lyr, color, formulation,orient,offset, _
release, orientSET, orientID, nodes, connectTYPE, connectSEG )
'tc=rc



vXYZ = xyz(0)
El.type = 6
eset.AddRule(El.type,FGD_ELEM_BYTYPE)
fSet.AddSetRule(eset.ID,FGD_NODE_ONELEM)
nodeID=fSet.First


App.feAppMessage(FCL_NORMAL, _
" Number of Cbush elements: " + Str$(numElem))
For i = 0 To numElem-1
' Load ID, property, type, and nodes into String for display
Msg = "Load ID" + Str$(entID(i)) + ","+ " Propety ID"+Str$(propID(i)) + ", "+ _
    "Elem Type" + Str$(elemType(i))



    For u = 0 To MAX_NODES_PER_ELEM-1
If nodes(i*MAX_NODES_PER_ELEM + u) <> 0 Then

    vc=fNode.Get(nodeID)
    vc=App.feCoordOnNode(nodeID, vXYZ)
'tc=vc
    nodeID= nodes(i*MAX_NODES_PER_ELEM + u)
    ID = nodeID
    nodeID=fSet.Next
    x=vXYZ(0)
    y=vXYZ(1)
    z=vXYZ(2)
    nodeIDstr=ID
    xcoordinate=x
    ycoordinate=y
    zcoordinate=z

Msg = Msg + ", " +"Nodes" + Str$(nodes(i*MAX_NODES_PER_ELEM + u))
CSVstring = "The coordinate of Node "+nodeIDstr+" is ("+xcoordinate+", "+ycoordinate+", "+zcoordinate+")."
App.feAppMessage(0,CSVstring)
'**************
'Directly Print a string to CSV file
Print #1,CSVstring
'**************
End If




Next u

App.feAppMessage(FCM_NORMAL, Msg)

'**************
'Directly Print a string to CSV file
Print #1,Msg
'**************
C0= count+1
C1=entID
count = count + 1


rc = Data.AddColumn(BOOL, False, FT_ELEM, 0, "ID", FCT_DOUBLE, 5, count, "test", newids)


Next i


count2=count2+1


End If
'rc= Data.Save(True, fname, FSF_CSV)


jumping_out:
Close #1

End Sub

 

 

Re: Can't Export Data into a CSV file using this API. CSV is just empty.

Creator
Creator

This has helped tremendously. I was able to finally fix my code when using the print function.

 

Thank you, I appreciate it.