Cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

Using feElem.PutAllArray in python to place create elements

Experimenter
Experimenter

 

I am trying to place nodes and elements in femap using python. I have a working, slow approach using Put for the nodes. I'm trying to use PutAllArray to place my elements but cannot get it to work. This code returns a Bad Type error. I'm not sure how to format my inputs in python in an acceptable manner for Femap. I've truncated my element data for testing.

feElem = App.feElem
rc = feElem.PutAllArray(numElem, elIdTest,0,25,8,1,0,0,0,0,0,0,0,nodesTest,0,0)

I've tried passing in lists, tuples, numpy arrays containing floats and ints. I'm at a loss.

 

Working Node Placement Code:

try:
existObj = pythoncom.connect(Pyfemap.model.CLSID) # Grabs active model
App = Pyfemap.model(existObj)
except:
# Exits program if there is no active femap model
sys.exit("Femap is not open")
else:
App.feAppUndoCheckpoint('Undo {}'.format(sys.argv[0]))
App.feAppMessage(0, "Python API Started: + {}".format(sys.argv[0]))
fe_model_app = App.feAppGetModel()
fe_model_name = App.feAppGetModelName(fe_model_app[1])
App.feAppBringToTop(True, 0)
feNode = App.feNode
nodes =[[0.0000000e+00, -8.7000000e+01, -9.6000000e+01],[-1.4802974e-15, -8.7000000e+01, -9.5000000e+01],[ 2.9605947e-16, -8.7000000e+01, -9.4000000e+01],[-9.0000000e+00, -8.7000000e+01, -9.5000000e+01],[-8.0000000e+00, -8.7000000e+01, -9.5000000e+01],[-5.0000000e+00, -8.7000000e+01, -9.5000000e+01],[-1.0000000e+01, -8.7000000e+01, -9.4000000e+01],[-8.0000000e+00, -8.7000000e+01, -9.4000000e+01],[-7.0000000e+00, -8.7000000e+01, -9.4000000e+01],[-3.0000000e+00, -8.7000000e+01, -9.4000000e+01],[-2.0000000e+00, -8.7000000e+01, -9.4000000e+01],[-1.0000000e+01, -8.7000000e+01, -9.3000000e+01],[-3.0000000e+00, -8.7000000e+01, -9.3000000e+01],[-1.0000000e+00, -8.9000000e+01, -9.5000000e+01],[-1.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-1.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-1.0000000e+00, -8.8000000e+01, -9.4000000e+01],[-1.0000000e+00, -9.0000000e+01, -9.3000000e+01],[-6.0000000e+00, -8.7000000e+01, -9.4000000e+01],[-3.0000000e+00, -8.9000000e+01, -9.5000000e+01],[-2.0000000e+00, -8.9000000e+01, -9.5000000e+01],[-9.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-8.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-4.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-3.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-2.0000000e+00, -8.8000000e+01, -9.5000000e+01],[-9.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-8.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-7.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-6.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-3.0000000e+00, -9.0000000e+01, -9.4000000e+01],[-8.0000000e+00, -8.9000000e+01, -9.4000000e+01],[-7.0000000e+00, -8.9000000e+01, -9.4000000e+01],[-6.0000000e+00, -8.9000000e+01, -9.4000000e+01],[-5.0000000e+00, -8.9000000e+01, -9.4000000e+01],[-2.0000000e+00, -8.9000000e+01, -9.4000000e+01],[-1.0000000e+01, -8.8000000e+01, -9.4000000e+01],[-8.0000000e+00, -8.8000000e+01, -9.4000000e+01],[-7.0000000e+00, -8.8000000e+01, -9.4000000e+01],[-3.0000000e+00, -8.8000000e+01, -9.4000000e+01],[-2.0000000e+00, -8.8000000e+01, -9.4000000e+01],[-8.0000000e+00, -9.2000000e+01, -9.3000000e+01],[-8.0000000e+00, -9.0000000e+01, -9.3000000e+01],[-7.0000000e+00, -9.0000000e+01, -9.3000000e+01],[-6.0000000e+00, -9.0000000e+01, -9.3000000e+01],[-5.0000000e+00, -9.0000000e+01, -9.3000000e+01],[-8.0000000e+00, -8.9000000e+01, -9.3000000e+01],[-7.0000000e+00, -8.9000000e+01, -9.3000000e+01],[-6.0000000e+00, -8.9000000e+01, -9.3000000e+01],[-5.0000000e+00, -8.9000000e+01, -9.3000000e+01]]
nodeIds = [[1274, 1279, 1284, 1428, 1429, 1430, 1432, 1434, 1435, 1437, 1438,1439, 1447, 1514, 1515, 1516, 1518, 1519, 1802, 1822, 1823, 1824,1825, 1826, 1827, 1828, 1829, 1830, 1831, 1832, 1833, 1836, 1837,1838, 1839, 1842, 1843, 1845, 1846, 1850, 1851, 1852, 1859, 1860,1861, 1862, 1867, 1868, 1869, 1870]] for i, line in enumerate(nodes): feNode.ID = nodeIds[i] feNode.x, feNode.y, feNode.z = line rc_put = feNode.Put(feNode.ID)

Element Code Test Data:

