在OpenCASCADE中生成WIRE時要求添加到WIRE中的邊EDGE是有順序要求的。當給定的邊沒有按順序添加到WIRE之前,需要自己將EDGE按順序處理。OpenCASCADE中也提供了對EDGE按順序進行排序的功能,方便WIRE的生成。
在模型檢查模塊TKShHealing中,OpenCASCADE提供了類ShapeAnalysis_WireOrder用來將用于生成WIRE的一系列EDGE進行排序。這個類的實現原理是根據EDGE的起點、終點坐標來進行連接,生成順序。
/*
The MIT License (MIT)
---------------------
Copyright(C) 2018 Shing Liu(eryar@163.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <vector>
#include <gp_Pnt.hxx>
#include <gp_Circ.hxx>
#include <TopTools_ListOfShape.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <ShapeAnalysis_WireOrder.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")
void test(void)
{
std::vector<TopoDS_Edge> anEdges;
// 5 Segment edges.
BRepBuilderAPI_MakeEdge anEdgeMaker1(
gp_Pnt(-1650.0, 2857.88383249, 0.0),
gp_Pnt(-750.0, 1299.03810568, 0.0));
BRepBuilderAPI_MakeEdge anEdgeMaker2(
gp_Pnt(1299.03810568, 750.0, 0.0),
gp_Pnt(2857.88383249, 1650.0, 0.0));
BRepBuilderAPI_MakeEdge anEdgeMaker3(
gp_Pnt(2857.88383249, 1650.0, 0.0),
gp_Pnt(8000.0, 1650.0, 0.0));
BRepBuilderAPI_MakeEdge anEdgeMaker4(
gp_Pnt(8000.0, 1650.0, 0.0),
gp_Pnt(8000.0, -3300.0, 0.0));
BRepBuilderAPI_MakeEdge anEdgeMaker5(
gp_Pnt(8000.0, -3300.0, 0.0),
gp_Pnt(0.0, -3300.0, 0.0));
anEdges.push_back(anEdgeMaker1.Edge());
anEdges.push_back(anEdgeMaker2.Edge());
anEdges.push_back(anEdgeMaker3.Edge());
anEdges.push_back(anEdgeMaker4.Edge());
anEdges.push_back(anEdgeMaker5.Edge());
// 2 Arc edges.
gp_Circ aCircle1(gp::XOY(), 1500.0);
gp_Circ aCircle2(gp::XOY(), 3300.0);
BRepBuilderAPI_MakeEdge anEdgeMaker6(aCircle1,
gp_Pnt(-750.0, 1299.03810568, 0.0),
gp_Pnt(1299.03810568, 750.0, 0.0));
BRepBuilderAPI_MakeEdge anEdgeMaker7(aCircle2,
gp_Pnt(-1650.0, 2857.88383249, 0.0),
gp_Pnt(0.0, -3300.0, 0.0));
anEdges.push_back(anEdgeMaker6.Edge());
anEdges.push_back(anEdgeMaker7.Edge());
// Get edges order for the wire.
ShapeAnalysis_Edge anEdgeAnalyser;
ShapeAnalysis_WireOrder aWireOrder;
for (std::vector<TopoDS_Edge>::const_iterator i = anEdges.begin();
i != anEdges.end(); ++i)
{
TopoDS_Vertex aVf = anEdgeAnalyser.FirstVertex(*i);
TopoDS_Vertex aVl = anEdgeAnalyser.LastVertex(*i);
gp_Pnt aPf = BRep_Tool::Pnt(aVf);
gp_Pnt aPl = BRep_Tool::Pnt(aVl);
aWireOrder.Add(aPf.XYZ(), aPl.XYZ());
}
//
TopTools_ListOfShape aOrderedEdges;
for (Standard_Integer e = 1; e <= aWireOrder.NbEdges(); ++e)
{
const TopoDS_Edge& anEdge = anEdges.at(e - 1);
aOrderedEdges.Append(anEdge);
}
BRepBuilderAPI_MakeWire aWireMaker;
aWireMaker.Add(aOrderedEdges);
if (aWireMaker.IsDone())
{
BRepTools::Write(aWireMaker.Shape(), "d:/wire.brep");
}
}
int main(int argc, char* argv[])
{
test();
return 0;
}
程序先添加5條線段到EDGE數組,再添加兩個圓弧到EDGE數組。再使用類ShapeAnalysis_WireOrder來對EDGE數組進行排序,將排序后的EDGE數組去生成WIRE。如果生成WIRE成功,會在D盤得到一個wire.brep文件。在Draw中加載后顯示如下圖所示:
OpenCASCADE中生成WIRE時對添加的EDGE是有順序的要求。如何對散亂的EDGE進行排序以達到生成WIRE的要求呢?OpenCASCADE在TKShHealing模塊中提供了對EDGE排序的功能。