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][python][run femap from python using python code] 2

Status
Not open for further replies.

rgr16

Marine/Ocean
Sep 21, 2013
9
Hi!

Im trying to run femap from within python script. I was trying to use win32com, but im doing something wrong. Have maybe someone have some expirence in that? For example, how to run in python equivalent code of vba (its writing to txt all nodes and elems, what im reading later in python to numpy arrays):


Code:
Sub LoadSetElemCoordsData()
'1.      Attach to the model in a FEMAP session that is already running.
Dim femap As Object
Set femap = GetObject(, "femap.model")

'2.     Create a Node object.
Dim El As Object
Set El = femap.feElem
Dim nd As Object
Set nd = femap.feNode

Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long


'3.     Dimension local variables to receive the data.
 
Dim numElem As Long
Dim i As Long, u As Long, Row As Long
Dim ELEMENTSentID 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

'4.     Make the titles in the first row of the worksheet.
Dim numNode As Long
Dim NODSentID As Variant
Dim nxyz As Variant
Dim nLyr As Variant
Dim ncolor As Variant
Dim ntyp As Variant
Dim ndefCSys As Variant
Dim noutCSys As Variant
Dim npermBC As Variant

ELEMS = El.GetAllArray(0, numElem, ELEMENTSentID, propID, _
elemType, topology, Lyr, color, formulation, orient, offset, _
release, orientSET, orientID, nodes, connectTYPE, connectSEG)

NODS = nd.GetAllArray(0, numNode, NODESentID, nxyz, nLyr, _
ncolor, ntyp, ndefCSys, noutCSys, npermBC)


FilePathELEMS = "G:\\Workspace\\Programming\\Python\\SHIP_1DEG_MOTIONS_ON_WAVE\\A3_CRANE\\PEF\\DATA_VBA\\vbaELEMS.txt"
Open FilePathELEMS For Output As #1

FilePathNODES = "G:\\Workspace\\Programming\\Python\\SHIP_1DEG_MOTIONS_ON_WAVE\\A3_CRANE\\PEF\\DATA_VBA\\vbaNODES.txt"
Open FilePathNODES For Output As #2

MAX_NODES_PER_ELEM = 20
'#1, "numElem " + Str(numElem) + ", NODES in 1 elem 20"
For i1 = 0 To numElem - 1
    Write #1, ELEMENTSentID(i1), _
    nodes(i1 * MAX_NODES_PER_ELEM), _
    nodes(i1 * MAX_NODES_PER_ELEM + 1), _
    nodes(i1 * MAX_NODES_PER_ELEM + 2), _
    nodes(i1 * MAX_NODES_PER_ELEM + 4)
Next i1

'#1, "numNode " + Str(numNode) + ", X,Y,Z, constraints 6 in one node"
For i2 = 0 To numNode - 1
    Write #2, NODESentID(i2), _
    nxyz(i2 * 3), _
    nxyz(i2 * 3 + 1), _
    nxyz(i2 * 3 + 2), _
    npermBC(i2 * 6), _
    npermBC(i2 * 6 + 1), _
    npermBC(i2 * 6 + 2), _
    npermBC(i2 * 6 + 3), _
    npermBC(i2 * 6 + 4), _
    npermBC(i2 * 6 + 5)
Next i2


Close #1
Close #2
End Sub
 
Replies continue below

Recommended for you

Oddly enough had this same issue a 2 weeks ago.

For minor things where you don't need to get output (as variants) from the functions, you can connect to an open session like this:

from win32com.client import Dispatch
femap = Dispatch("femap.model")
femap.feAppMessage(0, "Python has attached to FEMAP")

Otherwise, the other method that I had to use was based on the one reference I could find on the net here: Link
First thing was to make a python file from the femap.tlb file. So...

import sys
from win32com.client import makepy
sys.argv = ["makepy", "-o PyFemap.py", r"C:\FEMAPv111\femap.tlb"]
makepy.main()

The new file is PyFemap.py and you import it like any other custom module. This allows you to call the functions get a list of the outputs like normal.

For clarity, as an example:

Python:
import pythoncom
import PyFemap
from pywintypes import IID

# Get existing FEMAP object
#   The CLSID comes from PyFemap
#
existingobj = pythoncom.connect(PyFemap.model.CLSID)

# Connect to Femap
femap = PyFemap.model(existingobj)
femap.feAppBringToTop(True, 0)
femap.feAppMessage(0, "Python connected FEMAP")

# Element Set
etype = 8
elementset = femap.feSet

rc = elementset.Select(etype, True, "Select Elements")
element = femap.feElem
numselected = elementset.Count()

dAvg = 0.
dAtC1 = 0.
dAtC2 = 0.
dAtC3 = 0.
dAtC4 = 0.

# loop the element set
for i in range(numselected):
    element.Get(elementset.Next())
    if element.ID != -1:
        # Please note that in the API the function is "Thickness", but
        #  in the PyFemap file from the type library file the command is "thickness"
        #  Note that capitalization.  class IElem, def thickness

        # If there is a return code, that is the first item in the return list
        rc, dAvg, dAtC1, dAtC2, dAtC3, dAtC4 = element.thickness()

        if rc == -1: # FE_OK
            print("Element {0} has average thickness of: {1}".format(element.ID,dAvg))
        elif rc == 8: # FE_BAD_TYPE
            print("Element {0} does not support a thickness".format(element.ID))
        elif rc == 6: # FE_NOT_AVAILABLE
            print("Element {0} does not reference a property".format(element.ID))

femap.feAppMessage(0, "Python has disconnected from FEMAP")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor