今天分享一个自动创建压盘的小脚本,没有写完,但是想实现的功能都已经实现了,就差写一个窗口,输入压盘的半径,来任意创建大小不同的压盘(目前默认创建的是直径50mm的压盘)。
下面请看效果:
代码写的怎么样不重要,重要的是我们思考的过程,想法和逻辑是很重要的!这也是写代码的乐趣之一。
首先我们要明确我们要实现什么功能。我的想法是:
用鼠标选择我们需要放置压盘的网格单元上的点
通过点知道这个单元的厚度和pid
用得到的单元厚度和创建的压盘的单元厚度来得到我们生成压盘的位置,防止干涉。
用得到的pid和创建的压盘的pid来做surface2surface接触
在压盘上施加好载荷
想法有了,然后就是去找对应的功能的API。一步步搭建我们的代码,我也是自己试了很久,问了一些大佬,才写完的。完成之后还是很快乐的,虽然这只是一个人的快乐,所以还是要分享,才有意思。对,过年了还是要卷你们哈哈哈哈
下面就直接上代码吧:
import ansa
from ansa import constants
from ansa import base
from ansa import mesh
@ansa.session.defbutton('LOAD','yapan')
def yapan():
nodes = base.PickNodes(constants.NASTRAN, ('SHELL', ))
print(nodes)
elem_dicts = base.NodesToElements(nodes)
for i in elem_dicts.values():
for j in i:
ret = base.GetEntityCardValues(constants.NASTRAN, j, ('PID',))
for k in ret.values():
print(k)#get shell_pid
pshell= base.GetEntity(constants.NASTRAN, 'PSHELL', k)
T= pshell.get_entity_values(constants.NASTRAN, 'T')
T_off=T.get('T')
for node in nodes:
node_id = base.GetEntityCardValues(constants.NASTRAN, node, ('NID', 'X1', 'X2', 'X3'))
n_id = node_id.get('NID')
x1 = node_id.get('X1')
x2 = node_id.get('X2')
x3 = node_id.get('X3')
A = (0.0, x2+10, x3+T_off)
B = (x1+10.0, 0.0, x3+T_off)
# define center of circle
C = (x1, x2, x3+T_off)
# define radius of circle
r = 25
# D will contain the created circle's curve segments
# create circle with center C and radius r that lies on the plane defined by points A, B and C
D = base.CreateCircleCenter2PointsRadius(C, A, B, r)
print("Output:", D)
input = []
input.append(base.GetEntity(constants.LSDYNA, "CURVE", D[0]))
input.append(base.GetEntity(constants.LSDYNA, "CURVE", D[1]))
cset=base.CollectEntities(constants.LSDYNA,None, "SET")
if len(cset) ==0:
set_id=0
else:
set_id=cset[-1]._id
yapans=mesh.FillGapCoons(input, improve_result_zones=0, result_set_id=set_id+1,ret_ents=True)
mesh.CreateCircularMesh(0, only_circular=True, radius_tol=10, pattern='o-grid')
cpid=base.CollectEntities(constants.LSDYNA,None,'SECTION_SHELL')
print(cpid[0]._id)
if cpid[0]._id!=1:
pid=1
else:
pid=cpid[-1]._id
print(pid)
#create mat
cmid=base.CollectEntities(constants.LSDYNA,None,'__MATERIALS__')
if cmid[0]._id!=1:
mid=1
else:
mid=cmid[-1]._id+1
print(mid)
val={'MID':mid,'CON1':1,'CON2':110111,'DEFINED':'YES','CMO':1.0}
base.CreateEntity(constants.LSDYNA,'MAT20 MAT_RIGID',val)
ent=base.GetEntity(constants.LSDYNA, 'SECTION_SHELL', pid)
base.SetEntityCardValues(constants.LSDYNA,ent,{"MID":mid})
#LOAD
curves= base.CollectEntities(constants.LSDYNA, None, 'DEFINE_CURVE')
curve_num = len(curves)
curve1 = base.CreateLoadCurve("DEFINE_CURVE", {"Name": "356N","SFO": "1",'LCID':1+curve_num})
curve_val = ((0,0.),(0.1,0.05),(0.2,0.05),(0.3,1),(0.4,1),(0.6,0.05),(0.7,0.05))
base.SetLoadCurveData(curve1, curve_val)
val1={'by':'rigid','PID':pid,'LCID':1+curve_num,'SF':-356,'DOF':'3: Fz'}
base.CreateEntity(constants.LSDYNA,'LOAD',val1)
#CONTACT
vals={'TYPE':'AUTOMATIC_SURFACE_TO_SURFACE','SSTYP':'3: Part id','MSTYP':'3: Part id','SSID':k,'MSID':pid,'FS':0.12,'FD':0.12,'DC':1,'VDC':20,'OPTIONAL CARDS A,B,C,D,E':"A&B&C&D",'SOFT':2,"SBOPT":3,"DEPTH":5,"IGNORE":2,"SHLEDG":1}
base.CreateEntity(constants.LSDYNA,'CONTACT',vals)
本文完。
。