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>
31 #include <algorithm> // for std::transform
33 #define MY_SHAPE implPtr<TopoDS_Shape>()
35 GeomAPI_Shape::GeomAPI_Shape()
36 : GeomAPI_Interface(new TopoDS_Shape())
40 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
42 GeomShapePtr aShape(new GeomAPI_Shape());
43 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
47 bool GeomAPI_Shape::isNull() const
49 return MY_SHAPE->IsNull() == Standard_True;
52 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
57 return theShape->isNull();
58 if (theShape->isNull())
61 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
64 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
69 return theShape->isNull();
70 if (theShape->isNull())
73 return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
76 bool GeomAPI_Shape::isVertex() const
78 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
79 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
82 bool GeomAPI_Shape::isEdge() const
84 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
85 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
88 bool GeomAPI_Shape::isFace() const
90 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
91 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
94 bool GeomAPI_Shape::isCompound() const
96 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
97 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
100 bool GeomAPI_Shape::isCompoundOfSolids() const
102 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
103 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
105 bool isAtLeastOne = false;
106 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
107 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
114 // adds the nopt-compound elements recursively to the list
115 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
117 if (!theShape.IsNull()) {
118 if (theShape.ShapeType() == TopAbs_COMPOUND) {
119 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
120 addSimpleToList(aSubs.Value(), theList);
123 theList.Append(theShape);
128 bool GeomAPI_Shape::isConnectedTopology() const
130 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
131 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
133 // list of simple elements that are not detected in connection to others
134 NCollection_List<TopoDS_Shape> aNotConnected;
135 addSimpleToList(aShape, aNotConnected);
136 if (aNotConnected.IsEmpty()) // an empty compound
139 // collect here the group of connected subs, starting with one first element
140 NCollection_List<TopoDS_Shape> aNewConnected;
141 aNewConnected.Append(aNotConnected.First());
142 aNotConnected.RemoveFirst();
143 // iterate until some new element become connected
144 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
145 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
146 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
147 while(aNotIter.More()) {
148 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
149 NCollection_List<TopoDS_Shape> aNotVertices;
150 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
151 aNotVertices.Append(anExp1.Current());
154 bool aConnected = false;
155 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
156 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
157 // checking topological connecion of aNotIter and aNewIter
158 // (if shapes are connected, vertices are connected for sure)
159 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
160 for(; !aConnected && anExp2.More(); anExp2.Next()) {
161 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
162 for(; aNotIter.More(); aNotIter.Next()) {
163 if (aNotIter.Value().IsSame(anExp2.Current())) {
171 aNew.Append(aNotIter.Value());
172 aNotConnected.Remove(aNotIter);
177 // remove all new connected and put to this list very new connected
178 aNewConnected.Clear();
179 aNewConnected.Append(aNew);
181 return aNotConnected.IsEmpty() == Standard_True;
184 bool GeomAPI_Shape::isSolid() const
186 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
187 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
190 bool GeomAPI_Shape::isCompSolid() const
192 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
193 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
196 bool GeomAPI_Shape::isPlanar() const
198 TopoDS_Shape aShape = impl<TopoDS_Shape>();
200 if(aShape.IsNull()) {
204 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
205 if(aShapeType == TopAbs_COMPOUND) {
206 TopoDS_Iterator anIt(aShape);
208 for(; anIt.More(); anIt.Next()) {
212 anIt.Initialize(aShape);
213 aShape = anIt.Value();
217 aShapeType = aShape.ShapeType();
218 if(aShapeType == TopAbs_VERTEX) {
220 } else if(aShapeType == TopAbs_FACE) {
221 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
222 Handle(Standard_Type) aType = aSurface->DynamicType();
224 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
225 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
226 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
227 aType = aTrimSurface->BasisSurface()->DynamicType();
229 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
231 BRepBuilderAPI_FindPlane aFindPlane(aShape);
232 bool isFound = aFindPlane.Found() == Standard_True;
234 if(!isFound && aShapeType == TopAbs_EDGE) {
235 Standard_Real aFirst, aLast;
236 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
237 Handle(Standard_Type) aType = aCurve->DynamicType();
239 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
240 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
241 aType = aTrimCurve->BasisCurve()->DynamicType();
244 if(aType == STANDARD_TYPE(Geom_Line)
245 || aType == STANDARD_TYPE(Geom_Conic)
246 || aType == STANDARD_TYPE(Geom_Circle)
247 || aType == STANDARD_TYPE(Geom_Ellipse)
248 || aType == STANDARD_TYPE(Geom_Hyperbola)
249 || aType == STANDARD_TYPE(Geom_Parabola)) {
260 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
262 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
264 ShapeType aST = GeomAPI_Shape::SHAPE;
266 switch(aShape.ShapeType()) {
267 case TopAbs_COMPOUND:
268 aST = GeomAPI_Shape::COMPOUND;
270 case TopAbs_COMPSOLID:
271 aST = GeomAPI_Shape::COMPSOLID;
274 aST = GeomAPI_Shape::SOLID;
277 aST = GeomAPI_Shape::SHELL;
280 aST = GeomAPI_Shape::FACE;
283 aST = GeomAPI_Shape::WIRE;
286 aST = GeomAPI_Shape::EDGE;
289 aST = GeomAPI_Shape::VERTEX;
292 aST = GeomAPI_Shape::SHAPE;
299 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
301 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
302 if (theType == "COMPOUND")
304 if (theType == "COMPSOLID")
306 if (theType == "SOLID")
308 if (theType == "SHELL")
310 if (theType == "FACE")
312 if (theType == "WIRE")
314 if (theType == "EDGE")
316 if (theType == "VERTEX")
318 return SHAPE; // default
321 std::string GeomAPI_Shape::shapeTypeStr() const
323 ShapeType aShapeType = shapeType();
324 std::string aShapeTypeStr;
328 aShapeTypeStr = "COMPOUND";
332 aShapeTypeStr = "COMPSOLID";
336 aShapeTypeStr = "SOLID";
340 aShapeTypeStr = "SHELL";
344 aShapeTypeStr = "FACE";
348 aShapeTypeStr = "WIRE";
352 aShapeTypeStr = "EDGE";
356 aShapeTypeStr = "VERTEX";
360 aShapeTypeStr = "SHAPE";
365 return aShapeTypeStr;
368 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
370 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
372 switch(anOrientation) {
373 case TopAbs_FORWARD: return FORWARD;
374 case TopAbs_REVERSED: return REVERSED;
375 case TopAbs_INTERNAL: return INTERNAL;
376 case TopAbs_EXTERNAL: return EXTERNAL;
377 default: return FORWARD;
381 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
383 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
385 switch(theOrientation) {
386 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
387 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
388 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
389 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
393 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
395 if(!theShape.get()) {
399 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
400 if(aShapeToSearch.IsNull()) {
404 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
405 if(aShapeToSearch.IsEqual(anExp.Current())) {
413 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
414 double& theXmax, double& theYmax, double& theZmax) const
416 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
420 BRepBndLib::Add(aShape, aBndBox);
421 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
425 std::string GeomAPI_Shape::getShapeStream() const
427 std::ostringstream aStream;
428 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
429 BRepTools::Write(aShape, aStream);
430 return aStream.str();