occ+vtk显示igs模型

本文介绍如何使用Opencascade读取igs文件,并利用VTK进行三维模型渲染展示。通过CMake配置工程,实现了从igs文件加载模型到VTK中渲染的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Opencascade读取igs文件内模型,使用vtk进行显示。

本案例环境:Opencascade6.6.0 +  vtk-5.10 + VS2005(win32)


使用CMake管理工程。

CMakeLists.txt :

PROJECT (IgesReader)

#VTK Part:
FIND_PACKAGE(VTK)
IF (VTK_FOUND)
  INCLUDE(${VTK_USE_FILE})
ELSE(VTK_FOUND)
  MESSAGE(FATAL_ERROR
     "Cannot build without VTK.  Please set VTK_DIR.")
ENDIF (VTK_FOUND)

#OpenCascade Part:
INCLUDE_DIRECTORIES(
  C:\OpenCASCADE6.6.0\ros\inc
)

LINK_LIBRARIES(
  vtkCommon
  vtkGraphics
  vtkRendering
  vtkIO
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKIGES.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKernel.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKBRep.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKMath.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKGeomBase.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKGeomAlgo.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKG3d.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKG2d.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKTopAlgo.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKXSBase.lib
  C:\OpenCASCADE6.6.0\ros\win32\vc8\libd\TKMesh.lib
)

ADD_EXECUTABLE(IgesReader Readiges.cpp)

main.cpp:

#define WNT
#include <gp_Circ.hxx>
#include <gp_Elips.hxx>
#include <gp_Sphere.hxx>

#include <Poly_Polygon3D.hxx>
#include <Poly_Triangulation.hxx>

#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_HSequenceOfShape.hxx>

#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>

#include <IGESControl_Controller.hxx>
#include <IGESControl_Writer.hxx>
#include <IGESControl_Reader.hxx>

#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS.hxx>



#include <BRep_Tool.hxx>
#include <BRepMesh.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>



#include <BRepAdaptor_Curve.hxx>
#include <GCPnts_TangentialDeflection.hxx>
#include <TopExp_Explorer.hxx>
#include <Standard_TypeDef.hxx>

#include <iostream>

#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

//vtk lib
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkTriangle.h>

