Workbench中利用Python驅(qū)動(dòng)DM執(zhí)行Jscript腳本參數(shù)化建模
2017-09-15 by:CAE仿真在線 來(lái)源:互聯(lián)網(wǎng)
Workbench的工作平臺(tái)下可以利用Python進(jìn)行一些操作,包括添加system和component等等,在工作臺(tái)所做的一些操作也都能夠被py代碼所記錄,例如添加幾何模塊DM,打開(kāi)、更新、以及關(guān)閉DM模塊。
而DM模塊本身的腳本語(yǔ)言是jscript,也即是在DM中所進(jìn)行的建模操作(主要是sketch草圖命令能夠被DM所記錄,而一些簡(jiǎn)單的三維操作例如拉伸、旋轉(zhuǎn)等則可以通過(guò)添加js API命令完成)。因此,DM可以通過(guò)執(zhí)行Jscript腳本進(jìn)行自動(dòng)參數(shù)化建模。
基于這兩方面的理解,可以結(jié)合這兩塊內(nèi)容,可以利用Python驅(qū)動(dòng)DM執(zhí)行Js腳本進(jìn)行自動(dòng)化建模。
首先創(chuàng)建好Jscript腳本文件,文件代碼如下所示,代碼創(chuàng)建了一個(gè)簡(jiǎn)單的模型,一個(gè)名稱為Sketch1的草圖,草圖中定義了坐標(biāo),草圖的主要內(nèi)容是橢圓線,采用的函數(shù)為Ellipse,該函數(shù)在Workbench的幫助文檔有比較詳細(xì)的解釋。
這里補(bǔ)充一點(diǎn),DM中的js腳本記錄是比較有意思的,完全不像ANSYS 經(jīng)典中的log文件記錄,js記錄的是最終所呈現(xiàn)的結(jié)果,而不關(guān)心這個(gè)結(jié)果是怎么得來(lái)的,也即是其內(nèi)部有一個(gè)轉(zhuǎn)化過(guò)程。
var ps1 = new Object();
ps1.Plane = agb.GetActivePlane();
ps1.Origin = ps1.Plane.GetOrigin();
ps1.XAxis = ps1.Plane.GetXAxis();
ps1.YAxis = ps1.Plane.GetYAxis();
ps1.Sk1 = ps1.Plane.NewSketch();
ps1.Sk1.Name = "Sketch1";
with (ps1.Sk1) { ps1.El7 = Ellipse( 8.0, 10.0, 9.0, 6.0, 5.0, 12.0); }
agb.Regen();
然后需要寫Python代碼,用于在Workbench下面添加一個(gè)DM模塊,并自動(dòng)打開(kāi)。
利用Python向DM傳遞js代碼需要用到SendCommand函數(shù),具體應(yīng)用如下所示:
# encoding: utf-8
# Release 16.0
SetScriptVersion(Version="16.0.361")
template1 = GetTemplate(TemplateName="Geometry")
system1 = template1.CreateSystem()
system1 = GetSystem(Name="Geom")
geometry1 = system1.GetContainer(ComponentName="Geometry")
geometry1.Edit()
geometry1.SendCommand( Command = """var ps1 = new Object();
ps1.Plane = agb.GetActivePlane();
ps1.Origin = ps1.Plane.GetOrigin();
ps1.XAxis = ps1.Plane.GetXAxis();
ps1.YAxis = ps1.Plane.GetYAxis();
ps1.Sk1 = ps1.Plane.NewSketch();
ps1.Sk1.Name = "Sketch1";
with (ps1.Sk1) { ps1.El7 = Ellipse( 8.0, 10.0, 9.0, 6.0, 5.0, 12.0); }
agb.Regen();""")
其中打開(kāi)DM的函數(shù)是geometry1.Edit(),之后通過(guò)SendCommand命令像DM中傳輸一段js腳本命令,執(zhí)行后即可在Workbench中顯示如下所示:
注意:上段python代碼中有一條geometry1.Edit(),該函數(shù)是打開(kāi)DM,若沒(méi)有該行,那么執(zhí)行該段代碼時(shí)不會(huì)打開(kāi)DM,而是相當(dāng)于批處理的形式進(jìn)行運(yùn)行,結(jié)果依然會(huì)生成相應(yīng)的幾何模型,只是看不到彈出的DM窗口。
生成的簡(jiǎn)單模型如下所示:
上文中所說(shuō)的workbench可以自動(dòng)記錄py代碼,DM可以自動(dòng)記錄js代碼,代碼的提取過(guò)程這里不做詳細(xì)闡述,大概前面所推送的文章可能已經(jīng)說(shuō)清楚了怎么操作。
另外,關(guān)于DM自動(dòng)記錄的js腳本,可以簡(jiǎn)單說(shuō)一點(diǎn)。DM自動(dòng)記錄的js腳本是比較完備的,因此整個(gè)結(jié)構(gòu)也相對(duì)較長(zhǎng),可以根據(jù)自己實(shí)際需要,進(jìn)行一些修改和調(diào)整,將比較長(zhǎng)的js代碼減縮成較短。下面是一個(gè)例子,首先是DM自動(dòng)記錄的代碼,如下:
function planeSketchesOnly (p)
{
//Plane
p.Plane = agb.GetActivePlane();
p.Origin = p.Plane.GetOrigin();
p.XAxis = p.Plane.GetXAxis();
p.YAxis = p.Plane.GetYAxis();
//Sketch
p.Sk1 = p.Plane.NewSketch();
p.Sk1.Name = "Sketch1";
//Edges
with (p.Sk1)
{
p.Ln7 = Line(0.50000000, 0.00000000, 0.52000000, 0.00000000);
p.Ln8 = Line(0.52000000, 0.00000000, 0.52000000, -1.00000000);
p.Ln9 = Line(0.52000000, -1.00000000, 0.50000000, -1.00000000);
p.Ln10 = Line(0.50000000, 0.00000000, 0.50000000, -1.00000000);
}
//Sketch
p.Sk2 = p.Plane.NewSketch();
p.Sk2.Name = "Sketch2";
//Edges
with (p.Sk2)
{
p.Ln11 = Line(0.10000000, -0.60000000, 1.00000000, -0.60000000);
p.Ln12 = Line(1.00000000, -0.60000000, 1.00000000, -0.62000000);
p.Ln13 = Line(1.00000000, -0.62000000, 0.10000000, -0.62000000);
p.Ln14 = Line(0.10000000, -0.62000000, 0.10000000, -0.60000000);
p.Ln15 = Line(0.00000000, -0.50000000, 1.36996958, -0.50000000);
}
//Dimensions and/or constraints
with (p.Plane)
{
//Dimensions
var dim;
dim = HorizontalDim(p.Ln7.End, 0.52000000, 0.00000000,
p.Origin, 0.00000000, 0.00000000,
0.17309513, 0.08524206);
if(dim) dim.Name = "H1";
dim = HorizontalDim(p.Ln7.Base, 0.50000000, 0.00000000,
p.Origin, 0.00000000, 0.00000000,
0.21784972, 0.04405282);
if(dim) dim.Name = "H3";
dim = HorizontalDim(p.Ln9.End, 0.50000000, -1.00000000,
p.Origin, 0.00000000, 0.00000000,
0.18917694, -1.09429710);
if(dim) dim.Name = "H5";
dim = HorizontalDim(p.Ln13.Base, 1.00000000, -0.62000000,
p.Origin, 0.00000000, 0.00000000,
0.63773372, -0.70127145);
if(dim) dim.Name = "H8";
dim = HorizontalDim(p.Ln14.Base, 0.10000000, -0.62000000,
p.Origin, 0.00000000, 0.00000000,
0.06909186, -0.66559300);
if(dim) dim.Name = "H9";
dim = VerticalDim(p.Ln8.End, 0.52000000, -1.00000000,
p.Origin, 0.00000000, 0.00000000,
0.68257161, -0.32089647);
if(dim) dim.Name = "V4";
dim = VerticalDim(p.Ln11.Base, 0.10000000, -0.60000000,
p.Origin, 0.00000000, 0.00000000,
0.99779297, -0.16801956);
if(dim) dim.Name = "V6";
dim = VerticalDim(p.Ln13.Base, 1.00000000, -0.62000000,
p.Origin, 0.00000000, 0.00000000,
1.41320222, -0.10522609);
if(dim) dim.Name = "V7";
dim = VerticalDim(p.Ln15.Base, 0.00000000, -0.50000000,
p.Origin, 0.00000000, 0.00000000,
-0.22013445, -0.13289963);
if(dim) dim.Name = "V10";
//Constraints
HorizontalCon(p.Ln7);
HorizontalCon(p.Ln9);
HorizontalCon(p.Ln11);
HorizontalCon(p.Ln13);
HorizontalCon(p.Ln15);
VerticalCon(p.Ln8);
VerticalCon(p.Ln12);
VerticalCon(p.Ln14);
CoincidentCon(p.Ln7.End, 0.52000000, 0.00000000,
p.XAxis, 0.54446962, 0.00000000);
CoincidentCon(p.Ln8.Base, 0.52000000, 0.00000000,
p.Ln7.End, 0.52000000, 0.00000000);
CoincidentCon(p.Ln9.Base, 0.52000000, -1.00000000,
p.Ln8.End, 0.52000000, -1.00000000);
CoincidentCon(p.Ln10.Base, 0.50000000, 0.00000000,
p.Ln7.Base, 0.50000000, 0.00000000);
CoincidentCon(p.Ln10.End, 0.50000000, -1.00000000,
p.Ln9.End, 0.50000000, -1.00000000);
CoincidentCon(p.Ln11.End, 1.00000000, -0.60000000,
p.Ln12.Base, 1.00000000, -0.60000000);
CoincidentCon(p.Ln12.End, 1.00000000, -0.62000000,
p.Ln13.Base, 1.00000000, -0.62000000);
CoincidentCon(p.Ln13.End, 0.10000000, -0.62000000,
p.Ln14.Base, 0.10000000, -0.62000000);
CoincidentCon(p.Ln14.End, 0.10000000, -0.60000000,
p.Ln11.Base, 0.10000000, -0.60000000);
CoincidentCon(p.Ln15.Base, 0.00000000, -0.50000000,
p.YAxis, 0.00000000, -0.50650669);
}
p.Plane.EvalDimCons(); //Final evaluate of all dimensions and constraints in plane
return p;
}
var ps1 = planeSketchesOnly (new Object());
var rev1=agb.Revolve(agc.Add,ps1.Sk1,ps1.YAxis,agc.DirReversed,180.0, 0.0, agc.No, 0.0, 0.0)
agb.Regen();
從上面這段js代碼可以看出,整個(gè)草圖建立的過(guò)程是用一個(gè)函數(shù)來(lái)定義的,通過(guò)調(diào)用函數(shù)var ps1 = planeSketchesOnly (new Object())即可生成草圖,有點(diǎn)面向?qū)ο蟮囊馑肌?
還可以注意到,代碼中包含了很多關(guān)于尺寸約束的部分,雖然在利用GUI操作時(shí),尺寸約束過(guò)程通常必不可少,但是在js代碼中卻是可以省略的,原因在于代碼建立線(舉例)直接用的就是約束后的尺寸,已經(jīng)實(shí)現(xiàn)了參數(shù)化,無(wú)需再添加尺寸約束部分的代碼,除非是想要強(qiáng)行修改尺寸。
基于此觀點(diǎn),可以將上段的代碼進(jìn)行精簡(jiǎn),精簡(jiǎn)后的js腳本如下:
var ps1 = new Object();
//Plane
ps1.Plane = agb.GetActivePlane();
ps1.Origin = ps1.Plane.GetOrigin();
ps1.XAxis = ps1.Plane.GetXAxis();
ps1.YAxis = ps1.Plane.GetYAxis();
ps1.Sk1 = ps1.Plane.NewSketch();
ps1.Sk1.Name = "Sketch1";
with (ps1.Sk1)
{
ps1.Ln7 = Line(0.50000000, 0.00000000, 0.52000000, 0.00000000);
ps1.Ln8 = Line(0.52000000, 0.00000000, 0.52000000, -1.00000000);
ps1.Ln9 = Line(0.52000000, -1.00000000, 0.50000000, -1.00000000);
ps1.Ln10 = Line(0.50000000, 0.00000000, 0.50000000, -1.00000000);
}
ps1.Sk2 = ps1.Plane.NewSketch();
ps1.Sk2.Name = "Sketch2";
with (ps1.Sk2)
{
ps1.Ln11 = Line(0.10000000, -0.60000000, 1.00000000, -0.60000000);
ps1.Ln12 = Line(1.00000000, -0.60000000, 1.00000000, -0.62000000);
ps1.Ln13 = Line(1.00000000, -0.62000000, 0.10000000, -0.62000000);
ps1.Ln14 = Line(0.10000000, -0.62000000, 0.10000000, -0.60000000);
ps1.Ln15 = Line(0.00000000, -0.50000000, 1.36996958, -0.50000000);
}
var rev1=agb.Revolve(agc.Add,ps1.Sk1,ps1.YAxis,agc.DirReversed,180.0, 0.0, agc.No, 0.0, 0.0)
agb.Regen();
代碼長(zhǎng)度短了很多,而效果依然一樣。
相關(guān)標(biāo)簽搜索:Workbench中利用Python驅(qū)動(dòng)DM執(zhí)行Jscript腳本參數(shù)化建模 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)