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_CompositeFeature.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultCompSolid.h>
29 #include <ModelAPI_ResultConstruction.h>
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_Pnt.h>
36 #pragma warning(disable : 4996) // for sprintf
39 namespace ModelGeomAlgo_Shape
41 void shapesOfType(const FeaturePtr& theFeature,
42 const GeomAPI_Shape::ShapeType& theType,
43 std::set<ResultPtr>& theShapeResults)
45 theShapeResults.clear();
46 std::list<ResultPtr> aResults = theFeature->results();
47 std::list<ResultPtr>::const_iterator aRIter = aResults.cbegin();
48 for (; aRIter != aResults.cend(); aRIter++) {
49 ResultPtr aResult = *aRIter;
50 GeomShapePtr aShape = aResult->shape();
51 if (aShape.get() && aShape->shapeType() == theType)
52 theShapeResults.insert(aResult);
56 // Check the point is within shape's bounding box
57 static bool isPointWithinBB(const GeomPointPtr& thePoint,
58 const GeomShapePtr& theShape,
59 const double theTolerance)
61 double aXMin, aXMax, aYMin, aYMax, aZMin, aZMax;
62 return theShape->computeSize(aXMin, aYMin, aZMin, aXMax, aYMax, aZMax) &&
63 thePoint->x() >= aXMin - theTolerance && thePoint->x() <= aXMax + theTolerance &&
64 thePoint->y() >= aYMin - theTolerance && thePoint->y() <= aYMax + theTolerance &&
65 thePoint->z() >= aZMin - theTolerance && thePoint->z() <= aZMax + theTolerance;
68 // Select sub-shape of the given type, which contains the given point
69 static GeomShapePtr findSubShape(const GeomShapePtr& theShape,
70 const GeomAPI_Shape::ShapeType& theType,
71 const GeomPointPtr& thePoint,
72 const double theTolerance)
74 std::list<GeomShapePtr> aSubs = theShape->subShapes(theType);
75 for (std::list<GeomShapePtr>::const_iterator aSubIt = aSubs.begin();
76 aSubIt != aSubs.end(); ++aSubIt) {
77 GeomPointPtr aMiddlePoint = (*aSubIt)->middlePoint();
78 if (aMiddlePoint && aMiddlePoint->distance(thePoint) < theTolerance)
83 return GeomShapePtr();
86 bool findSubshapeByPoint(const std::shared_ptr<ModelAPI_Feature>& theFeature,
87 const std::shared_ptr<GeomAPI_Pnt>& thePoint,
88 const GeomAPI_Shape::ShapeType& theShapeType,
89 std::shared_ptr<ModelAPI_Result>& theResult,
90 std::shared_ptr<GeomAPI_Shape>& theSubshape)
92 static const double TOLERANCE = 1.e-7;
94 theResult = ResultPtr();
95 theSubshape = GeomShapePtr();
96 const std::list<ResultPtr>& aResults = theFeature->results();
97 for (std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
98 aResIt != aResults.end(); ++aResIt) {
99 GeomShapePtr aCurShape = (*aResIt)->shape();
100 // first of all, check the point is within bounding box of the result
101 if (!aCurShape || !isPointWithinBB(thePoint, aCurShape, TOLERANCE))
103 // now, process all sub-shapes of the given type and check their inner points,
104 // but skip the case the selected type is COMPOUND and the shape is a list of sketch edges
105 // (it will be processed later)
106 std::shared_ptr<GeomAPI_PlanarEdges> aSketchEdges =
107 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aCurShape);
108 if (theShapeType != GeomAPI_Shape::COMPOUND || !aSketchEdges) {
109 ResultCompSolidPtr aCompSolid =
110 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
113 int aNbSolids = aCompSolid->numberOfSubs();
114 for (int i = 0; i < aNbSolids && !theSubshape; ++i) {
115 ResultPtr aSubResult = aCompSolid->subResult(i);
116 GeomShapePtr aSubSolid = aSubResult->shape();
117 if (aSubSolid && isPointWithinBB(thePoint, aSubSolid, TOLERANCE)) {
118 theSubshape = findSubShape(aSubSolid, theShapeType, thePoint, TOLERANCE);
120 theResult = aSubResult;
128 theSubshape = findSubShape(aCurShape, theShapeType, thePoint, TOLERANCE);
135 // special case for ResultConstruction if the FACE is selected
136 ResultConstructionPtr aResConstr =
137 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIt);
138 if (aResConstr && theShapeType >= GeomAPI_Shape::FACE) {
139 int aNbFaces = aResConstr->facesNum();
140 for (int aFaceInd = 0; aFaceInd < aNbFaces; ++aFaceInd) {
141 GeomFacePtr aCurFace = aResConstr->face(aFaceInd);
142 // check the point is within bounding box of the face
143 if (!isPointWithinBB(thePoint, aCurFace, TOLERANCE))
145 theSubshape = findSubShape(aCurFace, theShapeType, thePoint, TOLERANCE);
155 // next special case: the full sketch is selected
156 // the selection type is a COMPOUND
158 aSketchEdges->middlePoint()->distance(thePoint) < TOLERANCE) {
159 // select whole result
161 theSubshape = GeomShapePtr();
166 // one more special case: a vertex selected is a sketch point;
167 // it is not included into sketch result; thus, it is necessary
168 // to pass through the sketch sub-features and verify all points
169 if (!theResult && theShapeType == GeomAPI_Shape::VERTEX && !aResults.empty()) {
170 CompositeFeaturePtr aCF = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
171 std::shared_ptr<GeomAPI_PlanarEdges> aSketchEdges =
172 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aResults.front()->shape());
174 if (aSketchEdges && aCF) {
175 bool isContinue = true;
176 int aNbSubs = aCF->numberOfSubs();
177 for (int aSubInd = 0; aSubInd < aNbSubs && isContinue; ++aSubInd) {
178 FeaturePtr aSub = aCF->subFeature(aSubInd);
179 const std::list<ResultPtr>& aSubResults = aSub->results();
180 for (std::list<ResultPtr>::const_iterator aSRIt = aSubResults.begin();
181 aSRIt != aSubResults.end(); ++aSRIt) {
182 GeomShapePtr aCurShape = (*aSRIt)->shape();
183 theSubshape = findSubShape(aCurShape, theShapeType, thePoint, TOLERANCE);
185 theResult = aResults.front();
194 return (bool)theResult;
196 } // namespace ModelGeomAlgo_Shape