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 <BRepAlgoAPI_Section.hxx>
11 #include <BRepBndLib.hxx>
12 #include <BRepBuilderAPI_FindPlane.hxx>
13 #include <BRepTools.hxx>
14 #include <Bnd_Box.hxx>
15 #include <Geom_Circle.hxx>
16 #include <Geom_Conic.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom_Ellipse.hxx>
19 #include <Geom_Hyperbola.hxx>
20 #include <Geom_Line.hxx>
21 #include <Geom_Parabola.hxx>
22 #include <Geom_Plane.hxx>
23 #include <Geom_RectangularTrimmedSurface.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <NCollection_List.hxx>
32 #include <algorithm> // for std::transform
34 #define MY_SHAPE implPtr<TopoDS_Shape>()
36 GeomAPI_Shape::GeomAPI_Shape()
37 : GeomAPI_Interface(new TopoDS_Shape())
41 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
43 GeomShapePtr aShape(new GeomAPI_Shape());
44 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
48 bool GeomAPI_Shape::isNull() const
50 return MY_SHAPE->IsNull() == Standard_True;
53 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
58 return theShape->isNull();
59 if (theShape->isNull())
62 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
65 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
70 return theShape->isNull();
71 if (theShape->isNull())
74 return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
77 bool GeomAPI_Shape::isVertex() const
79 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
80 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
83 bool GeomAPI_Shape::isEdge() const
85 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
86 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
89 bool GeomAPI_Shape::isFace() const
91 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
92 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
95 bool GeomAPI_Shape::isCompound() const
97 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
98 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
101 bool GeomAPI_Shape::isCompoundOfSolids() const
103 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
104 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
106 bool isAtLeastOne = false;
107 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
108 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
115 // adds the nopt-compound elements recursively to the list
116 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
118 if (!theShape.IsNull()) {
119 if (theShape.ShapeType() == TopAbs_COMPOUND) {
120 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
121 addSimpleToList(aSubs.Value(), theList);
124 theList.Append(theShape);
129 bool GeomAPI_Shape::isConnectedTopology() const
131 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
132 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
134 // list of simple elements that are not detected in connection to others
135 NCollection_List<TopoDS_Shape> aNotConnected;
136 addSimpleToList(aShape, aNotConnected);
137 if (aNotConnected.IsEmpty()) // an empty compound
140 // collect here the group of connected subs, starting with one first element
141 NCollection_List<TopoDS_Shape> aNewConnected;
142 aNewConnected.Append(aNotConnected.First());
143 aNotConnected.RemoveFirst();
144 // iterate until some new element become connected
145 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
146 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
147 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
148 while(aNotIter.More()) {
149 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
150 NCollection_List<TopoDS_Shape> aNotVertices;
151 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
152 aNotVertices.Append(anExp1.Current());
155 bool aConnected = false;
156 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
157 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
158 // checking topological connecion of aNotIter and aNewIter
159 // (if shapes are connected, vertices are connected for sure)
160 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
161 for(; !aConnected && anExp2.More(); anExp2.Next()) {
162 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
163 for(; aNotIter.More(); aNotIter.Next()) {
164 if (aNotIter.Value().IsSame(anExp2.Current())) {
172 aNew.Append(aNotIter.Value());
173 aNotConnected.Remove(aNotIter);
178 // remove all new connected and put to this list very new connected
179 aNewConnected.Clear();
180 aNewConnected.Append(aNew);
182 return aNotConnected.IsEmpty() == Standard_True;
185 bool GeomAPI_Shape::isSolid() const
187 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
188 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
191 bool GeomAPI_Shape::isCompSolid() const
193 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
194 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
197 bool GeomAPI_Shape::isPlanar() const
199 TopoDS_Shape aShape = impl<TopoDS_Shape>();
201 if(aShape.IsNull()) {
205 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
206 if(aShapeType == TopAbs_COMPOUND) {
207 TopoDS_Iterator anIt(aShape);
209 for(; anIt.More(); anIt.Next()) {
213 anIt.Initialize(aShape);
214 aShape = anIt.Value();
218 aShapeType = aShape.ShapeType();
219 if(aShapeType == TopAbs_VERTEX) {
221 } else if(aShapeType == TopAbs_FACE) {
222 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
223 Handle(Standard_Type) aType = aSurface->DynamicType();
225 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
226 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
227 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
228 aType = aTrimSurface->BasisSurface()->DynamicType();
230 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
232 BRepBuilderAPI_FindPlane aFindPlane(aShape);
233 bool isFound = aFindPlane.Found() == Standard_True;
235 if(!isFound && aShapeType == TopAbs_EDGE) {
236 Standard_Real aFirst, aLast;
237 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
238 Handle(Standard_Type) aType = aCurve->DynamicType();
240 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
241 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
242 aType = aTrimCurve->BasisCurve()->DynamicType();
245 if(aType == STANDARD_TYPE(Geom_Line)
246 || aType == STANDARD_TYPE(Geom_Conic)
247 || aType == STANDARD_TYPE(Geom_Circle)
248 || aType == STANDARD_TYPE(Geom_Ellipse)
249 || aType == STANDARD_TYPE(Geom_Hyperbola)
250 || aType == STANDARD_TYPE(Geom_Parabola)) {
261 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
263 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
265 ShapeType aST = GeomAPI_Shape::SHAPE;
267 switch(aShape.ShapeType()) {
268 case TopAbs_COMPOUND:
269 aST = GeomAPI_Shape::COMPOUND;
271 case TopAbs_COMPSOLID:
272 aST = GeomAPI_Shape::COMPSOLID;
275 aST = GeomAPI_Shape::SOLID;
278 aST = GeomAPI_Shape::SHELL;
281 aST = GeomAPI_Shape::FACE;
284 aST = GeomAPI_Shape::WIRE;
287 aST = GeomAPI_Shape::EDGE;
290 aST = GeomAPI_Shape::VERTEX;
293 aST = GeomAPI_Shape::SHAPE;
300 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
302 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
303 if (theType == "COMPOUND")
305 if (theType == "COMPSOLID")
307 if (theType == "SOLID")
309 if (theType == "SHELL")
311 if (theType == "FACE")
313 if (theType == "WIRE")
315 if (theType == "EDGE")
317 if (theType == "VERTEX")
319 return SHAPE; // default
322 std::string GeomAPI_Shape::shapeTypeStr() const
324 ShapeType aShapeType = shapeType();
325 std::string aShapeTypeStr;
329 aShapeTypeStr = "COMPOUND";
333 aShapeTypeStr = "COMPSOLID";
337 aShapeTypeStr = "SOLID";
341 aShapeTypeStr = "SHELL";
345 aShapeTypeStr = "FACE";
349 aShapeTypeStr = "WIRE";
353 aShapeTypeStr = "EDGE";
357 aShapeTypeStr = "VERTEX";
361 aShapeTypeStr = "SHAPE";
366 return aShapeTypeStr;
369 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
371 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
373 switch(anOrientation) {
374 case TopAbs_FORWARD: return FORWARD;
375 case TopAbs_REVERSED: return REVERSED;
376 case TopAbs_INTERNAL: return INTERNAL;
377 case TopAbs_EXTERNAL: return EXTERNAL;
378 default: return FORWARD;
382 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
384 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
386 switch(theOrientation) {
387 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
388 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
389 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
390 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
394 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
396 if(!theShape.get()) {
400 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
401 if(aShapeToSearch.IsNull()) {
405 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
406 if(aShapeToSearch.IsEqual(anExp.Current())) {
414 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
415 double& theXmax, double& theYmax, double& theZmax) const
417 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
421 BRepBndLib::Add(aShape, aBndBox);
422 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
426 std::string GeomAPI_Shape::getShapeStream() const
428 std::ostringstream aStream;
429 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
430 BRepTools::Write(aShape, aStream);
431 return aStream.str();
434 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
436 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
437 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
439 BRepAlgoAPI_Section aCommon(aShape1, aShape2);
440 if (!aCommon.IsDone())
441 return GeomShapePtr();
443 TopoDS_Shape aResult = aCommon.Shape();
444 if (aResult.ShapeType() == TopAbs_COMPOUND) {
445 NCollection_List<TopoDS_Shape> aSubs;
446 addSimpleToList(aResult, aSubs);
447 if(aSubs.Size() == 1) {
448 aResult = aSubs.First();
449 } else if(aSubs.Size() == 0) {
450 return GeomShapePtr();
454 GeomShapePtr aResShape(new GeomAPI_Shape);
455 aResShape->setImpl(new TopoDS_Shape(aResult));