跳转至

STK Basic Component

这一部分我们跟 官方文档

⚠️ 注意,要把科学上网的节点挂到“大🦌 / 🤔港”以外的地方才能访问

STK Python API

Remotable API using gRPC: In addition to traditional OLE communication with STK, the STK Python API has optional gRPC communication for out-of-process or remote interaction with STK.

特性 gRPC OLE
传输 HTTP/2 COM(组件对象模型)
通信方式 支持多种模式(一元、流式) 对对象的直接方法调用
语言支持 与语言无关 主要为 Windows 语言
用例 微服务,远程交互 Windows 应用程序内的自动化
性能 高吞吐量,低延迟 取决于 COM 性能

STK python的设计模式

在STK本身的代码库中,考虑的是面向对象的设计模式,声明对象,之后调用函数 / 返回特性

因此,我们经常使用类似的语句以调用:

Python
1
2
my_object = root.GetObjectFromPath('My Object Path')
my_scenario = root.CurrentScenario

最基础使用

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Start new instance of STK Engine using the new API
from agi.stk12.stkengine import STKEngine
from agi.stk12.stkobjects import *
stk = STKEngine.StartApplication(noGraphics=False) # optionally, noGraphics = True

# Get the IAgStkObjectRoot interface
root = stk.NewObjectRoot()

# Create a new scenario
root.NewScenario('NewSce-2025-0219')

# ......

Module Mapping

官方文档

此处省略,用到再单独看,先浏览一下有哪些即可,不用在入门时逐个啃

STK Desktop application

我们桌面上的图标,点击可运行的那个程序,叫做“STK Desktop application”

STKDesktop.StartApplication and STKDesktop.AttachToApplication methods are available to obtain the STKDesktopApplication class and begin interacting with the STK application through the AgUiApplication API.

From the application interface, the most common way to begin working with the STK application is to use the IAgStkObjectRoot interface, which is accessible as the Root property of the STKDesktopApplication object.

TL;DR
  • 类: STKDesktopApplication
  • 类的接口: AgUiApplication API (常见的是 IAgStkObjectRoot)
  • 调用类的方法: STKDesktop.StartApplicationSTKDesktop.AttachToApplication

Start a new STK Desktop instance

核心:

stk = STKDesktop.StartApplication(XXX)

常见参数见官网

实例:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Start new instance of STK using the new API
from agi.stk12.stkdesktop import STKDesktop
from agi.stk12.stkobjects import *

# Start a new stk application
stk = STKDesktop.StartApplication(visible=True)
# visible: STK GUI works
# not visible: STK GUI off, backend works

# Get the IAgStkObjectRoot interface
# which is a famous Interface for "STKDesktopApplication"
root = stk.Root

Attach to a running STK Desktop instance

核心:

STKDesktop.AttachToApplication(XXX)

这一步会根据进程PID去“集合”当前运行的STK的所有应用,“加入”一个正在运行的实例

常见参数见官网

实例:

Python
1
2
3
4
5
6
7
8
9
# Get reference to running STK instance using the new API
from agi.stk12.stkdesktop import STKDesktop
from agi.stk12.stkobjects import *

# Attach to an existing stk application
stk = STKDesktop.AttachToApplication()

# Get the IAgStkObjectRoot interface
root = stk.Root

Finish with STK Desktop application

STKDesktopApplication provides a ShutDown method that is the recommended way to terminate the connection to the STK application and free up resources.

Set the UserControl property on STKDesktopApplication or when calling StartApplication to set the application behavior after the call to ShutDown.

ShutDown

相当于析构函数,写法有两种:

  1. 在初始化直接写: stk = STKDesktop.StartApplication(userControl=False)
  2. 在代码末尾: stk.ShutDown()
Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Get reference to running STK instance using the new API
from agi.stk12.stkdesktop import STKDesktop
from agi.stk12.stkobjects import *

# Start a new stk application
stk = STKDesktop.StartApplication(userControl=False)
# userControl = False: STK application close after finishing a Python script.

