X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGeomAPI%2FGeomAPI_Shape.cpp;h=c4f30b6d321fb8aceee45910f7ce2f4cb0da47ee;hb=d2034a988bc73596aab1bb256ddb02ce9ad2bd5a;hp=9d3694bf1c114d8524cc8e8ede5786a5722e767c;hpb=5b6031b015602aa07f5a6fc668c13ac3faf7a8a9;p=modules%2Fshaper.git diff --git a/src/GeomAPI/GeomAPI_Shape.cpp b/src/GeomAPI/GeomAPI_Shape.cpp index 9d3694bf1..c4f30b6d3 100644 --- a/src/GeomAPI/GeomAPI_Shape.cpp +++ b/src/GeomAPI/GeomAPI_Shape.cpp @@ -1,14 +1,38 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: GeomAPI_Shape.cpp -// Created: 23 Apr 2014 -// Author: Mikhail PONIKAROV +// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or +// email : webmaster.salome@opencascade.com +// #include "GeomAPI_Shape.h" +#include +#include +#include +#include +#include +#include +#include + #include +#include #include #include +#include #include #include #include @@ -27,9 +51,14 @@ #include #include +#include +#include + #include #include // for std::transform +#include + #define MY_SHAPE implPtr() GeomAPI_Shape::GeomAPI_Shape() @@ -61,6 +90,18 @@ bool GeomAPI_Shape::isEqual(const std::shared_ptr theShape) const return MY_SHAPE->IsEqual(theShape->impl()) == Standard_True; } +bool GeomAPI_Shape::isSame(const std::shared_ptr theShape) const +{ + if (!theShape.get()) + return false; + if (isNull()) + return theShape->isNull(); + if (theShape->isNull()) + return false; + + return MY_SHAPE->IsSame(theShape->impl()) == Standard_True; +} + bool GeomAPI_Shape::isVertex() const { const TopoDS_Shape& aShape = const_cast(this)->impl(); @@ -73,12 +114,24 @@ bool GeomAPI_Shape::isEdge() const return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE; } +bool GeomAPI_Shape::isWire() const +{ + const TopoDS_Shape& aShape = const_cast(this)->impl(); + return !aShape.IsNull() && aShape.ShapeType() == TopAbs_WIRE; +} + bool GeomAPI_Shape::isFace() const { const TopoDS_Shape& aShape = const_cast(this)->impl(); return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE; } +bool GeomAPI_Shape::isShell() const +{ + const TopoDS_Shape& aShape = const_cast(this)->impl(); + return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SHELL; +} + bool GeomAPI_Shape::isCompound() const { const TopoDS_Shape& aShape = const_cast(this)->impl(); @@ -99,6 +152,23 @@ bool GeomAPI_Shape::isCompoundOfSolids() const return isAtLeastOne; } +GeomAPI_Shape::ShapeType GeomAPI_Shape::typeOfCompoundShapes() const +{ + const TopoDS_Shape& aShape = const_cast(this)->impl(); + if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) + return SHAPE; + int aType = -1; + for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) { + if (!aSubs.Value().IsNull()) { + if (aType == -1) + aType = aSubs.Value().ShapeType(); + else if (aSubs.Value().ShapeType() != aType) + return SHAPE; + } + } + return (GeomAPI_Shape::ShapeType) aType; +} + // adds the nopt-compound elements recursively to the list static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List& theList) { @@ -245,9 +315,94 @@ bool GeomAPI_Shape::isPlanar() const return false; } +std::shared_ptr GeomAPI_Shape::vertex() const +{ + GeomVertexPtr aVertex; + if (isVertex()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + aVertex = GeomVertexPtr(new GeomAPI_Vertex); + aVertex->setImpl(new TopoDS_Shape(aShape)); + } + return aVertex; +} + +std::shared_ptr GeomAPI_Shape::edge() const +{ + GeomEdgePtr anEdge; + if (isEdge()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + anEdge = GeomEdgePtr(new GeomAPI_Edge); + anEdge->setImpl(new TopoDS_Shape(aShape)); + } + return anEdge; +} + +std::shared_ptr GeomAPI_Shape::wire() const +{ + GeomWirePtr aWire; + if (isWire()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + aWire = GeomWirePtr(new GeomAPI_Wire); + aWire->setImpl(new TopoDS_Shape(aShape)); + } + return aWire; +} + +std::shared_ptr GeomAPI_Shape::face() const +{ + GeomFacePtr aFace; + if (isFace()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + aFace = GeomFacePtr(new GeomAPI_Face); + aFace->setImpl(new TopoDS_Shape(aShape)); + } + return aFace; +} + +std::shared_ptr GeomAPI_Shape::shell() const +{ + GeomShellPtr aShell; + if (isShell()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + aShell = GeomShellPtr(new GeomAPI_Shell); + aShell->setImpl(new TopoDS_Shape(aShape)); + } + return aShell; +} + +std::shared_ptr GeomAPI_Shape::solid() const +{ + GeomSolidPtr aSolid; + if (isSolid()) { + const TopoDS_Shape& aShape = const_cast(this)->impl(); + aSolid = GeomSolidPtr(new GeomAPI_Solid); + aSolid->setImpl(new TopoDS_Shape(aShape)); + } + return aSolid; +} + +std::list > +GeomAPI_Shape::subShapes(ShapeType theSubShapeType) const +{ + ListOfShape aSubs; + const TopoDS_Shape& aShape = impl(); + if (aShape.IsNull()) + return aSubs; + + for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theSubShapeType); + anExp.More(); anExp.Next()) { + GeomShapePtr aSub(new GeomAPI_Shape); + aSub->setImpl(new TopoDS_Shape(anExp.Current())); + aSubs.push_back(aSub); + } + return aSubs; +} + GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const { const TopoDS_Shape& aShape = impl(); + if (aShape.IsNull()) + return GeomAPI_Shape::SHAPE; ShapeType aST = GeomAPI_Shape::SHAPE; @@ -378,7 +533,13 @@ void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientati } } -bool GeomAPI_Shape::isSubShape(const std::shared_ptr theShape) const +void GeomAPI_Shape::reverse() +{ + MY_SHAPE->Reverse(); +} + +bool GeomAPI_Shape::isSubShape(const std::shared_ptr theShape, + const bool theCheckOrientation) const { if(!theShape.get()) { return false; @@ -390,7 +551,8 @@ bool GeomAPI_Shape::isSubShape(const std::shared_ptr theShape) co } for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) { - if(aShapeToSearch.IsEqual(anExp.Current())) { + if(theCheckOrientation ? + aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) { return true; } } @@ -405,11 +567,48 @@ bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmi if (aShape.IsNull()) return false; Bnd_Box aBndBox; - BRepBndLib::Add(aShape, aBndBox); + BRepBndLib::Add(aShape, aBndBox, false); + if (aBndBox.IsVoid()) + return false; aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax); return true; } +GeomPointPtr GeomAPI_Shape::middlePoint() const +{ + GeomPointPtr aMiddlePoint; + + switch (shapeType()) { + case VERTEX: + aMiddlePoint = vertex()->point(); + break; + case EDGE: + aMiddlePoint = edge()->middlePoint(); + break; + case WIRE: + aMiddlePoint = wire()->middlePoint(); + break; + case FACE: + aMiddlePoint = face()->middlePoint(); + break; + case SHELL: + aMiddlePoint = shell()->middlePoint(); + break; + case SOLID: + aMiddlePoint = solid()->middlePoint(); + break; + default: { + // get middle point as center of the bounding box + double aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ; + computeSize(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ); + aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt( + (aMinX + aMaxX) * 0.5, (aMinY + aMaxY) * 0.5, (aMinZ + aMaxZ) * 0.5)); + } + } + + return aMiddlePoint; +} + std::string GeomAPI_Shape::getShapeStream() const { std::ostringstream aStream; @@ -417,3 +616,72 @@ std::string GeomAPI_Shape::getShapeStream() const BRepTools::Write(aShape, aStream); return aStream.str(); } + +GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const +{ + const TopoDS_Shape& aShape1 = const_cast(this)->impl(); + const TopoDS_Shape& aShape2 = theShape->impl(); + + BRepAlgoAPI_Section aCommon(aShape1, aShape2); + if (!aCommon.IsDone()) + return GeomShapePtr(); + + TopoDS_Shape aResult = aCommon.Shape(); + if (aResult.ShapeType() == TopAbs_COMPOUND) { + NCollection_List aSubs; + addSimpleToList(aResult, aSubs); + if(aSubs.Size() == 1) { + aResult = aSubs.First(); + } else if(aSubs.Size() == 0) { + return GeomShapePtr(); + } + } + + GeomShapePtr aResShape(new GeomAPI_Shape); + aResShape->setImpl(new TopoDS_Shape(aResult)); + return aResShape; +} + +bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const +{ + if(!theShape.get()) { + return false; + } + + const TopoDS_Shape& aShape1 = const_cast(this)->impl(); + const TopoDS_Shape& aShape2 = theShape->impl(); + + BRepExtrema_DistShapeShape aDist(aShape1, aShape2); + aDist.Perform(); + if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) { + return true; + } + + return false; +} + +void GeomAPI_Shape::translate(const std::shared_ptr theDir, const double theOffset) +{ + gp_Dir aDir = theDir->impl(); + gp_Vec aTrsfVec(aDir.XYZ() * theOffset); + gp_Trsf aTranslation; + aTranslation.SetTranslation(aTrsfVec); + TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation); + setImpl(new TopoDS_Shape(aResult)); +} + +bool GeomAPI_Shape::isSelfIntersected(const int theLevelOfCheck) const +{ + BOPAlgo_CheckerSI aCSI; // checker of self-interferences + aCSI.SetLevelOfCheck(theLevelOfCheck); + TopTools_ListOfShape aList; + const TopoDS_Shape& aThisShape = const_cast(this)->impl(); + aList.Append(aThisShape); + aCSI.SetArguments(aList); + aCSI.Perform(); + if (aCSI.HasErrors() || aCSI.DS().Interferences().Extent() > 0) { + return true; + } + + return false; +}