1 // Copyright (C) 2020 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_Fillet1D.h>
22 #include <GeomAlgoAPI_Copy.h>
23 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_Pnt.h>
29 #include <GeomAPI_Wire.h>
30 #include <GeomAPI_WireExplorer.h>
32 #include <GEOMImpl_Fillet1d.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRepTools_WireExplorer.hxx>
36 #include <ShapeFix_Wire.hxx>
38 #include <TopoDS_Wire.hxx>
40 static GeomShapePtr convert(const TopoDS_Shape& theShape)
42 GeomShapePtr aNewShape(new GeomAPI_Shape);
43 aNewShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(theShape));
47 static void substituteNewEdge(GeomEdgePtr theEdge,
48 std::map<GeomShapePtr, ListOfShape, GeomAPI_Shape::Comparator>& theMap)
50 std::map<GeomShapePtr, ListOfShape, GeomAPI_Shape::Comparator>::iterator anIt = theMap.begin();
51 for (; anIt != theMap.end(); ++anIt) {
52 for (ListOfShape::iterator aEIt = anIt->second.begin(); aEIt != anIt->second.end(); ++aEIt)
53 if (theEdge->isEqual(*aEIt)) {
54 // substitute edge and stop iteration
62 GeomAlgoAPI_Fillet1D::GeomAlgoAPI_Fillet1D(const GeomShapePtr& theBaseWire,
63 const ListOfShape& theFilletVertices,
64 const double theFilletRadius)
66 build(theBaseWire, theFilletVertices, theFilletRadius);
69 void GeomAlgoAPI_Fillet1D::build(const GeomShapePtr& theBaseWire,
70 const ListOfShape& theFilletVertices,
71 const double theRadius)
73 if (!theBaseWire || theFilletVertices.empty() || theRadius < 0.)
76 // store all edges of a base wire as modified, because they will be rebuild by ShapeFix
77 for (GeomAPI_WireExplorer aWExp(theBaseWire->wire()); aWExp.more(); aWExp.next()) {
78 GeomShapePtr aCurrent = aWExp.current();
79 GeomAlgoAPI_Copy aCopy(aCurrent);
80 myModified[aCurrent].push_back(aCopy.shape());
83 GeomAlgoAPI_MapShapesAndAncestors aMapVE(theBaseWire, GeomAPI_Shape::VERTEX,
86 for (ListOfShape::const_iterator aVIt = theFilletVertices.begin();
87 aVIt != theFilletVertices.end(); ++aVIt) {
88 // get edges to perform fillet
89 MapShapeToShapes::const_iterator aVE = aMapVE.map().find(*aVIt);
90 if (aVE == aMapVE.map().end())
93 for (SetOfShapes::const_iterator aEIt = aVE->second.begin();
94 aEIt != aVE->second.end(); ++aEIt) {
95 ListOfShape aNewEdges;
96 modified(*aEIt, aNewEdges);
97 if (aNewEdges.empty())
98 anEdges.push_back(*aEIt);
100 anEdges.insert(anEdges.end(), aNewEdges.begin(), aNewEdges.end());
103 GeomPlanePtr aPlane = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
105 return; // non-planar edges
107 TopoDS_Edge anEdge1 = TopoDS::Edge(anEdges.front()->impl<TopoDS_Shape>());
108 TopoDS_Edge anEdge2 = TopoDS::Edge(anEdges.back()->impl<TopoDS_Shape>());
110 // create fillet builder
111 GEOMImpl_Fillet1d aFilletBuilder(anEdge1, anEdge2, aPlane->impl<gp_Pln>());
112 if (!aFilletBuilder.Perform(theRadius))
113 return; // fillet is failed, no way to continue
115 GeomPointPtr aPoint = aVE->first->vertex()->point();
116 TopoDS_Edge aFilletEdge = aFilletBuilder.Result(aPoint->impl<gp_Pnt>(), anEdge1, anEdge2);
118 // store modified shapes
119 myGenerated[aVE->first].push_back(convert(aFilletEdge));
120 SetOfShapes::const_iterator aEIt = aVE->second.begin();
121 myModified[*aEIt].clear();
122 myModified[*aEIt].push_back(convert(anEdge1));
123 myModified[*(++aEIt)].clear();
124 myModified[*aEIt].push_back(convert(anEdge2));
127 // compose a new wire
128 TopoDS_Wire aNewWire;
129 BRep_Builder aBuilder;
130 aBuilder.MakeWire(aNewWire);
131 GeomWirePtr aBaseWire = theBaseWire->wire();
132 GeomAPI_WireExplorer aWExp(aBaseWire);
133 GeomShapePtr aBaseFirstEdge = aWExp.current();
134 for (; aWExp.more(); aWExp.next()) {
135 ListOfShape aNewEdges;
136 modified(aWExp.current(), aNewEdges);
137 if (aNewEdges.empty())
138 aNewEdges.push_back(aWExp.current());
139 for (ListOfShape::iterator anIt = aNewEdges.begin(); anIt != aNewEdges.end(); ++anIt)
140 aBuilder.Add(aNewWire, TopoDS::Edge((*anIt)->impl<TopoDS_Shape>()));
142 for (MapModified::iterator aGenIt = myGenerated.begin(); aGenIt != myGenerated.end(); ++aGenIt) {
143 for (ListOfShape::iterator anIt = aGenIt->second.begin(); anIt != aGenIt->second.end(); ++anIt)
144 aBuilder.Add(aNewWire, TopoDS::Edge((*anIt)->impl<TopoDS_Shape>()));
146 // fix the wire connectivity
147 ShapeFix_Wire aFixWire;
148 aFixWire.Load(aNewWire);
149 aFixWire.ClosedWireMode() = aBaseWire->isClosed();
150 aFixWire.FixReorder();
151 aNewWire = aFixWire.WireAPIMake();
152 if (aNewWire.IsNull())
155 // update the map of modified shapes, because the edges are changed by ShapeFix
156 for (BRepTools_WireExplorer anExp(aNewWire); anExp.More(); anExp.Next()) {
157 GeomEdgePtr aCurrent(new GeomAPI_Edge);
158 aCurrent->setImpl(new TopoDS_Edge(anExp.Current()));
159 substituteNewEdge(aCurrent, myGenerated);
160 substituteNewEdge(aCurrent, myModified);
163 // rebuild the wire once again to get the first edge of fillet wire correspond
164 // to the first edge of original wire
165 TopoDS_Edge aFirstEdge = TopoDS::Edge(aBaseFirstEdge->impl<TopoDS_Shape>());
166 ListOfShape aNewEdges;
167 modified(aBaseFirstEdge, aNewEdges);
168 if (!aNewEdges.empty())
169 aFirstEdge = TopoDS::Edge(aNewEdges.front()->impl<TopoDS_Shape>());
170 TopTools_ListOfShape aKeptForEnd;
171 BRepTools_WireExplorer aNewExp(aNewWire);
172 for (; aNewExp.More(); aNewExp.Next())
173 if (aNewExp.Current().IsEqual(aFirstEdge))
175 if (aNewExp.More()) {
176 TopoDS_Wire aReorderedWire;
177 aBuilder.MakeWire(aReorderedWire);
178 for (; aNewExp.More(); aNewExp.Next())
179 aBuilder.Add(aReorderedWire, aNewExp.Current());
180 for (aNewExp.Init(aNewWire); aNewExp.More(); aNewExp.Next()) {
181 if (aNewExp.Current().IsEqual(aFirstEdge))
183 aBuilder.Add(aReorderedWire, aNewExp.Current());
185 aNewWire = aReorderedWire;
188 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
189 aShape->setImpl(new TopoDS_Shape(aNewWire));
190 myModified[theBaseWire].push_back(aShape);
196 void GeomAlgoAPI_Fillet1D::generated(const GeomShapePtr theOldShape,
197 ListOfShape& theNewShapes)
199 MapModified::iterator aFound = myGenerated.find(theOldShape);
200 if (aFound != myGenerated.end())
201 theNewShapes = aFound->second;
204 void GeomAlgoAPI_Fillet1D::modified(const GeomShapePtr theOldShape,
205 ListOfShape& theNewShapes)
207 MapModified::iterator aFound = myModified.find(theOldShape);
208 if (aFound != myModified.end())
209 theNewShapes = aFound->second;