ANSA secondary development - two methods of drawing middle surface

Posted by skyturk on Fri, 18 Feb 2022 18:04:05 +0100

  ANSA is a very powerful and fast pre-processing software. As everyone who has used it knows, ANSA is particularly outstanding in geometric cleaning and model simplification. Moreover, ANSA also provides a secondary development interface based on python language, which greatly meets the needs of users. The basic idea behind the scripting language is to automate many repetitive and cumbersome programs with minimal user interaction, so the interface provided by ANSA meets this demand. It is believed that users who have drawn the shell mesh are familiar with the function of drawing the middle surface. However, it is also a manual and repetitive work. Next, we will introduce two methods of drawing the middle surface using ANSA secondary development.

(1) Use the Skin command to center the face.

First, let's introduce the required APIs (the following APIs are based on ansa.base Library):

  1. CollectEntities is used to collect entities. It returns a list of collected entities.

  2. GetEntityCardValues is used to obtain the values of pid, mid and other parameters from the "Card" shown in the figure below.

  its return value is a dictionary.

  1. CollectNewModelEntities collects newly created or imported model entities. It returns an object and must use the report() method to return a list to collect entities. The example code is as follows:

   the example code is as follows:

import ansa

from ansa importbase

def main():

collector = base.CollectNewModelEntities()

n = base.CreateEntity(ansa.constants.ABAQUS, 'NODE')

new_entities =

del collector


Note: there must be del collector here

  1. SetEntityCardValues sets or changes the value of the parameter in "Card".

  2. CheckAndFixGeometry checks and automatically cleans up the geometry. It returns a dictionary or None containing error information.

  3. In addition, there are auxiliary methods such as Or(),All(),ZoomAll(),Compress(').

  the following describes the program logic:

   our program is For an assembly, so there will be many parts, so we must use a cycle to take out each Part in turn For operation. First collect all entities, and then use the For loop to operate on each Part in turn. First use Or() and ZoomAll() to display a single entity, and then use CheckAndFixGeometry For automatic geometric cleaning. If there is no error, proceed to the next step (if there is an error, it indicates that automatic cleaning cannot be performed and manual cleaning is required). Next, we need to collect the entity 'FACE'. If the collected entity is not empty, we will Skin the collected 'FACE' to draw the middle FACE. Next, collect new entity objects according to "PSHELL". "PSHELL" is the "Card" as shown in the figure below

   then execute the report() method to get the new entity list. If the length of this list is not zero, get new PID information through GetEntityCardValues method, and then set property information through SetEntityCardValues method. Finally, use All() and ZoomAll() to display all entities, and then use Compress() to compress them.

  the complete code is as follows:

import ansa

from ansa importbase

from ansa importconstants

def Test():

all_parts =base.CollectEntities(constants.NASTRAN, None, "ANSAPART")



for part in all_parts:



vals_1 = ('Name', 'PID')

part_info =base.GetEntityCardValues(constants.NASTRAN, part, vals_1)




if ret==None:


if len(faces) != 0:

collector =base.CollectNewModelEntities(constants.NASTRAN, 'PSHELL')

num_shell_deions=base.Skin(apply_thickness=True,new_pid=True,offset_type=2,ok_to_offset=True,max_thickness=5.0,delete=True,entities= faces)



if len(new_entities) != 0:

vals_2 =('Name', 'PID' ,'T')

new_pid_info=base.GetEntityCardValues(constants.NASTRAN,new_entities[0], vals_2)


base.SetEntityCardValues(constants.NASTRAN,new_entities[0], vals_3)

del collector

print("num . "+str(i)+" :"+new_entities[0]._name)

i += 1




(2) Use Casting to center the face.

   I won't talk about it here. The method logic is basically not much different. If you don't know the API, you can view the help document. Directly on the code.

import ansa

from ansa import base

from ansa import constants

def mid_Casting():

all_pshell =base.CollectEntities(constants.NASTRAN, None, "PSHELL")


i = 0

for pshell inall_pshell:

print("pshell_name: "+pshell._name)

if pshell._name[0] != "S":

collectorPshell = base.CollectNewModelEntities(constants.NASTRAN,'PSHELL')

collectorShell = base.CollectNewModelEntities(constants.NASTRAN,"SHELL")

all_faces = base.CollectEntities(constants.NASTRAN,pshell,"FACE")

result = base.MidSurfAuto(faces = all_faces,









base.DeleteEntity(pshell, True)

new_Pshell =

print("The new pshell's length is : ", len(new_Pshell))

print("The new Pshell's name is :",new_Pshell[0]._name)

new_Shell =

print("The new Shell's number is :", len(new_Shell))

AnsaPartReference = base.GetEntity(constants.NASTRAN,"SHELL",new_Shell[0]._id )

print("The AnsaPartReference's id is :", AnsaPartReference)

CurrentPart = base.GetEntityPart(AnsaPartReference)

CurrentPartName = base.GetEntityCardValues(constants.NASTRAN, CurrentPart,("Name",))

print("The CurrentPartName is :",CurrentPartName["Name"])

base.SetEntityCardValues(constants.NASTRAN, new_Pshell[0],{"Name": "C"+CurrentPartName["Name"]})

del collectorPshell

del collectorShell

Reprinted from:
Original author: finite element Online
Published on: July 17, 2017

Scan the bottom two dimensional code, pay attention to my WeChat official account - CAE software two development Lab, read more wonderful content!

CAE software secondary development Lab