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