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 <BRepExtrema_DistShapeShape.hxx>
14 #include <BRepTools.hxx>
15 #include <Bnd_Box.hxx>
16 #include <Geom_Circle.hxx>
17 #include <Geom_Conic.hxx>
18 #include <Geom_Curve.hxx>
19 #include <Geom_Ellipse.hxx>
20 #include <Geom_Hyperbola.hxx>
21 #include <Geom_Line.hxx>
22 #include <Geom_Parabola.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom_RectangularTrimmedSurface.hxx>
25 #include <Geom_TrimmedCurve.hxx>
26 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <NCollection_List.hxx>
33 #include <algorithm> // for std::transform
35 #include <BRepTools.hxx>
37 #define MY_SHAPE implPtr<TopoDS_Shape>()
39 GeomAPI_Shape::GeomAPI_Shape()
40 : GeomAPI_Interface(new TopoDS_Shape())
44 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
46 GeomShapePtr aShape(new GeomAPI_Shape());
47 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
51 bool GeomAPI_Shape::isNull() const
53 return MY_SHAPE->IsNull() == Standard_True;
56 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
61 return theShape->isNull();
62 if (theShape->isNull())
65 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
68 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
73 return theShape->isNull();
74 if (theShape->isNull())
77 return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
80 bool GeomAPI_Shape::isVertex() const
82 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
83 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
86 bool GeomAPI_Shape::isEdge() const
88 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
89 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
92 bool GeomAPI_Shape::isFace() const
94 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
95 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
98 bool GeomAPI_Shape::isCompound() const
100 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
101 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
104 bool GeomAPI_Shape::isCompoundOfSolids() const
106 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
107 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
109 bool isAtLeastOne = false;
110 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
111 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
118 // adds the nopt-compound elements recursively to the list
119 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
121 if (!theShape.IsNull()) {
122 if (theShape.ShapeType() == TopAbs_COMPOUND) {
123 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
124 addSimpleToList(aSubs.Value(), theList);
127 theList.Append(theShape);
132 bool GeomAPI_Shape::isConnectedTopology() const
134 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
135 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
137 // list of simple elements that are not detected in connection to others
138 NCollection_List<TopoDS_Shape> aNotConnected;
139 addSimpleToList(aShape, aNotConnected);
140 if (aNotConnected.IsEmpty()) // an empty compound
143 // collect here the group of connected subs, starting with one first element
144 NCollection_List<TopoDS_Shape> aNewConnected;
145 aNewConnected.Append(aNotConnected.First());
146 aNotConnected.RemoveFirst();
147 // iterate until some new element become connected
148 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
149 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
150 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
151 while(aNotIter.More()) {
152 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
153 NCollection_List<TopoDS_Shape> aNotVertices;
154 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
155 aNotVertices.Append(anExp1.Current());
158 bool aConnected = false;
159 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
160 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
161 // checking topological connecion of aNotIter and aNewIter
162 // (if shapes are connected, vertices are connected for sure)
163 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
164 for(; !aConnected && anExp2.More(); anExp2.Next()) {
165 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
166 for(; aNotIter.More(); aNotIter.Next()) {
167 if (aNotIter.Value().IsSame(anExp2.Current())) {
175 aNew.Append(aNotIter.Value());
176 aNotConnected.Remove(aNotIter);
181 // remove all new connected and put to this list very new connected
182 aNewConnected.Clear();
183 aNewConnected.Append(aNew);
185 return aNotConnected.IsEmpty() == Standard_True;
188 bool GeomAPI_Shape::isSolid() const
190 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
191 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
194 bool GeomAPI_Shape::isCompSolid() const
196 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
197 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
200 bool GeomAPI_Shape::isPlanar() const
202 TopoDS_Shape aShape = impl<TopoDS_Shape>();
204 if(aShape.IsNull()) {
208 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
209 if(aShapeType == TopAbs_COMPOUND) {
210 TopoDS_Iterator anIt(aShape);
212 for(; anIt.More(); anIt.Next()) {
216 anIt.Initialize(aShape);
217 aShape = anIt.Value();
221 aShapeType = aShape.ShapeType();
222 if(aShapeType == TopAbs_VERTEX) {
224 } else if(aShapeType == TopAbs_FACE) {
225 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
226 Handle(Standard_Type) aType = aSurface->DynamicType();
228 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
229 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
230 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
231 aType = aTrimSurface->BasisSurface()->DynamicType();
233 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
235 BRepBuilderAPI_FindPlane aFindPlane(aShape);
236 bool isFound = aFindPlane.Found() == Standard_True;
238 if(!isFound && aShapeType == TopAbs_EDGE) {
239 Standard_Real aFirst, aLast;
240 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
241 Handle(Standard_Type) aType = aCurve->DynamicType();
243 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
244 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
245 aType = aTrimCurve->BasisCurve()->DynamicType();
248 if(aType == STANDARD_TYPE(Geom_Line)
249 || aType == STANDARD_TYPE(Geom_Conic)
250 || aType == STANDARD_TYPE(Geom_Circle)
251 || aType == STANDARD_TYPE(Geom_Ellipse)
252 || aType == STANDARD_TYPE(Geom_Hyperbola)
253 || aType == STANDARD_TYPE(Geom_Parabola)) {
264 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
266 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
268 ShapeType aST = GeomAPI_Shape::SHAPE;
270 switch(aShape.ShapeType()) {
271 case TopAbs_COMPOUND:
272 aST = GeomAPI_Shape::COMPOUND;
274 case TopAbs_COMPSOLID:
275 aST = GeomAPI_Shape::COMPSOLID;
278 aST = GeomAPI_Shape::SOLID;
281 aST = GeomAPI_Shape::SHELL;
284 aST = GeomAPI_Shape::FACE;
287 aST = GeomAPI_Shape::WIRE;
290 aST = GeomAPI_Shape::EDGE;
293 aST = GeomAPI_Shape::VERTEX;
296 aST = GeomAPI_Shape::SHAPE;
303 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
305 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
306 if (theType == "COMPOUND")
308 if (theType == "COMPSOLID")
310 if (theType == "SOLID")
312 if (theType == "SHELL")
314 if (theType == "FACE")
316 if (theType == "WIRE")
318 if (theType == "EDGE")
320 if (theType == "VERTEX")
322 return SHAPE; // default
325 std::string GeomAPI_Shape::shapeTypeStr() const
327 ShapeType aShapeType = shapeType();
328 std::string aShapeTypeStr;
332 aShapeTypeStr = "COMPOUND";
336 aShapeTypeStr = "COMPSOLID";
340 aShapeTypeStr = "SOLID";
344 aShapeTypeStr = "SHELL";
348 aShapeTypeStr = "FACE";
352 aShapeTypeStr = "WIRE";
356 aShapeTypeStr = "EDGE";
360 aShapeTypeStr = "VERTEX";
364 aShapeTypeStr = "SHAPE";
369 return aShapeTypeStr;
372 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
374 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
376 switch(anOrientation) {
377 case TopAbs_FORWARD: return FORWARD;
378 case TopAbs_REVERSED: return REVERSED;
379 case TopAbs_INTERNAL: return INTERNAL;
380 case TopAbs_EXTERNAL: return EXTERNAL;
381 default: return FORWARD;
385 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
387 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
389 switch(theOrientation) {
390 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
391 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
392 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
393 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
397 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape) const
399 if(!theShape.get()) {
403 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
404 if(aShapeToSearch.IsNull()) {
408 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
409 if(aShapeToSearch.IsEqual(anExp.Current())) {
417 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
418 double& theXmax, double& theYmax, double& theZmax) const
420 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
424 BRepBndLib::Add(aShape, aBndBox);
425 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
429 std::string GeomAPI_Shape::getShapeStream() const
431 std::ostringstream aStream;
432 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
433 BRepTools::Write(aShape, aStream);
434 return aStream.str();
437 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
439 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
440 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
442 BRepAlgoAPI_Section aCommon(aShape1, aShape2);
443 if (!aCommon.IsDone())
444 return GeomShapePtr();
446 TopoDS_Shape aResult = aCommon.Shape();
447 if (aResult.ShapeType() == TopAbs_COMPOUND) {
448 NCollection_List<TopoDS_Shape> aSubs;
449 addSimpleToList(aResult, aSubs);
450 if(aSubs.Size() == 1) {
451 aResult = aSubs.First();
452 } else if(aSubs.Size() == 0) {
453 return GeomShapePtr();
457 GeomShapePtr aResShape(new GeomAPI_Shape);
458 aResShape->setImpl(new TopoDS_Shape(aResult));
462 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
464 if(!theShape.get()) {
468 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
469 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
471 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
473 if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {