当前位置:   article > 正文

[C++] OpenCasCade空间几何库的模型展现_怎么用opencascade显示几何模型

怎么用opencascade显示几何模型

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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

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提供调用接口函数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

源文件构造中进行初始化

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

//绘制图形修改这里
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);//绘制图形
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

5.最终呈现

OCC模型效果

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/245699
推荐阅读
相关标签
  

闽ICP备14008679号