1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_WireBuilder.h"
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_ShapeExplorer.h>
28 #include <BRepBuilderAPI_MakeWire.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopExp_Explorer.hxx>
33 //=================================================================================================
34 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
36 TopTools_ListOfShape aListOfEdges;
38 ListOfShape::const_iterator anIt = theShapes.cbegin();
39 for(; anIt != theShapes.cend(); ++anIt) {
40 const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
41 switch(aShape.ShapeType()) {
43 aListOfEdges.Append(aShape);
47 for(TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
48 aListOfEdges.Append(anExp.Current());
53 return GeomShapePtr();
58 BRepBuilderAPI_MakeWire aWireBuilder;
59 aWireBuilder.Add(aListOfEdges);
60 if(aWireBuilder.Error() != BRepBuilderAPI_WireDone) {
61 return GeomShapePtr();
64 GeomShapePtr aResultShape(new GeomAPI_Shape());
65 aResultShape->setImpl(new TopoDS_Shape(aWireBuilder.Wire()));
69 //=================================================================================================
70 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
75 GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
76 for (; anExp.more(); anExp.next()) {
77 GeomShapePtr anEdge = anExp.current();
78 anEdges.push_back(anEdge);
81 // Check intersections between edges pair-wise
82 int aNbEdges = (int)anEdges.size();
83 std::list<GeomShapePtr>::const_iterator anEdgesIt = anEdges.begin();
84 for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
85 GeomEdgePtr anEdge1(new GeomAPI_Edge(*anEdgesIt));
87 std::list<GeomShapePtr>::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
88 for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
89 GeomEdgePtr anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
90 GeomShapePtr anInter = anEdge1->intersect(anEdge2);
97 if (anInter->isVertex()) {
98 GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
99 GeomPointPtr aPnt = aVertex->point();
101 GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
102 anEdge1->firstPoint() : anEdge1->lastPoint();
103 GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
104 anEdge1->lastPoint() : anEdge1->firstPoint();
105 GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
106 anEdge2->firstPoint() : anEdge2->lastPoint();
107 GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
108 anEdge2->lastPoint() : anEdge2->firstPoint();
110 GeomPointPtr aCommonEndPnt;
111 if (aFirstPnt1->isEqual(aLastPnt2)) {
112 aCommonEndPnt = aFirstPnt1;
113 } else if(aLastPnt1->isEqual(aFirstPnt2)) {
114 aCommonEndPnt = aLastPnt1;
117 isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);