Standard_Integer ReadIGES(const Standard_CString& aFileName,
						   Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
{

    IGESControl_Reader Reader;

    Standard_Integer status = Reader.ReadFile(aFileName);

    if (status != IFSelect_RetDone) 
    {
        return status;
    }
        
    Reader.TransferRoots();
    
    TopoDS_Shape aShape = Reader.OneShape();     
    aHSequenceOfShape->Append(aShape);


    return status;
}


void BuildMesh(vtkRenderer* render, const TopoDS_Face& face, double deflection = 0.1)
{
	TopLoc_Location location;
	BRepMesh::Mesh(face, deflection);

	Handle_Poly_Triangulation triFace = BRep_Tool::Triangulation(face, location);

	Standard_Integer nTriangles = triFace->NbTriangles();

	gp_Pnt vertex1;
	gp_Pnt vertex2;
	gp_Pnt vertex3;

	Standard_Integer nVertexIndex1 = 0;
	Standard_Integer nVertexIndex2 = 0;
	Standard_Integer nVertexIndex3 = 0;

	TColgp_Array1OfPnt nodes(1, triFace->NbNodes());
	Poly_Array1OfTriangle triangles(1, triFace->NbTriangles());

	nodes = triFace->Nodes();
	triangles = triFace->Triangles();       

	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
	vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
	points->Allocate(nTriangles * 3);
	cells->Allocate(nTriangles);

	int id = 0;

	for (Standard_Integer i = 1; i <= nTriangles; i++)
	{
		Poly_Triangle aTriangle = triangles.Value(i);

		aTriangle.Get(nVertexIndex1, nVertexIndex2, nVertexIndex3);

		vertex1 = nodes.Value(nVertexIndex1).Transformed(location.Transformation());
		vertex2 = nodes.Value(nVertexIndex2).Transformed(location.Transformation());
		vertex3 = nodes.Value(nVertexIndex3).Transformed(location.Transformation());

		points->InsertNextPoint(vertex1.X(), vertex1.Y(), vertex1.Z());
		points->InsertNextPoint(vertex2.X(), vertex2.Y(), vertex2.Z());
		points->InsertNextPoint(vertex3.X(), vertex3.Y(), vertex3.Z());

		vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
		triangle->GetPointIds()->SetId(0,id * 3);
		triangle->GetPointIds()->SetId(1,id * 3 + 1);
		triangle->GetPointIds()->SetId(2,id *3 + 2);

		// Add the triangle to a cell array
		cells->InsertNextCell(triangle);
		id++;
	}

	polyData->SetPoints(points);
	polyData->SetPolys(cells);

	vtkSmartPointer<vtkPolyDataMapper> sourceMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	sourceMapper->SetInput(polyData);

	vtkSmartPointer<vtkActor> sourceActor = vtkSmartPointer<vtkActor>::New();
	sourceActor->SetMapper(sourceMapper);
	sourceActor->GetProperty()->SetColor(1,0,0);

	render->AddActor(sourceActor);

}

void BuildScene(  vtkRenderer *renderer, Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
{
	Standard_Integer index = aHSequenceOfShape->Length();
	TopoDS_Shape theCompSolid = aHSequenceOfShape->ChangeValue(index);

	for (TopExp_Explorer faceExp(theCompSolid, TopAbs_FACE); faceExp.More(); faceExp.Next())
	{
		// The 3d-mesh of the FACE is assembled to form the
		// boundary of the SOLID.
		
		const TopoDS_Face& theFace = TopoDS::Face(faceExp.Current());
		BuildMesh(renderer, theFace );



	}		
	



}



int main(void)
{
	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
	
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);





	Handle(TopTools_HSequenceOfShape) aHSequenceOfShape =  new TopTools_HSequenceOfShape();
	Standard_Integer status = ReadIGES("e:\\p3.igs",aHSequenceOfShape);
	cout<<"return status:"<<status<<endl;


	BuildScene(renderer, aHSequenceOfShape);


	renderer->SetBackground(1,1,1);

	// Render and interact
	renderWindow->Render();
	renderWindowInteractor->Start();

	return 0;

}



实例显示:


### 如何在Qt中集成OCC显示3D模型 为了在Qt环境中成功展示由OCCOpenCASCADE)创建的3D模型,可以遵循以下方法构建应用程序。此过程涉及多个文件和类之间的协作。 #### 创建项目结构 首先建立一个新的Qt Widgets Application工程,在该工程项目下准备四个主要部分:`main.cpp`, `mainwindow.h`, `mainwindow.cpp`以及UI界面定义文件`mainwindow.ui`[^3]。 #### 初始化图形环境 在`MainWindow`构造函数内部调用必要的初始化操作,比如设置视窗尺寸、颜色等属性;同时还需要配置好OCC中的`V3d_View`对象以便于后续绘制工作能够顺利开展。这一步骤通常也包含了对AIS_InteractiveContext上下文环境变量的设定,它负责管理场景内的所有可视实体及其交互逻辑[^4]。 ```cpp // mainwindow.cpp 中的部分代码片段 #include "mainwindow.h" #include <QApplication> #include <Standard_Version.hxx> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), myAISContext(new AIS_InteractiveContext(myView)) { // 设置背景色和其他基本参数... } ``` #### 加载并显示STEP模型 对于想要加载外部CAD数据的情况来说,则可以通过读取特定格式(例如.STEP)的方式导入几何信息到内存当中去。具体做法是在适当位置加入如下所示的一段C++语句: ```cpp // 继续在 mainwindow.cpp 文件内添加功能 Handle(AIS_Shape) aShape; if (IFSelect_ReturnStatus status = STEPControl_Reader().ReadFile(fileName.toStdString(), stepReader)) { TopoDS_Shape shape = stepReader.OneShape(); aShape = new AIS_Shape(shape); } myAISContext->Display(aShape, Standard_True); // 显示模型 ``` 上述代码实现了从指定路径下的`.step`文件解析出拓扑形状(TopoDS_Shape),再将其转换成适合屏幕呈现的形式——即AIS_Shape类型的实例化对象aShape,并最终交给之前提到过的AIS_InteractiveContext来进行可视化处理。 #### 完整示例流程概述 整个项目的实现大致分为以下几个方面的工作: - 构建基础的应用框架; - 配置OCC与Qt间的桥梁机制; - 实现具体的业务需求,如打开文件对话框选取要查看的设计图纸(.step); - 将获取的数据映射至三维空间坐标系之下并通过OpenGL管线渲染出来给用户观看。 通过这种方式,不仅可以完成最基本的功能目标—让计算机屏幕上呈现出逼真的机械零件图像,而且也为进一步探索更复杂的特性打下了坚实的基础,例如支持多视角切换、测量工具接入等功能扩展[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值