1 // Copyright (C) 2014-2022 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 <GeomAPI_Pln.h>
25 #include <BOPAlgo_Builder.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRepTools_WireExplorer.hxx>
28 #include <BRepTopAdaptor_FClass2d.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom_TrimmedCurve.hxx>
31 #include <Precision.hxx>
33 #include <TopExp_Explorer.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <GProp_GProps.hxx>
38 #include <BRepGProp.hxx>
44 static TopoDS_Vertex findStartVertex(const TopoDS_Shape& theShape)
46 static const double aTol = Precision::PConfusion();
48 TopExp_Explorer anExp(theShape, TopAbs_VERTEX);
49 TopoDS_Vertex aStart = TopoDS::Vertex(anExp.Current());
50 gp_Pnt aStartPnt(BRep_Tool::Pnt(aStart));
51 TopoDS_Vertex aCurrent;
54 for (anExp.Next(); anExp.More(); anExp.Next()) {
55 aCurrent = TopoDS::Vertex(anExp.Current());
56 aCurrentPnt = BRep_Tool::Pnt(aCurrent);
57 if ((aCurrentPnt.X() > aStartPnt.X() + aTol) ||
58 (aCurrentPnt.X() > aStartPnt.X() - aTol && aCurrentPnt.Y() > aStartPnt.Y() + aTol) ||
59 (aCurrentPnt.X() > aStartPnt.X() - aTol && aCurrentPnt.Y() > aStartPnt.Y() - aTol &&
60 aCurrentPnt.Z() > aStartPnt.Z() + aTol)) {
62 aStartPnt = aCurrentPnt;
68 static TopoDS_Vertex findStartVertex(const TopoDS_Wire& theWire, const TopoDS_Face& theFace,
69 const std::list<std::shared_ptr<GeomAPI_Shape> >& theInitialShapes)
71 // Try to find edge lying on the one of original edges.
72 // First found edge will be taken as a start edge for the result wire
73 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theInitialShapes.begin();
74 for (; aFeatIt != theInitialShapes.end(); aFeatIt++) {
75 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
76 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
77 if (anEdge.ShapeType() != TopAbs_EDGE)
81 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
82 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
83 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
85 BRepTools_WireExplorer anExp(theWire, theFace);
86 for (; anExp.More(); anExp.Next()) {
87 const TopoDS_Edge& aShapeEdge = anExp.Current();
89 Handle(Geom_Curve) aShapeCurve = BRep_Tool::Curve(aShapeEdge, aF, aL);
90 if (aShapeCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
91 aShapeCurve = Handle(Geom_TrimmedCurve)::DownCast(aShapeCurve)->BasisCurve();
93 if (aCurve != aShapeCurve)
96 // the edge is found, search vertex
97 TopoDS_Vertex aV1, aV2;
98 TopExp::Vertices(aShapeEdge, aV1, aV2);
99 return fabs(aF - aFirst) <= fabs(aL - aFirst) ? aV1 : aV2;
103 // start vertex is not found, use algorithm to search vertex with the greatest coordinates
104 return findStartVertex(theWire);
107 // returns true if the first shape must be located earlier than the second
108 bool isFirst(const TopoDS_Shape& theFirst, const TopoDS_Shape& theSecond,
109 NCollection_DataMap<TopoDS_Shape, NCollection_Array1<int> >& theAreaToIndex,
110 const NCollection_DataMap<Handle(Geom_Curve), int>& theCurveToIndex)
112 // fill theAreaToIndex for both shapes if needed
113 for(int aShapeNum = 1; aShapeNum <= 2; aShapeNum++) {
114 TopoDS_Shape aShape = aShapeNum == 1 ? theFirst : theSecond;
115 if (!theAreaToIndex.IsBound(aShape)) { // fill the list of curve indices
116 NCollection_List<int> aNewList;
117 TopExp_Explorer anEdgesExp(aShape, TopAbs_EDGE);
118 for (; anEdgesExp.More(); anEdgesExp.Next()) {
119 double aFirst, aLast;
120 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(
121 TopoDS::Edge(anEdgesExp.Current()), aFirst, aLast);
122 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
123 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
124 if (theCurveToIndex.IsBound(aCurve)) {
125 aNewList.Append(theCurveToIndex.Find(aCurve));
128 if (aNewList.Extent()) {
129 NCollection_Array1<int> aNewArray(1, aNewList.Extent());
130 NCollection_List<int>::Iterator aListIter(aNewList);
131 for (int anIndex = 1; aListIter.More(); aListIter.Next(), anIndex++) {
132 aNewArray.SetValue(anIndex, aListIter.Value());
134 std::sort(aNewArray.begin(), aNewArray.end());
135 theAreaToIndex.Bind(aShape, aNewArray);
140 bool aGeomCompare = !theAreaToIndex.IsBound(theFirst) || !theAreaToIndex.IsBound(theSecond);
142 // compare lists of indices one by one to find chich list indices are lower
143 NCollection_Array1<int>::Iterator aFirstList(theAreaToIndex.ChangeFind(theFirst));
144 NCollection_Array1<int>::Iterator aSecondList(theAreaToIndex.ChangeFind(theSecond));
145 for (; aFirstList.More() && aSecondList.More(); aFirstList.Next(), aSecondList.Next()) {
146 if (aFirstList.Value() < aSecondList.Value()) return true;
147 if (aFirstList.Value() > aSecondList.Value()) return false;
149 aGeomCompare = !aFirstList.More() && !aSecondList.More();
150 isFirst = !aFirstList.More();
152 isFirst = !theAreaToIndex.IsBound(theFirst);
154 // if faces are identical by curves names (circle splitted by line in seam-point), use parameters
156 GProp_GProps aGProps;
157 BRepGProp::SurfaceProperties(theFirst, aGProps);
158 gp_Pnt aCentre1 = aGProps.CentreOfMass();
159 BRepGProp::SurfaceProperties(theSecond, aGProps);
160 gp_Pnt aCentre2 = aGProps.CentreOfMass();
161 return aCentre1.X() + aCentre1.Y() + aCentre1.Z() < aCentre2.X() + aCentre2.Y() + aCentre2.Z();
163 // if in first list there is no elements left, it is the first
167 // sorts faces (in theAreas list) to make persistent order: by initial shapes edges
168 static void sortAreas(TopTools_ListOfShape& theAreas,
169 const std::list<std::shared_ptr<GeomAPI_Shape> >& theInitialShapes)
171 // collect indices of all edges to operate them quickly
172 NCollection_DataMap<Handle(Geom_Curve), int> aCurveToIndex; // curve -> index in initial shapes
173 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theInitialShapes.begin();
174 for (int anIndex = 0; aFeatIt != theInitialShapes.end(); aFeatIt++) {
175 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
176 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
177 if (anEdge.ShapeType() != TopAbs_EDGE)
180 double aFirst, aLast;
181 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
182 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
183 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
184 if (!aCurveToIndex.IsBound(aCurve))
185 aCurveToIndex.Bind(aCurve, anIndex++);
187 // map from area to the most first indices of curves (to compare) in it
188 NCollection_DataMap<TopoDS_Shape, NCollection_Array1<int> > anAreaToIndex;
190 TopTools_ListOfShape::Iterator anArea1(theAreas);
191 for(; anArea1.More(); anArea1.Next()) {
192 TopTools_ListOfShape::Iterator anArea2 = anArea1;
193 for(anArea2.Next(); anArea2.More(); anArea2.Next()) {
194 if (!isFirst(anArea1.Value(), anArea2.Value(), anAreaToIndex, aCurveToIndex)) { // exchange
195 TopoDS_Shape aTmp = anArea1.Value();
196 anArea1.ChangeValue() = anArea2.Value();
197 anArea2.ChangeValue() = aTmp;
203 void GeomAlgoAPI_SketchBuilder::build(
204 const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
205 const std::shared_ptr<GeomAPI_Dir>& theDirX,
206 const std::shared_ptr<GeomAPI_Dir>& theNorm,
207 const std::list<std::shared_ptr<GeomAPI_Shape> >& theEdges)
209 myResultFaces.clear();
211 if (theEdges.empty())
214 BRep_Builder aBuilder;
215 // Planar face, where the sketch was built
216 gp_Ax3 aPlnAxes(theOrigin->impl<gp_Pnt>(), theNorm->impl<gp_Dir>(), theDirX->impl<gp_Dir>());
217 Handle(Geom_Surface) aPlane(new Geom_Plane(aPlnAxes));
218 TopoDS_Face aPlnFace;
219 aBuilder.MakeFace(aPlnFace, aPlane, Precision::Confusion());
221 // Use General Fuse algorithm to prepare all subfaces, bounded by given list of edges
222 BOPAlgo_Builder* aBB = new BOPAlgo_Builder;
223 aBB->AddArgument(aPlnFace);
224 // Set fuzzy value for BOP, because PlaneGCS can solve the set of constraints with
225 // the precision up to 5.e-5 if the sketch contains arcs.
226 static const double THE_FUZZY_TOL = 5.e-5;
227 aBB->SetFuzzyValue(THE_FUZZY_TOL);
230 setBuilderType(OCCT_BOPAlgo_Builder);
232 NCollection_List<TopoDS_Shape> anEdges;
233 NCollection_List<TopoDS_Shape>::Iterator aShapeIt;
234 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theEdges.begin();
235 for (; aFeatIt != theEdges.end(); aFeatIt++) {
236 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
237 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
238 if (anEdge.ShapeType() == TopAbs_EDGE)
239 aBB->AddArgument(anEdge);
242 if (aBB->HasErrors())
245 TopoDS_Compound aResult;
246 aBuilder.MakeCompound(aResult);
249 TopTools_ListOfShape anAreas = aBB->Modified(aPlnFace);
250 sortAreas(anAreas, theEdges); // sort faces by the edges in them
251 TopTools_ListIteratorOfListOfShape anIt(anAreas);
252 for (; anIt.More(); anIt.Next()) {
253 TopoDS_Face aFace = TopoDS::Face(anIt.Value());
254 // avoid infinite faces
255 BRepTopAdaptor_FClass2d aFClass(aFace, Precision::Confusion());
256 if (aFClass.PerformInfinitePoint() == TopAbs_IN)
260 TopoDS_Face aNewFace;
261 aBuilder.MakeFace(aNewFace, aPlane, Precision::Confusion());
263 // sort inner wires according to the original edges as well as faces
264 TopTools_ListOfShape aWires;
265 TopExp_Explorer aWireExp(aFace, TopAbs_WIRE);
266 for (; aWireExp.More(); aWireExp.Next())
267 aWires.Append(aWireExp.Current());
268 if (aWires.Size() > 2) {
269 TopoDS_Shape anOuterWire = aWires.First();
270 aWires.RemoveFirst();
271 sortAreas(aWires, theEdges);
272 aWires.Prepend(anOuterWire);
276 for (TopTools_ListIteratorOfListOfShape aWIt(aWires); aWIt.More(); aWIt.Next()) {
277 TopoDS_Wire aWire = TopoDS::Wire(aWIt.Value());
279 // to make faces equal on different platforms, we will find
280 // a vertex lying on an edge with the lowest index in the list of initial edges
281 TopoDS_Vertex aStartVertex = findStartVertex(aWire, aFace, theEdges);
283 TopoDS_Wire aNewWire;
284 aBuilder.MakeWire(aNewWire);
285 std::list<TopoDS_Edge> aSkippedEdges;
286 bool aStartFound = false;
288 // remove internal edges from faces and make wire start from found vertex
289 BRepTools_WireExplorer anExp(aWire, aFace);
290 for (; anExp.More(); anExp.Next()) {
291 if (anExp.Current().Orientation() == TopAbs_INTERNAL)
294 const TopoDS_Edge& anEdge = anExp.Current();
295 TopoDS_Vertex aV1, aV2;
296 TopExp::Vertices(anEdge, aV1, aV2, Standard_True);
297 if (aV1.IsSame(aStartVertex) == Standard_True)
300 aSkippedEdges.push_back(anEdge);
303 aBuilder.Add(aNewWire, anExp.Current());
305 // add skipped edges to the end of wire
306 std::list<TopoDS_Edge>::const_iterator aSkIt = aSkippedEdges.begin();
307 for (; aSkIt != aSkippedEdges.end(); ++aSkIt)
308 aBuilder.Add(aNewWire, *aSkIt);
310 // check the wire is empty
311 anExp.Init(aNewWire);
313 aBuilder.Add(aNewFace, aNewWire);
317 aBuilder.Add(aResult, aNewFace);
318 std::shared_ptr<GeomAPI_Shape> aResFace(new GeomAPI_Shape);
319 aResFace->setImpl(new TopoDS_Face(aNewFace));
320 myResultFaces.push_back(aResFace);
324 GeomShapePtr aResShape(new GeomAPI_Shape);
325 aResShape->setImpl(new TopoDS_Shape(aResult));
330 GeomAlgoAPI_SketchBuilder::GeomAlgoAPI_SketchBuilder(
331 const std::shared_ptr<GeomAPI_Pln>& thePlane,
332 const std::list<std::shared_ptr<GeomAPI_Shape> >& theEdges)
334 build(thePlane->location(), thePlane->xDirection(), thePlane->direction(), theEdges);
337 GeomAlgoAPI_SketchBuilder::GeomAlgoAPI_SketchBuilder(
338 const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
339 const std::shared_ptr<GeomAPI_Dir>& theDirX,
340 const std::shared_ptr<GeomAPI_Dir>& theNorm,
341 const std::shared_ptr<GeomAPI_Shape>& theWire)
343 std::shared_ptr<GeomAPI_PlanarEdges> aWire =
344 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theWire);
346 // Filter wires, return only faces.
347 build(theOrigin, theDirX, theNorm, aWire->getEdges());
348 } else { // it may be only one circle
349 std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(theWire);
351 std::list<std::shared_ptr<GeomAPI_Shape> > aList;
352 aList.push_back(anEdge);
353 build(theOrigin, theDirX, theNorm, aList);