This is an Abaqus application of a Python scripting but still a general question that doesn't require Abaqus knowledge.
Im no programmer and lack this necessary kind of thinking so would be very grateful for a help.
I have a database containing something called step names. I want to create an entity, which is a step too (a new step). If such name exists already, I want the script to add +1 to the name. If that one exists, I want it to add another +1 and so on.
Nothing important so far, just importing:
# importing some libraries
from abaqus import *
from odbAccess import *
from abaqusConstants import *
import visualization
import fileinput
import os
from odbAccess import *
from textRepr import *
Now we open the file:
odb_path="analysis6.odb"
my_odb=session.openOdb(name=odb_path,readOnly=FALSE)
# based upon our knowledge of the database, will be automated later
lastStep="loading step"
Lets automate some name-sourcing, still not that important for understanding the problem:
NaseInstance = (my_odb.rootAssembly.instances.keys())
MojeInstance = ( NaseInstance[-1] )
CelaMojeInstance=my_odb.rootAssembly.instances[MojeInstance]
Now the crucial part:
# initial number = zero
MyStepNumber=0
# This is how we want our step to be called
MyStepName="Artifficial_Step"
# if such name is there already, add one
if MyStepName in my_odb.steps.keys():
MyStepNumber=MyStepNumber+1
MyStepName=MyStepName+str(MyStepNumber)
print ( "it is there, now we will rather call it", MyStepName, "Now you have to reopen the odb to see it")
# this is how we stuff data into this new step. Not important for this query
artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])
# save and close to make the changes work
my_odb.save()
my_odb.close()
else:
# if such name doesnt exist yet, no problem to use it
print ( "it is not there, no problem to call it", MyStepName, "Now you have to reopen the odb")
# rest same as in the -if- statement
artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])
# save and close to make the changes work
my_odb.save()
my_odb.close()
Unfortunately, as these of you with any coding knowledge probably immediately see, if the name is not yet used, it creates the new step for the first time, calling it "Artifficial_Step"
. If it exists and the else
statement comes to action, it says "....we will rather call it "Artifficial_Step1"
but then doesn't create anything new (probably as such step is already there). Instead of checking whether Artifficial_Step1
doesn't already exist too, or Step2
, Step3
, Step4
...
I guess that I need some loop to go through the names - my_odb.steps.keys()
- and keep adding one, until it reaches a nonexistent one to create it later. May I ask for a help with such, please?
Edit: Tried this now:
MyStepNumber=int(0)
MyStepName="Artifficial_Step"
while MyStepName in my_odb.steps.keys():
MyStepNumber=MyStepNumber+1
MyStepName=MyStepName+str(MyStepNumber)
print ( "it is there, now we will rather call it", MyStepName, "Now you have to reopen the odb")
artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])
# save and close to make the changes work
my_odb.save()
my_odb.close()
else:
print ( "it is not there, no problem to call it", MyStepName, "Now you have to reopen the odb")
artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])
# save and close to make the changes work
my_odb.save()
my_odb.close()
But the while
goes on even after closing the database, therefore issuing a message that the database doesn't exist (meaning in the current GUI session) any more.
Moreover it created Artifficial_Step
, then after reruning it created Artifficial_Step1
and then (after closing and reruning) nothing more.
# create a new step, frame, field. If it exists, add number:
MyStepNumber=int(1)
MyStepName="Artifficial_Step"+str(MyStepNumber)
while MyStepName in my_odb.steps.keys():
MyStepNumber=MyStepNumber+1
MyStepName="Artifficial_Step"+str(MyStepNumber)
artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])
# save and close to make the changes work
my_odb.save()
my_odb.close()