1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAPI_Shape.h"
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Wire.h>
27 #include <GeomAPI_Face.h>
28 #include <GeomAPI_Shell.h>
29 #include <GeomAPI_Solid.h>
31 #include <BRep_Tool.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
33 #include <BRepBndLib.hxx>
34 #include <BRepBuilderAPI_FindPlane.hxx>
35 #include <BRepExtrema_DistShapeShape.hxx>
36 #include <BRepTools.hxx>
37 #include <Bnd_Box.hxx>
38 #include <Geom_Circle.hxx>
39 #include <Geom_Conic.hxx>
40 #include <Geom_Curve.hxx>
41 #include <Geom_Ellipse.hxx>
42 #include <Geom_Hyperbola.hxx>
43 #include <Geom_Line.hxx>
44 #include <Geom_Parabola.hxx>
45 #include <Geom_Plane.hxx>
46 #include <Geom_RectangularTrimmedSurface.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <TopoDS_Iterator.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <NCollection_List.hxx>
54 #include <BOPAlgo_CheckerSI.hxx>
55 #include <BOPDS_DS.hxx>
58 #include <algorithm> // for std::transform
60 #include <BRepTools.hxx>
62 #define MY_SHAPE implPtr<TopoDS_Shape>()
64 GeomAPI_Shape::GeomAPI_Shape()
65 : GeomAPI_Interface(new TopoDS_Shape())
69 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
71 GeomShapePtr aShape(new GeomAPI_Shape());
72 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
76 bool GeomAPI_Shape::isNull() const
78 return MY_SHAPE->IsNull() == Standard_True;
81 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
86 return theShape->isNull();
87 if (theShape->isNull())
90 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
93 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
98 return theShape->isNull();
99 if (theShape->isNull())
102 return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
105 bool GeomAPI_Shape::isVertex() const
107 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
108 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
111 bool GeomAPI_Shape::isEdge() const
113 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
114 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
117 bool GeomAPI_Shape::isWire() const
119 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
120 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_WIRE;
123 bool GeomAPI_Shape::isFace() const
125 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
126 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
129 bool GeomAPI_Shape::isShell() const
131 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
132 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SHELL;
135 bool GeomAPI_Shape::isCompound() const
137 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
138 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
141 bool GeomAPI_Shape::isCompoundOfSolids() const
143 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
144 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
146 bool isAtLeastOne = false;
147 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
148 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
155 GeomAPI_Shape::ShapeType GeomAPI_Shape::typeOfCompoundShapes() const
157 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
158 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
161 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
162 if (!aSubs.Value().IsNull()) {
164 aType = aSubs.Value().ShapeType();
165 else if (aSubs.Value().ShapeType() != aType)
169 return (GeomAPI_Shape::ShapeType) aType;
172 // adds the nopt-compound elements recursively to the list
173 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
175 if (!theShape.IsNull()) {
176 if (theShape.ShapeType() == TopAbs_COMPOUND) {
177 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
178 addSimpleToList(aSubs.Value(), theList);
181 theList.Append(theShape);
186 bool GeomAPI_Shape::isConnectedTopology() const
188 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
189 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
191 // list of simple elements that are not detected in connection to others
192 NCollection_List<TopoDS_Shape> aNotConnected;
193 addSimpleToList(aShape, aNotConnected);
194 if (aNotConnected.IsEmpty()) // an empty compound
197 // collect here the group of connected subs, starting with one first element
198 NCollection_List<TopoDS_Shape> aNewConnected;
199 aNewConnected.Append(aNotConnected.First());
200 aNotConnected.RemoveFirst();
201 // iterate until some new element become connected
202 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
203 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
204 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
205 while(aNotIter.More()) {
206 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
207 NCollection_List<TopoDS_Shape> aNotVertices;
208 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
209 aNotVertices.Append(anExp1.Current());
212 bool aConnected = false;
213 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
214 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
215 // checking topological connecion of aNotIter and aNewIter
216 // (if shapes are connected, vertices are connected for sure)
217 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
218 for(; !aConnected && anExp2.More(); anExp2.Next()) {
219 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
220 for(; aNotIter.More(); aNotIter.Next()) {
221 if (aNotIter.Value().IsSame(anExp2.Current())) {
229 aNew.Append(aNotIter.Value());
230 aNotConnected.Remove(aNotIter);
235 // remove all new connected and put to this list very new connected
236 aNewConnected.Clear();
237 aNewConnected.Append(aNew);
239 return aNotConnected.IsEmpty() == Standard_True;
242 bool GeomAPI_Shape::isSolid() const
244 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
245 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
248 bool GeomAPI_Shape::isCompSolid() const
250 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
251 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
254 bool GeomAPI_Shape::isPlanar() const
256 TopoDS_Shape aShape = impl<TopoDS_Shape>();
258 if(aShape.IsNull()) {
262 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
263 if(aShapeType == TopAbs_COMPOUND) {
264 TopoDS_Iterator anIt(aShape);
266 for(; anIt.More(); anIt.Next()) {
270 anIt.Initialize(aShape);
271 aShape = anIt.Value();
275 aShapeType = aShape.ShapeType();
276 if(aShapeType == TopAbs_VERTEX) {
278 } else if(aShapeType == TopAbs_FACE) {
279 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
280 Handle(Standard_Type) aType = aSurface->DynamicType();
282 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
283 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
284 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
285 aType = aTrimSurface->BasisSurface()->DynamicType();
287 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
289 BRepBuilderAPI_FindPlane aFindPlane(aShape);
290 bool isFound = aFindPlane.Found() == Standard_True;
292 if(!isFound && aShapeType == TopAbs_EDGE) {
293 Standard_Real aFirst, aLast;
294 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
295 Handle(Standard_Type) aType = aCurve->DynamicType();
297 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
298 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
299 aType = aTrimCurve->BasisCurve()->DynamicType();
302 if(aType == STANDARD_TYPE(Geom_Line)
303 || aType == STANDARD_TYPE(Geom_Conic)
304 || aType == STANDARD_TYPE(Geom_Circle)
305 || aType == STANDARD_TYPE(Geom_Ellipse)
306 || aType == STANDARD_TYPE(Geom_Hyperbola)
307 || aType == STANDARD_TYPE(Geom_Parabola)) {
318 std::shared_ptr<GeomAPI_Vertex> GeomAPI_Shape::vertex() const
320 GeomVertexPtr aVertex;
322 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
323 aVertex = GeomVertexPtr(new GeomAPI_Vertex);
324 aVertex->setImpl(new TopoDS_Shape(aShape));
329 std::shared_ptr<GeomAPI_Edge> GeomAPI_Shape::edge() const
333 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
334 anEdge = GeomEdgePtr(new GeomAPI_Edge);
335 anEdge->setImpl(new TopoDS_Shape(aShape));
340 std::shared_ptr<GeomAPI_Wire> GeomAPI_Shape::wire() const
344 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
345 aWire = GeomWirePtr(new GeomAPI_Wire);
346 aWire->setImpl(new TopoDS_Shape(aShape));
351 std::shared_ptr<GeomAPI_Face> GeomAPI_Shape::face() const
355 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
356 aFace = GeomFacePtr(new GeomAPI_Face);
357 aFace->setImpl(new TopoDS_Shape(aShape));
362 std::shared_ptr<GeomAPI_Shell> GeomAPI_Shape::shell() const
366 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
367 aShell = GeomShellPtr(new GeomAPI_Shell);
368 aShell->setImpl(new TopoDS_Shape(aShape));
373 std::shared_ptr<GeomAPI_Solid> GeomAPI_Shape::solid() const
377 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
378 aSolid = GeomSolidPtr(new GeomAPI_Solid);
379 aSolid->setImpl(new TopoDS_Shape(aShape));
384 std::list<std::shared_ptr<GeomAPI_Shape> >
385 GeomAPI_Shape::subShapes(ShapeType theSubShapeType) const
388 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
392 for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theSubShapeType);
393 anExp.More(); anExp.Next()) {
394 GeomShapePtr aSub(new GeomAPI_Shape);
395 aSub->setImpl(new TopoDS_Shape(anExp.Current()));
396 aSubs.push_back(aSub);
401 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
403 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
405 return GeomAPI_Shape::SHAPE;
407 ShapeType aST = GeomAPI_Shape::SHAPE;
409 switch(aShape.ShapeType()) {
410 case TopAbs_COMPOUND:
411 aST = GeomAPI_Shape::COMPOUND;
413 case TopAbs_COMPSOLID:
414 aST = GeomAPI_Shape::COMPSOLID;
417 aST = GeomAPI_Shape::SOLID;
420 aST = GeomAPI_Shape::SHELL;
423 aST = GeomAPI_Shape::FACE;
426 aST = GeomAPI_Shape::WIRE;
429 aST = GeomAPI_Shape::EDGE;
432 aST = GeomAPI_Shape::VERTEX;
435 aST = GeomAPI_Shape::SHAPE;
442 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
444 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
445 if (theType == "COMPOUND")
447 if (theType == "COMPSOLID")
449 if (theType == "SOLID")
451 if (theType == "SHELL")
453 if (theType == "FACE")
455 if (theType == "WIRE")
457 if (theType == "EDGE")
459 if (theType == "VERTEX")
461 return SHAPE; // default
464 std::string GeomAPI_Shape::shapeTypeStr() const
466 ShapeType aShapeType = shapeType();
467 std::string aShapeTypeStr;
471 aShapeTypeStr = "COMPOUND";
475 aShapeTypeStr = "COMPSOLID";
479 aShapeTypeStr = "SOLID";
483 aShapeTypeStr = "SHELL";
487 aShapeTypeStr = "FACE";
491 aShapeTypeStr = "WIRE";
495 aShapeTypeStr = "EDGE";
499 aShapeTypeStr = "VERTEX";
503 aShapeTypeStr = "SHAPE";
508 return aShapeTypeStr;
511 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
513 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
515 switch(anOrientation) {
516 case TopAbs_FORWARD: return FORWARD;
517 case TopAbs_REVERSED: return REVERSED;
518 case TopAbs_INTERNAL: return INTERNAL;
519 case TopAbs_EXTERNAL: return EXTERNAL;
520 default: return FORWARD;
524 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
526 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
528 switch(theOrientation) {
529 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
530 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
531 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
532 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
536 void GeomAPI_Shape::reverse()
541 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape,
542 const bool theCheckOrientation) const
544 if(!theShape.get()) {
548 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
549 if(aShapeToSearch.IsNull()) {
553 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
554 if(theCheckOrientation ?
555 aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) {
563 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
564 double& theXmax, double& theYmax, double& theZmax) const
566 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
570 BRepBndLib::Add(aShape, aBndBox, false);
571 if (aBndBox.IsVoid())
573 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
577 GeomPointPtr GeomAPI_Shape::middlePoint() const
579 GeomPointPtr aMiddlePoint;
581 switch (shapeType()) {
583 aMiddlePoint = vertex()->point();
586 aMiddlePoint = edge()->middlePoint();
589 aMiddlePoint = wire()->middlePoint();
592 aMiddlePoint = face()->middlePoint();
595 aMiddlePoint = shell()->middlePoint();
598 aMiddlePoint = solid()->middlePoint();
601 // get middle point as center of the bounding box
602 double aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
603 computeSize(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
604 aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(
605 (aMinX + aMaxX) * 0.5, (aMinY + aMaxY) * 0.5, (aMinZ + aMaxZ) * 0.5));
612 std::string GeomAPI_Shape::getShapeStream() const
614 std::ostringstream aStream;
615 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
616 BRepTools::Write(aShape, aStream);
617 return aStream.str();
620 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
622 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
623 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
625 BRepAlgoAPI_Section aCommon(aShape1, aShape2);
626 if (!aCommon.IsDone())
627 return GeomShapePtr();
629 TopoDS_Shape aResult = aCommon.Shape();
630 if (aResult.ShapeType() == TopAbs_COMPOUND) {
631 NCollection_List<TopoDS_Shape> aSubs;
632 addSimpleToList(aResult, aSubs);
633 if(aSubs.Size() == 1) {
634 aResult = aSubs.First();
635 } else if(aSubs.Size() == 0) {
636 return GeomShapePtr();
640 GeomShapePtr aResShape(new GeomAPI_Shape);
641 aResShape->setImpl(new TopoDS_Shape(aResult));
645 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
647 if(!theShape.get()) {
651 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
652 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
654 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
656 if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
663 void GeomAPI_Shape::translate(const std::shared_ptr<GeomAPI_Dir> theDir, const double theOffset)
665 gp_Dir aDir = theDir->impl<gp_Dir>();
666 gp_Vec aTrsfVec(aDir.XYZ() * theOffset);
667 gp_Trsf aTranslation;
668 aTranslation.SetTranslation(aTrsfVec);
669 TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation);
670 setImpl(new TopoDS_Shape(aResult));
673 bool GeomAPI_Shape::isSelfIntersected(const int theLevelOfCheck) const
675 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
676 aCSI.SetLevelOfCheck(theLevelOfCheck);
677 TopTools_ListOfShape aList;
678 const TopoDS_Shape& aThisShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
679 aList.Append(aThisShape);
680 aCSI.SetArguments(aList);
682 if (aCSI.HasErrors() || aCSI.DS().Interferences().Extent() > 0) {