使用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; }

实例显示:

occ+vtk显示igs模型的更多相关文章

  1. 自制C#版3DS文件的解析器并用SharpGL显示3DS模型

    自制C#版3DS文件的解析器并用SharpGL显示3DS模型 我已经重写了3ds解析器,详情在此(http://www.cnblogs.com/bitzhuwei/p/CSharpGL-2-parse ...

  2. Unity在UI界面上显示3D模型/物体,控制模型旋转

    Unity3D物体在UI界面的显示 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  3. PHP--TP框架----把查询到的数据,显示在模型(模板)里面

    MainController.class.php <?php namespace Home\Controller; use Think\Controller; class MainControl ...

  4. Flash Stage3D 在2D UI 界面上显示3D模型问题完美解决

    一直以来很多Stage3D开发者都在为3D模型在2DUI上显示的问题头疼.Stage3D一直是在 Stage2D下面.为了做到3D模型在2DUI上显示通常大家有几种实现方式,下面来说说这几种实现方式吧 ...

  5. VTK拾取网格模型上的可见点

    消隐与Z-Buffer 使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度 ...

  6. 十ITK读取一张dcm图像然后通过vtk显示

    一.功能 通过ITK读取一张图片(dcm格式),然后通过vtk显示出来. 版本:VS2019 itk5.0.1 vtk 8.2.0 二.程序主要思路 1-读取dcm格式图片 2-转换为vtk可以读取的 ...

  7. VTK显示.vtk格式文件

    void ReadandShowVTKFile () { vtkSmartPointer<vtkRenderer > aRenderer = vtkSmartPointer<vtkR ...

  8. VTK计算网格模型上的最短路径

    Dijkstra algorithm to compute the graph geodesic.Takes as input a polygonal mesh and performs a sing ...

  9. 把pcl的VTK显示融合到MFC(代码找原作者)

    转自PCL中国,原文链接:http://www.pclcn.org/bbs/forum.php?mod=viewthread&tid=223&extra=page%3D1 本人做了少量 ...

随机推荐

  1. 微信小程序为什么看不到所有的console.log()的日志信息

    记录一个巨傻无比的问题 1.在首页的onLoad()函数里面,加了地理位置的加载,并打印到控制台上,可是今天就是没出现 2.然后纳闷的很久,各种google,发现没有人遇到这个问题 3.再然后,我就看 ...

  2. [JZOJ3320] 【BOI2013】文本编辑器

    题目 题目大意 给你一个文本,要删去其中所有的'e'. 有三种操作: h光标左移. x删除光标上面的字母(光标是横着的). fc跳到后面的第一个字符为'c'的位置. 问操作序列的最短长度. 思考历程 ...

  3. 使用java Graphics 绘图工具生成顺丰快递电子面单

    最近公司需要开发一个公司内部使用的快递下单系统,给我的开发任务中有一个生成电子面单功能,为了下单时更方便,利用此功能使用快递公司给我们的打印机直接打印出电子面单,刚接到这个任务时我想这应该很简单,不就 ...

  4. win10 mysql5.7指定某个配置文件启动

    点击开始菜单,搜索cmd.exe,左击以管理员身份运行 C:\Users\Administrator>cd C:\Program Files\MySQL\MySQL Server 5.7\bin ...

  5. LUOGU P2294 [HNOI2005]狡猾的商人(差分约束)

    [传送门] (https://www.luogu.org/problemnew/show/P2294) 解题思路 差分约束.先总结一下差分约束,差分约束就是解决一堆不等式混在一起,左边是差的形式,右边 ...

  6. 372 在O(1)时间复杂度删除链表节点

    原题网址:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/ 给定一个单链表中 ...

  7. play framework 从环境搭建到简单运行

    download 官网:https://www.playframework.com/ 将zip下载至本地,进行unzip. 环境变量 检测是否安装成功:解压成功后进入解压的目录,运行 play 终端显 ...

  8. 实验室系统tomcat 6 java.lang.OutOfMemoryError: Java heap space

    java.lang.OutOfMemoryError: Java heap space

  9. day23_4_hashlib

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...

  10. 洛谷P1792——[国家集训队]种树

    传送门:QAQQAQ 题意:$n$个点中选$m$个不相邻的点,使得这些点不相邻(1和n算相邻),求这些点的最大值 思路:这不是神仙题不是神仙题…… 刚看到这题觉得不难,好像只要贪心就可以了但贪心不知从 ...