Beta_script_to_python_conversion_guide (1)
Beta_script_to_python_conversion_guide (1)
Conversion Guide
ANSA Application Programming Interface
Introduction
Beginning with ANSA and META v14.0 the Python programming language is embedded in
both programs.
ANSA and META scripting is now Python scripting.
The old BETA Script language is still supported and will be supported for as long as
possible.
Users are recommended to switch to Python Scripting as soon as possible, since Python
is now the main scripting language.
Python v3.3 is embedded in ANSA and META. For additional help in Python language
features and functionality please refer to:
• python.org
• stackoverflow.com
or simply do a Google search with your python related query
Documentation on the ANSA and META libraries of functions are available in the online
script help of each program and on the respective scripting guides of their installation
directory
complex c = 4 + 3j
bool b = True
Integer division
In Python (version 3) all divisions, by default, are floating point divisions. Thus
3/2 = 1.5
In order to perform integer division the use of // is needed. i.e 3//2 = 1
Strings behaviour is similar in both languages. A few differences are shown below:
s = „Earth‟
String ranges are requested with s[0:4] syntax. This range is closed on the left side
and open on the right side [0,4). Thus printing this in python we get:
print(s[0:4])
>>„Eart‟ #print result
In a string concatenation, non string variables need to be converted to a string
before they can be concatenated
x = 4.5
y = „Earth‟
z = y+str(4.5)
Strings can be created with both single and double quotes
x = „Earth‟
y = “Earth”
The way new member are added in a dictionary is similar for both languages
BETA Sript myMap = CreateMap();
myMap[“planet”] = “earth”;
myMap[“circum”] = 40075;
Set
A set is a non-homogeneous un-ordered mutable sequence. All the members of a
set are unique. There are no duplicate values.
mySet = {1, 2.3, „world‟}
if „earth‟ in myList
if „pid‟ in myDict
Deletion of data
The del statement is used to delete references
del data[k]
del data[i:j]
del obj.attr
del variable
BETA Script to Python Conversion Guide 9
BETA Script to Python
General coding Syntax
The most noticeable difference between BETA Script and Python is the absence of
curly braces ({}) in python. New blocks in python are defined by (:) followed by
indentation in the next line
Dictionary iteration:
BETA Script
my_map = CreateMap();
…
for (ret=GetFirstNode(my_map,key,data);ret;ret=GetNextNode(my_map,key,data)){
Print(key);
Print(data);
}
Python
my_dict = {„Name‟:‟aluminum‟, „pid‟:19987, „thick‟:1.83}
for key,val in my_dict.items():
print(key)
print(val)
import math
import os
def main():
x = math.sqrt(9)
print(„The square root is: ‟,x)
os.stdout.write(„The square root of 9 is: ‟+str(x))
In python exceptions are handled with try and except statements. Any code that
runs inside the try block and raises an exception, the exception can be handled in
the except block.
a=1
b=0
try:
res = a/b
except ZeroDivisionError:
print('Division by zero.')
To split a comma or semi-colon separated line into its components, you can use
the line.split(‘;’) method which is the equivalent to BETA Script’s Tokenize Function
The equivalent to MatchString function in Python is the line.find(‘my_string’)
method
Opening an existing file with the option “w” in open will overwrite its contents.
To add lines at the end of an existing file, you can open it with the option “a”
(append), i.e. open (f, “a”)
and methods:
_isDefunct()
returns true the referenced ANSA entity has been destroyed
_ansaType(deck)
returns the ANSA type of an entity in the specific deck
import ansa
from ansa import base
from ansa import morph
from ansa import constants
def test_fun():
mp1 = base.GetEntity(constants.LSDYNA, „MORPHPOINT‟, 1)
mp2 = base.GetEntity(constants.LSDYNA, „MORPHPOINT‟, 2)
m_points = [mp1,mp2]
me1 = base.GetEntity(constants.LSDYNA, „MORPHEDGE‟, 1)
me2 = base.GetEntity(constants.LSDYNA, „MORPHEDGE‟, 2)
m_edges = [me1, me2]
morph.MorphMove(m_points, m_edges, 1, 100)
BETA Script
def main()
{
ents = CollectEntities(LSDYNA, 0, “SECTION_SHELL”, “recursive”, “YES”);
}
Python
import ansa
from ansa import base
from ansa import constants
def main():
ents = base.CollectEntities(constants.LSDYNA, None, „SECTION_SHELL‟, recursive=True)
Named arguments can be used for the optional arguments such as “recursive”.
Booleans replace the “YES”/”NO” strings
None is now used to denote a none container
Python
import ansa
from ansa import base
from ansa import constants
def main():
fields = [„T1‟,‟NIP‟,‟MID‟]
vals = base.GetEntityCardValues(constants.LSDYNA, ent, fields)
print(„The Thickness is:‟, vals[„T1‟])
A list with the fields that we want to extract values from is given
The function return a Dictionary with keys the field tags given in the list
The values of the Dictionary is the same with the field’s value type. A float will return as a float, a
strings as a string etc
BETA Script to Python Conversion Guide 28
BETA Script to Python
Common functions differences
SetEntityCardValues
BETA Script
def main()
{
SetEntityCardValues(LSDYNA, ent, “T1”, thick, “NIP”, nip, “MID”, mid);
}
Python
import ansa
from ansa import base
from ansa import constants
def main():
fields = {„T1‟:1.2,‟NIP‟:2,‟MID‟:1200}
base.SetEntityCardValues(constants.LSDYNA, ent, fields)
A Dictionary with keys the card’s field tags and values the new desired values is given
BETA Script
def main()
{
CreateEntityVa(LSDYNA, “SECTION_SHELL”, “Name”, “Test Prop”, “T1”, “1.2”);
}
Python
import ansa
from ansa import base
from ansa import constants
def main():
fields = {„Name‟:‟Test Prop‟,‟T1‟:1.2}
base.CreateEntity(constants.LSDYNA, „SECTION_SHELL‟, fields)
def main():
ent = base.GetEntity(constants.LSDYNA, “SECTION_SHELL”, 1)
print(„The name is: ‟,ent._name)
print(„The id is: ‟, ent._id)
In addition the type of entity can be retrieved by calling the object method _ansaType().
This does the same job as the GetEntityType() function.
ent._ansaType(constants.LSDYNA)
Script buttons are automatically created with the use of a special decorator. On top of
the programs main function the following syntax can be used
Decorator Description
@ansa.session.defbutton The function name is the button name placed
in the default button group
@ansa.session.defbutton(‘Button Group’) The function name is the button name placed
in the specified button group
@ansa.session.defbutton(‘Button Group’, Button name and group is specified
‘Function Name’)
import ansa
def main()
{
part p;
model_id = 0;
part_type = PSHELL;
collected_parts = PartsByType(model_id, part_type);
foreach p in collected_parts {
Print(p.id);
}
}
Python
import meta
from meta import parts
from meta import constants
def main():
model_id = 0
part_type = constants.PSHELL
collected_parts = parts.PartsByType(model_id, part_type)
for p in collected_parts:
print(p.id,p)
Python
import ansa
from ansa import guitk
def win():
TopWindow = guitk.BCWindowCreate(„My Window‟, guitk.constants.BCOnExitDestroy)
…
guitk.BCButtonSetClickedFunction(BCPushButton_1, my_callback_function, my_data)