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 "ModelGeomAlgo_Shape.h"
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_ResultConstruction.h>
29 #include <GeomAPI_PlanarEdges.h>
30 #include <GeomAPI_Pnt.h>
34 #pragma warning(disable : 4996) // for sprintf
37 namespace ModelGeomAlgo_Shape
39 void shapesOfType(const FeaturePtr& theFeature,
40 const GeomAPI_Shape::ShapeType& theType,
41 std::set<ResultPtr>& theShapeResults)
43 theShapeResults.clear();
44 std::list<ResultPtr> aResults = theFeature->results();
45 std::list<ResultPtr>::const_iterator aRIter = aResults.cbegin();
46 for (; aRIter != aResults.cend(); aRIter++) {
47 ResultPtr aResult = *aRIter;
48 GeomShapePtr aShape = aResult->shape();
49 if (aShape.get() && aShape->shapeType() == theType)
50 theShapeResults.insert(aResult);
54 // Check the point is within shape's bounding box
55 static bool isPointWithinBB(const GeomPointPtr& thePoint,
56 const GeomShapePtr& theShape,
57 const double theTolerance)
59 double aXMin, aXMax, aYMin, aYMax, aZMin, aZMax;
60 theShape->computeSize(aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
61 return thePoint->x() >= aXMin - theTolerance && thePoint->x() <= aXMax + theTolerance &&
62 thePoint->y() >= aYMin - theTolerance && thePoint->y() <= aYMax + theTolerance &&
63 thePoint->z() >= aZMin - theTolerance && thePoint->z() <= aZMax + theTolerance;
66 // Select sub-shape of the given type, which contains the given point
67 static GeomShapePtr findSubShape(const GeomShapePtr& theShape,
68 const GeomAPI_Shape::ShapeType& theType,
69 const GeomPointPtr& thePoint,
70 const double theTolerance)
72 std::list<GeomShapePtr> aSubs = theShape->subShapes(theType);
73 for (std::list<GeomShapePtr>::const_iterator aSubIt = aSubs.begin();
74 aSubIt != aSubs.end(); ++aSubIt) {
75 if ((*aSubIt)->middlePoint()->distance(thePoint) < theTolerance)
80 return GeomShapePtr();
83 bool findSubshapeByPoint(const std::shared_ptr<ModelAPI_Feature>& theFeature,
84 const std::shared_ptr<GeomAPI_Pnt>& thePoint,
85 const GeomAPI_Shape::ShapeType& theShapeType,
86 std::shared_ptr<ModelAPI_Result>& theResult,
87 std::shared_ptr<GeomAPI_Shape>& theSubshape)
89 static const double TOLERANCE = 1.e-7;
91 theResult = ResultPtr();
92 const std::list<ResultPtr>& aResults = theFeature->results();
93 for (std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
94 aResIt != aResults.end(); ++aResIt) {
95 GeomShapePtr aCurShape = (*aResIt)->shape();
96 // first of all, check the point is within bounding box of the result
97 if (!aCurShape || !isPointWithinBB(thePoint, aCurShape, TOLERANCE))
99 // now, process all sub-shapes of the given type and check their inner points,
100 // but skip the case the selected type is COMPOUND and the shape is a list of sketch edges
101 // (it will be processed later)
102 std::shared_ptr<GeomAPI_PlanarEdges> aSketchEdges =
103 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aCurShape);
104 if (theShapeType != GeomAPI_Shape::COMPOUND || !aSketchEdges)
105 theSubshape = findSubShape(aCurShape, theShapeType, thePoint, TOLERANCE);
111 // special case for ResultConstruction if the FACE is selected
112 ResultConstructionPtr aResConstr =
113 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIt);
114 if (aResConstr && theShapeType >= GeomAPI_Shape::FACE) {
115 int aNbFaces = aResConstr->facesNum();
116 for (int aFaceInd = 0; aFaceInd < aNbFaces; ++aFaceInd) {
117 GeomFacePtr aCurFace = aResConstr->face(aFaceInd);
118 // check the point is within bounding box of the face
119 if (!isPointWithinBB(thePoint, aCurFace, TOLERANCE))
121 theSubshape = findSubShape(aCurFace, theShapeType, thePoint, TOLERANCE);
131 // next special case: the full sketch is selected
132 // the selection type is a COMPOUND
134 aSketchEdges->middlePoint()->distance(thePoint) < TOLERANCE) {
135 // select whole result
137 theSubshape = GeomShapePtr();
142 return (bool)theResult;
144 } // namespace ModelGeomAlgo_Shape