本節(jié)書摘來自異步社區(qū)《Abaqus GUI程序開發(fā)指南(Python語言)》一書中的第1章,第1.1節(jié),作者: 賈利勇 , 富琛陽子 , 賀高 , 周正光 更多章節(jié)內(nèi)容可以訪問云棲社區(qū)“異步社區(qū)”公眾號查看。
第一章?概述 Abaqus GUI程序開發(fā)指南(Python語言) 工欲善其事,必先利其器。對于CAE分析工程師而言,一個好的軟件平臺就是一把利器,基于軟件平臺的二次開發(fā)就是讓工具更加豐富、便捷和高效。Abaqus的二次開發(fā)一般分為子程序開發(fā)和用戶圖形界面程序(Graphical User Interface,GUI)開發(fā)兩類,有很多初學(xué)者經(jīng)常把兩者混淆,分不清兩者的區(qū)別。Abaqus子程序開發(fā)基于Fortran語言,用戶可以根據(jù)實際需求編寫材料本構(gòu)關(guān)系(UMAT/VUMAT)、自定義單元UEL以及其他多種子程序(UVARM、DLOAD、USDFLD等等)。用戶圖形界面程序開發(fā)則是基于Python語言,主要是根據(jù)需求對原有Abaqus/CAE功能組件進行擴展,開發(fā)專用的前后處理模塊以及GUI工具等。本書的內(nèi)容僅針對基于Python語言的GUI程序開發(fā)。在本章中,將通過一個簡單的插件實例介紹Abaqus GUI程序開發(fā)的概念、作用及意義。
1.1 簡單插件實例——創(chuàng)建帶孔板有限元模型 熟悉Abaqus/CAE的人應(yīng)該知道,當點擊Plug-ins→Toolboxes→Examples菜單時,會彈出如圖1.1所示工具條。
這是Abaqus/CAE內(nèi)嵌的一個簡單插件程序(plug-in),在上述窗口中輸入零件名、零件寬度、高度以及圓孔半徑之后,點擊“OK”,則會自動在Abaqus中生成帶孔板的幾何模型。
該程序僅能實現(xiàn)帶孔板的幾何創(chuàng)建,本節(jié)將以此為基礎(chǔ),在源程序的基礎(chǔ)上稍加改進,使其可實現(xiàn)帶孔板有限元分析模型的參數(shù)化建模。圖1.3為改寫之后的插件程序界面示意圖。下面將對該插件程序的構(gòu)成進行詳細介紹。
該插件程序共由4個文件組成。
(1)注冊文件createPlateWithhole_plugin.py
(2)圖形界面文件createPlateWithholeDB.py
(3)內(nèi)核執(zhí)行文件createPlateModul.py
(4)圖標文件planewithhole.png
其中圖標文件如圖1.4所示,圖標文件即為插件窗口中的示意圖,Abaqus支持bmp、 gif、 png、 xpm等多種格式的圖片,本實例中采用的是png格式圖片,其他3個組成文件將在后面分別介紹。
https://yqfile.alicdn.com/11fec8c93bd10991f708ce3d284550d22247cdbf.png" >
上述文件均存放在隨書配套資源chapter 1createPlateWithhole文件夾內(nèi)。將文件夾createPlateWithhole及內(nèi)部文件復(fù)制到Abaqus工作目錄或者安裝目錄下的Abaqus_plugins文件夾下(沒有的話請自行創(chuàng)建該文件夾),例如:
安裝目錄D:SIMULIA6.10Abaqus_pluginscreatePlateWithhole
當前工作目錄D:TEMPAbaqus_plugins
重新啟動Abaqus/CAE主界面,在Plug-ins菜單下會出現(xiàn)名為“創(chuàng)建帶孔板”的功能子菜單,如圖1.5所示。
https://yqfile.alicdn.com/0e26a5d47d48d12e3ac92e3837ae9de05fae97c8.png ">
將圖1.3中所示的參數(shù)填寫完整并執(zhí)行程序,可自動生成帶孔板拉伸的有限元模型,并可直接將其提交計算,模型如圖1.6所示。
提交計算后,可以得到圖1.7所示的應(yīng)力云圖。
https://yqfile.alicdn.com/9f90a4ab75405157c47e6dad0ade1ddb5670f7df.png" >
1.1.1 注冊文件 本插件程序中的注冊文件命名為createPlateWithhole_plugin.py,其主要作用是注冊插件程序以及其中的關(guān)鍵字等。其程序源代碼如下。
#文件名createPlateWithhole_plugin.py
#中文編碼
# -* - coding:UTF-8 -*-
from abaqusGui import *
from abaqusConstants import ALL
import osutils, os
#####################################################################
# Class definition
#####################################################################
class createPlateWithhole_plugin(AFXForm): #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def __init__(self, owner): # Construct the base class. # AFXForm.__init__(self, owner) self.radioButtonGroups = {} self.cmd = AFXGuiCommand(mode=self, method='createPlateFunction', objectName='createPlateModul', registerQuery=False) pickedDefault = '' self.partnameKw = AFXStringKeyword(self.cmd, 'partname', True, 'part-1') self.widthKw = AFXFloatKeyword(self.cmd, 'width', True,100) self.heightKw = AFXFloatKeyword(self.cmd, 'height', True,100) self.radiusKw = AFXFloatKeyword(self.cmd, 'radius', True,5) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def getFirstDialog(self): import createPlateWithholeDB return createPlateWithholeDB.createPlateWithholeDB(self) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def doCustomChecks(self): # Try to set the appropriate radio button on. If the user did # not specify any buttons to be on, do nothing. # for kw1,kw2,d in self.radioButtonGroups.values(): try: value = d[ kw1.getValue() ] kw2.setValue(value) except: pass #對輸入數(shù)據(jù)格式進行檢查,不滿足要求時提示警告 if self.widthKw.getValue()<=0: showAFXErrorDialog(getAFXApp().getAFXMainWindow(), '寬度必須為正數(shù)') return False elif self.heightKw.getValue()<=0: showAFXErrorDialog(getAFXApp().getAFXMainWindow(), '高度必須為正數(shù)') return False elif self.radiusKw.getValue()<=0: showAFXErrorDialog(getAFXApp().getAFXMainWindow(), '孔半徑必須是正數(shù).') return False else: return True #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def okToCancel(self): # No need to close the dialog when a file operation (such # as New or Open) or model change is executed. # return False #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Register the plug-in
#
thisPath = os.path.abspath(__file__) #指定路徑
thisDir = os.path.dirname(thisPath)
toolset = getAFXApp().getAFXMainWindow().getPluginToolset()
toolset.registerGuiMenuButton( buttonText='創(chuàng)建帶孔板', object=createPlateWithhole_plugin(toolset), messageId=AFXMode.ID_ACTIVATE, icon=None, #指定圖標為空 kernelInitString='import createPlateModul' , #內(nèi)核初始化指令 applicableModules=ALL, #指定使用模塊,默認為全部 version='1.0', #軟件版本 author='jly', #軟件作者 description='N/A', #軟件描述 helpUrl='N/A' #幫助文檔路徑指定
)
提示
以上代碼是本書中出現(xiàn)的第一段代碼,需要提醒讀者注意的是,在Python語言中,代碼的縮進是一種語法,Python依靠每行代碼的縮進位置來判斷代碼的分塊、區(qū)分代碼塊之間的層次關(guān)系,錯誤的縮進位置將會導(dǎo)致程序執(zhí)行錯誤,讀者應(yīng)對此予以特別注意,有關(guān)Python編碼規(guī)則的內(nèi)容請參照本書2.3節(jié)內(nèi)容。 編寫代碼時建議使用UltraEdit或者Notepad++等文本編輯軟件,本書在編寫過程中采用的是Notepad++軟件(免費),該軟件支持Python等多種語言的語法著色。
1.1.2 圖形界面文件 圖形界面文件的主要作用是定義各類窗體、控件,關(guān)聯(lián)控件的執(zhí)行命令及對象。通過定義文本框、單復(fù)選按鈕、表格等各類控件,用戶可以方便地將所需參數(shù)集成到統(tǒng)一的圖形界面下,形成友好的輸入輸出界面。本插件程序中的圖形界面文件命名為createPlateWithholeDB.py,其程序源代碼如下。
# -* - coding:UTF-8 -*-
from abaqusConstants import *
from abaqusGui import *
from kernelAccess import mdb, session
import os
thisPath = os.path.abspath(__file__) #設(shè)置當前路徑
thisDir = os.path.dirname(thisPath)
#####################################################################
# Class definition
#####################################################################
class createPlateWithholeDB(AFXDataDialog): #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def __init__(self, form): # Construct the base class. # AFXDataDialog.__init__(self, form, ' 帶孔板參數(shù)化建模程序', self.OK|self.CANCEL, DIALOG_ACTIONS_SEPARATOR) #創(chuàng)建對話框,該對話框有OK、CANCEL按鍵 okBtn = self.getActionButton(self.ID_CLICKED_OK) okBtn.setText('OK') #定義OK按鈕 GroupBox_1 = FXGroupBox(p=self, text='參數(shù)', opts=FRAME_GROOVE) AFXTextField(p=GroupBox_1, ncols=12, labelText='零件名 :', tgt=form.partnameKw, sel=0) #定義文本框 AFXTextField(p=GroupBox_1, ncols=12, labelText='寬度(w):', tgt=form.widthKw, sel=0) AFXTextField(p=GroupBox_1, ncols=12, labelText='高度(h):', tgt=form.heightKw, sel=0) AFXTextField(p=GroupBox_1, ncols=12, labelText='半徑(r):', tgt=form.radiusKw, sel=0) GroupBox_3 = FXGroupBox(p=self, text='示意圖', opts=FRAME_GROOVE) fileName = os.path.join(thisDir, r'planewithhole.png') #指定示意圖圖標 icon = afxCreatePNGIcon(fileName)#創(chuàng)建圖標 FXLabel(p=GroupBox_3, text='', ic=icon) #定義示意圖標簽
1.1.3 內(nèi)核執(zhí)行文件 內(nèi)核執(zhí)行文件是插件程序的核心,其作用是驅(qū)動Abaqus/CAE執(zhí)行內(nèi)部命令,完成CAE建模以及數(shù)據(jù)處理等功能。本插件程序中的內(nèi)核執(zhí)行文件命名為createPlateModul.py,其程序源代碼如下。
# -* - coding:UTF-8 -*-
from abaqus import *
from abaqusConstants import *
def createPlateFunction(partname, width, height, radius): #參數(shù)調(diào)用 mdb.models['Model-1'].ConstrainedSketch(name= ' __profile__' , sheetSize=200.0) mdb.models['Model-1'].sketches[ '__profile__ ' ].rectangle(point1=(0.0, 0.0), point2=(width, height)) mdb.models['Model-1'].sketches[ ' __profile__' ].CircleByCenterPerimeter(center=( width/2, height/2), point1=(width/2+radius, height/2)) mdb.models['Model-1'].Part(dimensionality=THREE_D, name=partname, type= DEFORMABLE_BODY) mdb.models['Model-1'].parts[partname].BaseShell(sketch= mdb.models['Model-1'].sketches['__profile__']) p = mdb.models['Model-1'].parts[partname] session.viewports['Viewport: 1'].setValues(displayedObject=p) del mdb.models['Model-1'].sketches['__profile__'] #創(chuàng)建幾何 mdb.models['Model-1'].Material(name='AL') mdb.models['Model-1'].materials['AL'].Elastic(table=((70000.0, 0.3), )) mdb.models['Model-1'].HomogeneousShellSection(name='al', preIntegrate=OFF, material='AL', thicknessType=UNIFORM, thickness=1.0, thicknessField='', idealization=NO_IDEALIZATION, poissonDefinition=DEFAULT, thicknessModulus=None, temperature=GRADIENT, useDensity=OFF, integrationRule=SIMPSON, numIntPts=5) f = p.faces faces = f.getSequenceFromMask(mask=('[#1 ]', ), ) region = p.Set(faces=faces, name='Set-2') p.SectionAssignment(region=region, sectionName='al', offset=0.0, offsetType=MIDDLE_SURFACE, offsetField='', thicknessAssignment=FROM_SECTION) #建立材料并賦予屬性 session.viewports['Viewport: 1'].partDisplay.setValues(mesh=ON) session.viewports['Viewport: 1'].partDisplay.meshOptions.setValues( meshTechnique=ON) session.viewports['Viewport: 1'].partDisplay.geometryOptions.setValues( referenceRepresentation=OFF) #切換到mesh模塊 p = mdb.models['Model-1'].parts[partname] p.seedPart(size=4.0, deviationFactor=0.1, minSizeFactor=0.1) f = p.faces pickedRegions = f.findAt(((width/2, 0.0, 0.0), )) p.setMeshControls(regions=pickedRegions, elemShape=QUAD,algorithm=MEDIAL_AXIS) #設(shè)定網(wǎng)格劃分格式 p.generateMesh() #網(wǎng)格劃分 a = mdb.models['Model-1'].rootAssembly session.viewports['Viewport: 1'].setValues(displayedObject=a) #切換到裝配模塊 a = mdb.models['Model-1'].rootAssembly a.DatumCsysByDefault(CARTESIAN) p = mdb.models['Model-1'].parts[partname] a.Instance(name=partname+'-1', part=p, dependent=ON) #創(chuàng)建裝配實例 mdb.models['Model-1'].StaticStep(name='Step-1', previous='Initial', nlgeom=ON) session.viewports['Viewport: 1'].assemblyDisplay.setValues(step='Step-1') #創(chuàng)建分析步 session.viewports['Viewport: 1'].view.setValues(nearPlane=335.564, farPlane=385.546, width=212.48, height=142.547, viewOffsetX=13.3712, viewOffsetY=-7.13345) a = mdb.models['Model-1'].rootAssembly e1 = a.instances[partname+'-1'].edges edges1 = e1.findAt(((0.0, height/2, 0.0), )) region = a.Set(edges=edges1, name='Set-1') mdb.models['Model-1'].DisplacementBC(name='BC-1', createStepName='Step-1', region=region, u1=0.0, u2=0.0, u3=0.0, ur1=0.0, ur2=0.0, ur3=0.0, amplitude=UNSET, fixed=OFF, distributionType=UNIFORM, fieldName='', localCsys=None) #施加邊界條件 edges1 = e1.findAt(((width, height/2, 0.0), )) region = a.Set(edges=edges1, name='Set-2') mdb.models['Model-1'].DisplacementBC(name='BC-2', createStepName='Step-1', region=region, u1=2.0, u2=UNSET, u3=UNSET, ur1=UNSET, ur2=UNSET, ur3=UNSET, amplitude=UNSET, fixed=OFF, distributionType=UNIFORM, fieldName='', localCsys=None) #施加位移載荷 mdb.Job(name='Job-hole', model='Model-1', description='', type=ANALYSIS, atTime=None, waitMinutes=0, waitHours=0, queue=None, memory=50, memoryUnits=PERCENTAGE, getMemoryFromAnalysis=True, explicitPrecision=SINGLE, nodalOutputPrecision=SINGLE, echoPrint=OFF, modelPrint=OFF, contactPrint=OFF, historyPrint=OFF, userSubroutine='', scratch='', multiprocessingMode=DEFAULT, numCpus=1) #創(chuàng)建job
與50位技術(shù)專家面對面 20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔 為你收集整理的《Abaqus GUI程序开发指南(Python语言)》——第一章 概述1.1 简单插件实例——创建带孔板有限元模型... 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。