2021年12月23日 星期四

HFSS API用來載入激發源設定的csv檔

現有的AEDT API不支援載入激發源的csv,這裡我自己寫了一個。

oProject = oDesktop.GetActiveProject()
oDesign = oProject.GetActiveDesign()
oModule = oDesign.GetModule("Solutions")


def loadExcitation(csv_path):
with open(csv_path) as f:
text = f.readlines()

if 'Source,Magnitude,Phase,Terminated,Resistance,Reactance' in text[0]:
items = []
for line in text[1:]:
Source, Magnitude, Phase, Terminated, Resistance, Reactance = line.strip().split(',')
try:
if Terminated == '1':
items.append([
"Name:=" , Source,
"Terminated:=" , True,
"Resistance:=" , Resistance,
"Reactance:=" , Reactance
])
else:
items.append([
"Name:=" , Source,
"Terminated:=" , False,
"Magnitude:=" , Magnitude,
"Phase:=" , Phase
])
except:
pass
AddWarningMessage(str(items))
oModule.EditSources(
[
[
"UseIncidentVoltage:=" , False,
"IncludePortPostProcessing:=", False,
"SpecifySystemPower:=" , False
]
] + items)

else:
items = []
for line in text[1:]:
try:
Source ,Magnitude ,Phase = line.strip().split(',')
items.append([
"Name:=" , Source,
"Magnitude:=" , Magnitude,
"Phase:=" , Phase])
except:
pass
AddWarningMessage(str(items))
oModule.EditSources(
[
[
"IncludePortPostProcessing:=", False,
"SpecifySystemPower:=" , False
]
] + items)


loadExcitation('d:/demo/sss2.csv')


將3D Layout當中沒有封閉的Polyline封閉成Polygon

如果沒有封閉的曲線不是一個polyline,而是多個分離的線段(line),那麼可以先執行Draw > Stitch Lines將其轉換成單一Polyline,再執行下面腳本。形成封閉polygon之後,接下來便可以進行電磁模擬。

oDesktop.ClearMessages("", "", 2)

oProject = oDesktop.GetActiveProject()
oDesign = oProject.GetActiveDesign()
oEditor = oDesign.GetActiveEditor()

selected = oEditor.GetSelections()

for i in selected:
data = []
placement_layer = oEditor.GetPropertyValue('BaseElementTab', i, 'PlacementLayer')

for j in oEditor.GetProperties('BaseElementTab', i):
if j.startswith('Pt'):
location = oEditor.GetPropertyValue('BaseElementTab', i, j)
x, y = [float(k) for k in location.split(',')]
data += ["x:=", x, "y:=", y]

name = oEditor.CreatePolygon(
[
"NAME:Contents",
"polyGeometry:=",
["Name:=", "poly_1002", "LayerName:=", placement_layer, "lw:=", "0", "n:=", len(data), "U:=", "mm", ] + data
])
AddWarningMessage('{} is created!'.format(name))

oEditor.Delete(selected)



2021年12月7日 星期二

在Q3D輸出頻率點

在Q3D當中找不到輸出頻率的API函數。一個方法是在模擬完成之後,用下面程式碼輸出頻率值:

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

oModule = oDesign.GetModule("ReportSetup")
arr = oModule.GetSolutionDataPerVariation(
"Matrix",
"Setup1 : Sweep1",
["Context:=", "Original"],
['Freq:=', ['All']],
["mag(Freq)"])

freqs = arr[0].GetRealDataValues("mag(Freq)")
AddWarningMessage(str(freqs))

2021年12月1日 星期三

匯入.asc的程式碼

 PADS .asc轉檔程式已沒有更新,不建議使用

import os

os.environ['path'] = 'C:\Program Files\AnsysEM\AnsysEM21.2\Win64'

os.system('padstoanf.exe d:/demo/41266AAB-JOB.asc')
os.system('anfv4toanfv2.exe d:/demo/PadsLayout.anf')

oTool = oDesktop.GetTool("ImportExport")
oTool.ImportANFV2("D:/demo/PadsLayout_V2.anf", "D:/temp/PadsLayout_V21.aedb", "", "")