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 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
36 TopTools_ListOfShape aListOfEdges;
38 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); 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 std::shared_ptr<GeomAPI_Shape>& theWire)
74 for (GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
75 GeomShapePtr anEdge = anExp.current();
76 anEdges.push_back(anEdge);
79 // Check intersections between edges pair-wise
80 int aNbEdges = (int)anEdges.size();
81 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anEdgesIt = anEdges.begin();
82 for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
83 std::shared_ptr<GeomAPI_Edge> anEdge1(new GeomAPI_Edge(*anEdgesIt));
85 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
86 for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
87 std::shared_ptr<GeomAPI_Edge> anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
88 GeomShapePtr anInter = anEdge1->intersect(anEdge2);
95 if (anInter->isVertex()) {
96 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
97 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
99 GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
100 anEdge1->firstPoint() : anEdge1->lastPoint();
101 GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
102 anEdge1->lastPoint() : anEdge1->firstPoint();
103 GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
104 anEdge2->firstPoint() : anEdge2->lastPoint();
105 GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
106 anEdge2->lastPoint() : anEdge2->firstPoint();
108 std::shared_ptr<GeomAPI_Pnt> aCommonEndPnt;
109 if (aFirstPnt1->isEqual(aLastPnt2)) {
110 aCommonEndPnt = aFirstPnt1;
111 } else if(aLastPnt1->isEqual(aFirstPnt2)) {
112 aCommonEndPnt = aLastPnt1;
115 isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);