OpenCascade Notes: Access to TopoDS_Shape data structure

Take box as an example

BRepPrimAPI_MakeBox mkBox(10, 10, 10);
TopoDS_Shape aShape = mkBox.Shape();

TopoDS_Shape data structure

See: OpenCascade notes: [OpenCascade topology object: three elements of TopoDS_Shape]

TopoDS_Shape的HashCode

The following two members serve as the main elements for generating hashCode

  • Handle(TopoDS_TShape) myTShape;
  • TopLoc_Location myLocation;

The direction member TopAbs_Orientation myOrient; does not participate in the generation of HashCode.

Visit vertices

The first traversal method gets the number of TopoDS_Face, TopoDS_Edge, and TopoDS_Vertex built by the body; the
second traversal method gets the number of BRep_TFace, BRep_TEdge, and BRep_TVertex built by the body (the final display form is still TopoDS_Face, TopoDS_Edge, TopoDS_Vertex) . For example: Taking BOX as an example, TopExp_Explorer traverses 48 vertices, 24 edges, and 6 faces; TopExp traverses 8 vertices, 12 edges, and 6 faces.

	//第一种遍历方式拿到的是体构建的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;
	}

access edge

	//第一种遍历方式拿到的是体构建的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);
			}
		}

	}

Attached is the class hierarchy of geometric curves in occ
Insert image description here

face-to-face visit

	//拆分三角形
	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());
			}
		}

		//.....

	}

Attached is the class hierarchy of geometric surfaces in occ
Insert image description here

Wire, Shell, Solid access

Similar to above

See

https://blog.csdn.net/Mengxy_2021/article/details/117732021

Guess you like

Origin blog.csdn.net/s634772208/article/details/129267261