[C++] OpenCasCade空间几何库的模型展现

article/2025/9/24 11:18:57

OpenCasCade是什么

Open CASCADE(简称OCC)平台是由法国Matra Datavision公司开发的CAD/CAE/CAM软件平台,可以说是世界上最重要的几何造型基础软件平台之一。开源OCC对象库是一个面向对象C++类库,用于快速开发设计领域的专业应用程序。

最近一直在利用OpenCasCade来进行空间几何的相关算法,于是有了这个教程让大家可以更直观的看到occ内部对象最终组装呈现的实际效果是什么。

利用OpenGL如何让occ做简单模型展示

1.依赖引用

预编译头stdafx.h中加入头文件引用与静态描述引用

#include <V3d_View.hxx>               //V3d_View
#include <WNT_Window.hxx>             //Handle_WNT_Window
#include <AIS_InteractiveContext.hxx> //AIS_InteractiveContext
#include <AIS_Shape.hxx>              //Handle_AIS_Shape
#include <Standard.hxx>
#include <Standard_PrimitiveTypes.hxx>
#include <AIS_InteractiveContext.hxx>
#include <AIS_Line.hxx>
#include <AIS_Shape.hxx>
#include <AIS_Point.hxx>
#include <AIS_TexturedShape.hxx>
#include <Aspect_Grid.hxx>
#include <Aspect_PolygonOffsetMode.hxx>
#include <Aspect_DisplayConnection.hxx>
#include <gp.hxx>
#include <gp_Ax1.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax2d.hxx>
#include <gp_Dir.hxx>
#include <gp_Dir2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Trsf.hxx>
#include <gp_Vec.hxx>
#include <gp_Lin2d.hxx>
#include <gp_Pln.hxx>
#include <gp.hxx>
#include <gp_Pnt2d.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_Surface.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <GeomLib.hxx>
#include <Geom_Surface.hxx>
#include <Geom_Curve.hxx>
#include <Geom_CartesianPoint.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_Circle.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <GCPnts_QuasiUniformDeflection.hxx>
#include <Graphic3d_Group.hxx>
#include <Graphic3d_HorizontalTextAlignment.hxx>
#include <Graphic3d_VerticalTextAlignment.hxx>
#include <Graphic3d_ArrayOfPolylines.hxx>
#include <Graphic3d_ExportFormat.hxx>
#include <Graphic3d_ArrayOfPolylines.hxx>
#include <Graphic3d_AspectFillArea3d.hxx>
#include <Graphic3d_AspectText3d.hxx>
#include <Graphic3d_AspectLine3d.hxx>
#include <Graphic3d_AspectMarker3d.hxx>
#include <Graphic3d_Texture1Dsegment.hxx>
#include <OpenGl_GraphicDriver.hxx>
#include <Prs3d_Root.hxx>
#include <Prs3d_Drawer.hxx>
#include <Prs3d_IsoAspect.hxx>
#include <Prs3d_ShadingAspect.hxx>
#include <Prs3d_Presentation.hxx>
#include <PrsMgr_PresentationManager3d.hxx>
#include <Prs3d_TextAspect.hxx>
#include <Prs3d_Text.hxx>
#include <Select3D_SensitiveBox.hxx>
#include <Select3D_SensitiveCurve.hxx>
#include <Select3D_SensitiveGroup.hxx>
#include <SelectMgr_Selection.hxx>
#include <SelectMgr_SequenceOfOwner.hxx>
#include <SelectMgr_EntityOwner.hxx>
#include <ShapeBuild_Edge.hxx>
#include <StdSelect_ViewerSelector3d.hxx>
#include <StdPrs_ShadedShape.hxx>
#include <StdPrs_HLRPolyShape.hxx>
#include <StdSelect_BRepSelectionTool.hxx>
#include <StdPrs_WFShape.hxx>
#include <StdPrs_ToolRFace.hxx>
#include <StdSelect.hxx>
#include <StdSelect_BRepOwner.hxx>
#include <StdSelect_BRepSelectionTool.hxx>#include <TCollection_AsciiString.hxx>
#include "TopExp.hxx"
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Builder.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_ListOfShape.hxx>
#include <TopoDS_ListIteratorOfListOfShape.hxx>
#include <TopoDS_Iterator.hxx>
#include "TopoDS_Edge.hxx"
#include "TopoDS_Vertex.hxx"
#include <TopoDS_Wire.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Compound.hxx>
#include <TopTools_HSequenceOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <V3d_Viewer.hxx>
#include <V3d_View.hxx>
#include <WNT_Window.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepBuilderAPI_MakePolygon.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <BRepLib.hxx>
#include <BRepOffsetAPI_MakeThickSolid.hxx>
#include <BRepOffsetAPI_ThruSections.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools.hxx>
#include <BRepBuilderAPI_NurbsConvert.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepBndLib.hxx>
#include <BRepAdaptor_HArray1OfCurve.hxx>
#include <BRepAdaptor_Curve2d.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <GC_MakeSegment.hxx>
#include <TopTools_ListOfShape.hxx>
#include <GProp_PEquation.hxx>
#include <math_Matrix.hxx>
#include <gce_MakeCirc.hxx>
#include <geom2d_line.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <TopExp_Explorer.hxx>#pragma comment(lib,"TKVCAF.lib")
#pragma comment(lib,"TKVrml.lib")
#pragma comment(lib,"TKStl.lib")
#pragma comment(lib,"TKBrep.lib")
#pragma comment(lib,"TKIGES.lib")
#pragma comment(lib,"TKShHealing.lib")
#pragma comment(lib,"TKStep.lib")
#pragma comment(lib,"TKXSBase.lib")
#pragma comment(lib,"TKBool.lib")
#pragma comment(lib,"TKCAF.lib")
#pragma comment(lib,"TKCDF.lib")
#pragma comment(lib,"TKernel.lib")
#pragma comment(lib,"TKFeat.lib")
#pragma comment(lib,"TKFillet.lib")
#pragma comment(lib,"TKG2d.lib")
#pragma comment(lib,"TKG3d.lib")
#pragma comment(lib,"TKGeomAlgo.lib")
#pragma comment(lib,"TKGeomBase.lib")
#pragma comment(lib,"TKHLR.lib")
#pragma comment(lib,"TKMath.lib")
#pragma comment(lib,"TKOffset.lib")
#pragma comment(lib,"TKPrim.lib")
#pragma comment(lib,"TKService.lib")
#pragma comment(lib,"TKTopAlgo.lib")
#pragma comment(lib,"TKV3d.lib")
#pragma comment(lib,"TKOpenGl.lib")
#pragma comment(lib,"TKMesh.lib")
#pragma comment(lib,"TKBO.lib")

