利用Python和API函數(shù)創(chuàng)建幾何模型實(shí)例【轉(zhuǎn)發(fā)】

2017-07-03  by:CAE仿真在線  來(lái)源:互聯(lián)網(wǎng)

利用Python、Workbench的API函數(shù)以及xml進(jìn)行Workbench二次開(kāi)發(fā)的一個(gè)比較完整的例子,文中代碼來(lái)自于ANSYS幫助文檔,筆者對(duì)代碼做了一些細(xì)致的解釋。


正文


以下是Workbench的DM中一個(gè)extension的Python代碼,代碼的主要功能是針對(duì)一個(gè)已經(jīng)存在的模型,在此基礎(chǔ)上畫(huà)圓、拉伸、做add體操作,以及一些顯示。

#導(dǎo)入unitsmath模塊

import units

import math

#定義函數(shù)createMyFeature,創(chuàng)建用戶對(duì)象geometry,名稱顯示在Tree Outline。

def createMyFeature(feature):

#利用CreateFeature函數(shù)創(chuàng)建用戶對(duì)象;

#函數(shù)CreateFeature的變量是字符串,返回用戶對(duì)象;

#函數(shù)CreateFeature入口是ExtAPI.

ExtAPI.CreateFeature("MyFeature")

#定義函數(shù)vectorProduct,創(chuàng)建矢量

def vectorProduct(v1, v2):

#返回[y1*z2-z1*y2,-x1*z2+z1*x2,x1*y2-y1*x2]

return [v1[1]*v2[2]-v1[2]*v2[1], -v1[0]*v2[2]+v1[2]*v2[0], v1[0]*v2[1]-v1[1]*v2[0]]

#定義標(biāo)量函數(shù)scalarProduct,創(chuàng)建標(biāo)量

def scalarProduct(v1, v2):

#返回 x1*x2+y1*y2+z1*z2

return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]

#定義函數(shù),求平方根

def norm(v):

#利用math模塊的sqrt函數(shù)

return math.sqrt(scalarProduct(v,v))

#定義函數(shù)生成模型

def generateMyFeature(feature,function):

#定義變量,賦值為對(duì)象的長(zhǎng)度,這個(gè)長(zhǎng)度值是在xml里面定義的;

#xml定義好界面,會(huì)有"Length“項(xiàng),取值即可。

length = feature.Properties["Length"].Value

#單位轉(zhuǎn)換

length = units.ConvertUnit(length, ExtAPI.DataModel.CurrentUnitFromQuantityName("Length"), "m")

#界面所選擇的面作為faces的取值,這個(gè)也是xml定義的。

faces = feature.Properties["Face"].Value.Entities

#定義空列表

bodies = []

#獲取GeometryBuilder入口

builder = ExtAPI.DataModel.GeometryBuilder

#通過(guò)循環(huán)面

for face in faces:

#獲取面的中心,是三個(gè)數(shù)(x,y,z)組成的數(shù)組

centroid = face.Centroid

#由于需要自動(dòng)判別某個(gè)面的法向等,所以計(jì)算過(guò)程看起來(lái)比較復(fù)雜

#定義uv數(shù)組,含有兩個(gè)量,可以理解為針對(duì)某個(gè)面自動(dòng)確定的xy坐標(biāo),再結(jié)合面的中心點(diǎn);

#可以在這個(gè)面的中心建立一個(gè)局部坐標(biāo)系,利用函數(shù)ParamAtPoint

uv = face.ParamAtPoint(centroid)

#獲取面的法向向量,應(yīng)該是通過(guò)面的中心點(diǎn)一級(jí)uv數(shù)組三個(gè)數(shù)一起算出來(lái)的。

normal = face.NormalAtParam(uv[0], uv[1])

#定義半徑

radius = math.sqrt(face.Area/(math.pi*2))

#初始化向量xdir

xdir = [1., 0., 0.]

#調(diào)用函數(shù)vectorProduct計(jì)算

vector = vectorProduct(xdir, normal)

if norm(vector)<1.e-12:

xdir = [0., 1., 1.]

s = scalarProduct(xdir, normal)

xdir = [xdir[0]-s*normal[0],xdir[1]-s*normal[1],xdir[2]-s*normal[2]]

n = norm(xdir)

xdir[0] /= n

xdir[1] /= n

xdir[2] /= n

#中間這一段代碼確實(shí)晦澀難懂,其用途就是計(jì)算圓弧的主向量。

#之后通過(guò)CreateArc函數(shù)創(chuàng)建一段圓弧

arc_generator = builder.Primitives.Wire.CreateArc(radius, centroid, xdir, normal)

#利用Generate()函數(shù)生成圓弧

arc_generated = arc_generator.Generate()

#利用WireToSheetBody函數(shù)將圓弧轉(zhuǎn)換成面

disc_generator = builder.Operations.Tools.WireToSheetBody(arc_generated)

#單位化向量

normal[0] *= -1

normal[1] *= -1

normal[2] *= -1

#利用CreateExtrudeOperation函數(shù)進(jìn)行拉伸操作

extrude = builder.Operations.CreateExtrudeOperation(normal,length)

#拉伸的對(duì)象是創(chuàng)建的面【第一個(gè)對(duì)象】

cylinder_generator = extrude.ApplyTo(disc_generator)[0]

#存放在列表中

bodies.Add(cylinder_generator)

#返回值

feature.Bodies = bodies

#類型為Add

feature.MaterialType = MaterialTypeEnum.Add

return True

#定義函數(shù)afterGenerateMyFeature,當(dāng)<ongenerate>完成后自動(dòng)執(zhí)行該函數(shù)

