Home > Back-end >  Loop only prints last value
Loop only prints last value

Time:12-23

I have some working python code I am using in Abaqus, but I don't manage to make a loop out of it. Can someone help me with this rather simple problem? I would like to print x, y, z coordinates (called partxcord etc) for different SetsScrews. It only prints the last SetsScrews. What am I missing? Thank you in advance.

SetsScrews = ["Screw['T6_R']", "Screw['T7_R']", "Screw['T8_R']"]

for i in range(len(SetsScrews)):
   # Select nodeset
   PartLevel = mdb.models[modelname].rootAssembly.sets[SetsScrews[i]]
   print('ParTLEVEL=', PartLevel)

   # Calculate the amount of nodes in nodeset
   numNodes = len(PartLevel.nodes)

  # Create lists to write data to
  partlabel=[]
  partxcord=[]
  partycord=[]
  partzcord=[]

  for curNode in PartLevel.nodes:
        partlabel.append(curNode.label)
        partxcord.append(curNode.coordinates[0])
        partycord.append(curNode.coordinates[1])
        partzcord.append(curNode.coordinates[2])
        print('numNodes=', numNodes)

CodePudding user response:

partlabel=[]
partxcord=[]
partycord=[]
partzcord=[]

these should be intialized outside the loop, maybe make a dictionary with screwnames and then add these lists there

or your indentation could be an issue too

CodePudding user response:

If you want to just print the coordinates, then you just need print after inner loop ends. Also, you can store coordinates for all parts. Please refer below code:

SetsScrews = ["Screw['T6_R']", "Screw['T7_R']", "Screw['T8_R']"]
# create the list to save all coordinates
aLabels,axcoords,aycoords,azcoords = [],[],[],[]
for i in range(len(SetsScrews)):
    # Select nodeset
    PartLevel = mdb.models[modelname].rootAssembly.sets[SetsScrews[i]]
    print('ParTLEVEL=', PartLevel)
    
    # Calculate the amount of nodes in nodeset
    numNodes = len(PartLevel.nodes)

    # Create lists to write data to
    partlabel,partxcord,partycord,partzcord=[],[],[],[]
    print('Screw= ', SetsScrews[i])
    for curNode in PartLevel.nodes:
        partlabel.append(curNode.label)
        partxcord.append(curNode.coordinates[0])
        partycord.append(curNode.coordinates[1])
        partzcord.append(curNode.coordinates[2])
        # you can print it here...
        print(curNode.label,curNode.coordinates[0],curNode.coordinates[1],curNode.coordinates[2])
    
    # or you can print all data here and save it
    print('partlabel= ', partlabel)
    print('partxcord= ', partxcord)
    print('partycord= ', partycord)
    print('partzcord= ', partzcord)
    aLabels.append(partlabel)
    axcoords.append(partxcord)
    aycoords.append(partycord)
    azcoords.append(partzcord)

Now at the end of the both the for loops, you have all the coordinates in aLabels, axcoords, aycoords, azcoords these lists.

  • Related