• No results found

Example - Compare replay with a well written script 83

In document PythonScriptsForAbaqus_BookPreview (Page 93-98)

Replay files, Macros and IDEs

of 20. Click O or Fillet too

6.3 Example - Compare replay with a well written script 83

The remaining statements are the meat of the script. They rename the model, draw the sketch and create the part, and fillet it. However they are written in a very literal sense.

For example, the ObliqueDimensions() command is used to dimension the edges of the rectangle. When you are using a script you are more likely to enter in the exact coordinates in the rectangle() method as point1 and point2 as we did in the cantilever beam example.

In addition the statements dealing with the edge round

e = p.edges 

p.Round(radius=1.0, edgeList=(e[4], )) 

appear to assign all the edges of the block to a variable ‘e’, and then Abaqus refers to the desired edge as e[4] which makes sense to it internally as it stores each of the Edge objects in a certain order; but this does not make any sense to a human.

Here is what this same script would look like if I wrote it.

# ******************************************************************************** 

# Create a block with a rounded edge   

# Created for the book "Python Scripts for Abaqus ‐ Learn by Example" 

# Author: Gautam Puri 

# ******************************************************************************** 

 

from abaqus import * 

from abaqusConstants import *   

# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ 

# Create the model (or more accurately, rename the existing one)   

mdb.models.changeKey(fromName='Model‐1', toName='Block Model')  blockModel = mdb.models['Block Model'] 

 

session.viewports['Viewport: 1'].setValues(displayedObject=None)   

# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ 

# Create the part   

import sketch  import part   

# a) Sketch the block cross section using the rectangle tool 

blockProfileSketch = blockModel.ConstrainedSketch(name='Block CS Profile',         sheetSize=200) 

blockProfileSketch.rectangle(point1=(0.0,0.0), point2=(25.0,15.0))   

84 Replay files, Macros and IDEs

# b) Create a 3D deformable part named "Block" by extruding the sketch  blockPart=blockModel.Part(name='Block', dimensionality=THREE_D,  type=DEFORMABLE_BODY) 

blockPart.BaseSolidExtrude(sketch=blockProfileSketch, depth=20)   

# ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ 

# Round the edge   

edge_for_round = blockPart.edges.findAt((12.5, 15.0, 20.0), )  blockPart.Round(radius=1.0, edgeList=(edge_for_round, )) 

The first thing you notice is how much more readable this script is. Secondly (and more importantly), we do not refer to internal edge or vertex lists. The statements for rounding the edge are

edge_for_round = blockPart.edges.findAt((12.5, 15.0, 20.0), )  blockPart.Round(radius=1.0, edgeList=(edge_for_round, )) 

The findAt() method refers to coordinates that we can visualize by scribbling the block on a piece of paper. If you decided you wanted to round another edge in a second iteration of the analysis, you could change the coordinates right here and rerun the script.

The replay file script on the other hand cannot be modified, since you wouldn’t know what to change e[4] to since we do not know the sequence of Abaqus’s internal edge list.

So you see that the replay file is useful only if you want to exactly replay what was done in Abaqus. However it requires some work to modify it for any other use. As it gets longer it will require too many major changes to be worth the effort.

However having a replay file helps you write your own script. You can see that the major methods used were the same in the replay script and the one I wrote. These include changeKey(), ConstrainedSketch(), rectangle(), BaseSolidExtrude() and Round(). By performing a task in Abaqus/CAE and looking at the replay file we very quickly know the names of the methods that need to be used and what arguments they require. While it is easy to remember a name like Round(), you are unlikely to remember the names of the thousands of other methods available through the Abaqus Scripting Interface. The replay file will tell you at a glance the names of the methods you need, and you can then look these up in the Abaqus Scripting Reference Manual to understand and use them.

Note also that my code is very similar to that used in the Cantilever Beam example. I have infact copied and pasted that code here, and modified it using some help from the replay file. The fastest way to write Python scripts is to reuse code where possible,

modify it starts at th Abaqus/C Windows the other logged, an it later fro except yo find the fe Macros ar

