abaqus class method agent for pyCharm
借用 pyCharm 的自动补全及提示功能,在 pyCharm 中进行Abaqus 二次开发。
通过编写下面的脚本,在Abaqus CAE 或 Abaqus python 运行后,将生成代理模块,将其复制到
C:\ProgramData\Anaconda2\Lib\site-packages 即可。
注意:在pycharm 中只能语法提示,不能直接调试。
优点:无需添加额外配置代码
缺点:代理模块 污染 site-packages 目录,不过可用生成的 deleteAbaqusAgent.bat 删除
资源地址:源码及生成的代理文件
脚本源码如下:
# -*- coding:utf-8 -*-
'''
abaqus class method agent for pyCharm
1. run this file in abaqus cae
2. copy the files in abaqusAgent folder to xxx/Lib/site-packages
3. enjoy!
by alienwxy 2020-04-27 alienwxy@126.com
Usage:
from abaqus import *
from abaqusConstants import *
import mesh
'''
import types,os
import abaqusConstants
def getMethodObjectList():
import docstringLookup as dlk
methodDict = {}
objectDict = {}
#get diction
for name in dir(dlk):
module = getattr(dlk,name)
if type(module) is types.ModuleType:
if hasattr(module,'objectDocstringTable'):
dic = getattr(module,'objectDocstringTable')
objectDict.update(dic)
if hasattr(module,'methodDocstringTable'):
dic = getattr(module,'methodDocstringTable')
methodDict.update(dic)
methodList = [(k,v) for k,v in methodDict.items()]
objectList = [(k,v) for k,v in objectDict.items()]
methodList.sort()
objectList.sort()
return methodList,objectList
def write2Txt(methodList,objectList,subDir='abaqusAgent'):
if not os.path.exists(subDir):
os.mkdir(subDir)
with file(os.path.join(subDir,'abaqusMethodList.txt'),'w') as fp:
for k,v in methodList:
fp.write('%-60s\t%s\n' % (k,v))
with file(os.path.join(subDir,'abaqusObjectList.txt'),'w') as fp:
for k,v in objectList:
fp.write('%-60s\t%s\n' % (k,v))
def getMethodArgs(argstr):
# 'name, edges <, startPoint, flipDirection, pointCreationMethod>'
# 'stepName <, loadCaseNames, magnitude, amplitude>'
# 'stepName <, allowGrowth>'
# '<useCut, printResults>'
# '<useCut>'
# 'center, radius'
# 'mask'
# 'keyword argument not implemented'
# ''
argstr = argstr.strip()
if argstr == '' or 'not implemented' in argstr:
return ''
if '<' not in argstr:
return argstr
#optional
if argstr.startswith('<'):
optargs = argstr.lstrip('<').rstrip('>').strip()
return ', '.join(['%s=None' % (arg.strip(),) for arg in optargs.split(',')])
keyargs,optargs = argstr.split('<,')
keyargs = keyargs.strip()
optargs = optargs.strip().rstrip('>').strip()
if ',' in optargs: #multi
optargs = ', '.join(['%s=None' % (arg.strip(),) for arg in optargs.split(',')])
else:
optargs = '%s=None' % optargs
return '%s, %s' % (keyargs,optargs)
def selfMethodArgs(argstr):
s = getMethodArgs(argstr)
if s:
return 'self, ' + s
else:
return 'self'
def write2Python(methodList,subDir='abaqusAgent'):
# XYPlot.previous
# sys.modules['odbAccess'].openOdb odbAccess 的函数
# Abaqus.Mdb abaqus 模块函数
# Odb.Part odbAccess 模块 的类 以Odb开头
# OdbStep.setDefaultField
modules = {} #moduleName: [ [functionName,functionArgs] ]
abaqusFunctions = [] #[ [functionName,functionArgs]]
odbAccesFunctions = [] #[ [functionName,functionArgs]]
abaqusConstantsFunctions = []#[ [functionName,functionArgs]]
odbAccesClasses = {} #className: [[methodName,methodArgs]]
classes = {} #className: [[methodName,methodArgs]]
for name,lst in methodList:
if 'modules' in name:
if 'odbAccess' in name:
odbAccesFunctions.append([name.split('.')[-1], getMethodArgs(lst[0])])
elif 'abaqusConstants' in name:
abaqusConstantsFunctions.append([name.split('.')[-1], getMethodArgs(lst[0])])
else:
modName = name.split("'")[1]
if modName not in modules:
modules[modName] = []
modules[modName].append([name.split('.')[-1], getMethodArgs(lst[0])])
elif name.startswith('Odb'): #odbAccesClasses
clsName,methodName = name.split('.')
if clsName not in odbAccesClasses:
odbAccesClasses[clsName] = []
odbAccesClasses[clsName].append([methodName, selfMethodArgs(lst[0])])
elif name.startswith('Abaqus'): #abaqusFunctions
funcName = name.split('.')[-1]
if funcName != 'Mdb': #Mbd('<pathName>')
abaqusFunctions.append([name.split('.')[-1], getMethodArgs(lst[0])])
else:
clsName,methodName = name.split('.')
if clsName not in classes:
classes[clsName] = []
classes[clsName].append([methodName, selfMethodArgs(lst[0])])
if not os.path.exists(subDir):
os.mkdir(subDir)
def writeAbaqusConstants():
with file(os.path.join(subDir,'abaqusConstants.py'),'w') as fp:
for fname,fargs in abaqusConstantsFunctions:
fp.write('def %s(%s):\n' % (fname,fargs))
fp.write(' pass\n')
for name in dir(abaqusConstants):
if name.isupper():
fp.write('%s = "%s"\n' % (name,name))
def writeOdbAccess():
with file(os.path.join(subDir,'odbAccess.py'),'w') as fp:
writeClasses(fp,odbAccesClasses)
for fn,fa in odbAccesFunctions:
writeFunction(fp,fn,fa)
def writeModules():
for module in modules.keys():
with file(os.path.join(subDir,module+'.py'),'w') as fp:
for n,a in modules[module]:
writeFunction(fp,n,a)
def writeAbaqus():
with file(os.path.join(subDir,'abaqus.py'),'w') as fp:
writeClasses(fp,classes)
writeClasses(fp,odbAccesClasses)
for n,a in abaqusFunctions:
writeFunction(fp,n,a)
fp.write('mdb = Mdb()\n')
fp.write('session = Session()\n')
writeAbaqusConstants()
writeOdbAccess()
writeModules()
writeAbaqus()
with file(os.path.join(subDir,'__init__.py'),'w') as fp:
fp.write('\n')
with file(os.path.join(subDir,'deleteAbaqusAgent.bat'),'w') as fp:
for name in modules.keys():
fp.write('del %s.py; %s.pyc\n' % (name,name))
fp.write('del abaqus.py;abaqus.pyc;abaqusConstants.py;abaqusConsta.pyc;odbAccess.py;odbAccess.pyc\n')
fp.write('del deleteAbaqusAgent.bat\n')
def writeFunction(fp,functionName,functionArgs):
if functionName in ('print','as','if','class','def','None','True','False','for'):
functionName += '_confilictWithPythonReserveVars'
fp.write('def %s(%s):\n' % (functionName,functionArgs))
fp.write(' pass\n')
def writeClass(fp,clsName,clsMethods):
fp.write('class %s(object):\n' % clsName)
fp.write(' def __init__(self, *args, **kwargs): pass\n')
for mtdname,mtdarg in clsMethods:
if mtdname in ('print','as','if','elif','else','class','def','None','True','False','for','while'):
mtdname += '_confilictWithPythonReserveVars'
fp.write(' def %s(%s):\n' % (mtdname, mtdarg))
fp.write(' return %s(%s)\n' % (mtdname, mtdarg))
fp.write(' pass\n')
def writeClasses(fp,classesDict):
keys = classesDict.keys()
keys.sort()
for key in keys:
value = classesDict[key]
writeClass(fp,key,value)
if __name__ == '__main__':
m,o = getMethodObjectList()
write2Txt(m,o)
write2Python(m)