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 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
41 GeomShapePtr aShape(new GeomAPI_Shape());
42 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
46 bool GeomAPI_Shape::isNull() const
48 return MY_SHAPE->IsNull() == Standard_True;
51 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
56 return theShape->isNull();
57 if (theShape->isNull())
60 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
63 bool GeomAPI_Shape::isVertex() const
65 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
66 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
69 bool GeomAPI_Shape::isEdge() const
71 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
72 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
75 bool GeomAPI_Shape::isFace() const
77 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
78 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
81 bool GeomAPI_Shape::isCompound() const
83 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
84 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
87 bool GeomAPI_Shape::isCompoundOfSolids() const
89 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
90 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
92 bool isAtLeastOne = false;
93 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
94 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
101 // adds the nopt-compound elements recursively to the list
102 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
104 if (!theShape.IsNull()) {
105 if (theShape.ShapeType() == TopAbs_COMPOUND) {
106 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
107 addSimpleToList(aSubs.Value(), theList);
110 theList.Append(theShape);
115 bool GeomAPI_Shape::isConnectedTopology() const
117 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
118 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
120 NCollection_List<TopoDS_Shape> aNotConnected; // list of simple elements that are not detected in connection to others
121 addSimpleToList(aShape, aNotConnected);
122 if (aNotConnected.IsEmpty()) // an empty compound
125 // collect here the group of connected subs, starting with one first element
126 NCollection_List<TopoDS_Shape> aNewConnected;
127 aNewConnected.Append(aNotConnected.First());
128 aNotConnected.RemoveFirst();
129 // iterate until some new element become connected
130 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
131 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
132 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
133 while(aNotIter.More()) {
134 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
135 NCollection_List<TopoDS_Shape> aNotVertices;
136 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
137 aNotVertices.Append(anExp1.Current());
140 bool aConnected = false;
141 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
142 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
143 // checking topological connecion of aNotIter and aNewIter (if shapes are connected, vertices are connected for sure)
144 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
145 for(; !aConnected && anExp2.More(); anExp2.Next()) {
146 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
147 for(; aNotIter.More(); aNotIter.Next()) {
148 if (aNotIter.Value().IsSame(anExp2.Current())) {
156 aNew.Append(aNotIter.Value());
157 aNotConnected.Remove(aNotIter);
162 // remove all new connected and put to this list very new connected
163 aNewConnected.Clear();
164 aNewConnected.Append(aNew);
166 return aNotConnected.IsEmpty() == Standard_True;
169 bool GeomAPI_Shape::isSolid() const
171 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
172 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
175 bool GeomAPI_Shape::isCompSolid() const
177 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
178 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
181 bool GeomAPI_Shape::isPlanar() const
183 TopoDS_Shape aShape = impl<TopoDS_Shape>();
185 if(aShape.IsNull()) {
189 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
190 if(aShapeType == TopAbs_COMPOUND) {
191 TopoDS_Iterator anIt(aShape);
193 for(; anIt.More(); anIt.Next()) {
197 anIt.Initialize(aShape);
198 aShape = anIt.Value();
202 aShapeType = aShape.ShapeType();
203 if(aShapeType == TopAbs_VERTEX) {
205 } else if(aShapeType == TopAbs_FACE) {
206 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
207 Handle(Standard_Type) aType = aSurface->DynamicType();
209 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
210 Handle(Geom_RectangularTrimmedSurface) aTrimSurface = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
211 aType = aTrimSurface->BasisSurface()->DynamicType();
213 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
215 BRepBuilderAPI_FindPlane aFindPlane(aShape);
216 bool isFound = aFindPlane.Found() == Standard_True;
218 if(!isFound && aShapeType == TopAbs_EDGE) {
219 Standard_Real aFirst, aLast;
220 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
221 Handle(Standard_Type) aType = aCurve->DynamicType();
223 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
224 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
225 aType = aTrimCurve->BasisCurve()->DynamicType();
228 if(aType == STANDARD_TYPE(Geom_Line)
229 || aType == STANDARD_TYPE(Geom_Conic)
230 || aType == STANDARD_TYPE(Geom_Circle)
231 || aType == STANDARD_TYPE(Geom_Ellipse)
232 || aType == STANDARD_TYPE(Geom_Hyperbola)
233 || aType == STANDARD_TYPE(Geom_Parabola)) {
244 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
246 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
248 ShapeType aST = GeomAPI_Shape::SHAPE;
250 switch(aShape.ShapeType()) {
251 case TopAbs_COMPOUND:
252 aST = GeomAPI_Shape::COMPOUND;
254 case TopAbs_COMPSOLID:
255 aST = GeomAPI_Shape::COMPSOLID;
258 aST = GeomAPI_Shape::SOLID;
261 aST = GeomAPI_Shape::SHELL;
264 aST = GeomAPI_Shape::FACE;
267 aST = GeomAPI_Shape::WIRE;
270 aST = GeomAPI_Shape::EDGE;
273 aST = GeomAPI_Shape::VERTEX;
276 aST = GeomAPI_Shape::SHAPE;
283 std::string GeomAPI_Shape::shapeTypeStr() const
285 ShapeType aShapeType = shapeType();
286 std::string aShapeTypeStr;
290 aShapeTypeStr = "COMPOUND";
294 aShapeTypeStr = "COMPSOLID";
298 aShapeTypeStr = "SOLID";
302 aShapeTypeStr = "SHELL";
306 aShapeTypeStr = "FACE";
310 aShapeTypeStr = "WIRE";
314 aShapeTypeStr = "EDGE";
318 aShapeTypeStr = "VERTEX";
322 aShapeTypeStr = "SHAPE";
327 return aShapeTypeStr;
330 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
332 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
334 switch(anOrientation) {
335 case TopAbs_FORWARD: return FORWARD;
336 case TopAbs_REVERSED: return REVERSED;
337 case TopAbs_INTERNAL: return INTERNAL;
338 case TopAbs_EXTERNAL: return EXTERNAL;
339 default: return FORWARD;
343 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
345 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
347 switch(theOrientation) {
348 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
349 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
350 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
351 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
355 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
357 if(!theShape.get()) {
361 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
362 if(aShapeToSearch.IsNull()) {
366 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
367 if(aShapeToSearch.IsEqual(anExp.Current())) {
375 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
376 double& theXmax, double& theYmax, double& theZmax) const
378 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
382 BRepBndLib::Add(aShape, aBndBox);
383 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
387 std::string GeomAPI_Shape::getShapeStream() const
389 std::ostringstream aStream;
390 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
391 BRepTools::Write(aShape, aStream);
392 return aStream.str();