目录
[二、利用OCC 点线面实现](#二、利用OCC 点线面实现)
一、利用封装已有算法实现
1、盒子建模算法封装
BRepPrimAPI_MakeBox box(2, 2, 2);
2、可视化
void VTKRenderWindow(const TopoDS_Shape& shape)
{
vtkNew<vtkRenderWindow> renderWindow; //创建一个vtk窗口
vtkNew<vtkRenderer> render; //创建一个vtk渲染器
renderWindow->AddRenderer(render.GetPointer()); //在窗口中加入渲染器
vtkNew<vtkRenderWindowInteractor> iren; //创建一个vtk交互器
vtkNew<vtkInteractorStyleTrackballCamera> istyle; //创建vtk相机交互器样式
iren->SetRenderWindow(renderWindow.GetPointer()); //设置渲染窗口
iren->SetInteractorStyle(istyle.GetPointer()); //设置交互器样式
vtkNew<IVtkTools_ShapeDataSource> occSource; //创建一个可以被VTK使用的OCC数据源
occSource->SetShape(new IVtkOCC_Shape(shape)); //将shape添加到数据源中
vtkNew<vtkPolyDataMapper> mapper; //创建一个VTK数据类型
mapper->SetInputConnection(occSource->GetOutputPort()); //创建一个管道,将occ数据导入到VTK数据中
vtkNew<vtkActor> actor; //创建一个vtk actor
actor->SetMapper(mapper.GetPointer()); //将vtk数据交给actor
render->AddActor(actor.GetPointer()); //在渲染器中加入vtk actor
iren->Initialize(); //初始化交互器
iren->Start(); //开始运行交互器
}
二、利用OCC 点线面实现
1、实现过程
- 创建四个顶点,定义一个矩形的四个角。
- 使用这些顶点创建四条边。
- 将这些边连接成一个封闭的线框 (wire)。
- 使用这个线框创建一个面 (face)。
- 定义一个拉伸方向和距离,这里是沿 Z 轴方向拉伸 10 个单位长度。
- 使用
BRepPrimAPI_MakePrism
类将这个面拉伸成一个体(solid)
2、实现一个面
#include "BRepBuilderAPI_MakeVertex.hxx"
#include "BRepBuilderAPI_MakeEdge.hxx"
#include "BRepBuilderAPI_MakeWire.hxx"
#include "BRepBuilderAPI_MakeFace.hxx"
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include "Standard_Real.hxx"
int main()
{
//创建面
#pragma region MyRegion
// 创建四个顶点
gp_Pnt p1(0, 0, 0);
gp_Pnt p2(10, 0, 0);
gp_Pnt p3(10, 10, 0);
gp_Pnt p4(0, 10, 0);
// 使用顶点创建边,注意线框闭合
TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge(p1, p2);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge(p2, p3);
TopoDS_Edge edge3 = BRepBuilderAPI_MakeEdge(p3, p4);
TopoDS_Edge edge4 = BRepBuilderAPI_MakeEdge(p4, p1);
// 将边连接成一个线框
BRepBuilderAPI_MakeWire wireBuilder;
wireBuilder.Add(edge1);
wireBuilder.Add(edge2);
wireBuilder.Add(edge3);
wireBuilder.Add(edge4);
TopoDS_Wire wire = wireBuilder.Wire();
// 使用线框创建面
BRepBuilderAPI_MakeFace faceBuilder(wire);
TopoDS_Face face = faceBuilder.Face();
#pragma endregion
VTKRenderWindow(face);
}
3、拉伸面生成体
#include "BRepBuilderAPI_MakeVertex.hxx"
#include "BRepBuilderAPI_MakeEdge.hxx"
#include "BRepBuilderAPI_MakeWire.hxx"
#include "BRepBuilderAPI_MakeFace.hxx"
#include "BRepPrimAPI_MakePrism.hxx"
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include "Standard_Real.hxx"
int main()
{
//创建面
#pragma region MyRegion
// 创建四个顶点
gp_Pnt p1(0, 0, 0);
gp_Pnt p2(10, 0, 0);
gp_Pnt p3(10, 10, 0);
gp_Pnt p4(0, 10, 0);
// 使用顶点创建边,注意线框闭合
TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge(p1, p2);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge(p2, p3);
TopoDS_Edge edge3 = BRepBuilderAPI_MakeEdge(p3, p4);
TopoDS_Edge edge4 = BRepBuilderAPI_MakeEdge(p4, p1);
// 将边连接成一个线框
BRepBuilderAPI_MakeWire wireBuilder;
wireBuilder.Add(edge1);
wireBuilder.Add(edge2);
wireBuilder.Add(edge3);
wireBuilder.Add(edge4);
TopoDS_Wire wire = wireBuilder.Wire();
// 使用线框创建面
BRepBuilderAPI_MakeFace faceBuilder(wire);
TopoDS_Face face = faceBuilder.Face();
// 定义拉伸方向和距离
gp_Vec prismVec(0, 0, 10);
// 拉伸面形成体
BRepPrimAPI_MakePrism prismMaker(face, prismVec);
TopoDS_Shape solid = prismMaker;
#pragma endregion
VTKRenderWindow(solid);
}
4、旋转面生成体
#include "BRepBuilderAPI_MakeVertex.hxx"
#include "BRepBuilderAPI_MakeEdge.hxx"
#include "BRepBuilderAPI_MakeWire.hxx"
#include "BRepBuilderAPI_MakeFace.hxx"
#include "BRepPrimAPI_MakePrism.hxx"
#include "BRepPrimAPI_MakeRevol.hxx"
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include "Standard_Real.hxx"
int main()
{
//创建面
#pragma region MyRegion
// 创建四个顶点
gp_Pnt p1(0, 0, 0);
gp_Pnt p2(10, 0, 0);
gp_Pnt p3(10, 10, 0);
gp_Pnt p4(0, 10, 0);
// 使用顶点创建边,注意线框闭合
TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge(p1, p2);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge(p2, p3);
TopoDS_Edge edge3 = BRepBuilderAPI_MakeEdge(p3, p4);
TopoDS_Edge edge4 = BRepBuilderAPI_MakeEdge(p4, p1);
// 将边连接成一个线框
BRepBuilderAPI_MakeWire wireBuilder;
wireBuilder.Add(edge1);
wireBuilder.Add(edge2);
wireBuilder.Add(edge3);
wireBuilder.Add(edge4);
TopoDS_Wire wire = wireBuilder.Wire();
// 使用线框创建面
BRepBuilderAPI_MakeFace faceBuilder(wire);
TopoDS_Face face = faceBuilder.Face();
// 定义拉伸方向和距离
gp_Vec prismVec(0, 0, 10);
// 拉伸面形成体
/*BRepPrimAPI_MakePrism prismMaker(face, prismVec);
TopoDS_Shape solid = prismMaker;*/
// 定义旋转轴
gp_Ax1 axis(gp_Pnt(0, 0, 0), gp_Dir(0, 1, 0));
// 旋转面形成圆柱
BRepPrimAPI_MakeRevol revolMaker(face, axis);
TopoDS_Shape cylinder = revolMaker;
#pragma endregion
VTKRenderWindow(cylinder);
}
三、总结
OCC中帮我们封装了大量的几何图元,我们可以利用已经封装好的图元进行快速拓展自己的模型,建议多看看开发文档,多看看代码,不断深化自己对OCC的理解。