Qt+VTK+OCCT读取IGES/STEP模型

本文介绍了一个使用VTK和OCCT进行三维模型显示的例子。通过读取IGES和STEP文件,创建并显示了不同的三维形状。此外,还展示了如何在网格上放置点并进行视口分割。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

参考链接

主要代码

#include "VTKOCCT.h"
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeCone.hxx>
#include <IVtkTools_ShapeDataSource.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <vtkAutoInit.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyDataMapper.h>
#include <IGESControl_Reader.hxx> 
#include <STEPCAFControl_Reader.hxx>
//Lesson 17
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <vtkCubeSource.h>
#include <vtkEllipseArcSource.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkAppendPolyData.h >
#include <vtkCleanPolyData.h >
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkBoundingBox.h>
#include <vtkAxesActor.h>


decltype(auto) ReadIges(const char* file) {
	IGESControl_Reader reader;
	reader.ReadFile(file);
	reader.TransferRoots();
	TopoDS_Shape shape = reader.OneShape();
	vtkNew<vtkPolyData> polydata;
	vtkNew<vtkPolyDataMapper> mapper;
	vtkNew<vtkActor> actor;
	vtkNew<IVtkTools_ShapeDataSource> shapeSource;
	shapeSource->SetShape(new IVtkOCC_Shape(shape));
	shapeSource->Update();
	mapper->SetInputConnection(shapeSource->GetOutputPort());
	actor->SetMapper(mapper);
	return actor;
}

decltype(auto) ReadStep(const char* file) {
	STEPControl_Reader readerSTEP;
	IFSelect_ReturnStatus statSTEP = readerSTEP.ReadFile(file);
	IFSelect_PrintCount modeSTEP = IFSelect_ListByItem;
	readerSTEP.PrintCheckLoad(Standard_False, modeSTEP);
	readerSTEP.TransferRoots();
	TopoDS_Shape shape = readerSTEP.OneShape();
	
	vtkNew<vtkPolyDataMapper> mapper;
	vtkNew<vtkActor> actor;
	vtkNew<IVtkTools_ShapeDataSource> shapeSource;
	
	shapeSource->SetShape(new IVtkOCC_Shape(shape));
	shapeSource->Update();
	mapper->SetInputConnection(shapeSource->GetOutputPort());
	actor->SetMapper(mapper);
	return actor;
}

decltype(auto) TopoDSShapeSphere() {
	//TopoDS_ShapeSphere
	gp_Ax2 sphere_origin(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
	BRepPrimAPI_MakeCone mkCone(sphere_origin, 1.0, 0.01, 1);
	const TopoDS_Shape& TopoDS_ShapemkConeTMP = mkCone.Shape();
	TopoDS_Shape TopoDS_ShapeCONE = static_cast<TopoDS_Shape>(TopoDS_ShapemkConeTMP);
	gp_Trsf TSPHERE;
	TSPHERE.SetScaleFactor(0.9);
	TSPHERE.SetTranslationPart(gp_Vec(0, 0, 0));
	BRepBuilderAPI_Transform stepBRepTransformationTOPBOX(TopoDS_ShapeCONE, TSPHERE, Standard_True);
	TopoDS_ShapeCONE = stepBRepTransformationTOPBOX.Shape();

	vtkNew<IVtkTools_ShapeDataSource> TOPOCONESource;
	TOPOCONESource->SetShape(new IVtkOCC_Shape(TopoDS_ShapeCONE));

	vtkNew<vtkActor> actorTOPOCONE;
	vtkNew<vtkPolyDataMapper> mapperTOPOCONE;
	actorTOPOCONE->SetMapper(mapperTOPOCONE);
	mapperTOPOCONE->SetInputConnection(TOPOCONESource->GetOutputPort());
	return actorTOPOCONE;
}

decltype(auto) VTKpointOnMeshes(const char* file) {
	STEPControl_Reader readerSTEP;
	IFSelect_ReturnStatus statSTEP = readerSTEP.ReadFile(file);
	IFSelect_PrintCount modeSTEP = IFSelect_ListByItem;
	readerSTEP.PrintCheckLoad(Standard_False, modeSTEP);
	readerSTEP.TransferRoots();
	TopoDS_Shape TopoDS_ShapeSTEP = readerSTEP.OneShape();


	//VTKpointOnMeshes
	vtkNew<vtkPoints> VTKpointsOnMeshes;
	vtkNew<vtkCellArray> verticesForVTKpointsOnMeshes;
	vtkIdType pid[1];

	//vtkPolyData creation
	vtkNew<vtkPolyData> PolyDataVTKPointsOnMeshes;

	//vtkPolyData initialization
	PolyDataVTKPointsOnMeshes->SetPoints(VTKpointsOnMeshes);
	PolyDataVTKPointsOnMeshes->SetVerts(verticesForVTKpointsOnMeshes);

	//VTKpoint IGES, STEP TopoDS_ShapeBOX CALC   
	Bnd_Box aabb;
	//AABB ALGO Per TopoDS_Shape mesh  , Please choose the source you like to run and put it in next line
	BRepBndLib::Add(TopoDS_ShapeSTEP, aabb, true); //TopoDS_ShapeIGES  //TopoDS_ShapeSTEP //TopoDS_ShapeCONE
	//VTKpointIGES CALC per TopoDS_Shape mesh
	int density = 20;
	gp_XYZ Pmin = aabb.CornerMin().XYZ();
	gp_XYZ Pmax = aabb.CornerMax().XYZ();
	gp_XYZ D = Pmax - Pmin;
	double dim[3] = { D.X(),D.Y(),D.Z() };
	double mind = Min(dim[0], Min(dim[1], dim[2]));
	const double d = mind / density;
	int nslice[3] = {
		int(Round(dim[0] / d)) + 1 ,
		int(Round(dim[1] / d)) + 1 ,
		int(Round(dim[2] / d)) + 1 };
	for (int i = 0; i < nslice[0]; ++i)
		for (int j = 0; j < nslice[1]; ++j)
			for (int k = 0; k < nslice[2]; ++k)
			{
				gp_XYZ p = Pmin
					+ gp_XYZ(d * i, 0, 0)
					+ gp_XYZ(0, d * j, 0)
					+ gp_XYZ(0, 0, d * k);
				pid[0] = VTKpointsOnMeshes->InsertNextPoint(p.X(), p.Y(), p.Z());
				verticesForVTKpointsOnMeshes->InsertNextCell(1, pid);
			};

	//Append the meshes
	vtkNew<vtkAppendPolyData> appendFilter;
	appendFilter->AddInputData(PolyDataVTKPointsOnMeshes);

	//Remove any duplicate points.
	vtkNew<vtkCleanPolyData> cleanFilterForMapperVTKShapes;
	cleanFilterForMapperVTKShapes->SetInputConnection(appendFilter->GetOutputPort());
	cleanFilterForMapperVTKShapes->Update();

	vtkNew<vtkPolyDataMapper> mapperVTKShapes;
	mapperVTKShapes->SetInputConnection(cleanFilterForMapperVTKShapes->GetOutputPort());

	vtkNew<vtkActor> actorVTKShapes;
	actorVTKShapes->SetMapper(mapperVTKShapes);
	return actorVTKShapes;
}

VTKOCCT::VTKOCCT(QWidget* parent)
	: QMainWindow(parent)
{
	vtkWidget = new QVTKOpenGLNativeWidget(this);
	this->resize(600, 400);
	this->setCentralWidget(vtkWidget);

	// ViewPort分割
	double xmins[4] = { 0, 0.5, 0 ,0.5 };
	double xmaxs[4] = { 0.5, 1, 0.5,1 };
	double ymins[4] = { 0, 0, 0.5,0.5 };
	double ymaxs[4] = { 0.5, 0.5, 1,1 };

	auto igesActor = ReadIges("D:/TEST/3D/igs/激光剑 .igs");
	auto rendererIges = vtkSmartPointer< vtkRenderer >::New();
	rendererIges->SetViewport(xmins[0], ymins[0], xmaxs[0], ymaxs[0]);
	rendererIges->AddActor(igesActor);

	auto stepActor = ReadStep("D:/TEST/3D/step/Assem1.STEP");
	auto rendererStep = vtkSmartPointer< vtkRenderer >::New();
	rendererStep->SetViewport(xmins[1], ymins[1], xmaxs[1], ymaxs[1]);
	rendererStep->AddActor(stepActor);

	auto topoActor = TopoDSShapeSphere();
	auto rendererTopo = vtkSmartPointer< vtkRenderer >::New();
	rendererTopo->SetViewport(xmins[2], ymins[2], xmaxs[2], ymaxs[2]);
	rendererTopo->AddActor(topoActor);

	auto vTKpointOnMeshes = VTKpointOnMeshes("D:/TEST/3D/step/叶片.STEP");
	auto rendererVTKpointOnMeshes = vtkSmartPointer< vtkRenderer >::New();
	rendererVTKpointOnMeshes->SetViewport(xmins[3], ymins[3], xmaxs[3], ymaxs[3]);
	rendererVTKpointOnMeshes->AddActor(vTKpointOnMeshes);
	vtkNew<vtkAxesActor> axes;
	// 总长
	// 初始化数组
	double axesLength[3]{ 100,100,100 };
	axes->SetTotalLength(axesLength);
	rendererIges->AddActor(axes);
	vtkWidget->renderWindow()->AddRenderer(rendererIges);
	vtkWidget->renderWindow()->AddRenderer(rendererStep);
	vtkWidget->renderWindow()->AddRenderer(rendererTopo);
	vtkWidget->renderWindow()->AddRenderer(rendererVTKpointOnMeshes);
}

VTKOCCT::~VTKOCCT()
{}

##效果
在这里插入图片描述

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 集成 QtOCCTVTK 的方法 #### 设置环境变量 为了使 Visual Studio 能够识别并使用这些库,在集成之前需设置好系统的环境变量,确保 `PATH` 变量包含了各个库的 bin 文件夹路径。 #### 安装必要的组件和工具链 对于 Windows 平台上的开发工作,推荐采用如下配置: - 使用 **VC++ 141 64-bit 工具集** 来完成日常测试以及构建 OpenCASCADE Technology (OCCT) 正式的二进制分发包[^1]。 - 对于 Qt 库的选择应匹配所选的 MSVC 版本,并通过 MinGW 或者直接利用 Microsoft 提供的 C/C++ 编译器来进行编译支持。 #### 创建基于 Qt Creator 的项目结构 虽然可以完全依赖纯 Visual Studio 开展工作,但考虑到跨平台特性和图形界面设计便利性,建议借助 Qt Creator 新建工程模板。在此基础上引入其他第三方依赖项如 OCCTVTK。 #### 导入外部模块至 .pro 文件 编辑项目的 `.pro` 文件来声明额外包含路径与链接选项: ```qmake QT += core gui widgets opengl # 假设已安装到默认位置 INCLUDEPATH += "C:\Program Files\OpenCascade\inc" LIBS += -L"C:\Program Files\OpenCascade\lib" \ -lFWOSPlugin -lTKernel ... CONFIG(debug, debug|release){ INCLUDEPATH += "D:\VTK\bin\Debug" LIBS += -LD:\VTK\bin\Debug\ vtkCommonCore-8.2d.lib ... } else { INCLUDEPATH += "D:\VTK\bin\Release" LIBS += -LD:\VTK\bin\Release\ vtkCommonCore-8.2.lib ... } ``` #### 构建自定义属性表单以简化后续操作 当首次成功编译 VTK 后会在源码根目录下生成特定架构下的调试版本配置文件(例如 `vtkX64Debug.props`),可将其复制到全局共享区域以便未来创建的新工程项目能够快速加载相同的设定而无需重复劳动[^2]。 #### 执行完整的编译流程 针对 OCC 自身而言,除了 GUI 方面外还需要经历一次性的初始化过程,即在命令行环境下执行相应的 msbuild 指令完成最终产物部署前的最后一道工序[^3]。 #### 示例代码片段展示基本交互逻辑 下面给出一段简单的 Python 绑定案例用来验证整个链条是否通畅无阻: ```cpp #include <AIS_Shape.hxx> #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); Handle(AIS_InteractiveContext) ctx = new AIS_InteractiveContext(); // ... more initialization code here QWidget window; window.show(); return app.exec(); } ```
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值