elIdTest = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nodesTest = [[1822, 1514, 1274, 1428, 1829, 1516, 1279, 1432, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1823, 1824, 1829, 1828, 1842, 1843, 1852, 1851, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1824, 1515, 1516, 1829, 1843, 1518, 1519, 1852, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1825, 1826, 1430, 1429, 1845, 1846, 1435, 1434, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1827, 1828, 1437, 1802, 1850, 1851, 1438, 1447, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1828, 1829, 1432, 1437, 1851, 1852, 1439, 1438, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1829, 1516, 1279, 1432, 1852, 1519, 1284, 1439, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1830, 1831, 1837, 1836, 1859, 1860, 1868, 1867, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1831, 1832, 1838, 1837, 1860, 1861, 1869, 1868, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0],
[1832, 1833, 1839, 1838, 1861, 1862, 1870, 1869, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0]]
numElem = len(nodesTest)

 

3 REPLIES 3

Re: Using feElem.PutAllArray in python to place create elements

Siemens Phenom Siemens Phenom
Siemens Phenom

Python is especially nice for methods like this with a high number of parameters.

 

I always like to start by using GetAllArray with a test model to understand how the parameters are populated. In the gif below, I take a sample plate mesh and use feNode.GetAllArray then move that mesh using feNode.PutAllArray. As long as you use the syntax such as that show below and populate everything correctly (i.e. 3 coordinates per node) it should work.

 

Python.gif

 

nSet = app.feSet
nSet.AddAll(7 ) #7 = FT_NODE
[rc, numNode, idArray, xyz, layer, color, nType, defCsys, outCsys, permBC] = feNode.GetAllArray(nSet.ID)

#Lets add 10 to all coordinates
newXYZ = [move+10 for move in xyz]

feNode.PutAllArray(numNode, idArray, newXYZ, layer, color, nType, defCsys, outCsys, permBC)

app.feViewRegenerate( 0 )

 

 

Re: Using feElem.PutAllArray in python to place create elements

Experimenter
Experimenter

I figured it out! The orienatation and offset vectors require floats, not ints. Here's my final working code

 

        try:
            existObj = pythoncom.connect(Pyfemap.model.CLSID)  # Grabs active model
            App = Pyfemap.model(existObj)
        except:
            # Exits program if there is no active femap model
            sys.exit("Femap is not open")
        else:
            App.feAppUndoCheckpoint('Undo {}'.format(sys.argv[0]))
            App.feAppMessage(0, "Python API Started: + {}".format(sys.argv[0]))
            fe_model_app = App.feAppGetModel()
            fe_model_name = App.feAppGetModelName(fe_model_app[1])
            App.feAppBringToTop(True, 0)
            timing.log('Femap queried and connected.')
        # Place Nodes in Femap. Works but slow

        timing.log('Placing nodes.', elapsed=True)
        App.feAppMessage(0, 'Placing nodes')
        feNode = App.feNode

        numNode = len(nastranGrid)
        nodeId = range(1,numNode+1)

        rc_put_node = feNode.PutCoordArray(numNode, nodeId, nastranGrid.ravel())
#        for i, line in enumerate(nastranGrid):
#
#            feNode.ID = i + 1
#            feNode.x, feNode.y, feNode.z = line
#
#            rc_put = feNode.Put(feNode.ID)

        App.feViewRegenerate(0)
        App.feViewAutoscaleVisible(0, True)
        App.feAppMessage(0, ' All nodes placed. ')
        timing.log('Nodes placed', elapsed = True)

        # elements creation here

        feElem = App.feElem
        elemData = nastranElemNodeId
        if ' 0 ' in elemData[1:]:
            App.feAppMessageBox(
                0, "Node with ID = 0. Code will not work correctly")

        else: # Place elements
            nodes_list = [row[1:] for row in elemData]
            numElem = len(elemData)
            elId = [line[0] for line in elemData]
            propId = tuple([0]*numElem)
            elemType = 25  # element type solid linear
            topology = 8  # topology(elemCount) Brick8
            layer = 1
            color = propId
            fmln = tuple([0] * 2)*numElem
            ot = tuple([0.0]*3*numElem)
            offset = tuple([0.0]*6*numElem)
            release = tuple([0] * 12)*numElem
            orientSet = propId
            orientId = propId
            connectType = fmln
            connectSeg = fmln

            nodes_resized = [tuple(line + [0] * (20-len(line))) for line in nodes_list]
            nodes_flattened = tuple(np.asarray([tuple(node) for node in nodes_resized], dtype = np.int_).ravel())
            rc_put_elem = feElem.PutAllArray(numElem, elId,0,elemType,topology,layer,color,fmln,ot,offset,release,orientSet,orientId,nodes_flattened,connectType,connectSeg)
            if rc_put_elem != -1:
                App.feAppMessage(0, 'Failed to create elements. Error num: ' + str(rc_put_elem))
            App.feViewRegenerate(0)
            App.feViewAutoscaleVisible(0, True)
            App.feAppMessage(0, 'All elements placed.')

Re: Using feElem.PutAllArray in python to place create elements

Experimenter
Experimenter
Thank you for your example and time. I'm not changing a pre-existing mesh, I'm actually importing it from OpenFoam, so I had to create the other inputs. I now have it working. I was in the middle of uploading my 2nd post when you submitted yours.