Python 调用STK接口的常用操作

2024-01-23 06:52

本文主要是介绍Python 调用STK接口的常用操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

和Python如何调用STK接口编程-CSDN博客这里说的一样,STK Help文档里面其实有讲,但也有不少坑。所以这边整理了一些对Satellite, Target, Aircraft等对象的基本操作:

生成STK场景:

import pymysql
import win32comfrom win32com.client import GetActiveObject
import comtypes
from comtypes.client import CreateObject
from comtypes.gen import STKUtil
from comtypes.gen import STKObjects
from comtypes.gen import AgSTKGraphicsLib
from comtypes.gen import AgSTKVgtLib
from comtypes.gen import AgUiApplicationLib
from comtypes.gen import AgUiCoreLib
from comtypes.gen import stdole
from comtypes.gen import STKObjectsclass STKWindow:def __init__(self,flag=False):if flag==True:self.uiApplication = win32com.client.Dispatch('STK11.Application')self.uiApplication.Visible = Trueself.uiApplication = CreateObject('STK11.Application')self.uiApplication.Visible = True# Get our IAgStkObjectRoot interfaceself.root = self.uiApplication.Personality2elif flag==False:try:self.uiApplication=GetActiveObject('STK11.Application')self.root = self.uiApplication.Personality2self.sc=self.root.CurrentScenarioself.scIAg=self.sc.QueryInterface(STKObjects.IAgScenario)except:print("no active STK or Scenario") else:self.uiApplication = win32com.client.Dispatch('STK11.Application')self.uiApplication.Visible = Trueself.uiApplication = CreateObject('STK11.Application')self.uiApplication.Visible = Trueself.root = self.uiApplication.Personality2self.root.Load(flag)self.sc=self.root.CurrentScenarioself.scIAg=self.sc.QueryInterface(STKObjects.IAgScenario)def newScenario(self,name,startTime,endTime):self.root.NewScenario(name)self.sc=self.root.CurrentScenarioself.scIAg=self.sc.QueryInterface(STKObjects.IAgScenario)scIAg.SetTimePeriod(startTime,endTime)self.root.Rewind()return self.scdef connect(sql):conn = pymysql.connect(host="127.0.0.1",user ="root", passwd ="123456", db = "task scheduling", port=3306,charset = 'utf8')cur =conn.cursor() cur.execute(sql)if sql.strip()[:6].upper()=='SELECT':res=cur.fetchall()else:coon.commit()res='ok'cur.close()conn.close()return res

Spacecraft:

class Spacecraft:def __init__(self,name,Type='',Time='1 Jan 2020 00:00:00.000',revolve=10,inclination=0,RAAN=0,argumentOfPerigee=0,eccentricity=0,MeanAnomaly=0):self.name=nameself.Type=Typeself.Time=Timeself.revolve=revolveself.inclination=inclinationself.RAAN=RAANself.argumentOfPerigee=argumentOfPerigeeself.eccentricity=eccentricityself.MeanAnomaly=MeanAnomalyself.uniqueNumber=18self.cmds=[]self.cmds.append('New / */Satellite %s' %name)self.cmds.append('New / */Satellite %s' %name)def queryFromSecenario(self,sc):try:sats=sc.Children.GetElements(self.uniqueNumber)sat=sats.Item(self.name)return satexcept:return Nonedef deleteFromSecenario(self,sc):if self.queryFromSecenario(sc):sc.Children.Unload(self.uniqueNumber,self.name)return 'already deleted'else:return 'not exist'def addToScenario(self,root,sc):if self.queryFromSecenario(sc):
#             'can not add, this name already exists'return Nonesat=sc.Children.New(self.uniqueNumber, self.name) # eSatellite satIAg= sat.QueryInterface(STKObjects.IAgSatellite)satIAg.PropagatorSupportedTypessatIAg.SetPropagatorType(STKObjects.ePropagatorJ4Perturbation)satProp = satIAg.PropagatorsatProp=satProp.QueryInterface(STKObjects.IAgVePropagatorJ4Perturbation)satProp.InitialState.Epoch=self.Timekeplerian = satProp.InitialState.Representation.ConvertTo(STKUtil.eOrbitStateClassical)keplerian2 = keplerian.QueryInterface(STKObjects.IAgOrbitStateClassical)keplerian2.SizeShapeType =STKObjects.eSizeShapeMeanMotion
#         keplerian2.SizeShapeType =STKObjects.eSizeShapeSemimajorAxiskeplerian2.LocationType = STKObjects.eLocationMeanAnomalykeplerian2.Orientation.AscNodeType = STKObjects.eAscNodeRAANroot.UnitPreferences.Item('AngleUnit').SetCurrentUnit('revs')root.UnitPreferences.Item('TimeUnit').SetCurrentUnit('day')root.UnitPreferences.Item('Distance').SetCurrentUnit('km')print(keplerian2.SizeShape)keplerian2.SizeShape.QueryInterface(STKObjects.IAgClassicalSizeShapeMeanMotion).MeanMotion = self.revolvekeplerian2.SizeShape.QueryInterface(STKObjects.IAgClassicalSizeShapeMeanMotion).Eccentricity = self.eccentricity
#         keplerian2.SizeShape.QueryInterface(STKObjects.IAgClassicalSizeShapeSemimajorAxis).SemimajorAxis = self.SemimajorAxis
#         keplerian2.SizeShape.QueryInterface(STKObjects.IAgClassicalSizeShapeSemimajorAxis).Eccentricity = self.Eccentricityroot.UnitPreferences.Item('AngleUnit').SetCurrentUnit('deg')root.UnitPreferences.Item('TimeUnit').SetCurrentUnit('sec')keplerian2.Orientation.Inclination = self.inclinationkeplerian2.Orientation.ArgOfPerigee = self.argumentOfPerigeekeplerian2.Orientation.AscNode.QueryInterface(STKObjects.IAgOrientationAscNodeRAAN).Value = self.RAANkeplerian2.Location.QueryInterface(STKObjects.IAgClassicalLocationMeanAnomaly).Value = self.MeanAnomalysatProp.InitialState.Representation.Assign(keplerian)satProp.Propagate()self.sat=satself.satIAg=satIAgreturn satIAg

