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 | |
最基础使用
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
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 - 类的接口:
AgUiApplicationAPI (常见的是IAgStkObjectRoot) - 调用类的方法:
STKDesktop.StartApplication和STKDesktop.AttachToApplication
Start a new STK Desktop instance¶
核心:
stk = STKDesktop.StartApplication(XXX)
常见参数见官网
实例:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
Attach to a running STK Desktop instance¶
核心:
STKDesktop.AttachToApplication(XXX)
这一步会根据进程PID去“集合”当前运行的STK的所有应用,“加入”一个正在运行的实例
常见参数见官网
实例:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
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
相当于析构函数,写法有两种:
- 在初始化直接写:
stk = STKDesktop.StartApplication(userControl=False) - 在代码末尾:
stk.ShutDown()
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
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 - 类的接口:
STKRuntimeApplicationAPI, 常用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 | |
Attach to a running STKRuntime instance¶
核心:
STKRuntime.AttachToApplication()
常用参数见官网
实例:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 | |
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一样,都是有两种关闭方式,不做赘述
复盘上述的析构操作
值得注意的是,StkDesktop 和 StkRuntime 都具有两种关闭方式,如何因地制宜?
- 对于
Start操作,两种都行 - 对于
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 | |
Finish STK Engine application¶
还是比较推荐显式调用 ShutDown() 函数进行析构
| Python | |
|---|---|
1 2 3 4 5 6 7 | |
析构建议
回顾上面三个class 👀
对析构而言,我们更推荐在脚本末尾显式使用 stk.ShutDown() 方法
基础组件的介绍到此结束,下面我们来介绍STK中的基础数据类型