]> 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_CompositeFeature.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultCompSolid.h>
29 #include <ModelAPI_ResultConstruction.h>
30
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_Pnt.h>
33
34
35 #ifdef WIN32
36 #pragma warning(disable : 4996) // for sprintf
37 #endif
38
39 namespace ModelGeomAlgo_Shape
40 {
41   void shapesOfType(const FeaturePtr& theFeature,
42                     const GeomAPI_Shape::ShapeType& theType,
43                     std::set<ResultPtr>& theShapeResults)
44   {
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);
53     }
54   }
55
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)
60   {
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;
66   }
67
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)
73   {
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)
79         return *aSubIt;
80     }
81
82     // not found
83     return GeomShapePtr();
84   }
85
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)
91   {
92     static const double TOLERANCE = 1.e-7;
93
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))
102         continue;
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);
111         if (aCompSolid) {
112           // process solids
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);
119               if (theSubshape)
120                 theResult = aSubResult;
121             }
122           }
123           if (theSubshape)
124             break;
125         }
126
127         if (!theSubshape)
128           theSubshape = findSubShape(aCurShape, theShapeType, thePoint, TOLERANCE);
129         if (theSubshape) {
130           theResult = *aResIt;
131           break;
132         }
133       }
134
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))
144             continue;
145           theSubshape = findSubShape(aCurFace, theShapeType, thePoint, TOLERANCE);
146           if (theSubshape) {
147             theResult = *aResIt;
148             break;
149           }
150         }
151       }
152       if (theResult)
153         break;
154
155       // next special case: the full sketch is selected
156       // the selection type is a COMPOUND
157       if (aSketchEdges &&
158           aSketchEdges->middlePoint()->distance(thePoint) < TOLERANCE) {
159         // select whole result
160         theResult = *aResIt;
161         theSubshape = GeomShapePtr();
162         break;
163       }
164     }
165
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());
173
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);
184             if (theSubshape) {
185               theResult = aResults.front();
186               isContinue = false;
187               break;
188             }
189           }
190         }
191       }
192     }
193
194     return (bool)theResult;
195   }
196 } // namespace ModelGeomAlgo_Shape