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::isVertex() const
66 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
67 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
70 bool GeomAPI_Shape::isEdge() const
72 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
73 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
76 bool GeomAPI_Shape::isFace() const
78 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
79 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
82 bool GeomAPI_Shape::isCompound() const
84 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
85 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
88 bool GeomAPI_Shape::isCompoundOfSolids() const
90 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
91 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
93 bool isAtLeastOne = false;
94 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
95 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
102 // adds the nopt-compound elements recursively to the list
103 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
105 if (!theShape.IsNull()) {
106 if (theShape.ShapeType() == TopAbs_COMPOUND) {
107 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
108 addSimpleToList(aSubs.Value(), theList);
111 theList.Append(theShape);
116 bool GeomAPI_Shape::isConnectedTopology() const
118 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
119 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
121 // list of simple elements that are not detected in connection to others
122 NCollection_List<TopoDS_Shape> aNotConnected;
123 addSimpleToList(aShape, aNotConnected);
124 if (aNotConnected.IsEmpty()) // an empty compound
127 // collect here the group of connected subs, starting with one first element
128 NCollection_List<TopoDS_Shape> aNewConnected;
129 aNewConnected.Append(aNotConnected.First());
130 aNotConnected.RemoveFirst();
131 // iterate until some new element become connected
132 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
133 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
134 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
135 while(aNotIter.More()) {
136 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
137 NCollection_List<TopoDS_Shape> aNotVertices;
138 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
139 aNotVertices.Append(anExp1.Current());
142 bool aConnected = false;
143 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
144 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
145 // checking topological connecion of aNotIter and aNewIter
146 // (if shapes are connected, vertices are connected for sure)
147 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
148 for(; !aConnected && anExp2.More(); anExp2.Next()) {
149 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
150 for(; aNotIter.More(); aNotIter.Next()) {
151 if (aNotIter.Value().IsSame(anExp2.Current())) {
159 aNew.Append(aNotIter.Value());
160 aNotConnected.Remove(aNotIter);
165 // remove all new connected and put to this list very new connected
166 aNewConnected.Clear();
167 aNewConnected.Append(aNew);
169 return aNotConnected.IsEmpty() == Standard_True;
172 bool GeomAPI_Shape::isSolid() const
174 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
175 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
178 bool GeomAPI_Shape::isCompSolid() const
180 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
181 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
184 bool GeomAPI_Shape::isPlanar() const
186 TopoDS_Shape aShape = impl<TopoDS_Shape>();
188 if(aShape.IsNull()) {
192 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
193 if(aShapeType == TopAbs_COMPOUND) {
194 TopoDS_Iterator anIt(aShape);
196 for(; anIt.More(); anIt.Next()) {
200 anIt.Initialize(aShape);
201 aShape = anIt.Value();
205 aShapeType = aShape.ShapeType();
206 if(aShapeType == TopAbs_VERTEX) {
208 } else if(aShapeType == TopAbs_FACE) {
209 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
210 Handle(Standard_Type) aType = aSurface->DynamicType();
212 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
213 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
214 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
215 aType = aTrimSurface->BasisSurface()->DynamicType();
217 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
219 BRepBuilderAPI_FindPlane aFindPlane(aShape);
220 bool isFound = aFindPlane.Found() == Standard_True;
222 if(!isFound && aShapeType == TopAbs_EDGE) {
223 Standard_Real aFirst, aLast;
224 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
225 Handle(Standard_Type) aType = aCurve->DynamicType();
227 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
228 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
229 aType = aTrimCurve->BasisCurve()->DynamicType();
232 if(aType == STANDARD_TYPE(Geom_Line)
233 || aType == STANDARD_TYPE(Geom_Conic)
234 || aType == STANDARD_TYPE(Geom_Circle)
235 || aType == STANDARD_TYPE(Geom_Ellipse)
236 || aType == STANDARD_TYPE(Geom_Hyperbola)
237 || aType == STANDARD_TYPE(Geom_Parabola)) {
248 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
250 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
252 ShapeType aST = GeomAPI_Shape::SHAPE;
254 switch(aShape.ShapeType()) {
255 case TopAbs_COMPOUND:
256 aST = GeomAPI_Shape::COMPOUND;
258 case TopAbs_COMPSOLID:
259 aST = GeomAPI_Shape::COMPSOLID;
262 aST = GeomAPI_Shape::SOLID;
265 aST = GeomAPI_Shape::SHELL;
268 aST = GeomAPI_Shape::FACE;
271 aST = GeomAPI_Shape::WIRE;
274 aST = GeomAPI_Shape::EDGE;
277 aST = GeomAPI_Shape::VERTEX;
280 aST = GeomAPI_Shape::SHAPE;
287 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
289 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
290 if (theType == "COMPOUND")
292 if (theType == "COMPSOLID")
294 if (theType == "SOLID")
296 if (theType == "SHELL")
298 if (theType == "FACE")
300 if (theType == "WIRE")
302 if (theType == "EDGE")
304 if (theType == "VERTEX")
306 return SHAPE; // default
309 std::string GeomAPI_Shape::shapeTypeStr() const
311 ShapeType aShapeType = shapeType();
312 std::string aShapeTypeStr;
316 aShapeTypeStr = "COMPOUND";
320 aShapeTypeStr = "COMPSOLID";
324 aShapeTypeStr = "SOLID";
328 aShapeTypeStr = "SHELL";
332 aShapeTypeStr = "FACE";
336 aShapeTypeStr = "WIRE";
340 aShapeTypeStr = "EDGE";
344 aShapeTypeStr = "VERTEX";
348 aShapeTypeStr = "SHAPE";
353 return aShapeTypeStr;
356 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
358 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
360 switch(anOrientation) {
361 case TopAbs_FORWARD: return FORWARD;
362 case TopAbs_REVERSED: return REVERSED;
363 case TopAbs_INTERNAL: return INTERNAL;
364 case TopAbs_EXTERNAL: return EXTERNAL;
365 default: return FORWARD;
369 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
371 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
373 switch(theOrientation) {
374 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
375 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
376 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
377 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
381 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
383 if(!theShape.get()) {
387 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
388 if(aShapeToSearch.IsNull()) {
392 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
393 if(aShapeToSearch.IsEqual(anExp.Current())) {
401 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
402 double& theXmax, double& theYmax, double& theZmax) const
404 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
408 BRepBndLib::Add(aShape, aBndBox);
409 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
413 std::string GeomAPI_Shape::getShapeStream() const
415 std::ostringstream aStream;
416 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
417 BRepTools::Write(aShape, aStream);
418 return aStream.str();