×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

[femap api][python][run femap from python using python code]
2

[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):


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]

2
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:

CODE --> 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") 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

eBook - 10 Reasons to Choose CATIA on the Cloud
To compete in today’s fast-paced and competitive market, smaller and newer firms need a powerful platform that will enable them to compete with bigger players, without the heavy investments needed in computer hardware, software and personnel. Download Now
White Paper - Smart Manufacturing for Electronics
This white paper describes a transformative approach to electronics manufacturing made possible by the addition of Mentor Graphics to the Siemens family. It describes a completely digitalized strategy that supports both printed circuit board (PCB) and mechanical design and manufacturing, uniting the entire product lifecycle – from idea and production to customers and back. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close