OpenCASCADE+Qt创建建模平台

1、建模平台效果


2、三维控件OCCWidget

  将V3d_View视图与控件句柄绑定即可实现3d视图嵌入Qt中,为了方便也可以基于QOpenGLWidget控件进行封装,方便嵌入各种窗体使用并自由缩放。

cpp 复制代码
#ifndef OCCTWIDGET_H
#define OCCTWIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QKeyEvent>
#include <QApplication>
#include <AIS_InteractiveContext.hxx>
#include <OpenGl_GraphicDriver.hxx>
#include <V3d_View.hxx>
#include <Aspect_Handle.hxx>
#include <Aspect_DisplayConnection.hxx>
#include <Graphic3d_GraphicDriver.hxx>
#include <QOpenGLWidget>
#include <WNT_Window.hxx>

//三维显示窗口

class OCCTWidget : public QOpenGLWidget
{
    Q_OBJECT

public:
    explicit OCCTWidget(QWidget *parent = nullptr);

    //  获取三维环境交互对象
    Handle(AIS_InteractiveContext) getInteractiveContext(){return m_InteractiveContext;}

    //  获取三维显示界面
    Handle(V3d_View)  get3dView(){return m_3dView;}

private:

    // 初始化交互环境
    void initializeInteractiveContext();

    // 交互式上下文能够管理一个或多个查看器(viewer)中的图形行为和交互式对象的选择
    Handle(AIS_InteractiveContext) m_InteractiveContext;

    // 定义查看器(viewer)类型对象上的服务
    Handle(V3d_Viewer) m_3dViewer;

    // 创建一个视图
    Handle(V3d_View) m_3dView;

    // 创建3d接口定义图形驱动程序
    Handle(Graphic3d_GraphicDriver) m_graphicDriver;

protected:

    // 覆写绘图事件
    void paintEvent(QPaintEvent *);

    // 覆写窗口尺寸变化事件
    void resizeEvent(QResizeEvent *);

    // 覆写鼠标按键按下事件
    void mousePressEvent(QMouseEvent *event);

    // 覆写鼠标按键释放事件
    void mouseReleaseEvent(QMouseEvent *event);

    // 覆写鼠标移动事件
    void mouseMoveEvent(QMouseEvent *event);

    // 覆写鼠标滚轮事件
    void wheelEvent(QWheelEvent *event);
private:
    Standard_Integer m_xValue;    // 记录鼠标平移坐标X
    Standard_Integer m_yValue;    // 记录鼠标平移坐标Y
    CurrentAction3d m_currentMode; // 三维场景转换模式
};

#endif // OCCTWIDGET_H
cpp 复制代码
void OCCTWidget::initializeInteractiveContext()
{
    //若交互式上下文为空,则创建对象
    if (m_InteractiveContext.IsNull())
    {
        //此对象提供与X server的连接,在Windows和Mac OS中不起作用
        Handle(Aspect_DisplayConnection) m_display_donnection = new Aspect_DisplayConnection();
        //创建OpenGl图形驱动
        if (m_graphicDriver.IsNull())
        {
            m_graphicDriver = new OpenGl_GraphicDriver(m_display_donnection);
        }
        //获取QWidget的窗口系统标识符
        WId window_handle = (WId) winId();
        
        // 创建Windows NT 窗口
        Handle(WNT_Window) wind = new WNT_Window((Aspect_Handle) window_handle);

        //创建3D查看器
        m_3dViewer = new V3d_Viewer(m_graphicDriver);

        //创建视图
        m_3dView = m_3dViewer->CreateView();
        m_3dView->SetWindow(wind);

        //打开窗口
        if (!wind->IsMapped())
        {
            wind->Map();
        }
        //创建交互式上下文
        m_InteractiveContext = new AIS_InteractiveContext(m_3dViewer);  
        ..........................................
        ..........................................
    }
}

3、三维建模类OCCModeling

  对OCC提供的各种建模函数进行验证,并实现较为复杂的水瓶建模。

cpp 复制代码
#include <BRepPrimAPI_MakeSphere.hxx>
#include <BRepPrimAPI_MakeCone.hxx>
#include <BRepPrimAPI_MakeTorus.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepOffsetAPI_MakePipe.hxx>  
#include <AIS_Shape.hxx>

#include <Geom_TrimmedCurve.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <GC_MakeSegment.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <TopoDS_Wire.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <TopoDS.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <TopExp_Explorer.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <Geom_Plane.hxx>
#include <TopoDS_Edge.hxx>
#include <BRepOffsetAPI_MakeThickSolid.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <BRepLib.hxx>
#include <BRepOffsetAPI_ThruSections.hxx>
#include <Geom_BezierCurve.hxx>

class OCCTModeling
{
public:
	//  生成立方体
	static TopoDS_Shape CreateBoxModel(Standard_Real _dx = 1.0, Standard_Real _dy = 1.0, Standard_Real _dz = 1.0);

	//  生成圆柱
	static TopoDS_Shape CreateCylinder(Standard_Real _R = 0.5, Standard_Real _H = 2.0);

	//  球体
	static TopoDS_Shape CreateSphere(Standard_Real _R = 1.0);

	//  生成圆锥
	static TopoDS_Shape CreateCone(Standard_Real _R1 = 1.0, Standard_Real _R2 = 0.0, Standard_Real _H = 2.0);

	//  生成圆环体
	static TopoDS_Shape CreateTorus(Standard_Real _R1 = 2.0, Standard_Real _R2 = 0.5);

	//  生成水瓶 
	static TopoDS_Shape CreateBottle(Standard_Real _Width = 60.0, Standard_Real _Height = 40.0, Standard_Real _Thickness = 20.0);

	//  生成直管
	static TopoDS_Shape CreatePipe(const Standard_Real dRadius = 20.0, const Standard_Real dThickness = 2.0, const Standard_Real dLength = 100.0);
};

4、源码地址

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁3 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超3 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int3 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
慧都小项3 天前
当边缘AI上产线:QtitanDocking 如何让工业 HMI 实现多视图协同
人工智能·qt·ui·边缘计算·qt6.3
CoderYanger3 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读3 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
CCCCCCCCharlie3 天前
Linux进程控制四大核心操作
linux·开发语言