]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Shape.cpp
Salome HOME
Issue #2024: Redesign of circle and arc of circle
[modules/shaper.git] / src / GeomAPI / GeomAPI_Shape.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Shape.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomAPI_Shape.h"
8
9 #include <BRep_Tool.hxx>
10 #include <BRepAlgoAPI_Section.hxx>
11 #include <BRepBndLib.hxx>
12 #include <BRepBuilderAPI_FindPlane.hxx>
13 #include <BRepTools.hxx>
14 #include <Bnd_Box.hxx>
15 #include <Geom_Circle.hxx>
16 #include <Geom_Conic.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom_Ellipse.hxx>
19 #include <Geom_Hyperbola.hxx>
20 #include <Geom_Line.hxx>
21 #include <Geom_Parabola.hxx>
22 #include <Geom_Plane.hxx>
23 #include <Geom_RectangularTrimmedSurface.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS.hxx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <NCollection_List.hxx>
30
31 #include <sstream>
32 #include <algorithm> // for std::transform
33
34 #define MY_SHAPE implPtr<TopoDS_Shape>()
35
36 GeomAPI_Shape::GeomAPI_Shape()
37     : GeomAPI_Interface(new TopoDS_Shape())
38 {
39 }
40
41 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
42 {
43   GeomShapePtr aShape(new GeomAPI_Shape());
44   aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
45   return aShape;
46 }
47
48 bool GeomAPI_Shape::isNull() const
49 {
50   return MY_SHAPE->IsNull() == Standard_True;
51 }
52
53 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
54 {
55   if (!theShape.get())
56     return false;
57   if (isNull())
58     return theShape->isNull();
59   if (theShape->isNull())
60     return false;
61
62   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
63 }
64
65 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
66 {
67   if (!theShape.get())
68     return false;
69   if (isNull())
70     return theShape->isNull();
71   if (theShape->isNull())
72     return false;
73
74   return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
75 }
76
77 bool GeomAPI_Shape::isVertex() const
78 {
79   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
80   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
81 }
82
83 bool GeomAPI_Shape::isEdge() const
84 {
85   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
86   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
87 }
88
89 bool GeomAPI_Shape::isFace() const
90 {
91   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
92   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
93 }
94
95 bool GeomAPI_Shape::isCompound() const
96 {
97   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
98   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
99 }
100
101 bool GeomAPI_Shape::isCompoundOfSolids() const
102 {
103   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
104   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
105     return false;
106   bool isAtLeastOne = false;
107   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
108     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
109       return false;
110     isAtLeastOne = true;
111   }
112   return isAtLeastOne;
113 }
114
115 // adds the nopt-compound elements recursively to the list
116 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
117 {
118   if (!theShape.IsNull()) {
119     if (theShape.ShapeType() == TopAbs_COMPOUND) {
120       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
121         addSimpleToList(aSubs.Value(), theList);
122       }
123     } else {
124       theList.Append(theShape);
125     }
126   }
127 }
128
129 bool GeomAPI_Shape::isConnectedTopology() const
130 {
131   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
132   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
133     return false;
134   // list of simple elements that are not detected in connection to others
135   NCollection_List<TopoDS_Shape> aNotConnected;
136   addSimpleToList(aShape, aNotConnected);
137   if (aNotConnected.IsEmpty()) // an empty compound
138     return false;
139
140   // collect here the group of connected subs, starting with one first element
141   NCollection_List<TopoDS_Shape> aNewConnected;
142   aNewConnected.Append(aNotConnected.First());
143   aNotConnected.RemoveFirst();
144   // iterate until some new element become connected
145   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
146     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
147     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
148     while(aNotIter.More()) {
149       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
150       NCollection_List<TopoDS_Shape> aNotVertices;
151       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
152         aNotVertices.Append(anExp1.Current());
153       }
154
155       bool aConnected =  false;
156       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
157       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
158         // checking topological connecion of aNotIter and aNewIter
159         // (if shapes are connected, vertices are connected for sure)
160         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
161         for(; !aConnected && anExp2.More(); anExp2.Next()) {
162           NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
163           for(; aNotIter.More(); aNotIter.Next()) {
164             if (aNotIter.Value().IsSame(anExp2.Current())) {
165               aConnected = true;
166               break;
167             }
168           }
169         }
170       }
171       if (aConnected) {
172         aNew.Append(aNotIter.Value());
173         aNotConnected.Remove(aNotIter);
174       } else {
175         aNotIter.Next();
176       }
177     }
178     // remove all new connected and put to this list very new connected
179     aNewConnected.Clear();
180     aNewConnected.Append(aNew);
181   }
182   return aNotConnected.IsEmpty() == Standard_True;
183 }
184
185 bool GeomAPI_Shape::isSolid() const
186 {
187   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
188   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
189 }
190
191 bool GeomAPI_Shape::isCompSolid() const
192 {
193   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
194   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
195 }
196
197 bool GeomAPI_Shape::isPlanar() const
198 {
199   TopoDS_Shape aShape = impl<TopoDS_Shape>();
200
201   if(aShape.IsNull()) {
202     return false;
203   }
204
205   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
206   if(aShapeType == TopAbs_COMPOUND) {
207     TopoDS_Iterator anIt(aShape);
208     int aShNum = 0;
209     for(; anIt.More(); anIt.Next()) {
210       ++aShNum;
211     }
212     if(aShNum == 1) {
213       anIt.Initialize(aShape);
214       aShape = anIt.Value();
215     }
216   }
217
218   aShapeType = aShape.ShapeType();
219   if(aShapeType == TopAbs_VERTEX) {
220     return true;
221   } else if(aShapeType == TopAbs_FACE) {
222     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
223     Handle(Standard_Type) aType = aSurface->DynamicType();
224
225     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
226       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
227         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
228       aType = aTrimSurface->BasisSurface()->DynamicType();
229     }
230     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
231   } else {
232     BRepBuilderAPI_FindPlane aFindPlane(aShape);
233     bool isFound = aFindPlane.Found() == Standard_True;
234
235     if(!isFound && aShapeType == TopAbs_EDGE) {
236       Standard_Real aFirst, aLast;
237       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
238       Handle(Standard_Type) aType = aCurve->DynamicType();
239
240       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
241         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
242         aType = aTrimCurve->BasisCurve()->DynamicType();
243       }
244
245       if(aType == STANDARD_TYPE(Geom_Line)
246           || aType == STANDARD_TYPE(Geom_Conic)
247           || aType == STANDARD_TYPE(Geom_Circle)
248           || aType == STANDARD_TYPE(Geom_Ellipse)
249           || aType == STANDARD_TYPE(Geom_Hyperbola)
250           || aType == STANDARD_TYPE(Geom_Parabola)) {
251         isFound = true;
252       }
253     }
254
255     return isFound;
256   }
257
258   return false;
259 }
260
261 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
262 {
263   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
264
265   ShapeType aST = GeomAPI_Shape::SHAPE;
266
267   switch(aShape.ShapeType()) {
268   case TopAbs_COMPOUND:
269     aST = GeomAPI_Shape::COMPOUND;
270     break;
271   case TopAbs_COMPSOLID:
272     aST = GeomAPI_Shape::COMPSOLID;
273     break;
274   case TopAbs_SOLID:
275     aST = GeomAPI_Shape::SOLID;
276     break;
277   case TopAbs_SHELL:
278     aST = GeomAPI_Shape::SHELL;
279     break;
280   case TopAbs_FACE:
281     aST = GeomAPI_Shape::FACE;
282     break;
283   case TopAbs_WIRE:
284     aST = GeomAPI_Shape::WIRE;
285     break;
286   case TopAbs_EDGE:
287     aST = GeomAPI_Shape::EDGE;
288     break;
289   case TopAbs_VERTEX:
290     aST = GeomAPI_Shape::VERTEX;
291     break;
292   case TopAbs_SHAPE:
293     aST = GeomAPI_Shape::SHAPE;
294     break;
295   }
296
297   return aST;
298 }
299
300 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
301 {
302   std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
303   if (theType == "COMPOUND")
304     return COMPOUND;
305   if (theType == "COMPSOLID")
306     return COMPSOLID;
307   if (theType == "SOLID")
308     return SOLID;
309   if (theType == "SHELL")
310     return SHELL;
311   if (theType == "FACE")
312     return FACE;
313   if (theType == "WIRE")
314     return WIRE;
315   if (theType == "EDGE")
316     return EDGE;
317   if (theType == "VERTEX")
318     return VERTEX;
319   return SHAPE; // default
320 }
321
322 std::string GeomAPI_Shape::shapeTypeStr() const
323 {
324   ShapeType aShapeType = shapeType();
325   std::string aShapeTypeStr;
326
327   switch(aShapeType) {
328     case COMPOUND: {
329       aShapeTypeStr = "COMPOUND";
330       break;
331     }
332     case COMPSOLID: {
333       aShapeTypeStr = "COMPSOLID";
334       break;
335     }
336     case SOLID: {
337       aShapeTypeStr = "SOLID";
338       break;
339     }
340     case SHELL: {
341       aShapeTypeStr = "SHELL";
342       break;
343     }
344     case FACE: {
345       aShapeTypeStr = "FACE";
346       break;
347     }
348     case WIRE: {
349       aShapeTypeStr = "WIRE";
350       break;
351     }
352     case EDGE: {
353       aShapeTypeStr = "EDGE";
354       break;
355     }
356     case VERTEX: {
357       aShapeTypeStr = "VERTEX";
358       break;
359     }
360     case SHAPE: {
361       aShapeTypeStr = "SHAPE";
362       break;
363     }
364   }
365
366   return aShapeTypeStr;
367 }
368
369 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
370 {
371   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
372
373   switch(anOrientation) {
374     case TopAbs_FORWARD:  return FORWARD;
375     case TopAbs_REVERSED: return REVERSED;
376     case TopAbs_INTERNAL: return INTERNAL;
377     case TopAbs_EXTERNAL: return EXTERNAL;
378     default:              return FORWARD;
379   }
380 }
381
382 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
383 {
384   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
385
386   switch(theOrientation) {
387     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
388     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
389     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
390     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
391   }
392 }
393
394 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
395 {
396   if(!theShape.get()) {
397     return false;
398   }
399
400   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
401   if(aShapeToSearch.IsNull()) {
402     return false;
403   }
404
405   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
406     if(aShapeToSearch.IsEqual(anExp.Current())) {
407       return true;
408     }
409   }
410
411   return false;
412 }
413
414 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
415                                 double& theXmax, double& theYmax, double& theZmax) const
416 {
417   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
418   if (aShape.IsNull())
419     return false;
420   Bnd_Box aBndBox;
421   BRepBndLib::Add(aShape, aBndBox);
422   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
423   return true;
424 }
425
426 std::string GeomAPI_Shape::getShapeStream() const
427 {
428   std::ostringstream aStream;
429   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
430   BRepTools::Write(aShape, aStream);
431   return aStream.str();
432 }
433
434 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
435 {
436   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
437   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
438
439   BRepAlgoAPI_Section aCommon(aShape1, aShape2);
440   if (!aCommon.IsDone())
441     return GeomShapePtr();
442
443   TopoDS_Shape aResult = aCommon.Shape();
444   if (aResult.ShapeType() == TopAbs_COMPOUND) {
445     NCollection_List<TopoDS_Shape> aSubs;
446     addSimpleToList(aResult, aSubs);
447     if(aSubs.Size() == 1) {
448       aResult = aSubs.First();
449     } else if(aSubs.Size() == 0) {
450       return GeomShapePtr();
451     }
452   }
453
454   GeomShapePtr aResShape(new GeomAPI_Shape);
455   aResShape->setImpl(new TopoDS_Shape(aResult));
456   return aResShape;
457 }