# Do work...
stk.ShutDown()
marshalling across threads

官方文档还提到了“marshalling across threads”(跨线程编组)这个概念与基础操作

这一部分笔者就先忽略了

STKRuntime

STKRuntime is an executable that serves STK Engine functionality via gRPC. Use agi.stk12.stkruntime to start or attach to a running STKRuntime application.

Once the STKRuntimeApplication object is obtained, interact with STK, via IAgStkObjectRoot obtained from calling STKRuntimeApplication.NewObjectRoot().

Note
  • 类: STKRuntimeApplication
  • 类的接口: STKRuntimeApplication API, 常用 IAgStkObjectRoot
  • 调用方法: NewObjectRoot()

STKRuntime针对gRPC服务,并不提供可视化功能 (没有GUI),只在后台做操作!

Start a new STKRuntime instance

核心:

STKRuntime.StartApplication()

⚠️ 注意,STKRuntime does not offer visualizations.

常用参数见官网

实例:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from agi.stk12.stkruntime import STKRuntime

# Start a new stk_runtime app
stk = STKRuntime.StartApplication(grpc_host="0.0.0.0", grpc_port=40704, userControl=False)
# grpc_host: DNS host name
# grpc_port: TCP port
# userControl: auto-close when script finished
print(stk.Version)

root=stk.NewObjectRoot() # interface
root.NewScenario("MyScenario")

Attach to a running STKRuntime instance

核心:

STKRuntime.AttachToApplication()

常用参数见官网

实例:

Python
1
2
3
4
5
6
7
8
from agi.stk12.stkruntime import STKRuntime

# Attach to an existing stk_runtime app
stk = STKRuntime.AttachToApplication(grpc_host="localhost", grpc_port=40704)
print(stk.Version)

root=stk.NewObjectRoot() # interface
stk.ShutDown()

Finish with STK Runtime application

Shutting down the remote STKRuntime process is possible by calling STKRuntimeApplication.ShutDown(), or using the userControl=False option when starting the application.

跟上面STKDesktop一样,都是有两种关闭方式,不做赘述

复盘上述的析构操作

值得注意的是,StkDesktopStkRuntime 都具有两种关闭方式,如何因地制宜?

  1. 对于 Start 操作,两种都行
  2. 对于 Attach 操作,只有显式在末尾 OBJ.ShutDown() 才行

STK Engine application

The STK Engine application runs in process in your Python script, so unlike the STK Desktop application, only one instance of engine is possible, which is started using STKEngine.StartApplication, returning the STKEngineApplication class and giving access to the AgSTKXApplication API.

Unlike STKDesktopApplication, the object model root is not a property and a new root object may be obtained from the NewObjectRoot method on the STKEngineApplication object.

Note
  • 类: STKEngineApplication
  • 类的接口: AgSTKXApplication
  • 调用方法: StartApplication (常用 NewObjectRoot)
Warning

回顾上面三个class 👀

对于API的索取方式分别是:

  • STKDesktop: StartApplication / AttachToApplication
  • STKRuntime: StartApplication / AttachToApplication
  • STKEngine: StartApplication

Start STK Engine application

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Start new instance of STK Engine using the new API
from agi.stk12.stkengine import STKEngine
from agi.stk12.stkobjects import *

# Start a new stk_engine app
stk = STKEngine.StartApplication(noGraphics=False)
# noGraphics=False: show STK GUI

# Get the IAgStkObjectRoot interface
root = stk.NewObjectRoot()

Finish STK Engine application

还是比较推荐显式调用 ShutDown() 函数进行析构

Python
1
2
3
4
5
6
7
# Get reference to running STK instance using the new API
from agi.stk12.stkengine import STKEngine
from agi.stk12.stkobjects import *
stk = STKEngine.StartApplication()

# Do work...
stk.ShutDown()
析构建议

回顾上面三个class 👀

对析构而言,我们更推荐在脚本末尾显式使用 stk.ShutDown() 方法

基础组件的介绍到此结束,下面我们来介绍STK中的基础数据类型