梁凱旋
(新疆水利水電勘測(cè)設(shè)計(jì)研究院,烏魯木齊 830000)
?
淺談VB.net結(jié)合InventoriLogic在工程中的應(yīng)用
梁凱旋
(新疆水利水電勘測(cè)設(shè)計(jì)研究院,烏魯木齊 830000)
利用VB.net面向?qū)ο蟮膹?qiáng)大功能和內(nèi)嵌的iLogic對(duì)Inventor進(jìn)行二次開發(fā),創(chuàng)建自定義用戶界面,使交互起來更方便、更直觀, 實(shí)現(xiàn)自身fx參數(shù)表不能實(shí)現(xiàn)的功能,從而在實(shí)際工作中提高效率。文章介紹了VB.net結(jié)合InventoriLogic在工程中的應(yīng)用。
VB.net;Inventor;iLogic;水利工程;結(jié)構(gòu)模型;水力學(xué)計(jì)算
VB.net語言繼承了VB的特點(diǎn),即簡(jiǎn)單易學(xué),功能強(qiáng)大,容易上手,開發(fā)周期短 ,而且VB.net最大的優(yōu)點(diǎn)是完全面向?qū)ο?,甚至可以與C++相媲美,目前眾多行業(yè)軟件比如AutoCad,Inventor,Civil3D等都提供.Net API二次開發(fā)接口,以便豐富自己的函數(shù)庫,是目前眾多流行軟件的比較理想的二次開發(fā)語言,此外VB.net的開發(fā)環(huán)境為微軟強(qiáng)大的Visual Studio(VS)集成開發(fā)平臺(tái),借助VS提供的強(qiáng)大的編輯和錯(cuò)誤調(diào)試功能,使得開發(fā)過程能夠快速高效的完成[1]。iLogic是嵌入到Inventor中的一個(gè)規(guī)則驅(qū)動(dòng)程序,通過設(shè)定一些規(guī)則來達(dá)到驅(qū)動(dòng)模型進(jìn)行修改的目的,只需要寫一些簡(jiǎn)單的代碼即可完成一些繁瑣的步驟,比較簡(jiǎn)單易學(xué)。
通過VB.net和iLogic的結(jié)合,發(fā)揮各自的優(yōu)勢(shì),來實(shí)現(xiàn)一些特定的功能,減少繁瑣的步驟,提高設(shè)計(jì)的效率。
1.1各自的功能
在VB.net中,主要利用面向?qū)ο蠹夹g(shù)來完成自定義交互界面的創(chuàng)建、一些計(jì)算方法或函數(shù)的創(chuàng)建、錯(cuò)誤的處理等,最后將其封裝成一個(gè)叫做.dll的文件,這個(gè).dll文件就可以被iLogic調(diào)用。
iLogic 起到中間傳遞的作用,主要完成Inventor模型參數(shù)的傳遞和接收。工作原理是首先將模型中參數(shù)名稱傳遞給交互給界面,然后通過窗體內(nèi)部的計(jì)算,最后將計(jì)算的值進(jìn)行接收并賦值給inventor模型中對(duì)應(yīng)的參數(shù)名稱,從而驅(qū)動(dòng)模型的修改。
1.2如何結(jié)合
首先在iLogic環(huán)境中引用VB.net生成的.dll程序,其次將inventor模型中fx參數(shù)表中的參數(shù)名稱傳遞到交互界面中,然后通過調(diào)用封裝在里面的函數(shù)或方法,完成一些特定的計(jì)算或分析,并返回需要的參數(shù),然后將參數(shù)施加給模型即可[2]。
2.1分析并創(chuàng)建水工結(jié)構(gòu)模型
分析模型由哪幾部分構(gòu)成,需要哪些參數(shù),根據(jù)部位一一列舉出來。比如:閘底板長,閘底板寬,閘底板厚,邊墩厚,中墩厚,閘墩高,孔數(shù),單孔寬,檢修門槽寬,檢修門槽深等等(見圖1),然后創(chuàng)建水閘模型。其中參數(shù)名稱盡量用英文表示,這樣做的目的主要是為了和VB.net代碼中的參數(shù)名稱進(jìn)行相對(duì)應(yīng)。
2.2定義用戶參數(shù)
打開inventor的fx參數(shù)表,將前面列舉出來的參數(shù)依次添加到表中,并賦初始值,然后在注釋列中添加中文解釋,以方便后期進(jìn)行檢查。見圖2。
圖1 水閘結(jié)構(gòu)模型預(yù)覽
圖2 fx自定義用戶參數(shù)列表
2.3將自定義參數(shù)賦予模型
將fx中的定義好的參數(shù)賦給模型參數(shù),以便對(duì)模型進(jìn)行驅(qū)動(dòng)。見圖3。
圖3 fx模型參數(shù)列表
2.4VB.net中的主要代碼
'定義公共變量,變量名稱要和前面定義的fx用戶參數(shù)名稱保持一致。
Public Class MainForm
Public BaseboardLength As Double'底板長
Public BaseboardThickness As Double '底板厚
Public SingleOrificeWidth As Double '單孔寬
Public OrificeCount As Integer '孔數(shù)
Public BaseboardWidth As Double '底板寬
Public MidPierThickness As Double '中墩厚
Public OutsidePierThickness As Double '邊墩厚
Public PierHigh As Double '閘墩高
Public RepairGateGrooveWidth As Double '檢修門槽寬
Public RepairGateGrooveDepth As Double '檢修門槽深
Public RepairGateDistanceToUpper As Double '檢修門槽中心線距離上游距離
Public WorkingGateGrooveWidth As Double '弧形門槽寬
Public WorkingGateGrooveDepth As Double '弧形門槽深
Public ArticulatedPositionX As Double '支鉸位置X
Public ArticulatedPositionY As Double '支鉸位置y
Public WorkingGateGrooveRadius As Double '弧形門槽半徑
Public TrafficBridgeWidth As Double '交通橋?qū)?/p>
Public TrafficBridgeThickness As Double '交通橋厚
Public WaterGateLo As Double '水閘凈寬
Public WaterQ As Double '水閘過流量
'初始化函數(shù)
Private Sub Initialize()
BaseboardLengthTxtbox.Text = BaseboardLength.ToString
BaseboardThicknessTxtbox.Text = BaseboardThickness.ToString
BaseboardWidthTxtbox.Text = BaseboardWidth.ToString
OrificeCountTxtbox.Text = OrificeCount.ToString
SingleOrificeWidthTxtbox.Text = SingleOrificeWidth.ToString
OutsidePierThicknessTxtbox.Text = OutsidePierThickness.ToString
MidPierThicknessTxtbox.Text = MidPierThickness.ToString
PierHighTxtbox.Text = PierHigh.ToString
RepairGateGrooveWidthTxtbox.Text = RepairGateGrooveWidth.ToString
RepairGateGrooveDepthTxtbox.Text = RepairGateGrooveDepth.ToString
RepairGateDistanceToUpperTxtbox.Text= epairGateDistanceToUpper.ToString
WorkingGateGrooveWidthTxtbox.Text = WorkingGateGrooveWidth.ToString
WorkingGateGrooveDepthTxtbox.Text = WorkingGateGrooveDepth.ToString
ArticulatedPositionXTxtbox.Text = ArticulatedPositionX.ToString
ArticulatedPositionYTxtbox.Text = ArticulatedPositionY.ToString
WorkingGateGrooveRadiusTxtbox.Text = WorkingGateGrooveRadius.ToString
TrafficBridgeWidthTxtbox.Text = TrafficBridgeWidth.ToString
TrafficBridgeThicknessTxtbox.Text = TrafficBridgeThickness.ToString
End Sub
'當(dāng)文本框的內(nèi)容更新后,重新給變量賦值
Private Sub UpdateValues()
BaseboardLength = Val(BaseboardLengthTxtbox.Text)
BaseboardThickness = Val(BaseboardThicknessTxtbox.Text)
BaseboardWidth = Val(BaseboardWidthTxtbox.Text)
OrificeCount = Val(OrificeCountTxtbox.Text)
SingleOrificeWidth = Val(SingleOrificeWidthTxtbox.Text)
OutsidePierThickness = Val(OutsidePierThicknessTxtbox.Text)
MidPierThickness = Val(MidPierThicknessTxtbox.Text)
PierHigh = Val(PierHighTxtbox.Text)
RepairGateGrooveWidth = Val(RepairGateGrooveWidthTxtbox.Text)
RepairGateGrooveDepth = Val(RepairGateGrooveDepthTxtbox.Text)
RepairGateDistanceToUpper = Val(RepairGateDistanceToUpperTxtbox.Text)
WorkingGateGrooveWidth = Val(WorkingGateGrooveWidthTxtbox.Text)
WorkingGateGrooveDepth = Val(WorkingGateGrooveDepthTxtbox.Text)
ArticulatedPositionX = Val(ArticulatedPositionXTxtbox.Text)
ArticulatedPositionY = Val(ArticulatedPositionYTxtbox.Text)
WorkingGateGrooveRadius = Val(WorkingGateGrooveRadiusTxtbox.Text)
TrafficBridgeWidth = Val(TrafficBridgeWidthTxtbox.Text)
TrafficBridgeThickness = Val(TrafficBridgeThicknessTxtbox.Text)
End Sub
'重寫父類 onload 方法
Protected Overrides Sub OnLoad(e As EventArgs)
Initialize()
End Sub
'重寫父類的onFormClosed的方法
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
UpdateValues()
End Sub
'包含一些約束判斷條件,使所有的數(shù)據(jù)都在和合理有效的范圍之內(nèi),以防止模型出現(xiàn)錯(cuò)誤。
'門槽深度和邊墩厚的判斷,包括檢修門槽和弧形門槽
Private Sub RepairGateGrooveDepthTxtbox_Leave(sender As Object,e As EventArgs) Handles RepairGateGrooveDepthTxtbox.Leave
If Not (Val(RepairGateGrooveDepthTxtbox.Text) < Val(OutsidePierThicknessTxtbox.Text)) Then
MsgBox("檢修門槽的深度過大,已超過邊墩厚")
RepairGateGrooveDepthTxtbox.SelectAll()
End If
If Not (Val(RepairGateGrooveDepthTxtbox.Text) < Val(MidPierThicknessTxtbox.Text)) Then
MsgBox("檢修門槽的深度過大,已超過中墩厚")
RepairGateGrooveDepthTxtbox.SelectAll()
End If
End Sub
Private Sub WorkingGateGrooveDepthTxtbox_Leave(sender As Object,e As EventArgs) Handles WorkingGateGrooveDepthTxtbox.Leave
If Not (Val(WorkingGateGrooveDepthTxtbox.Text) < Val(OutsidePierThicknessTxtbox.Text)) Then
MsgBox("弧形門槽的深度過大,已超過邊墩厚")
WorkingGateGrooveDepthTxtbox.SelectAll()
End If
If Not (Val(WorkingGateGrooveDepthTxtbox.Text) < Val(MidPierThicknessTxtbox.Text)) Then
MsgBox("弧形門槽的深度過大,已超過中墩厚")
WorkingGateGrooveDepthTxtbox.SelectAll()
End If
End Sub
2.5加載到inventor中進(jìn)行應(yīng)用
將前面創(chuàng)建好的窗體及代碼生成WaterGate.dll文件,并將該文件放到模型的同一目錄下,然后打開規(guī)則瀏覽器,創(chuàng)建一個(gè)規(guī)則,把下面的代碼粘貼進(jìn)去即可。
AddReference "WaterGate.dll"
localTrigger = iTrigger0
Using dlg As New woxing1987.MainForm
dlg.BaseboardLength = BaseboardLength
dlg.BaseboardThickness = BaseboardThickness
dlg.SingleOrificeWidth = SingleOrificeWidth
dlg.OrificeCount = OrificeCount
dlg.BaseboardWidth = BaseboardWidth
dlg.MidPierThickness = MidPierThickness
dlg.OutsidePierThickness = OutsidePierThickness
dlg.PierHigh = PierHigh
dlg.RepairGateGrooveWidth = RepairGateGrooveWidth
dlg.RepairGateGrooveDepth = RepairGateGrooveDepth
dlg.RepairGateDistanceToUpper = RepairGateDistanceToUpper
dlg.WorkingGateGrooveWidth = WorkingGateGrooveWidth
dlg.WorkingGateGrooveDepth = WorkingGateGrooveDepth
dlg.ArticulatedPositionX = ArticulatedPositionX
dlg.ArticulatedPositionY = ArticulatedPositionY
dlg.WorkingGateGrooveRadius = WorkingGateGrooveRadius
dlg.TrafficBridgeWidth = TrafficBridgeWidth
dlg.TrafficBridgeThickness = TrafficBridgeThickness
Dim i As Integer = dlg.ShowDialog()
If i <> vbOK Then Return
BaseboardLength =dlg.BaseboardLength
BaseboardThickness =dlg.BaseboardThickness
SingleOrificeWidth =dlg.SingleOrificeWidth
OrificeCount =dlg.OrificeCount
If OrificeCount=1 Then
' ***中墩陣列***
Feature.IsActive("中墩陣列") = False
' ***中墩弧形門槽拉伸2***
Feature.IsActive("中墩弧形門槽拉伸2") = False
' ***中墩弧形門槽拉伸1***
Feature.IsActive("中墩弧形門槽拉伸1") = False
' ***中墩起始拉伸***
Feature.IsActive("中墩起始拉伸") = False
ElseIfOrificeCount > 1 Then
'***中墩起始拉伸***
Feature.IsActive("中墩起始拉伸") = True
' ***中墩弧形門槽拉伸1***
Feature.IsActive("中墩弧形門槽拉伸1") = True
' ***中墩弧形門槽拉伸2***
Feature.IsActive("中墩弧形門槽拉伸2") = True
' ***中墩陣列***
Feature.IsActive("中墩陣列") = True
End If
BaseboardWidth =dlg.BaseboardWidth
MidPierThickness =dlg.MidPierThickness
OutsidePierThickness =dlg.OutsidePierThickness
PierHigh =dlg.PierHigh
RepairGateGrooveWidth =dlg.RepairGateGrooveWidth
RepairGateGrooveDepth =dlg.RepairGateGrooveDepth
RepairGateDistanceToUpper =dlg.RepairGateDistanceToUpper
WorkingGateGrooveWidth =dlg.WorkingGateGrooveWidth
WorkingGateGrooveDepth =dlg.WorkingGateGrooveDepth
ArticulatedPositionX =dlg.ArticulatedPositionX
ArticulatedPositionY =dlg.ArticulatedPositionY
WorkingGateGrooveRadius =dlg.WorkingGateGrooveRadius
TrafficBridgeWidth =dlg.TrafficBridgeWidth
TrafficBridgeThickness =dlg.TrafficBridgeThickness
End Using
iLogicVb.UpdateWhenDone = True
運(yùn)行效果見圖5,當(dāng)更改文本框中的值并點(diǎn)擊確定按鈕后,inventor中的模型會(huì)立即發(fā)生更改。
圖5 在inventor中運(yùn)行
在自定義用戶界面中可以包含一些簡(jiǎn)單的水力學(xué)計(jì)算,這樣就可以不需要通過Excel計(jì)算,就可以在同一個(gè)界面內(nèi)完成,另外還可以利用VS集成開發(fā)平臺(tái)構(gòu)建更復(fù)雜,更強(qiáng)大,更友好的交互界面,融合一些材料計(jì)算、結(jié)構(gòu)計(jì)算等常規(guī)的力學(xué)內(nèi)容,使得自定義用戶界面元素更豐富,功能更強(qiáng),甚至還可以結(jié)合數(shù)據(jù)庫進(jìn)行更深層次的二次開發(fā)。
總之通過VB.net創(chuàng)建自定義用戶界面,不僅可以達(dá)到界面友好美觀,而且可以很方便的同用戶進(jìn)行交互,比fx參數(shù)列表的界面更加友好,更加直觀,同時(shí)可以擴(kuò)展更多的專業(yè)功能,這一點(diǎn)是f(x)參數(shù)表不能達(dá)到的。
[1]紐瑟姆.Visual Basic 2012入門經(jīng)典[M].北京:清華大學(xué)出版社,2013:26-38.
[2]陳伯雄,馮偉.Inventor-iLogic程序設(shè)計(jì)解析與實(shí)戰(zhàn)[M].北京:化學(xué)工業(yè)出版社,2014:45-51.
1007-7596(2016)05-0139-04
2016-03-15
梁凱旋(1987-),男,河南許昌人,助理工程師,從事水利工程專業(yè)。
TU415
B