概述
OCC(全称OpenCascade)是一个近年来比较受欢迎的开源三维CAD建模平台,曲线、曲面、实体、渲染等方面功能强大,并且在机械、航空、船舶等许多领域应用广泛。基于OCC的强大功能考虑,本人尝试将其引入桥梁领域。桥梁设计中,比较常见的梁体类型是箱梁,因此本文将在OCC上实现箱梁梁体建模。
基本思路
实现箱梁梁体建模步骤如下:
- 通过点创建闭合的外轮廓(顺时针),生成面;
- 通过点创建内轮廓(逆时针),对面掏洞处理(多箱室时,遍历多次);
- 生成的面沿路径拉伸,生成体;
- 保存为.Brep文件;
- 查看效果。
本文只介绍箱梁实现思路,如果对开发环境搭建有疑问的,请查看其他资料。
代码实现
本功能用C++实现,具体业务代码如下:
#include <vector>
#include <tuple>
#include <gp_Pnt.hxx>
#include <gp_Circ.hxx>
#include <TopTools_ListOfShape.hxx>
#include <BRepTools.hxx>
#include <BRep_Tool.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <ShapeAnalysis_WireOrder.hxx>
#include <AIS_Shape.hxx>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG2d.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TKGeomAlgo.lib")
#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKTopAlgo.lib")
#pragma comment(lib, "TKShHealing.lib")
//通过初始数据生成边
std::vector<TopoDS_Edge> GetEdges(std::vector<std::tuple <double, double, double>> tuples)
{
std::vector<TopoDS_Edge> anEdges;
for (int i = 0; i < tuples.size(); ++i)
{
int i1 = i;
int i2 = (i + 1) % tuples.size();
BRepBuilderAPI_MakeEdge tempRdge(gp_Pnt(std::get<0>(tuples[i1]), std::get<1>(tuples[i1]), std::get<2>(tuples[i1])),
gp_Pnt(std::get<0>(tuples[i2]), std::get<1>(tuples[i2]), std::get<2>(tuples[i2])));
anEdges.push_back(tempRdge.Edge());
}
return anEdges;
}
//生成外轮廓
std::vector<TopoDS_Edge> GetOuterEdges()
{
std::vector<std::tuple <double, double, double>> tuples;
tuples.push_back(std::make_tuple(-800, 0, 0.0));
tuples.push_back(std::make_tuple(800, 0, 0.0));
tuples.push_back(std::make_tuple(800, -20, 0.0));
tuples.push_back(std::make_tuple(550.0, -80, 0.0));
tuples.push_back(std::make_tuple(550.0, -600, 0.00));
tuples.push_back(std::make_tuple(-550.0, -600, 0.0));
tuples.push_back(std::make_tuple(-550.0, -80, 0.0));
tuples.push_back(std::make_tuple(-800.0, -20, 0.0));
return GetEdges(tuples);
}
//生成内轮廓列表
std::vector<std::vector<TopoDS_Edge> > GetInnerEdgesList()
{
std::vector<std::tuple <double, double, double>> innerTuples1;
innerTuples1.push_back(std::make_tuple(-460, -30, 0.0));
innerTuples1.push_back(std::make_tuple(-490, -60, 0.0));
innerTuples1.push_back(std::make_tuple(-490, -540, 0.0));
innerTuples1.push_back(std::make_tuple(-460, -570, 0.0));
innerTuples1.push_back(std::make_tuple(-60, -570, 0.0));
innerTuples1.push_back(std::make_tuple(-30, -540, 0.0));
innerTuples1.push_back(std::make_tuple(-30, -60, 0.0));
innerTuples1.push_back(std::make_tuple(-60, -30, 0.0));
std::vector<TopoDS_Edge> innerEdges1 = GetEdges(innerTuples1);
std::vector<std::tuple <double, double, double>> innerTuples2;
innerTuples2.push_back(std::make_tuple(60, -30, 0.0));
innerTuples2.push_back(std::make_tuple(30, -60, 0.0));
innerTuples2.push_back(std::make_tuple(30, -540, 0.0));
innerTuples2.push_back(std::make_tuple(60, -570, 0.0));
innerTuples2.push_back(std::make_tuple(460, -570, 0.0));
innerTuples2.push_back(std::make_tuple(490, -540, 0.0));
innerTuples2.push_back(std::make_tuple(490, -60, 0.0));
innerTuples2.push_back(std::make_tuple(460, -30, 0.0));
std::vector<TopoDS_Edge> innerEdges2 = GetEdges(innerTuples2);
std::vector<std::vector<TopoDS_Edge> > innerEdgesList;
innerEdgesList.push_back(innerEdges1);
innerEdgesList.push_back(innerEdges2);
return innerEdgesList;
}
//通过边生成BRepBuilderAPI_MakeWire
BRepBuilderAPI_MakeWire GetWire(std::vector<TopoDS_Edge> outerEdges)
{
TopTools_ListOfShape aOrderedEdges = GetShape(outerEdges);
BRepBuilderAPI_MakeWire aWireMaker;
aWireMaker.Add(aOrderedEdges);
return aWireMaker;
}
//通过边生成形状
TopTools_ListOfShape GetShape(std::vector<TopoDS_Edge> edges)
{
TopTools_ListOfShape aOrderedEdges;
for (int e = 0; e < edges.size(); ++e)
{
const TopoDS_Edge& anEdge = edges[e];
aOrderedEdges.Append(anEdge);
}
return aOrderedEdges;
}
//生成箱梁
void GenerateBoxBeam(void)
{
std::vector<TopoDS_Edge> outerEdges = GetOuterEdges();
std::vector<std::vector<TopoDS_Edge> > innerEdgesList = GetInnerEdgesList();
BRepBuilderAPI_MakeWire aWireMaker= GetWire(outerEdges);
BRepBuilderAPI_MakeFace face1(aWireMaker);//外轮廓生成面
for (int i = 0; i < innerEdgesList.size(); i++)
{
BRepBuilderAPI_MakeWire aWireMaker1= GetWire(innerEdgesList[i]);
if (!aWireMaker1.IsDone()) continue;
face1.Add(aWireMaker1.Wire());//剪切内轮廓开洞
}
TopoDS_Shape S = BRepPrimAPI_MakePrism(face1, gp_Vec(0.0, 0.0, 200));//拉伸
BRepTools::Write(S, "d:/wire.brep");//保存文件
}
//主函数
int main(int argc, char* argv[])
{
GenerateBoxBeam();
return 0;
}
实现效果
将生成的.Brep文件用安装目录下的Import Export打开,模型如下图: