2021年3月13日 星期六

如何用命令列啟動script?

本範例可以用bat檔啟動視窗,視窗可以保持在最上層,但是又可以操作AEDT環境。兩者可以平行操作。不像之前的toolkit會鎖定AEDT環境。總共有三個檔案,以下為程式範例:

run.bat

SET PATH=C:\Program Files\AnsysEM\AnsysEM20.2\Win64\common\IronPython\
ipy64 test.py
pause

test.xaml
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Template Window" Height="144.165" Width="300" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Background="#FFEAEBFF" Topmost="True">
<Grid>
<Button Content="Execute" HorizontalAlignment="Right" Height="36" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="93" Click="Button_Click"/>
<TextBox x:Name="input_tb" HorizontalAlignment="Right" Height="27" Margin="0,10,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="214" VerticalContentAlignment="Center"/>
<Label Content="Value:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="55"/>
</Grid>
</Window>

test.py
# coding=UTF-8
import os, sys, re, clr, json, math, cmath

# 初始化環境,設定win64_path
win64_path = 'C:/Program Files/AnsysEM/AnsysEM20.2/Win64'
sys.path.append(win64_path)
sys.path.append(win64_path + '/PythonFiles/DesktopPlugin/')
sys.path.append(win64_path + '/common/IronPython/DLLs')

import ScriptEnv

ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")
from Ansys.Ansoft.CoreCOMScripting.Util import COMUtil

# 初始化WPF視窗環境
clr.AddReference('IronPython.Wpf')
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import wpf
from System.Windows import Window
from System.Windows.Forms import OpenFileDialog, SaveFileDialog, FolderBrowserDialog, DialogResult

# 程式主體放在try段落當中,
try:
oProject = oDesktop.GetActiveProject()
oDesign = oProject.GetActiveDesign()
oEditor = oDesign.SetActiveEditor("3D Modeler")


def AddWarningMessage(info):
oDesktop.AddMessage(oProject.GetName(), oDesign.GetName(), 1, info)


class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'test.xaml')
oDesktop.ClearMessages("", "", 2)

def Button_Click(self, sender, e):
AddWarningMessage(self.input_tb.Text)


MyWindow().ShowDialog()
except:
raise

# 程式執行成功與否,都能釋放AEDT專案
finally:
COMUtil.PInvokeProxyAPI.ReleaseCOMObjectScope(0)

(圖一)程式執行跳出視窗,同時仍能設定AEDT


沒有留言:

張貼留言