屏幕录像下载地址:
【STK+Python仿真】搭建仿真环境调试效果_屏幕录像.mp4.zip-其它文档类资源-CSDN下载
啥也不说, Code is everything!
# -*- coding: utf-8 -*-
"""
Python调用STK11.6
可以在STK软件的【STK Help】网页帮助里看到详细介绍:
file:///C:/Program%20Files/AGI/STK%2011/Help/index.htm#training/StartPython.htm%3FTocPath%3DTraining%7CLevel%25202%2520-%2520Advanced%2520Training%7C_____10
网页帮助的文档标题《Part 16: Integrating STK with Python》
"""
# Set up your python workspace
from win32api import GetSystemMetrics
"""
报错:ImportError: No module named win32api
需要安装 pypiwin32 , 手动安装 pypiwin32 如下:
pip install pywin32-226-cp36-cp36m-win32.whl
"""
import comtypes
from comtypes.client import CreateObject
# 以下2句:是因为【 root=uiApplication.Personality2 】 运行后,生成了【comtypes.gen】
from comtypes.gen import STKUtil
from comtypes.gen import STKObjects
###################################################################################
# 第01步: 打开STK软件
# 打开STK11软件 type(uiApplication) 为 <class 'comtypes.POINTER(_IAgUiApplication)'>
uiApplication = CreateObject("STK11.Application")
# 显示 STK11 软件界面
uiApplication.Visible = True
uiApplication.UserControl=True
# Personality2 返回【STK对象模型的根对象】的新实例 IAgStkObjectRoot 接口
# 假如是 Personality, 则 打开现有STK对象
root = uiApplication.Personality2 # root类型: IAgStkObjectRoot
"""
Note: When 'root=uiApplication.Personality2' is executed,
the comtypes library automatically creates a gen folder that contains STKUtil and STK Objects.
After running this at least once on your computer,
the following two lines should be moved before the 'uiApplication=CreateObject("STK11.Application")' line for improved performance.
"""
#from comtypes.gen import STKUtil
#from comtypes.gen import STKObjects
###################################################################################
# 第02步: 创建 一个新的场景
# 创建 一个新的场景
root.NewScenario("Python_Starter")
# 创建的场景 赋给 scenario
# type(scenario) 为 <class 'comtypes.POINTER(IAgStkObject)'>
scenario = root.CurrentScenario
# Remarks: Cast the returned object to IAgScenario to access scenario methods and properties
# COM组件编程 对象转换 用 "QueryInterface"
# 设置 仿真分析 的时间段
scenario2 = scenario.QueryInterface(STKObjects.IAgScenario)
scenario2.SetTimePeriod('Today', '+24hr') # 开始时间:今天;结束时间:24小时
""" +24hr 的语法规则
【网页帮助】:
file:///C:/Program%20Files/AGI/STK%2011/Help/index.htm#../Subsystems/connectCmds/Content/cmd_SetStateClassical.htm
【时间增量/StopTime结束时间】
“+n{Unit}” 输入时间增量。这仅对“StopTime结束时间”有效,并定义添加到开始时间的时间长度。
其中: n是一个整数,
{Unit}可以是以下任意值: Day, Days, Hour, Hours, Hr, Hrs, Min, Mins, Minute, Minutes, Sec, Secs, Second, Seconds
例如: "+1 day"
"""
# 【STK对象模型的根对象root】调用方法: 重置仿真时间;注意: Rewind 本身是 IAgAnimation 的方法
root.Rewind()
"""
Rewind 是 IAgAnimation 的方法:停止、重置动画(Stop and reset the animation.)
type(root) 为 <class 'comtypes.POINTER(_IAgStkObjectRoot)'>
"""
###################################################################################
# 第03步: 添加 一个【目标对象】“地面站目标对象” 、【卫星对象】 到场景中
""" 插入和配置对象
创建了一个新场景后,是时候用对象填充场景了。花点时间使用Python创建一个设施[a facility]和一个LEO卫星[a LEO satellite]。
==> SetState 的经典[Connect]命令和语法可在此处找到!
"""
# ① 添加 【目标对象】“地面站目标对象”
# scenario 类型为 IAgStkObject
# file:///C:/Program%20Files/AGI/STK%2011/Help/Programming/index.htm#DocX/STKObjects~IAgStkObject.html?Highlight=IAgStkObject
# Children属性: 返回当前对象的直接后代的集合; 返回类型为 IAgStkObjectCollection
# IAgStkObjectCollection.New 方法: 使用指定的类和实例名称创建STK对象。
# target = scenario.Children.New(STKObjects.eTarget, "地面站01")
# ==> 注意: COM对象模型编程时, 字符串只能是 英文,如果是中文,会报错!
# _ctypes.COMError:
# (-2147220982, '如果其事件类不存在,则无法储存订购', ('Instance name ???01 incorrectly formatted', None, None, 0, None))
target = scenario.Children.New(STKObjects.eTarget, "IAgTarget-GroundTarget01")
""" 第1个参数: EClassType
eTarget 23 Target
其他的参数取值还有:
eReceiver 17 Receiver
eSatellite 18 Satellite
第2个参数: InstName
对象的名称。将在STK的对象浏览器中显示。
返回类型: IAgStkObject
"""
# 将 New创建 的 IAgStkObject 用 [QueryInterface] 转换为 [STKObjects.IAgTarget] 类型
# ★★★ COM对象模型编程中的命名技巧:
# 在 New 新建时, STKObjects.eTarget 类型,
# 则强制转换时,为QueryInterface(STKObjects.IAgTarget)
# 即, 只需将 前缀 e 改为 前缀 IAg
target2 = target.QueryInterface(STKObjects.IAgTarget)
# 将 地面站对象 的位置 设置为 【 纬度50°,经度-100°,海拔高度0米 】
target2.Position.AssignGeodetic(50,-100,0)
"""
Position 是只读属性, 但“COM对象编程”时,其接口类型为 IAgPosition
通过 IAgPosition 接口的 AssignGeodetic(
Lat, # 纬度
Lon, # 经度
Alt # 海拔高度
)
使用大地坐标表示指定位置的辅助方法(Helper method to assign the position using the Geodetic representation.)
"""
# ②添加 【卫星对象】 到场景 satellite 的类型 为 IAgStkObject
satellite = scenario.Children.New(STKObjects.eSatellite, "LEOsatellite01") # 卫星名字取为LEOsatellite01
# 卫星轨道外推 Propagate the Satellite object's orbit.
# 使用 IAgStkObjectRoot 接口 的 ExecuteCommand 【执行命令】方法
# 注意: 如果更改了 卫星的名称后, 在 CMD命令字符串里面【*/Satellite/LeoSat】中的'LeoSat'名称 也要对应修改!
# 否则报错: _ctypes.COMError:
# (-2147220988, '在查询字符串中使用了一个无效的作用域名', ('Command has failed.', 'AgStkObjects11.AgStkObjectRoot.1', None, 0, None))
strCMD = 'SetState */Satellite/LEOsatellite01 Classical TwoBody "' + scenario2.StartTime + '" "' + scenario2.StopTime + '" 60 ICRF "' + scenario2.StartTime + '" 7200000.0 0.0 90 0.0 0.0 0.0'
root.ExecuteCommand(strCMD)
""" 执行的 命令 【详细分析】
SetState */Satellite/LEOsatellite01 Classical TwoBody "12 Dec 2021 16:00:00.000" "13 Dec 2021 16:00:00.000" 60 ICRF "12 Dec 2021 16:00:00.000" 7200000.0 0.0 90 0.0 0.0 0.0
JSLS备注: 轨道外推的命令是以'SetState''设置状态'为开头的。
★★★ */Satellite/LEOsatellite01 <VehObjectPath>对象路径 中 的 【*】 表示 通配符
调试时可以发现: satellite.Path = '/Application/STK/Scenario/Python_Starter/Satellite/LEOsatellite01'