在编译occ通过过后,我需要验证occ是否能够正常结合vtk进行开发工作
使用CMake进行环境变量设置:
CMakeList.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.5.1\ros\inc
)
LINK_LIBRARIES(
vtkCommon
vtkGraphics
vtkRendering
vtkIO
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKIGES.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKernel.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKBRep.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKMath.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKGeomBase.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKGeomAlgo.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKG3d.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKG2d.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKTopAlgo.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKXSBase.lib
C:\OpenCASCADE6.5.1\ros\win32\vc8\libd\TKMesh.lib
)
ADD_EXECUTABLE(IGESReader example.cxx)
添加一个example.cxx
// sampleExample.cpp : Defines the entry point for the console application.
//
// OpenCascade library.
#include <gp_Circ.hxx>
#include <gp_Elips.hxx>
#include <gp_Sphere.hxx>
#include <Poly_Polygon3D.hxx>
#include <Poly_Triangulation.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <BRep_Tool.hxx>
#include <BRepMesh.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
//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>
/*
* @breif Descret the shape: face.
* For Face will be discreted to triangles; (BRepMesh_FastDiscret)
* To get the triangles of the face, use BRep_Tool::Triangulation(Face, L);
*/
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* render)
{
gp_Ax2 axis;
// 1. Test sphere face while deflection is default 0.1.
axis.SetLocation(gp_Pnt(26.0, 0.0, 0.0));
TopoDS_Face sphereFace1 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0));
BuildMesh(render, sphereFace1);
// 2. Test sphere face while deflection is 2.0.
axis.SetLocation(gp_Pnt(26.0, 18.0, 0.0));
TopoDS_Face sphereFace2 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0));
BuildMesh(render,sphereFace2, 2.0);
// 3. Test sphere face while deflection is 0.001.
axis.SetLocation(gp_Pnt(26.0, -18.0, 0.0));
TopoDS_Face sphereFace3 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0));
BuildMesh(render, sphereFace3, 0.001);
}
int main(int argc, char *argv[])
{
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
//build mesh
BuildScene(renderer);
renderer->SetBackground(1,1,1);
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
你可能在编译的过程当中,遇到缺少dll文件,这个说明 你并没有把相应的dll文件放在环境变量当中去。
运行结果: