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>
31 #define MY_SHAPE implPtr<TopoDS_Shape>()
33 GeomAPI_Shape::GeomAPI_Shape()
34 : GeomAPI_Interface(new TopoDS_Shape())
38 bool GeomAPI_Shape::isNull() const
40 return MY_SHAPE->IsNull() == Standard_True;
43 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
48 return theShape->isNull();
49 if (theShape->isNull())
52 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
55 bool GeomAPI_Shape::isVertex() const
57 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
58 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
61 bool GeomAPI_Shape::isEdge() const
63 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
64 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
67 bool GeomAPI_Shape::isFace() const
69 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
70 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
73 bool GeomAPI_Shape::isCompound() const
75 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
76 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
79 bool GeomAPI_Shape::isCompoundOfSolids() const
81 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
82 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
84 bool isAtLeastOne = false;
85 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
86 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
93 bool GeomAPI_Shape::isSolid() const
95 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
96 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
99 bool GeomAPI_Shape::isCompSolid() const
101 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
102 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
105 bool GeomAPI_Shape::isPlanar() const
107 TopoDS_Shape aShape = impl<TopoDS_Shape>();
109 if(aShape.IsNull()) {
113 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
114 if(aShapeType == TopAbs_COMPOUND) {
115 TopoDS_Iterator anIt(aShape);
117 for(; anIt.More(); anIt.Next()) {
121 anIt.Initialize(aShape);
122 aShape = anIt.Value();
126 aShapeType = aShape.ShapeType();
127 if(aShapeType == TopAbs_VERTEX) {
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();
133 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
134 Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
135 aType = aTrimSurface->BasisSurface()->DynamicType();
137 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
139 BRepBuilderAPI_FindPlane aFindPlane(aShape);
140 bool isFound = aFindPlane.Found() == Standard_True;
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();
147 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
148 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
149 aType = aTrimCurve->BasisCurve()->DynamicType();
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)) {
168 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
170 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
172 ShapeType aST = GeomAPI_Shape::SHAPE;
174 switch(aShape.ShapeType()) {
175 case TopAbs_COMPOUND:
176 aST = GeomAPI_Shape::COMPOUND;
178 case TopAbs_COMPSOLID:
179 aST = GeomAPI_Shape::COMPSOLID;
182 aST = GeomAPI_Shape::SOLID;
185 aST = GeomAPI_Shape::SHELL;
188 aST = GeomAPI_Shape::FACE;
191 aST = GeomAPI_Shape::WIRE;
194 aST = GeomAPI_Shape::EDGE;
197 aST = GeomAPI_Shape::VERTEX;
200 aST = GeomAPI_Shape::SHAPE;
207 std::string GeomAPI_Shape::shapeTypeStr() const
209 ShapeType aShapeType = shapeType();
210 std::string aShapeTypeStr;
214 aShapeTypeStr = "Compound";
218 aShapeTypeStr = "CompSolid";
222 aShapeTypeStr = "Solid";
226 aShapeTypeStr = "Shell";
230 aShapeTypeStr = "Face";
234 aShapeTypeStr = "Wire";
238 aShapeTypeStr = "Edge";
242 aShapeTypeStr = "Vertex";
246 aShapeTypeStr = "Shape";
251 return aShapeTypeStr;
254 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
256 if(!theShape.get()) {
260 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
261 if(aShapeToSearch.IsNull()) {
265 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
266 if(aShapeToSearch.IsEqual(anExp.Current())) {
274 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
275 double& theXmax, double& theYmax, double& theZmax) const
277 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
281 BRepBndLib::Add(aShape, aBndBox);
282 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
286 std::string GeomAPI_Shape::getShapeStream() const
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();