Salome HOME
Merge branch 'V9_9_BR'
[modules/shaper.git] / src / GeomValidators / GeomValidators_ConstructionComposite.cpp
1 // Copyright (C) 2014-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_ConstructionComposite.h"
21
22 #include <Events_InfoMessage.h>
23
24 #include "ModelAPI_AttributeSelection.h"
25 #include "ModelAPI_ResultConstruction.h"
26 #include "ModelAPI_CompositeFeature.h"
27
28 bool GeomValidators_ConstructionComposite::isValid(const AttributePtr& theAttribute,
29                                                    const std::list<std::string>& /*theArguments*/,
30                                                    Events_InfoMessage& theError) const
31 {
32   bool aValid = true;
33   if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
34 // LCOV_EXCL_START
35     aValid = false;
36     theError = "The attribute with the %1 type is not processed";
37     theError.arg(theAttribute->attributeType());
38     return aValid;
39 // LCOV_EXCL_STOP
40   }
41
42   AttributeSelectionPtr aSelectionAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
43                                                                 (theAttribute);
44   ResultPtr aResult = aSelectionAttr->context();
45   // global selection should be ignored, the filter processes only selected sub-shapes
46   // that means, that the shape of the context result is equal to the shape value
47   ///*ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
48   if (!aResult.get()) {
49     aValid = false;
50     theError = "The result is empty";
51   }
52   else {
53     GeomShapePtr aShape = aSelectionAttr->value();
54     GeomShapePtr aShapePtr = aResult->shape();
55     // it is important to call isEqual of the shape of result.
56     // It is a GeomAPI_Vertex shape for the point. The shape of the parameter is
57     // GeomAPI_Shape. It is important to use the realization of the isEqual method from
58     // GeomAPI_Vertex class
59     if (!aShape.get()) {
60       // an empty shape is used in attribute selection if the shape of the result is equal to
61       // the selected shape, so according to the upper condition, the result is true
62       aValid = true;
63     }
64     else {
65       if (aShapePtr->isEqual(aShape)) {
66         aValid = true;
67       }
68       else {
69         ResultConstructionPtr aConstr =
70           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
71         if (!aConstr.get()) {
72           // non-construction results should be accepted by this filter, e.g. body results
73           aValid = true;
74         }
75         else {
76           // it provides selection only on composite features, construction without composite
77           // feature is not selectable.
78           FeaturePtr aFeature = ModelAPI_Feature::feature(aConstr);
79           CompositeFeaturePtr aComposite =
80             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
81           aValid = aComposite && aComposite->numberOfSubs() > 0;
82           if (!aValid)
83             theError = "Uses composite construction feature without sub-features.";
84         }
85       }
86     }
87   }
88   return aValid;
89 }