2021年4月15日 星期四

如何插入編號遞增的HFSS設計並返回設計名稱

在project插入新的HFSS design需要設定design name,如果design name存在的話,程式則會中斷。以下函式呼叫可以遞增design編號的方式插入新的HFSS design。

import re
import ScriptEnv

ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")
oDesktop.RestoreWindow()
oDesktop.ClearMessages("", "", 2)
oProject = oDesktop.GetActiveProject()


def insertHFSS(custom_name=None):
existing_designs = [i.GetName() for i in oProject.GetDesigns()]

if custom_name:
design_name = custom_name
else:
try:
oDesign = oProject.GetActiveDesign()
design_name = oDesign.GetName()
except:
design_name = 'HFSSDesign0'

m = re.search('(\d+)$', design_name)
if m:
N = int(m.group(1))
else:
design_name = design_name + str(0)
N = 0

while True:
design_name = re.sub('(\d+)$', str(N + 1), design_name)
if design_name not in existing_designs:
break
else:
N += 1

oProject.InsertDesign("HFSS", design_name, "DrivenTerminal", "")

return design_name


insertHFSS()

(圖一) 編號遞增插入設計


沒有留言:

張貼留言