1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_WireBuilder.h"
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_ShapeExplorer.h>
27 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopExp_Explorer.hxx>
32 //=================================================================================================
33 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
35 TopTools_ListOfShape aListOfEdges;
37 ListOfShape::const_iterator anIt = theShapes.cbegin();
38 for(; anIt != theShapes.cend(); ++anIt) {
39 const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
40 switch(aShape.ShapeType()) {
42 aListOfEdges.Append(aShape);
46 for(TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
47 aListOfEdges.Append(anExp.Current());
52 return GeomShapePtr();
57 BRepBuilderAPI_MakeWire aWireBuilder;
58 aWireBuilder.Add(aListOfEdges);
59 if(aWireBuilder.Error() != BRepBuilderAPI_WireDone) {
60 return GeomShapePtr();
63 GeomShapePtr aResultShape(new GeomAPI_Shape());
64 aResultShape->setImpl(new TopoDS_Shape(aWireBuilder.Wire()));
68 //=================================================================================================
69 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
74 GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
75 for (; anExp.more(); anExp.next()) {
76 GeomShapePtr anEdge = anExp.current();
77 anEdges.push_back(anEdge);
80 // Check intersections between edges pair-wise
81 int aNbEdges = (int)anEdges.size();
82 std::list<GeomShapePtr>::const_iterator anEdgesIt = anEdges.begin();
83 for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
84 GeomEdgePtr anEdge1(new GeomAPI_Edge(*anEdgesIt));
86 std::list<GeomShapePtr>::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
87 for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
88 GeomEdgePtr anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
89 GeomShapePtr anInter = anEdge1->intersect(anEdge2);
96 if (anInter->isVertex()) {
97 GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
98 GeomPointPtr aPnt = aVertex->point();
100 GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
101 anEdge1->firstPoint() : anEdge1->lastPoint();
102 GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
103 anEdge1->lastPoint() : anEdge1->firstPoint();
104 GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
105 anEdge2->firstPoint() : anEdge2->lastPoint();
106 GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
107 anEdge2->lastPoint() : anEdge2->firstPoint();
109 GeomPointPtr aCommonEndPnt;
110 if (aFirstPnt1->isEqual(aLastPnt2)) {
111 aCommonEndPnt = aFirstPnt1;
112 } else if(aLastPnt1->isEqual(aFirstPnt2)) {
113 aCommonEndPnt = aLastPnt1;
116 isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);