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_SketchBuilder.h>
21 #include <GeomAPI_PlanarEdges.h>
23 #include <BOPAlgo_Builder.hxx>
24 #include <BRep_Builder.hxx>
25 #include <BRepTools_WireExplorer.hxx>
26 #include <BRepTopAdaptor_FClass2d.hxx>
27 #include <Geom_Plane.hxx>
28 #include <Geom_TrimmedCurve.hxx>
29 #include <Precision.hxx>
31 #include <TopExp_Explorer.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <GProp_GProps.hxx>
36 #include <BRepGProp.hxx>
42 static TopoDS_Vertex findStartVertex(const TopoDS_Shape& theShape)
44 static const double aTol = Precision::PConfusion();
46 TopExp_Explorer anExp(theShape, TopAbs_VERTEX);
47 TopoDS_Vertex aStart = TopoDS::Vertex(anExp.Current());
48 gp_Pnt aStartPnt(BRep_Tool::Pnt(aStart));
49 TopoDS_Vertex aCurrent;
52 for (anExp.Next(); anExp.More(); anExp.Next()) {
53 aCurrent = TopoDS::Vertex(anExp.Current());
54 aCurrentPnt = BRep_Tool::Pnt(aCurrent);
55 if ((aCurrentPnt.X() > aStartPnt.X() + aTol) ||
56 (aCurrentPnt.X() > aStartPnt.X() - aTol && aCurrentPnt.Y() > aStartPnt.Y() + aTol) ||
57 (aCurrentPnt.X() > aStartPnt.X() - aTol && aCurrentPnt.Y() > aStartPnt.Y() - aTol &&
58 aCurrentPnt.Z() > aStartPnt.Z() + aTol)) {
60 aStartPnt = aCurrentPnt;
66 static TopoDS_Vertex findStartVertex(const TopoDS_Wire& theWire, const TopoDS_Face& theFace,
67 const std::list<std::shared_ptr<GeomAPI_Shape> >& theInitialShapes)
69 // Try to find edge lying on the one of original edges.
70 // First found edge will be taken as a start edge for the result wire
71 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theInitialShapes.begin();
72 for (; aFeatIt != theInitialShapes.end(); aFeatIt++) {
73 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
74 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
75 if (anEdge.ShapeType() != TopAbs_EDGE)
79 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
80 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
81 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
83 BRepTools_WireExplorer anExp(theWire, theFace);
84 for (; anExp.More(); anExp.Next()) {
85 const TopoDS_Edge& aShapeEdge = anExp.Current();
87 Handle(Geom_Curve) aShapeCurve = BRep_Tool::Curve(aShapeEdge, aF, aL);
88 if (aShapeCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
89 aShapeCurve = Handle(Geom_TrimmedCurve)::DownCast(aShapeCurve)->BasisCurve();
91 if (aCurve != aShapeCurve)
94 // the edge is found, search vertex
95 TopoDS_Vertex aV1, aV2;
96 TopExp::Vertices(aShapeEdge, aV1, aV2);
97 return fabs(aF - aFirst) <= fabs(aL - aFirst) ? aV1 : aV2;
101 // start vertex is not found, use algorithm to search vertex with the greatest coordinates
102 return findStartVertex(theWire);
105 // returns true if the first shape must be located earlier than the second
106 bool isFirst(const TopoDS_Shape& theFirst, const TopoDS_Shape& theSecond,
107 NCollection_DataMap<TopoDS_Shape, NCollection_Array1<int> >& theAreaToIndex,
108 const NCollection_DataMap<Handle(Geom_Curve), int>& theCurveToIndex)
110 // fill theAreaToIndex for both shapes if needed
111 for(int aShapeNum = 1; aShapeNum <= 2; aShapeNum++) {
112 TopoDS_Shape aShape = aShapeNum == 1 ? theFirst : theSecond;
113 if (!theAreaToIndex.IsBound(aShape)) { // fill the list of curve indices
114 NCollection_List<int> aNewList;
115 TopExp_Explorer anEdgesExp(aShape, TopAbs_EDGE);
116 for (; anEdgesExp.More(); anEdgesExp.Next()) {
117 double aFirst, aLast;
118 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(
119 TopoDS::Edge(anEdgesExp.Current()), aFirst, aLast);
120 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
121 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
122 if (theCurveToIndex.IsBound(aCurve)) {
123 aNewList.Append(theCurveToIndex.Find(aCurve));
126 if (aNewList.Extent()) {
127 NCollection_Array1<int> aNewArray(1, aNewList.Extent());
128 NCollection_List<int>::Iterator aListIter(aNewList);
129 for (int anIndex = 1; aListIter.More(); aListIter.Next(), anIndex++) {
130 aNewArray.SetValue(anIndex, aListIter.Value());
132 std::sort(aNewArray.begin(), aNewArray.end());
133 theAreaToIndex.Bind(aShape, aNewArray);
138 bool aGeomCompare = !theAreaToIndex.IsBound(theFirst) || !theAreaToIndex.IsBound(theSecond);
140 // compare lists of indices one by one to find chich list indices are lower
141 NCollection_Array1<int>::Iterator aFirstList(theAreaToIndex.ChangeFind(theFirst));
142 NCollection_Array1<int>::Iterator aSecondList(theAreaToIndex.ChangeFind(theSecond));
143 for (; aFirstList.More() && aSecondList.More(); aFirstList.Next(), aSecondList.Next()) {
144 if (aFirstList.Value() < aSecondList.Value()) return true;
145 if (aFirstList.Value() > aSecondList.Value()) return false;
147 aGeomCompare = !aFirstList.More() && !aSecondList.More();
148 isFirst = !aFirstList.More();
150 isFirst = !theAreaToIndex.IsBound(theFirst);
152 // if faces are identical by curves names (circle splitted by line in seam-point), use parameters
154 GProp_GProps aGProps;
155 BRepGProp::SurfaceProperties(theFirst, aGProps);
156 gp_Pnt aCentre1 = aGProps.CentreOfMass();
157 BRepGProp::SurfaceProperties(theSecond, aGProps);
158 gp_Pnt aCentre2 = aGProps.CentreOfMass();
159 return aCentre1.X() + aCentre1.Y() + aCentre1.Z() < aCentre2.X() + aCentre2.Y() + aCentre2.Z();
161 // if in first list there is no elements left, it is the first
165 // sorts faces (in theAreas list) to make persistent order: by initial shapes edges
166 static void sortFaces(TopTools_ListOfShape& theAreas,
167 const std::list<std::shared_ptr<GeomAPI_Shape> >& theInitialShapes)
169 // collect indices of all edges to operate them quickly
170 NCollection_DataMap<Handle(Geom_Curve), int> aCurveToIndex; // curve -> index in initial shapes
171 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theInitialShapes.begin();
172 for (int anIndex = 0; aFeatIt != theInitialShapes.end(); aFeatIt++) {
173 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
174 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
175 if (anEdge.ShapeType() != TopAbs_EDGE)
178 double aFirst, aLast;
179 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
180 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
181 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
182 if (!aCurveToIndex.IsBound(aCurve))
183 aCurveToIndex.Bind(aCurve, anIndex++);
185 // map from area to the most first indices of curves (to compare) in it
186 NCollection_DataMap<TopoDS_Shape, NCollection_Array1<int> > anAreaToIndex;
188 TopTools_ListOfShape::Iterator anArea1(theAreas);
189 for(; anArea1.More(); anArea1.Next()) {
190 TopTools_ListOfShape::Iterator anArea2 = anArea1;
191 for(anArea2.Next(); anArea2.More(); anArea2.Next()) {
192 if (!isFirst(anArea1.Value(), anArea2.Value(), anAreaToIndex, aCurveToIndex)) { // exchange
193 TopoDS_Shape aTmp = anArea1.Value();
194 anArea1.ChangeValue() = anArea2.Value();
195 anArea2.ChangeValue() = aTmp;
201 void GeomAlgoAPI_SketchBuilder::createFaces(
202 const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
203 const std::shared_ptr<GeomAPI_Dir>& theDirX,
204 const std::shared_ptr<GeomAPI_Dir>& theNorm,
205 const std::list<std::shared_ptr<GeomAPI_Shape> >& theFeatures,
206 std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces)
208 if (theFeatures.empty())
211 BRep_Builder aBuilder;
212 // Planar face, where the sketch was built
213 Handle(Geom_Surface) aPlane(new Geom_Plane(theOrigin->impl<gp_Pnt>(), theNorm->impl<gp_Dir>()));
214 TopoDS_Face aPlnFace;
215 aBuilder.MakeFace(aPlnFace, aPlane, Precision::Confusion());
217 // Use General Fuse algorithm to prepare all subfaces, bounded by given list of edges
219 aBB.AddArgument(aPlnFace);
221 NCollection_List<TopoDS_Shape> anEdges;
222 NCollection_List<TopoDS_Shape>::Iterator aShapeIt;
223 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theFeatures.begin();
224 for (; aFeatIt != theFeatures.end(); aFeatIt++) {
225 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
226 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
227 if (anEdge.ShapeType() == TopAbs_EDGE)
228 aBB.AddArgument(anEdge);
234 TopTools_ListOfShape anAreas = aBB.Modified(aPlnFace);
235 sortFaces(anAreas, theFeatures); // sort faces by the edges in them
236 TopTools_ListIteratorOfListOfShape anIt(anAreas);
237 for (; anIt.More(); anIt.Next()) {
238 TopoDS_Face aFace = TopoDS::Face(anIt.Value());
239 // avoid infinite faces
240 BRepTopAdaptor_FClass2d aFClass(aFace, Precision::Confusion());
241 if (aFClass.PerformInfinitePoint() == TopAbs_IN)
245 TopoDS_Face aNewFace;
246 aBuilder.MakeFace(aNewFace, aPlane, Precision::Confusion());
249 TopExp_Explorer aWireExp(aFace, TopAbs_WIRE);
250 for (; aWireExp.More(); aWireExp.Next()) {
251 TopoDS_Wire aWire = TopoDS::Wire(aWireExp.Current());
253 // to make faces equal on different platforms, we will find
254 // a vertex lying on an edge with the lowest index in the list of initial edges
255 TopoDS_Vertex aStartVertex = findStartVertex(aWire, aFace, theFeatures);
257 TopoDS_Wire aNewWire;
258 aBuilder.MakeWire(aNewWire);
259 std::list<TopoDS_Edge> aSkippedEdges;
260 bool aStartFound = false;
262 // remove internal edges from faces and make wire start from found vertex
263 BRepTools_WireExplorer anExp(aWire, aFace);
264 for (; anExp.More(); anExp.Next()) {
265 if (anExp.Current().Orientation() == TopAbs_INTERNAL)
268 const TopoDS_Edge& anEdge = anExp.Current();
269 TopoDS_Vertex aV1, aV2;
270 TopExp::Vertices(anEdge, aV1, aV2, Standard_True);
271 if (aV1.IsSame(aStartVertex) == Standard_True)
274 aSkippedEdges.push_back(anEdge);
277 aBuilder.Add(aNewWire, anExp.Current());
279 // add skipped edges to the end of wire
280 std::list<TopoDS_Edge>::const_iterator aSkIt = aSkippedEdges.begin();
281 for (; aSkIt != aSkippedEdges.end(); ++aSkIt)
282 aBuilder.Add(aNewWire, *aSkIt);
284 // check the wire is empty
285 anExp.Init(aNewWire);
287 aBuilder.Add(aNewFace, aNewWire);
292 std::shared_ptr<GeomAPI_Shape> aResFace(new GeomAPI_Shape);
293 aResFace->setImpl(new TopoDS_Face(aFace));
294 theResultFaces.push_back(aResFace);
298 void GeomAlgoAPI_SketchBuilder::createFaces(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
299 const std::shared_ptr<GeomAPI_Dir>& theDirX,
300 const std::shared_ptr<GeomAPI_Dir>& theNorm,
301 const std::shared_ptr<GeomAPI_Shape>& theWire,
302 std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces)
304 std::shared_ptr<GeomAPI_PlanarEdges> aWire =
305 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theWire);
307 // Filter wires, return only faces.
308 createFaces(theOrigin, theDirX, theNorm, aWire->getEdges(), theResultFaces);
309 } else { // it may be only one circle
310 std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(theWire);
312 std::list<std::shared_ptr<GeomAPI_Shape> > aList;
313 aList.push_back(anEdge);
314 createFaces(theOrigin, theDirX, theNorm, aList, theResultFaces);