]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelGeomAlgo/ModelGeomAlgo_Shape.cpp
Salome HOME
Dump with geometrical selection
[modules/shaper.git] / src / ModelGeomAlgo / ModelGeomAlgo_Shape.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_Shape.h>
22
23 #include "ModelGeomAlgo_Shape.h"
24
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_ResultConstruction.h>
28
29 #include <GeomAPI_PlanarEdges.h>
30 #include <GeomAPI_Pnt.h>
31
32
33 #ifdef WIN32
34 #pragma warning(disable : 4996) // for sprintf
35 #endif
36
37 namespace ModelGeomAlgo_Shape
38 {
39   void shapesOfType(const FeaturePtr& theFeature,
40                     const GeomAPI_Shape::ShapeType& theType,
41                     std::set<ResultPtr>& theShapeResults)
42   {
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);
51     }
52   }
53
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)
58   {
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;
64   }
65
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)
71   {
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)
76         return *aSubIt;
77     }
78
79     // not found
80     return GeomShapePtr();
81   }
82
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)
88   {
89     static const double TOLERANCE = 1.e-7;
90
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))
98         continue;
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);
106       if (theSubshape) {
107         theResult = *aResIt;
108         break;
109       }
110
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))
120             continue;
121           theSubshape = findSubShape(aCurFace, theShapeType, thePoint, TOLERANCE);
122           if (theSubshape) {
123             theResult = *aResIt;
124             break;
125           }
126         }
127       }
128       if (theResult)
129         break;
130
131       // next special case: the full sketch is selected
132       // the selection type is a COMPOUND
133       if (aSketchEdges &&
134           aSketchEdges->middlePoint()->distance(thePoint) < TOLERANCE) {
135         // select whole result
136         theResult = *aResIt;
137         theSubshape = GeomShapePtr();
138         break;
139       }
140     }
141
142     return (bool)theResult;
143   }
144 } // namespace ModelGeomAlgo_Shape