[femap api][python][run femap from python using python code]
[femap api][python][run femap from python using python code]
(OP)
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):
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 --> vba
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
RE: [femap api][python][run femap from python using python code]
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:
CODE --> Python