-1

I am new to coding in Python for Abaqus, and I am trying to create a for loop, where I can create X number of instances based on a part, I have made. Furthermore, I would like to update the naming of the X instances.

Currently, when I manually create four instances based on the part CenMid, the code looks like this

mdb.models[panelName].rootAssembly.Instance(dependent=ON, name='CenMid-1', 
    part=mdb.models[panelName].parts['CenMid'])
mdb.models[panelName].rootAssembly.Instance(dependent=ON, name='CenMid-2', 
    part=mdb.models[panelName].parts['CenMid'])
mdb.models[panelName].rootAssembly.Instance(dependent=ON, name='CenMid-3', 
    part=mdb.models[panelName].parts['CenMid'])
mdb.models[panelName].rootAssembly.Instance(dependent=ON, name='CenMid-4', 
    part=mdb.models[panelName].parts['CenMid']) 

This is the code, I would like to transform to a loop, so that I simply can choose X instances and X instances will be created with the respective name, instead of this "hard coding" solution.

Thank you in advance!

  • Here's a control flow tutorial, it covers for and while loops: https://docs.python.org/3/tutorial/controlflow.html – Mike Scotty Mar 04 '21 at 15:09
  • Thank you for that, however my concern is more regarding how I implement the showed code as a function, where the number of instances and the name of it is changed? – Mette Friis Mar 05 '21 at 07:10
  • Look also for the built-in `range` method (https://docs.python.org/3/library/functions.html#func-range) and methods for the string formatting (https://docs.python.org/3/tutorial/inputoutput.html) – Roman Zh. Mar 05 '21 at 08:29
  • Thank you. I've only managed to create the range loop (where nMz is the number of parts in the length direction). However the next part, the function itself, I am having a hard time coding. `for n in range(1,nMz+1):` – Mette Friis Mar 05 '21 at 08:57

1 Answers1

0

You can either use a for loop with range und adjust the names accordingly or use the names from a list of either by for loop created names or, userdefined names.

Here is a for loop approach

pt_name = 'CenMid'
r_A = mdb.models[panelName].rootAssembly # root assembly
inst_basename = 'CenMid-'
inst_numbers = 5 # number of created instances plus 1

for inst_num in range(1,inst_numbers):
    i_name = inst_basename + str(inst_num) # instance name
    
    r_A.Instance(dependent=ON, name=i_name, 
    part=mdb.models[panelName].parts[pt_name])
Nic
  • 56
  • 6