2.设置驱动环境

在 App 类头文件中初始化一个驱动设备对象的实例 myGraphicDriver

 class CMFCApplication1App : public CWinAppEx
{
public:CMFCApplication1App() noexcept;Handle(Aspect_DisplayConnection) myDisplayConnection;Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver(myDisplayConnection);Handle(OpenGl_GraphicDriver) GetGraphicDriver() const { return aGraphicDriver; }//为C**App提供调用接口函数

3.设置View容器

在Doc类头文件中声明V3d_Viewer 和 AIS_InteractiveContext两个类
V3d_Viewer 是一个 V3d_View也就是视图的管理器(做一个承载)

public:Handle_V3d_Viewer myViewer;Handle_AIS_InteractiveContext myAISContext;Handle(V3d_Viewer) GetViewer() { return myViewer; }Handle(AIS_InteractiveContext)& GetAISContext() { return myAISContext; }

源文件构造中进行初始化

CMFCApplication1Doc::CMFCApplication1Doc() noexcept
{// TODO: 在此添加一次性构造代码Handle(Graphic3d_GraphicDriver) theGraphicDriver =((CMFCApplication1App*)AfxGetApp())->GetGraphicDriver();myViewer = new V3d_Viewer(theGraphicDriver);myViewer->SetDefaultLights();myViewer->SetLightOn();myAISContext = new AIS_InteractiveContext(myViewer);
}

4.在view绘制图形

做完了上边三步occ的view就已经基本配置完毕,接下来就是装在到窗口上,并且在上面绘制图形
下面是源文件内容

//将Occ的view绑到窗口的view上去
void CMFCApplication1View::OnInitialUpdate()
{myView = GetDocument()->GetViewer()->CreateView();bool myHlrModeIsOn = Standard_False;myView->SetComputedMode(myHlrModeIsOn);Handle(Graphic3d_GraphicDriver) theGraphicDriver = ((CMFCApplication1App*)AfxGetApp())->GetGraphicDriver();Handle(WNT_Window) aWNTWindow = new WNT_Window(GetSafeHwnd());//,Quantity_NOC_MATRAGRAY);myView->SetWindow(aWNTWindow);//if(!aWNTWindow->IsMapped()) aWNTWindow->Map();
}

绘制图形方法(这里我就画一个长方体)

//绘制图形修改这里
void CMFCApplication1View::OnDraw(CDC* /*pDC*/)
{CMFCApplication1Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;// TODO: 在此处为本机数据添加绘制代码BRepBuilderAPI_MakePolygon MP;MP.Add(gp_Pnt(0, 0, 0));MP.Add(gp_Pnt(100, 0, 0));MP.Add(gp_Pnt(100, 100, 0));MP.Add(gp_Pnt(0, 100, 0));MP.Close();//完成构造 封闭空间TopoDS_Face F = BRepBuilderAPI_MakeFace(MP.Wire());gp_Vec aPrimVec(0, 0, 300);//拉伸 形成拉伸体TopoDS_Shape shape = BRepPrimAPI_MakePrism(F, aPrimVec);Handle(AIS_Shape) anAISShape = new AIS_Shape(shape);pDoc->myAISContext->Display(anAISShape);//绘制图形
}

5.最终呈现

OCC模型效果

本工程于vs2017用X64编译选项编译,完整工程代码可以密我(最近正在整理GitHub到时候会放到上边)


http://chatgpt.dhexx.cn/article/Fu7wag3H.shtml

相关文章

MFC中使用OpenCasCade示例

目录&#xff1a; 一、OpenCasCade开发环境搭建 二、创建一个MFC应用程序 三、在MFC工程中添加代码 四、画个瓶子 一、OpenCasCade开发环境搭建 参见《OpenCasCade开发环境搭建》&#xff0c;这篇文章最后运行示例前所做的工作为以后开发OpenCasCade工程铺平了路&#xff…

HTML<HBuilder X>

一&#xff1a;网页基本标签元素 HTML常用标签(HTML不是一种编程语言&#xff0c;而是一种标记语言&#xff09;&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>文档标题</title> </head><…

Opencascade 开发 1章

序 这一系列的文章旨在介绍一个方便大家开始开发自己CAD的方法。需要指出的是&#xff0c;本人主要希望通过分享一些相关技术&#xff0c;提升国人软件自主的意识和途径。通过本文构建自己的CAD只是软件自主化的非常非常小的一步&#xff0c;希望大家在不停尝试的过程中有所提…

【OCC学习5】记录最新版本emcc编译occ的bug:opencascade-7.6.0/src/Standard/Standard_Time.hxx:29:25: error: redefinit

1. 在研究OCC与Webassembly结合使用&#xff0c;编译的时候遇到以下问题&#xff1a; C:/workspace/occ_wasm/opencascade-7.6.0/src/Standard/Standard_Integer.hxx:126:25: note: previous definition is here inline Standard_Boolean IsEqual (const Standard_Integer the…

NX二次开发CreateDialog函数在UI.hxx文件和WinUser.h中的冲突

NX二次开发CreateDialog函数在UI.hxx文件和WinUser.h中的冲突 在UG二次开发中&#xff0c;若使用MFC库&#xff0c;一旦加上#include<Afx.h>头文件&#xff0c;或者使用<windows.h>头文件下面这句话就报错 theDialog GetPoints::theUI->CreateDialog(theDlxF…

HLO--XLA

HLO: high level optimizer 高级优化器 XLA&#xff1a; XLA(Accelerated Linear Algebra)-加速线性代数&#xff0c;Google推出的高性能机器学习领域编译器&#xff08;编译型推理引擎&#xff09;&#xff0c;它可以在不更改源代码的条件下加速Tensorflow模型 提高TensorFlo…

C++:C++编译过程:看完还不懂C++编译过程来捶我

1&#xff1a;先看图 2&#xff1a;一个C源文件从文本到可执行文件经历的过程&#xff1a; gcc Hello.cpp 预处理阶段&#xff1a;gcc -E hello.c -o hello.i 对源代码文件中包含关系&#xff08;头文件&#xff09;&#xff0c;预编译语句&#xff08;宏定义&#xff09…

h计算机软件指什么,HXX 文件扩展名: 它是什么以及如何打开它?

解决难以打开 HXX 文件的问题 打开 HXX 文件过程中所遇到的常见问题 MacroMates TextMate 消失 尝试打开 HXX 时&#xff0c;你会遇到一条错误消息&#xff0c;例如 “%%os%% 无法打开 HXX 文件”。 如果是这种情况&#xff0c;通常是因为 你的计算机上没有安装 MacroMates Tex…

神器octotree

在Github上查看源代码的体验十分糟糕&#xff0c;尤其是从一个目录跳转到另一个目录的时候&#xff0c;非常麻烦。 直到遇到这款神器&#xff0c;相见恨晚&#xff01;&#xff01; 具体安装及使用步骤参考&#xff1a; https://www.cnblogs.com/12yang-ting/p/7485264.html …

有用的Chrome扩展介绍 - Octotree - GitHub code tree

明细&#xff1a; 安装之后&#xff0c;Github网站左边会自动出现类似Visual Studio Code的代码显示方式&#xff0c;可以通过树形结构方便地浏览代码&#xff0c;无需重复点击文件夹进入。 树形结构里的图标可以使用各种不同的风格显示&#xff1a; 快捷键&#xff1a;上箭头…

Octotree在GitHub中出错(已解决)

谷歌插件真的是很方便&#xff0c;像Octotree让我们github中的项目浏览起来更加条理&#xff0c;如图 但是当我在github中频繁的切换文件夹的时候&#xff0c;Outotree开始报错&#xff0c;也不显示目录结构&#xff0c;将错误代码放到谷歌翻译如下。 我理解的意思是github需…

Octotree在GitHub中出错

使用octotree 出现Error: Connection error octotree解决办法 解决方法&#xff1a;需要在github设置访问token 登录github&#xff0c;打开https://github.com/settings/profile 依次点击 Settings -> Developer settings -> Personal access tokens -> Generate n…

google扩展工具Octotree使用(2020-09-01)

不知道近期是不是改版了&#xff0c;反正我的需要github Acess Token权限设置。&#xff08;最近csdn出问题了&#xff0c;图片不能居中&#xff0c;勉强看&#xff09; 1.从google商店添加软件 2.打开github刷新并配置 &#xff08;1&#xff09;点钥匙的地方 &#xff08;2…

Octotree访问私有仓库:Error: Private repository

问题 在GitHub私有仓库中使用Octotree时出现下面情况&#xff1a; 这个是因为我们需要在GitHub中给Octotree设置一个访问令牌 解决 在GitHub中&#xff1a;Settings -> Developer settings -> Personal access tokens -> Generate new token 创建令牌 设置名称Oc…

基于 Octotree 的[码云]文件树插件

之前一直在用github上面的Octotree&#xff0c;现在在用gitee&#xff08;主要是github的访问速度太慢了&#xff09;。   现在主要转到了gitee上面了&#xff0c;那么有没有基于 Octotree 的[码云]文件树插件呢&#xff0c;当然是有的&#xff0c;下面就进行安装。 1.基于 O…

Octotree 神器 tips

1. 注册一个Gitlab的帐号&#xff0c;并登录进去 2.打开页面会看到这样一个图片 3.安装完插件&#xff0c;点击上面红色方框的内容&#xff0c;即可&#xff0c;效果图如下

Octotree Chrome插件离线安装 树形展示插件

Octotree 是国外程序员Buu Nguyen 做的一个 Chrome 插件。安装之后&#xff0c;可以以左侧的树形结构浏览代码&#xff0c;非常nice 牛啤 1、GitHub 树形展示插件 下载地址附上&#xff1a;https://www.crx4chrome.com/crx/271883/ 2、Gitee 树形展示插件 https://gitee.co…

好用的chrome插件之Octotree

github上经常要浏览代码。那么如何像在IDE 上一样浏览代码呢。 这里推荐一款不错的插件Octotree 如下&#xff0c;选择vscode的风格浏览github上vue的源码&#xff0c;是不是很happy 点击设置&#xff0c;进行设置不同的风格 注意&#xff1a;和vscode不同的是。vscode上双击…

chrome强烈推荐安装Octotree插件

chrome强烈推荐安装Octotree插件 插件下载网站 说明: 这是一个很好的谷歌浏览器插件下载网站&#xff0c;界面很清新 https://chrome.zzzmh.cn/ 简介 Octotree是专门为github设计的&#xff0c;可以做到将github项目结构在侧边展示&#xff08;在线的&#xff0c;无需下载到…