以box为例
BRepPrimAPI_MakeBox mkBox(10, 10, 10);
TopoDS_Shape aShape = mkBox.Shape();
TopoDS_Shape数据结构
参看:OpenCascade笔记:【OpenCascade拓扑对象之:TopoDS_Shape的三要素】
TopoDS_Shape的HashCode
以下俩成员,作为生成hashCode的主要元素
- Handle(TopoDS_TShape) myTShape;
- TopLoc_Location myLocation;
方向成员TopAbs_Orientation myOrient;不参与HashCode的生成。
访问顶点
第一种遍历方式拿到的是体构建的TopoDS_Face、TopoDS_Edge、TopoDS_Vertex的数量;
第二种遍历方式拿到的是体构建的BRep_TFace、BRep_TEdge、BRep_TVertex的数量(最终展现形式还是TopoDS_Face、TopoDS_Edge、TopoDS_Vertex)。 举个例子:以BOX为例 TopExp_Explorer 遍历出48个顶点、24条边、6片面; TopExp遍历出8个顶点、12条边、6片面
//第一种遍历方式拿到的是体构建的TopoDS_Face、TopoDS_Edge、TopoDS_Vertex的数量;
//第二种遍历方式拿到的是体构建的BRep_TFace、BRep_TEdge、BRep_TVertex的数量(最终展现形式还是TopoDS_Face、TopoDS_Edge、TopoDS_Vertex)。
//举个例子:以BOX为例
//TopExp_Explorer 遍历出48个顶点、24条边、6片面;
//TopExp遍历出8个顶点、12条边、6片面;
//第一种遍历方式
TopExp_Explorer aExp;
int i = 1;
for (aExp.Init(aShape, TopAbs_VERTEX); aExp.More(); aExp.Next(), ++i )
{
const TopoDS_Vertex& aVertex = TopoDS::Vertex(aExp.Current());
gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
std::cout << "vertex " << i << ": (" << aPnt.X() << ", " << aPnt.Y()
<< ", " << aPnt.Z() << "), vertex's hash code is "
<< aVertex.HashCode(INT_MAX) << std::endl;
}
//第二种遍历方式
TopTools_IndexedMapOfShape aVertexMap;
TopExp::MapShapes(aShape, TopAbs_VERTEX, aVertexMap);
cout << "vertex count is " << aVertexMap.Extent() << std::endl;
for (int i = 1; i <= aVertexMap.Extent(); ++i)
{
const TopoDS_Vertex& aVertex = TopoDS::Vertex(aVertexMap(i));
gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
std::cout << "vertex " << i << ": (" << aPnt.X() << ", " << aPnt.Y()
<< ", " << aPnt.Z() << "), vertex's hash code is "
<< aVertex.HashCode(INT_MAX) << std::endl;
}
访问边
//第一种遍历方式拿到的是体构建的TopoDS_Face、TopoDS_Edge、TopoDS_Vertex的数量;
//第二种遍历方式拿到的是体构建的BRep_TFace、BRep_TEdge、BRep_TVertex的数量(最终展现形式还是TopoDS_Face、TopoDS_Edge、TopoDS_Vertex)。
//举个例子:以BOX为例
//TopExp_Explorer 遍历出48个顶点、24条边、6片面;
//TopExp遍历出8个顶点、12条边、6片面;
//第一种遍历方式
TopExp_Explorer aExp;
int i = 1;
for (aExp.Init(aShape, TopAbs_EDGE); aExp.More(); aExp.Next(), ++i )
{
const TopoDS_Edge& aEdge = TopoDS::Edge(aExp.Current());
const TopoDS_Vertex& aFirstVertex = TopExp::FirstVertex(aEdge);
const TopoDS_Vertex& aLastVertex = TopExp::LastVertex(aEdge);
}
//第二种遍历方式
TopTools_IndexedMapOfShape aEdgeMap;
TopExp::MapShapes(aShape, TopAbs_EDGE, aEdgeMap);
cout << "edge count is " << aEdgeMap.Extent() << std::endl;
for (int i = 1; i <= aEdgeMap.Extent(); ++i)
{
const TopoDS_Edge& aEdge = TopoDS::Edge(aEdgeMap(i));
const TopoDS_Vertex& aFirstVertex = TopExp::FirstVertex(aEdge);
const TopoDS_Vertex& aLastVertex = TopExp::LastVertex(aEdge);
Standard_Real fBegin = 0, fEnd = 0;
Handle(Geom_Curve) hCurve = BRep_Tool::Curve(aEdge, fBegin, fEnd);
if (!hCurve.IsNull())
{
//边是条几何线,如直线、椭圆线、贝塞尔曲线等等
//散列化,进行取点
float fLen = float(fEnd - fBegin);
int ulNbOfPoints = 30;
for (int i = 0; i < ulNbOfPoints; i++)
{
const gp_Pnt& ptPoint = hCurve->Value(fBegin + (fLen * float(i)) / float(ulNbOfPoints - 1));
}
continue;
}
TopLoc_Location locPolygon;
Handle(Poly_Polygon3D) hPolygon3D = BRep_Tool::Polygon3D(aEdge, locPolygon);
if (!hPolygon3D.IsNull())
{
//边是条多段线(曲线的近似表示)
Standard_Integer nNodeCount = hPolygon3D->NbNodes();
const TColgp_Array1OfPnt& mNodes = hPolygon3D->Nodes();
for (int i = 1; i <= nNodeCount; ++i)
{
const gp_Pnt& ptPoint = mNodes(i);
}
}
}
附上occ中几何曲线的类层次
面的访问
//拆分三角形
Bnd_Box bounds;
BRepBndLib::Add(aShape, bounds);
bounds.SetGap(0.0);
Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
Standard_Real deflection = ((xMax - xMin) + (yMax - yMin) + (zMax - zMin)) / 300.0 *0.5;
BRepMesh_IncrementalMesh(aShape, deflection, false);
//第一种遍历方式拿到的是体构建的TopoDS_Face、TopoDS_Edge、TopoDS_Vertex的数量;
//第二种遍历方式拿到的是体构建的BRep_TFace、BRep_TEdge、BRep_TVertex的数量(最终展现形式还是TopoDS_Face、TopoDS_Edge、TopoDS_Vertex)。
//举个例子:以BOX为例
//TopExp_Explorer 遍历出48个顶点、24条边、6片面;
//TopExp遍历出8个顶点、12条边、6片面;
//第一种遍历方式
TopExp_Explorer aExp;
int i = 1;
for (aExp.Init(aShape, TopAbs_FACE); aExp.More(); aExp.Next(), ++i )
{
const TopoDS_Edge& aEdge = TopoDS::Edge(aExp.Current());
const TopoDS_Vertex& aFirstVertex = TopExp::FirstVertex(aEdge);
const TopoDS_Vertex& aLastVertex = TopExp::LastVertex(aEdge);
}
//第二种遍历方式
TopTools_IndexedMapOfShape aFaceMap;
TopExp::MapShapes(aShape, TopAbs_FACE, aFaceMap);
cout << "face count is " << aFaceMap.Extent() << std::endl;
for (int i = 1; i <= aFaceMap.Extent(); ++i)
{
const TopoDS_Face& curFace = TopoDS::Face(aFaceMap(i));
BRepAdaptor_Surface adaptorSurface(curFace);
GeomAbs_SurfaceType surfaceType = adaptorSurface.GetType();
//GeomAbs_Plane,
//GeomAbs_Cylinder,
//GeomAbs_Cone,
//GeomAbs_Sphere,
//GeomAbs_Torus,
//GeomAbs_BezierSurface,
//GeomAbs_BSplineSurface,
//GeomAbs_SurfaceOfRevolution,
//GeomAbs_SurfaceOfExtrusion,
//GeomAbs_OffsetSurface,
//GeomAbs_OtherSurface
//前面对面进行了三角化(BRepMesh_IncrementalMesh)
//此处可以取出三角面信息
TopLoc_Location aLoc;
Handle(Poly_Triangulation) mesh = BRep_Tool::Triangulation(
curFace, aLoc);
if (mesh.IsNull())
{
continue;
}
const Poly_Array1OfTriangle& mTriangles = mesh->Triangles();
const TColgp_Array1OfPnt& mNodes = mesh->Nodes();
for (int g = 1; g <= mesh->NbTriangles(); g++)
{
// Get the triangle
Standard_Integer N1, N2, N3;
mTriangles(g).Get(N1, N2, N3);
// change orientation of the triangles
if (curFace.Orientation() != TopAbs_FORWARD) {
Standard_Integer tmp = N1;
N1 = N2;
N2 = tmp;
}
gp_Pnt V1 = mNodes(N1);
gp_Pnt V2 = mNodes(N2);
gp_Pnt V3 = mNodes(N3);
if (!aLoc.IsIdentity())
{
V1.Transform(aLoc.Transformation());
V2.Transform(aLoc.Transformation());
V3.Transform(aLoc.Transformation());
}
}
//.....
}
附上occ中几何面的类层次
Wire、Shell、Solid的访问
与上文类似文章来源:https://www.toymoban.com/news/detail-666815.html
参看
https://blog.csdn.net/Mengxy_2021/article/details/117732021文章来源地址https://www.toymoban.com/news/detail-666815.html
到了这里,关于OpenCascade笔记:TopoDS_Shape数据结构的访问的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!