Salome HOME
Task #3231: Sketcher Offset of a curve
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_WireBuilder.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_WireBuilder.h"
21
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_ShapeExplorer.h>
26
27 #include <BRep_Tool.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <Geom_Curve.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopExp_Explorer.hxx>
33
34 static GeomShapePtr fromTopoDS(const TopoDS_Shape& theShape)
35 {
36   GeomShapePtr aResultShape(new GeomAPI_Shape());
37   aResultShape->setImpl(new TopoDS_Shape(theShape));
38   return aResultShape;
39 }
40
41 GeomAlgoAPI_WireBuilder::GeomAlgoAPI_WireBuilder(const ListOfShape& theShapes)
42 {
43   TopTools_ListOfShape aListOfEdges;
44
45   ListOfShape::const_iterator anIt = theShapes.cbegin();
46   for (; anIt != theShapes.cend(); ++anIt) {
47     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
48     switch (aShape.ShapeType()) {
49     case TopAbs_EDGE: {
50       aListOfEdges.Append(aShape);
51       break;
52     }
53     case TopAbs_WIRE: {
54       for (TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
55         aListOfEdges.Append(anExp.Current());
56       }
57       break;
58     }
59     default:
60       break;
61     }
62   }
63
64   BRepBuilderAPI_MakeWire* aWireBuilder = new BRepBuilderAPI_MakeWire;
65   aWireBuilder->Add(aListOfEdges);
66   if (aWireBuilder->Error() == BRepBuilderAPI_WireDone) {
67     setImpl(aWireBuilder);
68     setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
69
70     // store generated/modified shapes
71     TopoDS_Wire aWire = aWireBuilder->Wire();
72     for (TopTools_ListOfShape::Iterator aBaseIt(aListOfEdges); aBaseIt.More(); aBaseIt.Next()) {
73       TopoDS_Edge aBaseCurrent = TopoDS::Edge(aBaseIt.Value());
74       Standard_Real aFirst, aLast;
75       Handle(Geom_Curve) aBaseCurve = BRep_Tool::Curve(aBaseCurrent, aFirst, aLast);
76
77       for (TopExp_Explorer anExp(aWire, TopAbs_EDGE); anExp.More(); anExp.Next()) {
78         TopoDS_Edge aNewCurrent = TopoDS::Edge(anExp.Current());
79         Handle(Geom_Curve) aNewCurve = BRep_Tool::Curve(aNewCurrent, aFirst, aLast);
80         if (aBaseCurve == aNewCurve) {
81           GeomShapePtr aBaseShape = fromTopoDS(aBaseCurrent);
82           GeomShapePtr aNewShape = fromTopoDS(aNewCurrent);
83           addGenerated(aBaseShape, aNewShape);
84           addModified(aBaseShape, aNewShape);
85         }
86       }
87     }
88
89     setShape(fromTopoDS(aWire));
90     setDone(true);
91   }
92 }
93
94 //=================================================================================================
95 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
96 {
97   return GeomAlgoAPI_WireBuilder(theShapes).shape();
98 }
99
100 //=================================================================================================
101 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
102 {
103   // Collect edges.
104   ListOfShape anEdges;
105
106   GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
107   for (; anExp.more(); anExp.next()) {
108     GeomShapePtr anEdge = anExp.current();
109     anEdges.push_back(anEdge);
110   }
111
112   // Check intersections between edges pair-wise
113   int aNbEdges = (int)anEdges.size();
114   std::list<GeomShapePtr>::const_iterator anEdgesIt = anEdges.begin();
115   for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
116     GeomEdgePtr anEdge1(new GeomAPI_Edge(*anEdgesIt));
117
118     std::list<GeomShapePtr>::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
119     for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
120       GeomEdgePtr anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
121       GeomShapePtr anInter = anEdge1->intersect(anEdge2);
122       if (!anInter.get()) {
123         continue;
124       }
125
126       bool isOk = false;
127
128       if (anInter->isVertex()) {
129         GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
130         GeomPointPtr aPnt = aVertex->point();
131
132         GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
133                                   anEdge1->firstPoint() : anEdge1->lastPoint();
134         GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
135                                  anEdge1->lastPoint() : anEdge1->firstPoint();
136         GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
137                                   anEdge2->firstPoint() : anEdge2->lastPoint();
138         GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
139                                  anEdge2->lastPoint() : anEdge2->firstPoint();
140
141         GeomPointPtr aCommonEndPnt;
142         if (aFirstPnt1->isEqual(aLastPnt2)) {
143           aCommonEndPnt = aFirstPnt1;
144         } else if(aLastPnt1->isEqual(aFirstPnt2)) {
145           aCommonEndPnt = aLastPnt1;
146         }
147
148         isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);
149       }
150
151       if (!isOk) {
152         return true;
153       }
154     }
155   }
156
157   return false;
158 }