Cancel
Showing results for 
Search instead for 
Did you mean: 

API to read data from a text file

Pioneer
Pioneer

Hi.

 

Is it possible to link a spreadsheet to femap.

If so what code can I use to call it from a macro??

 

Basically I want to bring in surfaces of different color into femap, and the spreadsheet has a list of thicknesses that is allocated to a specific color, so after it is brought into femap I would like the API to get data from the spreadsheet and gropu the surfaces according to color and the gropus to be renamed to thickness value that corresponds with that specific color.

 

On the spreadsheet I have a column that specifies the color ID for femap.

Below is an idea of the code but it will not work because each time the thickness value and color will vary.

 

Sub Main
    'Group surfaces accoding to color
    Dim App As femap.model
    Set App = feFemap()

    Dim s As femap.Surface
    Set s = App.feSurface

    Dim colorSet1 As femap.Set, colorSet2 As femap.Set
    Set colorSet1 = App.feSet
    Set colorSet2 = App.feSet

    Dim sSet As femap.Set, gr As femap.Group
    Set sSet = App.feSet

    While s.Next
        If colorSet1.IsAdded(s.color) = FE_FAIL Then
            colorSet1.Add(s.color)
            colorSet2.Add(s.ID)
        End If
    Wend

    While colorSet2.Next
        sSet.Clear
        Set gr = App.feGroup

        sSet.AddRule(colorSet2.CurrentID,FGD_SURFACE_BYCOLOR)
        gr.SetAdd(FT_SURFACE,sSet.ID)

        s.Get(colorSet2.CurrentID)

        If colorSet2 = CStr(122) Then
        gr.title = "Plate thickness = 6mm " & CStr(s.color)
        End If

         If colorSet2 = CStr(119) Then
        gr.title = "Plate thickness = 8mm " & CStr(s.color)
        End If
        gr.Put(gr.NextEmptyID)
    Wend


End Sub

 Thanking You in advance

RedP

11 REPLIES

Re: API to read data from a spreadsheet

Legend
Legend

Hi RedP,

 

I maked a macro earlier to export contact forces to Excel. You can use it as example.

 

When you write a new macro, you must set reference to your Excel application with References... command (right mouse button) in Femap API Programming pane..

 

Best regards,

 

Peter Kaderasz

Re: API to read data from a spreadsheet

Legend
Legend

Dear RedP,

 

 

Can you show a screenshot from excel sheet? It will help to understand what data input is for the macro.

 

Yarko

 

Re: API to read data from a spreadsheet

Phenom
Phenom

It is worth taking a look at Section 2.3 (and Section 2.2) of the Femap API.pdf manual, as this has specific examples of shifting info from Excel to Femap, (and the other way around).

Re: API to read data from a spreadsheet

Pioneer
Pioneer

@kadpeter and @EndZ thank you for the suggestions will have a look at that.

 

@yarko I have attatched the screenshot. 

Re: API to read data from a spreadsheet

Legend
Legend

Dear RedP,

 

 

I have small question: why column with thicknesses is empty in spreedsheet? 

I suppose there should be values you want to read.

 

Your tool currently is missing 2 things:

1) Reading data from excel;

2) Make groups titles using correct thicknesses (correspondent to color).

Correct?

 

Yarko

Re: API to read data from a spreadsheet

Pioneer
Pioneer

@yarko

 

Yes there are values that I want to read but the values will everytime vary.

 

Yes that is correct, but I am having a problem with understanding how to read data from excel into the macro.

Re: API to read data from a spreadsheet

Pioneer
Pioneer

@yarko

 

I have attatched another jpeg with a few plate thicknesses that should be read in, by the macro.

Re: API to read data from a spreadsheet

Legend
Legend

Hi RedP,

 

here is a small API for example to read data from "D:\Test1.xlsx" C5 cell to "var1" variable:

 

Sub Main
    Dim App As femap.model
    Dim appExcel As Excel.Application
    Dim wbkReport As Excel.Workbook
    Dim wksReport As Excel.Worksheet
    Dim var1 As Integer
    Set App = feFemap()
    Set appExcel =  New Excel.Application
    Set wbkReport = appExcel.Workbooks.Open("D:\Test1.xlsx")
    Set wksReport = wbkReport.Worksheets(1)

    'Set Excel to visible if you want, uncommented the next line
    'appExcel.Visible = True

    var1=wksReport.Cells( 5, 3 )
    MsgBox Str(var1)

    wbkReport.Close
    appExcel.Quit

End Sub

 

Best regards,

 

Peter Kaderasz

 

 

 

 

Re: API to read data from a spreadsheet

Pioneer
Pioneer

Thanks a lot KadPeter. But I just can't wrap my head around the code for excel its way to mindboggling. I do however want to try the READFILE way, but I am having some difficulty in getting the code right.

Below I have the code and attatched the .txt file.

 

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

    Dim readFile As femap.Read
    Set readFile = App.feRead

    Dim s As femap.Surface
    Set s = App.feSurface

    Dim colorSet1 As femap.Set, colorSet2 As femap.Set
    Set colorSet1 = App.feSet
    Set colorSet2 = App.feSet

    Dim sSet As femap.Set, gr As femap.Group
    Set sSet = App.feSet

    Dim thickness As Variant
    Set gr = App.feGroup

    Dim t As String

    rc = App.feFileGetName( "Select File with FemapColorID", "Text Files", ".txt", True, t)
    If rc = FE_OK Then
        rc = readFile.Open(t, 80)
        If rc = FE_OK Then
            While Not readFile.AtEOF
                rc = readFile.Read
                gr.ID = readFile.RealField (1,0)
                colorSet2.ID = readFile.IntField (2, 0)
                thickness = readFile.IntField (3,0)
            Wend
    End If
        End If


    While s.Next
        If colorSet1.IsAdded(s.color) = FE_FAIL Then
            colorSet1.Add(s.color)
            colorSet2.Add(s.ID)
        End If
    Wend

    While colorSet2.Next
        sSet.Clear


        sSet.AddRule(colorSet2.CurrentID,FGD_SURFACE_BYCOLOR)
        gr.SetAdd(FT_SURFACE,sSet.ID)

       s.Get(colorSet2.CurrentID)
           gr.title = "Plate =  " & thickness
        gr.Put(gr.ID)

    Wend

End Sub
 

 

Basically I want to bring in a model into femap whereby the model is different colors based on its thickness. So I would like femap to read in the text file and then create groups based on the color of the surfaces.

Thanking you in advance