Target:

class Target:def __init__(self,name,Longitude=0,Latitude=0,Altitude=0):self.name=nameself.Longitude=Longitudeself.Latitude=Latitudeself.Altitude=Altitudeself.uniqueNumber=23def queryFromSecenario(self,sc):try:targets=sc.Children.GetElements(self.uniqueNumber)target=targets.Item(self.name)return targetexcept:return Nonedef deleteFromSecenario(self,sc):if self.queryFromSecenario(sc):sc.Children.Unload(self.uniqueNumber,self.name)return 'already deleted'else:return 'not exist'def addToScenario(self,sc):if self.queryFromSecenario(sc):
#             'can not add, this name already exists'return Nonetarget = sc.Children.New(self.uniqueNumber, self.name) targetIAg = target.QueryInterface(STKObjects.IAgTarget)pos = targetIAg.Positionpos.AssignGeodetic(self.Latitude,self.Longitude,self.Altitude)self.targetIAg=targetIAgself.target=targetreturn targetIAg

Aircraft:

class Aircraft:def __init__(self,name):self.name=nameself.uniqueNumber=1def queryFromSecenario(self,sc):try:aircrafts=sc.Children.GetElements(self.uniqueNumber)aircraft=aircrafts.Item(self.name)return aircraftexcept:return Nonedef deleteFromSecenario(self,sc):if self.queryFromSecenario(sc):sc.Children.Unload(self.uniqueNumber,self.name)return 'already deleted'else:return 'not exist'def addToScenario(self,sc):if self.queryFromSecenario(sc):
#             'can not add, this name already exists'return Noneaircraft = sc.Children.New(self.uniqueNumber, self.name) aircraftIAg = aircraft.QueryInterface(STKObjects.IAgAircraft)self.aircraftIAg=aircraftIAgself.aircraft=aircraftreturn aircraftIAgdef addWayPoints(self,points):self.aircraftIAg.SetRouteType(STKObjects.ePropagatorGreatArc)route = self.aircraftIAg.Routeroute = route.QueryInterface(STKObjects.IAgVePropagatorGreatArc)route.Method=STKObjects.AgEVeWayPtCompMethod(0)try:for point in points:waypoint = route.Waypoints.Add()waypoint.Longitude = point[0]waypoint.Latitude = point[1]waypoint.Altitude = point[2]waypoint.Speed=point[3]route.Propagate()return 'add waypoints successfully'except:return 'add waypoints unsuccessfully'def removeWayPoints(self):self.aircraftIAg.SetRouteType(STKObjects.ePropagatorGreatArc)route = self.aircraftIAg.Routeroute = route.QueryInterface(STKObjects.IAgVePropagatorGreatArc)route.Waypoints.RemoveAll()route.Propagate()

Ship:

