1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Shape.cpp
4 // Created: 23 Apr 2014
5 // Author: Mikhail PONIKAROV
7 #include "GeomAPI_Shape.h"
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>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <NCollection_List.hxx>
32 #define MY_SHAPE implPtr<TopoDS_Shape>()
34 GeomAPI_Shape::GeomAPI_Shape()
35 : GeomAPI_Interface(new TopoDS_Shape())
39 bool GeomAPI_Shape::isNull() const
41 return MY_SHAPE->IsNull() == Standard_True;
44 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
49 return theShape->isNull();
50 if (theShape->isNull())
53 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
56 bool GeomAPI_Shape::isVertex() const
58 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
59 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
62 bool GeomAPI_Shape::isEdge() const
64 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
65 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
68 bool GeomAPI_Shape::isFace() const
70 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
71 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
74 bool GeomAPI_Shape::isCompound() const
76 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
77 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
80 bool GeomAPI_Shape::isCompoundOfSolids() const
82 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
83 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
85 bool isAtLeastOne = false;
86 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
87 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
94 // adds the nopt-compound elements recursively to the list
95 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
97 if (!theShape.IsNull()) {
98 if (theShape.ShapeType() == TopAbs_COMPOUND) {
99 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
100 addSimpleToList(aSubs.Value(), theList);
103 theList.Append(theShape);
108 bool GeomAPI_Shape::isConnectedTopology() const
110 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
111 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
113 NCollection_List<TopoDS_Shape> aNotConnected; // list of simple elements that are not detected in connection to others
114 addSimpleToList(aShape, aNotConnected);
115 if (aNotConnected.IsEmpty()) // an empty compound
118 // collect here the group of connected subs, starting with one first element
119 NCollection_List<TopoDS_Shape> aNewConnected;
120 aNewConnected.Append(aNotConnected.First());
121 aNotConnected.RemoveFirst();
122 // iterate until some new element become connected
123 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
124 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
125 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
126 while(aNotIter.More()) {
127 bool aConnected = false;
128 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
129 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
130 // checking topological connecion of aNotIter and aNewIter (if shapes are connected, vertices are connected for sure)
131 TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX);
132 for(; !aConnected && anExp1.More(); anExp1.Next()) {
133 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
134 for(; anExp2.More(); anExp2.Next()) {
135 if (anExp1.Current().IsSame(anExp2.Current())) {
143 aNew.Append(aNotIter.Value());
144 aNotConnected.Remove(aNotIter);
149 // remove all new connected and put to this list very new connected
150 aNewConnected.Clear();
151 aNewConnected.Append(aNew);
153 return aNotConnected.IsEmpty() == Standard_True;
156 bool GeomAPI_Shape::isSolid() const
158 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
159 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
162 bool GeomAPI_Shape::isCompSolid() const
164 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
165 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
168 bool GeomAPI_Shape::isPlanar() const
170 TopoDS_Shape aShape = impl<TopoDS_Shape>();
172 if(aShape.IsNull()) {
176 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
177 if(aShapeType == TopAbs_COMPOUND) {
178 TopoDS_Iterator anIt(aShape);
180 for(; anIt.More(); anIt.Next()) {
184 anIt.Initialize(aShape);
185 aShape = anIt.Value();
189 aShapeType = aShape.ShapeType();
190 if(aShapeType == TopAbs_VERTEX) {
192 } else if(aShapeType == TopAbs_FACE) {
193 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
194 Handle(Standard_Type) aType = aSurface->DynamicType();
196 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
197 Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
198 aType = aTrimSurface->BasisSurface()->DynamicType();
200 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
202 BRepBuilderAPI_FindPlane aFindPlane(aShape);
203 bool isFound = aFindPlane.Found() == Standard_True;
205 if(!isFound && aShapeType == TopAbs_EDGE) {
206 Standard_Real aFirst, aLast;
207 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
208 Handle(Standard_Type) aType = aCurve->DynamicType();
210 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
211 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
212 aType = aTrimCurve->BasisCurve()->DynamicType();
215 if(aType == STANDARD_TYPE(Geom_Line)
216 || aType == STANDARD_TYPE(Geom_Conic)
217 || aType == STANDARD_TYPE(Geom_Circle)
218 || aType == STANDARD_TYPE(Geom_Ellipse)
219 || aType == STANDARD_TYPE(Geom_Hyperbola)
220 || aType == STANDARD_TYPE(Geom_Parabola)) {
231 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
233 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
235 ShapeType aST = GeomAPI_Shape::SHAPE;
237 switch(aShape.ShapeType()) {
238 case TopAbs_COMPOUND:
239 aST = GeomAPI_Shape::COMPOUND;
241 case TopAbs_COMPSOLID:
242 aST = GeomAPI_Shape::COMPSOLID;
245 aST = GeomAPI_Shape::SOLID;
248 aST = GeomAPI_Shape::SHELL;
251 aST = GeomAPI_Shape::FACE;
254 aST = GeomAPI_Shape::WIRE;
257 aST = GeomAPI_Shape::EDGE;
260 aST = GeomAPI_Shape::VERTEX;
263 aST = GeomAPI_Shape::SHAPE;
270 std::string GeomAPI_Shape::shapeTypeStr() const
272 ShapeType aShapeType = shapeType();
273 std::string aShapeTypeStr;
277 aShapeTypeStr = "Compound";
281 aShapeTypeStr = "CompSolid";
285 aShapeTypeStr = "Solid";
289 aShapeTypeStr = "Shell";
293 aShapeTypeStr = "Face";
297 aShapeTypeStr = "Wire";
301 aShapeTypeStr = "Edge";
305 aShapeTypeStr = "Vertex";
309 aShapeTypeStr = "Shape";
314 return aShapeTypeStr;
317 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
319 if(!theShape.get()) {
323 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
324 if(aShapeToSearch.IsNull()) {
328 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
329 if(aShapeToSearch.IsEqual(anExp.Current())) {
337 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
338 double& theXmax, double& theYmax, double& theZmax) const
340 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
344 BRepBndLib::Add(aShape, aBndBox);
345 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
349 std::string GeomAPI_Shape::getShapeStream() const
351 std::ostringstream aStream;
352 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
353 BRepTools::Write(aShape, aStream);
354 return aStream.str();