Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting a for loop over instances of unknown number 1

Status
Not open for further replies.

Martensite_Steel

Civil/Environmental
Feb 8, 2019
8
0
0
DE
Hello everyone,

although I'm getting a little bit better with the whole python and scripting thing in general, I'm still far from an expert and struggling with some things. [sad]

In my assembly, I have several lateral reinforcement bars. They are applied via a linear pattern in 3-direction and the total number of instances is defined in the variable x. For an embedded constraint, I want to put all the lateral reinforcement into the set "Reinforcement". Since the total amount of bars can vary, I'd like to put the instances into a set automatically with a for-loop. But I have absolutely no idea how to do this and this drives me crazy.

At first, I thought about something like this: (I know, that the variable 'cells' will overwrite itself for every pass through the loop, so this cannot be the right solution. Somehow it should be possible to append i to the variable 'cells', so that it goes: cells1=… , cells2=… )

Python:
for i in range(x+1):
[indent]cells=MdB.models['Model-1'].rootAssembly.instances['Reinforcement-'+str(i)].getByBoundingCylinder((0.0,0.0,0.0),(0.0,0.0,l),2*l)
[/indent]


I guess, it would be best, if every reinforcement bar was put into its own set, so that they can be put all together afterwards via MdB.models['Model-1'].rootAssembly. Set(cells=cells1+cells2+ … cellsx, name='Reinforcement')


I'm pretty sure, my whole idea is way too complicated. But it's all I got… I keep searching for a solution, but if someone could give me a little push in the right direction, it would be greatly appreciated.





Reinforcement_pa9k4k.png
 
Replies continue below

Recommended for you

You might create the set at part level. Then each instance of that part as that set and you just need to merge the sets.

Otherwise my example below shows how it can be done at assembly level. In my case I take all instances, but it should be easy to check the name or another property of the current instance and then use it or skip it.

Code:
from abaqus import *
from abaqusConstants import *
from caeModules import *

a = mdb.models['Model-1'].rootAssembly

i=0
for x in a.instances.keys():
	i=i+1
	c1 = a.instances[x].cells
	k=len(c1)
	cells1 = c1[0:k]
	a.Set(cells=cells1, name='Set-'+str(i))

mysets = a.sets.keys()
listsets=[]
for x in mysets:
	listsets.append(a.sets[str(x)])

a.SetByBoolean(name='Set-all', sets=listsets)
 
Thank you Mustaine3 and FEAway, for your answers!

@FEAway, it looks like there was a misunderstanding. I'm using the approach with a BoundingCylinder instead of your proposed BoundingBox. The BoundingCylinder has the length l, so it extends with the length of the model and all instances are inside it. However, Abaqus wants me to define the instances that will be affected by the getitem-command. And that's where I'd need the for-loop.

@Mustaine3: That's definitely a more elegant solution, than I got. Just for the record, I got it to work with a code like this:

Python:
cells_sum = cells.Reinforcement1  #In this variable, the cells of the first instance are saved
[indent]for i in range(2,x+1):
	cells_add = a.instances['Reinforcement-1-lin-'+str(i)+'-1'].cells.getByBoundingCylinder((0.0,0.0,0.0),(0.0,0.0,l),2*l)
	cells_sum += cells_add
[/indent]	
mdb.models['Model-1'].rootAssembly.Set(cells=cells_sum, name='Reinforcement')

I've built several models with it and didn't find any problems with "my" code. However, I'm going to change it to your presented option!

Thanks to both of you! [smile]
 
Status
Not open for further replies.
Back
Top