2022年2月25日 星期五

如何在編輯器環境編寫Classical AEDT API腳本

Spyder當中直接編輯Classical AEDT  API代碼,pip install pypiwin32

from win32com import client

oApp = client.Dispatch("Ansoft.ElectronicsDesktop.2022.1")
oDesktop = oApp.GetAppDesktop()
oDesktop.RestoreWindow()
oProject = oDesktop.NewProject()
oDesign = oProject.InsertDesign("HFSS", "HFSSDesign1", "HFSS Terminal Network", "")
oEditor = oDesign.SetActiveEditor("3D Modeler")
oEditor.CreateCylinder(
[
"NAME:CylinderParameters",
"XCenter:=" , "0mm",
"YCenter:=" , "-0.3mm",
"ZCenter:=" , "0mm",
"Radius:=" , "0.282842712474619mm",
"Height:=" , "0.8mm",
"WhichAxis:=" , "Z",
"NumSides:=" , "0"
],
[
"NAME:Attributes",
"Name:=" , "Cylinder1",
"Flags:=" , "",
"Color:=" , "(143 175 143)",
"Transparency:=" , 0,
"PartCoordinateSystem:=", "Global",
"UDMId:=" , "",
"MaterialValue:=" , "\"vacuum\"",
"SurfaceMaterialValue:=", "\"\"",
"SolveInside:=" , True,
"ShellElement:=" , False,
"ShellElementThickness:=", "0mm",
"IsMaterialEditable:=" , True,
"UseMaterialAppearance:=", False,
"IsLightweight:=" , False
])
#oDesktop.QuitApplication()



如何在編輯器環境編寫SIwave腳本

在Spyder當中使用win32com可以直接編輯SIwave代碼。可以pip install pypiwin32安裝win32com模組。

from win32com import client


oApp = client.Dispatch("SIwave.Application.2022.1")
oApp.RestoreWindow()
oDoc = oApp.GetActiveProject()
oDoc.ScrDrawCircle(10, 0, 20, 'METAL-1', 'NET-1', 'mm')
oDoc.ScrSaveProjectAs ('d:/demo/test215.siw')
oApp.Quit()



2022年2月24日 星期四

pyAEDT如何呼叫舊的API所寫的函數

假使我們之前已經用classical API開發了很多的自動化程式,能否在pyAEDT當中呼叫舊的程式碼,省去從頭開發的時間?答案是可以的,比方說,classical.py當中用舊的API開發的createBoxArray函數,我們可以在pyAEDT程式當中將其匯入,並使用該函數。底下範例使用non-graphical模式在不啟用GUI的狀況底下完成建模。

classical.py

def createBoxArray(m, n, spacing=1):
for i in range(m):
for j in range(n):
oEditor.CreateBox(
[
"NAME:BoxParameters",
"XPosition:=" , "{}mm".format(i*spacing),
"YPosition:=" , "{}mm".format(j*spacing),
"ZPosition:=" , "0mm",
"XSize:=" , "0.3mm",
"YSize:=" , "0.3mm",
"ZSize:=" , "0.4mm"
],
[
"NAME:Attributes",
"Name:=" , "Box1",
"Flags:=" , "",
"Color:=" , "(143 175 143)",
"Transparency:=" , 0,
"PartCoordinateSystem:=", "Global",
"UDMId:=" , "",
"MaterialValue:=" , "\"vacuum\"",
"SurfaceMaterialValue:=", "\"\"",
"SolveInside:=" , True,
"ShellElement:=" , False,
"ShellElementThickness:=", "0mm",
"IsMaterialEditable:=" , True,
"UseMaterialAppearance:=", False,
"IsLightweight:=" , False
])

new.py

from pyaedt import Hfss

hfss = Hfss(specified_version='2022.1', non_graphical=True)

import classical
classical.oEditor = hfss.modeler.oeditor
classical.createBoxArray(4, 8, 1)

hfss.plot()





2022年2月20日 星期日

inheritance Example

 類別繼承基本

class mylist(list):
def __init__(self, x):
super().__init__(x)

def multiply(self, x):
temp = [i for i in self]
self.clear()
for i in temp:
self.append(i * x)


x = mylist([1, 2, 3])
x.append(4)
x.multiply(3)
print(x)

2022年2月19日 星期六

Dynamically Add Method To Object

 動態加入方法到物件當中

class foo:
def __init__(self, v):
self.value = v


def new_method(self, v=None):
self.value += v


x = foo(3)
setattr(x, 'add', lambda v: new_method(x, v))

x.add(4)
print(x.value)