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