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 <BRep_Tool.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <Geom_Curve.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopExp_Explorer.hxx>
34 static GeomShapePtr fromTopoDS(const TopoDS_Shape& theShape)
36 GeomShapePtr aResultShape(new GeomAPI_Shape());
37 aResultShape->setImpl(new TopoDS_Shape(theShape));
41 GeomAlgoAPI_WireBuilder::GeomAlgoAPI_WireBuilder(const ListOfShape& theShapes)
43 TopTools_ListOfShape aListOfEdges;
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()) {
50 aListOfEdges.Append(aShape);
54 for (TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
55 aListOfEdges.Append(anExp.Current());
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);
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);
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);
89 setShape(fromTopoDS(aWire));
94 //=================================================================================================
95 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
97 return GeomAlgoAPI_WireBuilder(theShapes).shape();
100 //=================================================================================================
101 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
106 GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
107 for (; anExp.more(); anExp.next()) {
108 GeomShapePtr anEdge = anExp.current();
109 anEdges.push_back(anEdge);
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));
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()) {
128 if (anInter->isVertex()) {
129 GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
130 GeomPointPtr aPnt = aVertex->point();
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();
141 GeomPointPtr aCommonEndPnt;
142 if (aFirstPnt1->isEqual(aLastPnt2)) {
143 aCommonEndPnt = aFirstPnt1;
144 } else if(aLastPnt1->isEqual(aFirstPnt2)) {
145 aCommonEndPnt = aLastPnt1;
148 isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);