Salome HOME
3ae40cab1238b28e440b0050f1e29bd7645e98d5
[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_Plane.hxx>
15 #include <Geom_RectangularTrimmedSurface.hxx>
16 #include <TopoDS.hxx>
17 #include <TopoDS_Iterator.hxx>
18 #include <TopoDS_Shape.hxx>
19
20 #include <sstream>
21
22 #define MY_SHAPE implPtr<TopoDS_Shape>()
23
24 GeomAPI_Shape::GeomAPI_Shape()
25     : GeomAPI_Interface(new TopoDS_Shape())
26 {
27 }
28
29 bool GeomAPI_Shape::isNull() const
30 {
31   return MY_SHAPE->IsNull() == Standard_True;
32 }
33
34 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
35 {
36   if (!theShape.get())
37     return false;
38   if (isNull())
39     return theShape->isNull();
40   if (theShape->isNull())
41     return false;
42
43   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
44 }
45
46 bool GeomAPI_Shape::isVertex() const
47 {
48   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
49   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
50 }
51
52 bool GeomAPI_Shape::isEdge() const
53 {
54   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
55   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
56 }
57
58 bool GeomAPI_Shape::isFace() const
59 {
60   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
61   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
62 }
63
64 bool GeomAPI_Shape::isCompound() const
65 {
66   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
67   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
68 }
69
70 bool GeomAPI_Shape::isCompoundOfSolids() const
71 {
72   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
73   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
74     return false;
75   bool isAtLeastOne = false;
76   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
77     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
78       return false;
79     isAtLeastOne = true;
80   }
81   return isAtLeastOne;
82 }
83
84 bool GeomAPI_Shape::isSolid() const
85 {
86   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
87   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
88 }
89
90 bool GeomAPI_Shape::isCompSolid() const
91 {
92   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
93   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
94 }
95
96 bool GeomAPI_Shape::isPlanar() const
97 {
98   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
99
100   if(aShape.IsNull()) {
101     return false;
102   }
103
104   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
105
106   if(aShapeType == TopAbs_VERTEX) {
107     return true;
108   } else if(aShapeType == TopAbs_EDGE || aShapeType ==  TopAbs_WIRE || aShapeType == TopAbs_SHELL) {
109     BRepBuilderAPI_FindPlane aFindPlane(aShape);
110     return aFindPlane.Found() == Standard_True;
111   } else if(aShapeType == TopAbs_FACE) {
112     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
113     Handle(Standard_Type) aType = aSurface->DynamicType();
114
115     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
116       Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
117       aType = aTrimSurface->BasisSurface()->DynamicType();
118     }
119     return (aType == STANDARD_TYPE(Geom_Plane));
120   } else {
121     return false;
122   }
123 }
124
125 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
126 {
127   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
128
129   ShapeType aST = GeomAPI_Shape::SHAPE;
130
131   switch(aShape.ShapeType()) {
132   case TopAbs_COMPOUND:
133     aST = GeomAPI_Shape::COMPOUND;
134     break;
135   case TopAbs_COMPSOLID:
136     aST = GeomAPI_Shape::COMPSOLID;
137     break;
138   case TopAbs_SOLID:
139     aST = GeomAPI_Shape::SOLID;
140     break;
141   case TopAbs_SHELL:
142     aST = GeomAPI_Shape::SHELL;
143     break;
144   case TopAbs_FACE:
145     aST = GeomAPI_Shape::FACE;
146     break;
147   case TopAbs_WIRE:
148     aST = GeomAPI_Shape::WIRE;
149     break;
150   case TopAbs_EDGE:
151     aST = GeomAPI_Shape::EDGE;
152     break;
153   case TopAbs_VERTEX:
154     aST = GeomAPI_Shape::VERTEX;
155     break;
156   case TopAbs_SHAPE:
157     aST = GeomAPI_Shape::SHAPE;
158     break;
159   }
160
161   return aST;
162 }
163
164 std::string GeomAPI_Shape::shapeTypeStr() const
165 {
166   ShapeType aShapeType = shapeType();
167   std::string aShapeTypeStr;
168
169   switch(aShapeType) {
170     case COMPOUND: {
171       aShapeTypeStr = "Compound";
172       break;
173     }
174     case COMPSOLID: {
175       aShapeTypeStr = "CompSolid";
176       break;
177     }
178     case SOLID: {
179       aShapeTypeStr = "Solid";
180       break;
181     }
182     case SHELL: {
183       aShapeTypeStr = "Shell";
184       break;
185     }
186     case FACE: {
187       aShapeTypeStr = "Face";
188       break;
189     }
190     case WIRE: {
191       aShapeTypeStr = "Wire";
192       break;
193     }
194     case EDGE: {
195       aShapeTypeStr = "Edge";
196       break;
197     }
198     case VERTEX: {
199       aShapeTypeStr = "Vertex";
200       break;
201     }
202     case SHAPE: {
203       aShapeTypeStr = "Shape";
204       break;
205     }
206   }
207
208   return aShapeTypeStr;
209 }
210
211 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
212                                 double& theXmax, double& theYmax, double& theZmax) const
213 {
214   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
215   if (aShape.IsNull())
216     return false;
217   Bnd_Box aBndBox;
218   BRepBndLib::Add(aShape, aBndBox);
219   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
220   return true;
221 }
222
223 std::string GeomAPI_Shape::getShapeStream() const
224 {
225   std::ostringstream aStream;
226   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
227   BRepTools::Write(aShape, aStream);
228   return aStream.str();
229 }