cst-vba联合仿真之cst自动化导出数据
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 14:06:54 2020
@author: tWX5331009
"""
import numpy as np
import matplotlib.pyplot as plt
import sys
import os
from os.path import dirname, abspath
#设置环境变量
#os.environ['CST_INSTALLPATH_2020'] = r'E:\Program Files (x86)\CST Studio Suite 2020' -> if path is NOT default..
sys.path.append("D:\installation\CST2020\AMD64\python_cst_libraries")
# Import cst modules
# cst.interface - Allows controlling a running CST Studio Suite.
# cst.results - Provides access to 0D/1D Results of a cst file.
# For more information, please take a look at the online help "Automation and Scripting -> Python -> CST Python Libraries"
import cst.results
import cst
import cst.interface
print(cst.__file__) # should print '<PATH_TO_CST_AMD64>\python_cst_libraries\cst\__init__.py
CstFileName = input("请输入cst文件名(勿带cst后缀):")
ProjectName = CstFileName+'.cst'
#getcwd()方法获取当前工作目录+文件名=文件目录
# Initialize secondary variables
ProjectPath = os.path.join(os.getcwd(), ProjectName)
#判断项目是否打开状态
# 首先,用一个开放的DE生成所有PIDs的列表 (cst.interface).
AllPIDs = cst.interface.running_design_environments()
IsOpen = False
# Go through list of PIDs and query projects
for CurrentPID in AllPIDs:
MyCurrentDE = cst.interface.DesignEnvironment.connect(CurrentPID)
for ProjectPath in MyCurrentDE.list_open_projects():
if ProjectPath == ProjectPath:
MyPID = CurrentPID
MyCurrentProject = MyCurrentDE.get_open_project(ProjectPath)
IsOpen = True
break
# In version 2021 I could e.g. now query if a solver is running or not...
if IsOpen:
print('Project is already open...')
else:
print('Project is not open at the moment...')
# If the project is not open already, open it now (cst.interface).
#if not IsOpen:
# MyCurrentDE = cst.interface.DesignEnvironment()
# MyCurrentProject = MyCurrentDE.open_project(ProjectPath)
# Now the project is open and can be worked on...
# PLEASE NOTE: For workflows involving accessing 1D Results it is generally recommended to work with a closed project - although it is also possible to work in interactive mode.
# However, the assumption is that we want to trigger the solver, so we need an open project.
# PLEASE NOTE: With the python interface it is possible at the moment to execute vba commands, but we cannot get information back from the project through this method (at least currently)
# If we wanted to e.g. check on the existance of a 1D Result this could easily be done using the cst.results module.
# But to directly query the existance of e.g. a farfield result we (currently still) need to use the COM interface (which could in principle be done in conjunction with the python interface).
# But, at the moment we are assuming that the farfield has not been calculated yet.
#运行当前选定的解算器,直到它完成,并返回解算器运行是否成功。
# Run the solver:
#MyCurrentProject.modeler.run_solver()
a = input("请输入需要导出到的频率(以空格间隔):")
b = int(input("请输入需要连续导出的端口数,如果指定端口导出,请输入-1:"))
b_list = []
if b == -1:
b_assigned = input("请输入需要指定端口(以空格间隔):")
b_list = str(b_assigned).split(' ')