用OCC+VS+Qt创建并显示一个几何
OCC+VS+QT相关配置详细见
OCC+ QT+VS、OCC+QtCreator 环境配置
创建视图并显示几何
OCC中显示过程如下:
- 获取
OpenGl_GraphicDriverr
图形驱动 - 基于图形驱动初始化一个
V3d_Viewer
- 通过Viewer创建一个视图View,并设置显示窗口
- 通过Viewer初始化一个AIS_Context交互环境,并设置其显示方式(shaded或者wire)
- 将几何模型转换为拓扑类型
- 经拓扑模型转换为AIS类型(交互对象类型)
- 在context中显示交互对象
.h文件
#pragma once
#include <QtWidgets/QMainWindow>
#include <Standard_Handle.hxx>
#include <V3d_Viewer.hxx>
#include <OpenGl_GraphicDriver.hxx>
#include <WNT_Window.hxx>
#include <V3d_View.hxx>
#include <AIS_InteractiveContext.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <TopoDs_Shape.hxx>
#include <AIS_Shape.hxx>
class QtWidgetsApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication1(QWidget *parent = nullptr);
~QtWidgetsApplication1();
protected:
//重写绘图事件
void paintEvent(QPaintEvent *event) override;
//返回窗口绘图引擎
QPaintEngine *paintEngine() const;
private:
Ui::QtWidgetsApplication1Class ui;
//定义查看器viewer 3D查看器
Handle(V3d_Viewer) viewer;
//视图
Handle(V3d_View) view;
//交互式上下文,管理一个或者多个viewer
Handle(AIS_InteractiveContext) context;
//window NT窗口
Handle(WNT_Window) window;
};
cpp文件
#include "QtWidgetsApplication1.h"
QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent)
: QMainWindow(parent)
{
//提供X server的连接,在window和Mac中不起作用
Handle(Aspect_DisplayConnection) hAspect_DisplayConnect = new Aspect_DisplayConnection;
//创建3D接口定义图形驱动
Handle(OpenGl_GraphicDriver) driver = new OpenGl_GraphicDriver(hAspect_DisplayConnect);
//该类的方法允许编辑、询问连接该类的其他参数(如视图、光)
viewer = new V3d_Viewer(driver);
view = viewer->CreateView();
WId win_handle = winId();
//在已有的窗口上创建窗口
window = new WNT_Window((Aspect_Handle)win_handle);
view->SetWindow(window);
if (!window->IsMapped())
{
window->Map();//打开窗口
}
view->SetBackgroundColor(Quantity_NOC_BLACK);
view->MustBeResized();
viewer->SetDefaultLights();
setAttribute(Qt::WA_PaintOnScreen);
//交互式上下文
context = new AIS_InteractiveContext(viewer);
context->SetDisplayMode(AIS_Shaded, Standard_True);
TopoDS_Shape box = BRepPrimAPI_MakeBox(10, 10, 10);
Handle(AIS_Shape) abox = new AIS_Shape(box);
context->Display(abox, Standard_True);
view->FitAll();
}
QtWidgetsApplication1::~QtWidgetsApplication1()
{}
void QtWidgetsApplication1::paintEvent(QPaintEvent * event)
{
view->Redraw();
}
QPaintEngine * QtWidgetsApplication1::paintEngine() const
{
return 0;
}