QT5-STK二次开发实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012435142/article/details/80271227

QT-STK二次开发实例

版本
STK 9
Qt 5
Qt编译器 MSVC

STK是由美国Analytical Graphics公司开发的一款在航天领域处于领先地位的商业分析软件。

QT的一套跨平台的C++图形用户界面库,上手简单,功能强大,是目前GUI开发者广泛使用的C++图形库。这篇文章主要介绍使用QT进行STK二次开发过程中的STK接口的配置实例。STK是由美国Analytical Graphics公司开发的一款航天商业分析软件,本身支持C,C++,Java,Matlab等多种语言的二次开发。下面介绍在QT Creator中利用axWidgets显示STK三维场景简单例子,因为在整个二次开发中STK的类实例只能有一个,所以下面的例子中STK类是一个单例模式。

1 环境依赖

需要计算机首先正确安装STK(我安装的版本是STK9,其他版本应该类似),打开STK安装目录C:\Program Files (x86)\AGI\STK 9\CodeSamples\CommonFiles其中文件夹CppIncludes就是我们需要包含到自己的程序中的文件。
这里写图片描述

2 QT编程

在QT项目中添加STK.h和STK.cpp(当然,你也可以改成其他名字,我这里就简单设置为STK.h和STK.cpp了),其中STK.h中包含CppIncludes目录中的AgStkUtil.tlh、AgVGT.tlh、AgStkObjects.tlh、STKX.tlh,注意需要引用相应的命名空间:
STK.h

#include "AgStkUtil.tlh"
using namespace STKUtil;//引用相应命名空间
#include "AgVGT.tlh"
#include "AgStkObjects.tlh"
using namespace STKObjects;
#include "STKX.tlh"
using namespace STKXLib;

STK.cpp

#include "STK.h"
#include "AgStkUtil.tli"
#include "AgStkObjects.tli"
#include "STKX.tli"

3 创建STK功能类QSTKEarth

QSTKEarth.h

#ifndef QSTKEARTH_H
#define QSTKEARTH_H

#include "STK.h"
#include <QWidget>
#include <ActiveQt/QAxWidget>
#include <QDebug>

class QSTKEarth: public QWidget
{
    Q_OBJECT
public:
    static QSTKEarth &getInstance()
    {
      if(instance==NULL)
        {
          QMutexLocker locker(&mutex);
          if(NULL==instance)
            instance=new QSTKEarth;
        }
      return *instance;
    }
    bool enableControl;
    ~QSTKEarth();
private:
    static QMutex mutex;
    static QAtomicPointer<QSTKEarth> instance;
    QSTKEarth(const QSTKEarth &);
    QSTKEarth(QWidget *parent = 0);
    IAgStkObjectRootPtr  m_pRoot;
    IAgSTKXApplicationPtr m_app;

public:
    void PauseSTK();
    void StartSTK();
    void FasterSTK();
    void SlowerSTK();
    void ResetSTK();
    void NewScenario();
    void LoadScenario();
    void UnloadStkScence();
};

#endif // QSTKEARTH_H

QSTKEarth.cpp

#include "QSTKEarth.h"
#include <QMessageBox>
#include <QDebug>
#include <QVBoxLayout>
#include <QFileDialog>

QMutex QSTKEarth::mutex;
QAtomicPointer<QSTKEarth> QSTKEarth::instance=0;

QSTKEarth::QSTKEarth(QWidget *parent) : QWidget(parent)
{
    ::CoInitialize();//必须加上
    // Create a new instance of Automation Object Model Root Object
    HRESULT ha=m_app.CreateInstance(__uuidof(AgSTKXApplication));
    if(FAILED(ha))
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("系统提示"),
                             QString::fromLocal8Bit("创建程序对象失败!"));
    }
    HRESULT hr = m_pRoot.CreateInstance(__uuidof(AgStkObjectRoot));
    if(FAILED(hr))
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("系统提示"),
                             QString::fromLocal8Bit("创建程序对象失败!"));
    }
    enableControl=false;
}
QSTKEarth::~QSTKEarth()
{
    m_pRoot.Release();
    m_app.Release();
    //::CoUninitialize();
}
void QSTKEarth::NewScenario()
{
    Q_ASSERT(m_app!=NULL);
    STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);
    pSTKXapp->ExecuteCommand("Unload / *");
    pSTKXapp->ExecuteCommand("New / Scenario ScenOne");
    enableControl=true;
}
void QSTKEarth::LoadScenario()//加载场景
{
    Q_ASSERT(m_pRoot!=NULL);
    m_pRoot->CloseScenario();
    m_pRoot->LoadScenario(_bstr_t("..\\data\\Scenario1.sc"));
    enableControl=true;
}
void QSTKEarth::PauseSTK()
{
    if(enableControl)
    {
        Q_ASSERT(m_app != NULL);
        STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);
        pSTKXapp->ExecuteCommand("Animate * Pause");
                //    pSTKXapp->Pause();//也可以直接调用类成员函数
    }
}
void QSTKEarth::FasterSTK()
{
    if(enableControl)
    {
        Q_ASSERT(m_app != NULL);
        STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);
        pSTKXapp->ExecuteCommand("Animate * Faster");
    }
}
void QSTKEarth::SlowerSTK()
{
    if(enableControl)
    {
        Q_ASSERT(m_app != NULL);
        STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);
        pSTKXapp->ExecuteCommand("Animate * Slower");
    }
}
void QSTKEarth::ResetSTK()
{
    if(enableControl)
    {
        Q_ASSERT(m_pRoot != NULL);
        STKObjects::IAgAnimationPtr pAnimation( m_pRoot );
        pAnimation->Rewind();
    }
}
void QSTKEarth::UnloadStkScence()//卸载场景
{
    Q_ASSERT(m_app!=NULL);
    STKXLib::IAgSTKXApplicationPtr pSTKXapp(m_app);
    pSTKXapp->ExecuteCommand("UnloadMulti / */Satellite/*");
    pSTKXapp->ExecuteCommand("UnloadMulti / */Missile/*");
    pSTKXapp->ExecuteCommand("Unload / *");
    enableControl=false;
}

主界面程序

#include "QT_STK.h"
#include "ui_QT_STK.h"

QT_STK::QT_STK(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::QT_STK)
{
  ui->setupUi(this);
}

QT_STK::~QT_STK()
{
  delete ui;
}

void QT_STK::on_pushButton_clicked()
{
  QSTKEarth *m_stkEarth=&QSTKEarth::getInstance();
  m_stkEarth->NewScenario();
}

4 主界面设置和运行结果

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u012435142/article/details/80271227