class Ship:def __init__(self,name):self.name=nameself.uniqueNumber=21def queryFromSecenario(self,sc):try:ships=sc.Children.GetElements(self.uniqueNumber)ship=ships.Item(self.name)return shipexcept:return Nonedef deleteFromSecenario(self,sc):if self.queryFromSecenario(sc):sc.Children.Unload(self.uniqueNumber,self.name)return 'already deleted'else:return 'not exist'def addToScenario(self,sc):if self.queryFromSecenario(sc):
#             'can not add, this name already exists'return Noneship = sc.Children.New(self.uniqueNumber, self.name) shipIAg = ship.QueryInterface(STKObjects.IAgShip)self.shipIAg=shipIAgself.ship=shipreturn shipIAgdef addWayPoints(self,points):self.shipIAg.SetRouteType(STKObjects.ePropagatorGreatArc)route = self.shipIAg.Routeroute = route.QueryInterface(STKObjects.IAgVePropagatorGreatArc)route.Method=STKObjects.AgEVeWayPtCompMethod(0)try:for point in points:waypoint = route.Waypoints.Add()waypoint.Longitude = point[0]waypoint.Latitude = point[1]waypoint.Altitude = point[2]waypoint.Speed=point[3]route.Propagate()return 'add waypoints successfully'except:return 'add waypoints unsuccessfully'def removeWayPoints(self):self.shipIAg.SetRouteType(STKObjects.ePropagatorGreatArc)route = self.shipIAg.Routeroute = route.QueryInterface(STKObjects.IAgVePropagatorGreatArc)route.Waypoints.RemoveAll()route.Propagate()

获取可见性:

def addAccess(Monitor,Monitored):Monitor=Monitor.QueryInterface(STKObjects.IAgStkObject)Monitored=Monitored.QueryInterface(STKObjects.IAgStkObject)access = Monitor.GetAccessToObject(Monitored)access.ComputeAccess()results = access.ComputedAccessIntervalTimesresults=results.ToArray(0,results.Count)return results

使用示例:

def addAccess(Monitor,Monitored):Monitor=Monitor.QueryInterface(STKObjects.IAgStkObject)Monitored=Monitored.QueryInterface(STKObjects.IAgStkObject)access = Monitor.GetAccessToObject(Monitored)access.ComputeAccess()results = access.ComputedAccessIntervalTimesresults=results.ToArray(0,results.Count)return results# open STK and creat Scenario
STKW=STKWindow('E:/AGI/STKFiles/test1/test1.sc')
sc=STKW.newScenario('test1',"1 Jan 2020 00:00:00.000 ","8 Jan 2020 00:00:00.000 ")# select satellites from datebase
sql="select name,type,time,revolve,inclination,RAAN,argumentOfPerigee,eccentricity,MeanAnomaly from spacecraft"
sat=connect(sql)[0]
# (self,name,Type,Time,revolve=10,inclination=0,RAAN=0,argumentOfPerigee=0,eccentricity=0,MeanAnomaly=0):
sat=Spacecraft(sat[0],sat[1],sat[2],sat[3],sat[4],sat[5],sat[6],sat[7],sat[8])sql='select name,Longitude,Latitude,Altitude from Target'
target=connect(sql)[0]
target=Target(target[0],target[1],target[2],target[3])# new satellite and Target
addSat=sat.addToScenario(STKW.root,STKW.sc)
addTarget=target.addToScenario(STKW.sc)# sat.deleteFromSecenario(STKW.sc)
# target.deleteFromSecenario(STKW.sc)#add access
accessTimes=addAccess(sat.satIAg,target.targetIAg)
for accessTime in accessTimes:print(accessTime)
# sats = STKW.root.CurrentScenario.Children.GetElements(18)# print(type(sats.Item('satellite1')),type(sat.satIAg))aircraft=Aircraft('aircraft1')
aircraft.addToScenario(STKW.sc)sql="select Longitude,Latitude,Altitude,Speed from aircraft"
waypoints=connect(sql)
aircraft.addWayPoints(waypoints)ship=Ship('ship1')
ship.addToScenario(STKW.sc)sql="select Longitude,Latitude,Altitude,Speed from ship"
waypoints=connect(sql)
for point in waypoints:print(point[0])
ship.addWayPoints(waypoints)STKW.root.SaveAs('E:/AGI/STKFiles/test1/test1')ship = sc.Children.New(21, 'ship1') 
shipIAg = ship.QueryInterface(STKObjects.IAgShip)
shipIAg.SetRouteType(STKObjects.ePropagatorGreatArc)
route = shipIAg.Route
route = route.QueryInterface(STKObjects.IAgVePropagatorGreatArc)
route.Method=STKObjects.AgEVeWayPtCompMethod(0)waypoint1 = route.Waypoints.Add()
waypoint1.Latitude = 39
waypoint1.Longitude = -79
waypoint1.Altitude = 10
waypoint1.Speed=0.001waypoint2 = route.Waypoints.Add()
waypoint2.Latitude = 40
waypoint2.Longitude = -80
waypoint2.Altitude = 10
waypoint2.Speed=0.001waypoint3 = route.Waypoints.Add()
waypoint3.Latitude = 41
waypoint3.Longitude = -81
waypoint3.Altitude = 10
waypoint3.Speed=0.001route.Propagate()

这篇关于Python 调用STK接口的常用操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/635562

相关文章

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很