]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Shape.cpp
Salome HOME
Issue #1369: Added "Create Face" feature.
[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 <TopoDS.hxx>
25 #include <TopoDS_Iterator.hxx>
26 #include <TopoDS_Shape.hxx>
27
28 #include <sstream>
29
30 #define MY_SHAPE implPtr<TopoDS_Shape>()
31
32 GeomAPI_Shape::GeomAPI_Shape()
33     : GeomAPI_Interface(new TopoDS_Shape())
34 {
35 }
36
37 bool GeomAPI_Shape::isNull() const
38 {
39   return MY_SHAPE->IsNull() == Standard_True;
40 }
41
42 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
43 {
44   if (!theShape.get())
45     return false;
46   if (isNull())
47     return theShape->isNull();
48   if (theShape->isNull())
49     return false;
50
51   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
52 }
53
54 bool GeomAPI_Shape::isVertex() const
55 {
56   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
57   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
58 }
59
60 bool GeomAPI_Shape::isEdge() const
61 {
62   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
63   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
64 }
65
66 bool GeomAPI_Shape::isFace() const
67 {
68   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
69   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
70 }
71
72 bool GeomAPI_Shape::isCompound() const
73 {
74   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
75   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
76 }
77
78 bool GeomAPI_Shape::isCompoundOfSolids() const
79 {
80   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
81   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
82     return false;
83   bool isAtLeastOne = false;
84   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
85     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
86       return false;
87     isAtLeastOne = true;
88   }
89   return isAtLeastOne;
90 }
91
92 bool GeomAPI_Shape::isSolid() const
93 {
94   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
95   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
96 }
97
98 bool GeomAPI_Shape::isCompSolid() const
99 {
100   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
101   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
102 }
103
104 bool GeomAPI_Shape::isPlanar() const
105 {
106   TopoDS_Shape aShape = impl<TopoDS_Shape>();
107
108   if(aShape.IsNull()) {
109     return false;
110   }
111
112   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
113   if(aShapeType == TopAbs_COMPOUND) {
114     TopoDS_Iterator anIt(aShape);
115     int aShNum = 0;
116     for(; anIt.More(); anIt.Next()) {
117       ++aShNum;
118     }
119     if(aShNum == 1) {
120       anIt.Initialize(aShape);
121       aShape = anIt.Value();
122     }
123   }
124
125   aShapeType = aShape.ShapeType();
126   if(aShapeType == TopAbs_VERTEX) {
127     return true;
128   } else if(aShapeType == TopAbs_FACE) {
129     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
130     Handle(Standard_Type) aType = aSurface->DynamicType();
131
132     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
133       Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
134       aType = aTrimSurface->BasisSurface()->DynamicType();
135     }
136     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
137   } else {
138     BRepBuilderAPI_FindPlane aFindPlane(aShape);
139     bool isFound = aFindPlane.Found() == Standard_True;
140
141     if(!isFound && aShapeType == TopAbs_EDGE) {
142       Standard_Real aFirst, aLast;
143       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
144       Handle(Standard_Type) aType = aCurve->DynamicType();
145
146       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
147         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
148         aType = aTrimCurve->BasisCurve()->DynamicType();
149       }
150
151       if(aType == STANDARD_TYPE(Geom_Line)
152           || aType == STANDARD_TYPE(Geom_Conic)
153           || aType == STANDARD_TYPE(Geom_Circle)
154           || aType == STANDARD_TYPE(Geom_Ellipse)
155           || aType == STANDARD_TYPE(Geom_Hyperbola)
156           || aType == STANDARD_TYPE(Geom_Parabola)) {
157         isFound = true;
158       }
159     }
160
161     return isFound;
162   }
163
164   return false;
165 }
166
167 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
168 {
169   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
170
171   ShapeType aST = GeomAPI_Shape::SHAPE;
172
173   switch(aShape.ShapeType()) {
174   case TopAbs_COMPOUND:
175     aST = GeomAPI_Shape::COMPOUND;
176     break;
177   case TopAbs_COMPSOLID:
178     aST = GeomAPI_Shape::COMPSOLID;
179     break;
180   case TopAbs_SOLID:
181     aST = GeomAPI_Shape::SOLID;
182     break;
183   case TopAbs_SHELL:
184     aST = GeomAPI_Shape::SHELL;
185     break;
186   case TopAbs_FACE:
187     aST = GeomAPI_Shape::FACE;
188     break;
189   case TopAbs_WIRE:
190     aST = GeomAPI_Shape::WIRE;
191     break;
192   case TopAbs_EDGE:
193     aST = GeomAPI_Shape::EDGE;
194     break;
195   case TopAbs_VERTEX:
196     aST = GeomAPI_Shape::VERTEX;
197     break;
198   case TopAbs_SHAPE:
199     aST = GeomAPI_Shape::SHAPE;
200     break;
201   }
202
203   return aST;
204 }
205
206 std::string GeomAPI_Shape::shapeTypeStr() const
207 {
208   ShapeType aShapeType = shapeType();
209   std::string aShapeTypeStr;
210
211   switch(aShapeType) {
212     case COMPOUND: {
213       aShapeTypeStr = "Compound";
214       break;
215     }
216     case COMPSOLID: {
217       aShapeTypeStr = "CompSolid";
218       break;
219     }
220     case SOLID: {
221       aShapeTypeStr = "Solid";
222       break;
223     }
224     case SHELL: {
225       aShapeTypeStr = "Shell";
226       break;
227     }
228     case FACE: {
229       aShapeTypeStr = "Face";
230       break;
231     }
232     case WIRE: {
233       aShapeTypeStr = "Wire";
234       break;
235     }
236     case EDGE: {
237       aShapeTypeStr = "Edge";
238       break;
239     }
240     case VERTEX: {
241       aShapeTypeStr = "Vertex";
242       break;
243     }
244     case SHAPE: {
245       aShapeTypeStr = "Shape";
246       break;
247     }
248   }
249
250   return aShapeTypeStr;
251 }
252
253 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
254                                 double& theXmax, double& theYmax, double& theZmax) const
255 {
256   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
257   if (aShape.IsNull())
258     return false;
259   Bnd_Box aBndBox;
260   BRepBndLib::Add(aShape, aBndBox);
261   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
262   return true;
263 }
264
265 std::string GeomAPI_Shape::getShapeStream() const
266 {
267   std::ostringstream aStream;
268   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
269   BRepTools::Write(aShape, aStream);
270   return aStream.str();
271 }