#該函數(shù)主要完成兩個(gè)功能,一是顯示出最小body的體積

#二是將所用的edge放在一個(gè)集合里面

def afterGenerateMyFeature(feature):

edges = []

#定義一個(gè)系統(tǒng)內(nèi)的最大值,用于后續(xù)的比較

minimum_volume = System.Double.MaxValue

#通過(guò)循環(huán)找出最小體積

for body in feature.Bodies:

body_volume = 0

if str(body.BodyType) == "GeoBodySolid":

body_volume = body.Volume

if body_volume <= minimum_volume:

minimum_volume = body_volume

#將所用的edge放在edges列表里面

for edge in body.Edges:

edges.Add(edge)

else:

ExtAPI.Log.WriteMessage("Part: "+body.Name)

#feature的顯示值

feature.Properties["Minimum Volume"].Value = minimum_volume

#創(chuàng)建集合

ExtAPI.SelectionManager.NewSelection(edges)

named_selection = ExtAPI.DataModel.FeatureManager.CreateNamedSelection()

ExtAPI.SelectionManager.ClearSelection()


該P(yáng)ython代碼并不能直接在DM中使用,還需要接觸XML編寫(xiě)界面程序,界面程序可以將上面Python代碼中的函數(shù)進(jìn)行有效綁定。

最后能夠生成的模型以及在Workbench的DM中存在的形式分別如下圖所示。

利用Python和API函數(shù)創(chuàng)建幾何模型實(shí)例【轉(zhuǎn)發(fā)】ansys仿真分析圖片1

圖1


利用Python和API函數(shù)創(chuàng)建幾何模型實(shí)例【轉(zhuǎn)發(fā)】ansys仿真分析圖片2

圖2

對(duì)應(yīng)的xml代碼為:

<! 定義extension的版本號(hào)為1,名稱為GeometryFeature>

<extension version="1" name="GeometryFeature">

<! 指定該extension所執(zhí)行的python代碼名稱為main.py>

<script src="main.py" />

<! 說(shuō)明該extension應(yīng)用在DesignModeler模塊>

<interface context="DesignModeler">

<! 定義該extension的背景,文件名稱為image>

<images>images</images>

<! 定義一個(gè)工具條,名稱為ACTtoolbar,顯示名稱為ACTtoolbar>

<! 定義入口名稱為MyFeature,使用的標(biāo)志為construction>

<! 調(diào)用的函數(shù)為createMyFeature>

<toolbar name="ACTtoolbar" caption="ACTtoolbar">

<entry name="MyFeature" icon="construction">

<callbacks>

<onclick>createMyFeature</onclick>

</callbacks>

</entry>

</toolbar>

</interface>

<! simdata標(biāo)簽下定義對(duì)模型的定義>

<! 調(diào)用函數(shù)generateMyFeature和afterGenerateMyFeature>

<simdata context="DesignModeler">

<geometry name="MyFeature" caption="MyFeature" icon="construction" version="1">

<callbacks>

<ongenerate>generateMyFeature</ongenerate>

<onaftergenerate>afterGenerateMyFeature</onaftergenerate>

</callbacks>

<! 定義屬性,有一個(gè)叫做“Face”欄目,顯示名稱為“Face”,采用滾動(dòng)條控制>

<property name="Face" caption="Face" control="scoping">

<! 定義選擇類型為面>

<attibutes selection_filter="face"/>

</property>

<! 定義屬性,名稱為length,顯示Length,數(shù)據(jù)類型為float,單位為長(zhǎng)度單位,默認(rèn)值為1.2>

<property name="Length" caption="Length" control="float" unit="Length" default="1.2 [m]"></property>

<! 定義屬性,最小體積Minimum Volume,數(shù)據(jù)類型為float,單位為體積單位>

<property name="Minimum Volume" caption="Minimum Volume" control="float" unit="Volume" >

<attributes readonlyaftergenerate="true" />

</property>

</geometry>

</simdata>

</extension>


Xml文件與py文件相互之間實(shí)現(xiàn)數(shù)據(jù)傳遞,完成整個(gè)extension的建立。


轉(zhuǎn)自公眾號(hào):CAE技術(shù)分享

開(kāi)放分享:優(yōu)質(zhì)有限元技術(shù)文章,助你自學(xué)成才

相關(guān)標(biāo)簽搜索:利用Python和API函數(shù)創(chuàng)建幾何模型實(shí)例【轉(zhuǎn)發(fā)】 Ansys有限元培訓(xùn) Ansys workbench培訓(xùn) ansys視頻教程 ansys workbench教程 ansys APDL經(jīng)典教程 ansys資料下載 ansys技術(shù)咨詢 ansys基礎(chǔ)知識(shí) ansys代做 Fluent、CFX流體分析 HFSS電磁分析 Abaqus培訓(xùn) 

編輯
在線報(bào)名:
  • 客服在線請(qǐng)直接聯(lián)系我們的客服,您也可以通過(guò)下面的方式進(jìn)行在線報(bào)名,我們會(huì)及時(shí)給您回復(fù)電話,謝謝!
驗(yàn)證碼

全國(guó)服務(wù)熱線

1358-032-9919

廣州公司:
廣州市環(huán)市中路306號(hào)金鷹大廈3800
電話:13580329919
          135-8032-9919
培訓(xùn)QQ咨詢:點(diǎn)擊咨詢 點(diǎn)擊咨詢
項(xiàng)目QQ咨詢:點(diǎn)擊咨詢
email:kf@1cae.com