Python + STK(一)
1、前期软件基础准备
- Python(CSDN上已有详细教程)
- STK(11.6版本),CSDN上也有很多教程,这里不再赘述
注意: 在下载STK时,会有弹窗蹦出来,此时不需改动文件下载的位置,保持系统的位置继续下载,否则后续可能会有路径问题。
2、Python连接STK
2.1 首次连接配置
- 安装compytes库
创建一个Pycharm项目,首先使用pip install方法下载所需要的compytes模块,可以下载主路径,也可以隔离出虚拟环境进行下载(参考作者之前的文章)。
pip install comtypes
- 首次连接生成Python开发所需要的库
import comtypes
from comtypes.client import CreateObject
app=CreateObject("STK12.Application")app.Visible = True
print('app的类型为:',type(app))
# 获取Object Model的根对象:IAgStkObjectRoot
# 此接口为Object Model中的最顶层接口,由此接口可创建场景、地面站、卫星等
root = app.Personality2
print('root的类型为:',type(root))
# 创建Astrogator相关的模块:AgStkGatorLib
# 如果报错[WinError -2147319779] 库没有注册。修改为FEAEF02E-48CE-42AE-B99B-FB9871A69E4B
comtypes.client.GetModule((comtypes.GUID("{090D317C-31A7-4AF7-89CD-25FE18F4017C}") ,1,0))
print('python 首次连接STK完成!')
print('STK Object Model API 的python模块已在comtypes\gen目录下创建!')
print('请关闭已打开的STK!')
2.2 安装AGI库
- AGI库的位置
- AGI库的下载
同样使用pip install的方法进行下载,可以在命令提示符中进行下命令。如在Pycharm中引用模块时没有报警,则说明下载成功。
注意: 如果下载完,命令提示符中显示成功下载,但Pycharm仍然告警,可以看下面两张图的文件位置是否一致。
2.3 连接STK
- 导入模块
# 导入STK 12.2版本的Python API库
from agi.stk12.stkdesktop import STKDesktop
from comtypes.gen.STKObjects import AgESTKObjectType
from agi.stk12.stkobjects import *
from agi.stk12.stkutil import *
from agi.stk12.vgt import *
import os
-
获取STK_PID
首先打开STK,然后在命令提示符里输入“tasklist | findstr AgUiApplication.exe”进行查找,每次都需要更新 -
根据获取的STK_PID打开场景
stk = STKDesktop.AttachToApplication(pid=int(STK_PID)) # 通过进程ID连接到已经运行的STK应用程序实例
root = stk.Root # 获取STK的根对象,可通过此接口实现STK应用程序的访问和操作
print(type(root))
if root.CurrentScenario is not None: # 检查是否已有场景,如果有,则关闭当前场景。(STK_PID需要打开STK才能获取)
root.CloseScenario()
root.NewScenario("NewScenario") # 创建一个新场景
print("===================Duration time of new scenario=======================")
scenario = root.CurrentScenario # 获取当前场景对象
scenario.SetTimePeriod('Today','+24hr') # 设置场景的时间周期(当前时间-持续24hr)
print(scenario.StartTime)
print(scenario.StopTime)
- 根据想要的功能进行代码编写
# 在场景scenario中使用Children.new方法添加北京、喀什、三亚三个遥感卫星数据接收地面站
target1 = scenario.Children.New(AgESTKObjectType.eTarget, "BeiJing")
target1.Position.AssignGeodetic(40.37, 116.46, 0)
target2 = scenario.Children.New(AgESTKObjectType.eTarget, "KaShi")
target2.Position.AssignGeodetic(18.15, 109.30, 0)
target3 = scenario.Children.New(AgESTKObjectType.eTarget, "SanYa")
target3.Position.AssignGeodetic(39.32, 75.59, 0)
# 在场景中添加一个卫星
satellite = scenario.Children.New(AgESTKObjectType.eSatellite, "LeoSat")
# 设置卫星轨道(两极轨道模型、卫星轨道周期为60分钟、半长轴7200公里、偏心率0、轨道倾角90、升交点经度0、近地点角距0、真近角点0)
root.ExecuteCommand('SetState */Satellite/LeoSat Classical TwoBody "' +
str(scenario.StartTime) + '" "' + str(scenario.StopTime) +
'" 60 ICRF "' + str(scenario.StartTime) + '" 6678140 0.0 28.5 0.0 0.0 0.0')
# 计算卫星和目标之间的连接
access = satellite.GetAccessToObject(target1)
access.ComputeAccess()
# 导出连接数据
accessDP = access.DataProviders.Item('Access Data')
results = accessDP.Exec(scenario.StartTime, scenario.StopTime)
accessStartTime = results.DataSets.GetDataSetByName('Start Time').GetValues()
accessStopTime = results.DataSets.GetDataSetByName('Stop Time').GetValues()
3、如何写代码
3.1 官方文档库
Python Code Snippets
注意: 需要一点科技才能进入
3.2 大模型的使用
对模型输入想要的功能,获取对应的代码。但有时代码是有错的,需要自己进行理解然后对其进行修正