t suitably, an task in Abaqu s when deali tion (after a fe acros

re similar to he beginning CAE. In addi Explorer oth hand allow nd when it sh om within Ab ou won’t have

ew you need.

re stored in a with the name monstrate this:

aqus/CAE (o r.

he Macro Ma Create. You

nd find out w us/CAE and r

ing with out ew hundred p

replay files.

of your Abaq tion it can o herwise it will you to defin hould stop. In baqus. The sta e to search th

file called ‘a e you assign t :

or open a ne

anager dialog see the Creat

what new m eading the re tput database pages) and tea

The differen qus session an only be saved

l get overwrit ne at what po addition you atements in it hrough hundre

abaqusMacros to the macro.

ew model in

g box as show te Macro dia

methods are r play file. The s, but we’ll ach you what y

nce between nd is continuo

d by making tten during y oint the repla u can give the

our next sess ay data shou e replay data a

ame as those of other repla

s stores each

AE). Go to

ure.

.4 Macros

performing t you can’t real B object mod

know then.

the replay f d until you clo

the .rpy file sion. Macros uld start getti a name and c in the .rpy fi ay statements

macro within

File > Mac 85

86 Repla they will a Open ‘aba

ay files, Mac

a name for t ave a space in he function in he directory t ntinue.

egins recordin l the steps de nd round the BlockMacro a

appear here.

aqusMacros.p

cros and IDE

the macro suc n a macro nam n the abaqusM to Work so t

ng the macro scribed in the edge. Then cl appear in the l

py’ in the wor

ch as BlockM me. This is be Macros.py file hat the macro

.

e previous sec lick Stop Rec list in the Ma

rk directory. H

ort lines 

as dgm 

Macro. It ne ecause the na e and function o is saved in

ction to renam cording.

acro Manager.

Here’s what t

eeds to be on ame of the ma

n names cann the Abaqus w

me the model,

. As you crea

the contents w

ne word as y acro will be t not have space

work director

, create the pa

ate more macr

will look like:

ou

6.4 Macros 87

    import interaction      import load      import mesh      import job      import sketch      import visualization      import xyPlot 

    import displayGroupOdbToolset as dgo      import connectorBehavior 

    mdb.models.changeKey(fromName='Model‐1', toName='Block Model')      session.viewports['Viewport: 1'].setValues(displayedObject=None)      s1 = mdb.models['Block Model'].ConstrainedSketch(name='__profile__',           sheetSize=200.0) 

    g, v, d, c = s1.geometry, s1.vertices, s1.dimensions, s1.constraints      s1.setPrimaryObject(option=STANDALONE) 

    s1.rectangle(point1=(0.0, 0.0), point2=(22.5, 13.75)) 

    s1.ObliqueDimension(vertex1=v[3], vertex2=v[0], textPoint=(16.4174423217773,           ‐4.17431116104126), value=25.0) 

    s1.ObliqueDimension(vertex1=v[0], vertex2=v[1], textPoint=(‐5.90002059936523,           7.25688123703003), value=15.0) 

    p = mdb.models['Block Model'].Part(name='Block', dimensionality=THREE_D,           type=DEFORMABLE_BODY) 

    p = mdb.models['Block Model'].parts['Block'] 

    p.BaseSolidExtrude(sketch=s1, depth=20.0)      s1.unsetPrimaryObject() 

    p = mdb.models['Block Model'].parts['Block'] 

    session.viewports['Viewport: 1'].setValues(displayedObject=p)      del mdb.models['Block Model'].sketches['__profile__'] 

    p = mdb.models['Block Model'].parts['Block'] 

    e1 = p.edges 

    p.Round(radius=1.0, edgeList=(e1[4], )) 

You notice that the name of our macro ‘BlockMacro’ is the name of the function (indicated by the def keyword). In addition there are a number of import statements to import all modules that might be required by almost any script. Other than that the statements are the same as the ones in the replay file. Essentially what Abaqus has done is given you the statements of the replay file that were written while the macro was recording.

You can run an existing macro from the Macro Manager by choosing it from the list and clicking Run. In our case this will only work in a new model because we rename ‘Model-1’ to ‘Block Model’. (If no ‘Model-‘Model-1’ is present then you will get an error.) If you’d used the macro to do something like create a material, you could then run the macro inside any instance of Abaqus and it would create that material for you again.

88 Replay files, Macros and IDEs

You can see how macros help you perform a repetitive task without actually writing a single Python statement yourself. The added advantage is that users of Abaqus Student Edition can use this in place of the replay file which they do not have access to. In fact even if you’re using the Research or Commercial editions of Abaqus, you may prefer to create a macro of a task you are trying to script in order to see which commands Abaqus/CAE uses as opposed to reading the replay file which will include everything from the moment your Abaqus session began.

In document PythonScriptsForAbaqus_BookPreview (Page 93-98)