]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Validators.cpp
Salome HOME
Issue #2563: CEA 2018-1 Common
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Validators.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 "FeaturesPlugin_Validators.h"
22
23 #include "FeaturesPlugin_Boolean.h"
24 #include "FeaturesPlugin_BooleanFuse.h"
25 #include "FeaturesPlugin_BooleanCommon.h"
26 #include "FeaturesPlugin_BooleanSmash.h"
27 #include "FeaturesPlugin_Union.h"
28
29 #include <Events_InfoMessage.h>
30
31 #include <ModelAPI_Attribute.h>
32 #include <ModelAPI_AttributeInteger.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_AttributeReference.h>
36 #include <ModelAPI_AttributeRefList.h>
37 #include <ModelAPI_Feature.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultConstruction.h>
40 #include <ModelAPI_Tools.h>
41
42 #include <GeomValidators_BodyShapes.h>
43 #include <GeomValidators_Face.h>
44 #include <GeomValidators_FeatureKind.h>
45 #include <GeomValidators_ShapeType.h>
46
47 #include <GeomAPI_DataMapOfShapeShape.h>
48 #include <GeomAPI_Lin.h>
49 #include <GeomAPI_PlanarEdges.h>
50 #include <GeomAPI_Pln.h>
51 #include <GeomAPI_ShapeExplorer.h>
52 #include <GeomAPI_ShapeIterator.h>
53
54 #include <GeomAlgoAPI_CompoundBuilder.h>
55 #include <GeomAlgoAPI_ShapeBuilder.h>
56 #include <GeomAlgoAPI_ShapeTools.h>
57 #include <GeomAlgoAPI_WireBuilder.h>
58
59 #define _USE_MATH_DEFINES
60 #include <math.h>
61
62 //==================================================================================================
63 bool FeaturesPlugin_ValidatorPipePath::isValid(const AttributePtr& theAttribute,
64                                                const std::list<std::string>& theArguments,
65                                                Events_InfoMessage& theError) const
66 {
67   AttributeSelectionPtr aPathAttrSelection =
68     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
69   if(!aPathAttrSelection.get()) {
70     theError = "Error: This validator can only work with path selector in \"Pipe\" feature.";
71     return false;
72   }
73
74   GeomShapePtr aPathShape = aPathAttrSelection->value();
75   ResultPtr aContext = aPathAttrSelection->context();
76   if(!aContext.get()) {
77     theError = "Error: Empty context.";
78     return false;
79   }
80   GeomShapePtr aContextShape = aContext->shape();
81   if(aPathShape.get() && aPathShape->shapeType() == GeomAPI_Shape::WIRE &&
82       !aPathShape->isEqual(aContextShape)) {
83     theError = "Error: Local selection of wires not allowed.";
84     return false;
85   }
86
87   return true;
88 }
89
90 //==================================================================================================
91 bool FeaturesPlugin_ValidatorPipeLocations::isValid(
92   const std::shared_ptr<ModelAPI_Feature>& theFeature,
93   const std::list<std::string>& theArguments,
94   Events_InfoMessage& theError) const
95 {
96   static const std::string aCreationMethodID = "creation_method";
97   static const std::string aBaseObjectsID = "base_objects";
98   static const std::string aLocationsID = "locations_objects";
99
100   if(theFeature->getKind() != "Pipe") {
101     theError = "Error: Feature \"%1\" does not supported by this validator.";
102     theError.arg(theFeature->getKind());
103     return false;
104   }
105
106   AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
107   if(!aCreationMethodAttr.get()) {
108     theError = "Error: Could not get \"%1\" attribute.";
109     theError.arg(aCreationMethodID);
110     return false;
111   }
112
113   if(aCreationMethodAttr->value() != "locations") {
114     return true;
115   }
116
117   AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
118   if(!aBaseObjectsSelectionList.get()) {
119     theError = "Error: Could not get \"%1\" attribute.";
120     theError.arg(aBaseObjectsID);
121     return false;
122   }
123
124   AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
125   if(!aLocationsSelectionList.get()) {
126     theError = "Error: Could not get \"%1\" attribute.";
127     theError.arg(aBaseObjectsID);
128     return false;
129   }
130
131   if(aLocationsSelectionList->size() > 0 &&
132      aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
133     theError = "Error: Number of locations should be the same as base objects.";
134     return false;
135   }
136
137   return true;
138 }
139
140 //==================================================================================================
141 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
142                                                         const std::list<std::string>& theArguments,
143                                                         Events_InfoMessage& theError) const
144 {
145   if(theArguments.empty()) {
146     theError = "Error: Validator parameters is empty.";
147     return false;
148   }
149
150   // Checking attribute.
151   if(!isValidAttribute(theAttribute, theArguments, theError)) {
152     if(theError.empty()) {
153       theError = "Error: Attribute contains unacceptable shape.";
154     }
155     return false;
156   }
157
158   GeomAPI_DataMapOfShapeShape aSelectedWiresFromObjects;
159   std::string anAttributeType = theAttribute->attributeType();
160   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
161     AttributeSelectionListPtr aListAttr =
162       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
163     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
164       AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
165       ResultPtr aContext = aSelectionAttr->context();
166       if(!aContext.get()) {
167         theError = "Error: Empty context.";
168         return false;
169       }
170
171       ResultConstructionPtr aResultConstruction =
172         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
173       if(!aResultConstruction.get()) {
174         // It is not a result construction.
175         // If shape is compound check that it contains only faces and edges.
176         GeomShapePtr aShape = aSelectionAttr->value();
177         if(!aShape.get()) {
178           aShape = aContext->shape();
179         }
180
181         if(aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
182           for(GeomAPI_ShapeIterator anIt(aShape); anIt.more(); anIt.next()) {
183             GeomShapePtr aSubShape = anIt.current();
184             if(aSubShape->shapeType() != GeomAPI_Shape::EDGE
185                 && aSubShape->shapeType() != GeomAPI_Shape::FACE) {
186               theError = "Error: Compound should contain only faces and edges.";
187               return false;
188             }
189           }
190         }
191
192         continue;
193       }
194
195       GeomShapePtr aShape = aSelectionAttr->value();
196       GeomShapePtr aContextShape = aResultConstruction->shape();
197       if(!aShape.get()) {
198         // Whole sketch selected.
199         continue;
200       } else {
201         // Object from sketch selected.
202         for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
203           GeomShapePtr aWire = anExp.current();
204           if(aWire->orientation() != GeomAPI_Shape::FORWARD) {
205             theError = "Error: Wire with wrong orientation selected.";
206             return false;
207           }
208
209           if(aSelectedWiresFromObjects.isBound(aWire)) {
210             theError =
211               "Error: Objects with such wire already selected. Don't allow to select this object.";
212             return false;
213           }
214
215           aSelectedWiresFromObjects.bind(aWire, aWire);
216         }
217       }
218     }
219   }
220
221   return true;
222 }
223
224 //==================================================================================================
225 bool FeaturesPlugin_ValidatorBaseForGenerationSketchOrSketchObjects::isValid(
226   const std::shared_ptr<ModelAPI_Feature>& theFeature,
227   const std::list<std::string>& theArguments,
228   Events_InfoMessage& theError) const
229 {
230   const std::string aBaseObjectsID = theArguments.front();
231
232   AttributeSelectionListPtr aListAttr = theFeature->selectionList(aBaseObjectsID);
233   if(!aListAttr.get()) {
234     theError = "Error: Could not get \"%1\" attribute.";
235     theError.arg(aBaseObjectsID);
236     return false;
237   }
238
239   std::set<ResultConstructionPtr> aSelectedSketches;
240   std::set<ResultConstructionPtr> aSelectedSketchesFromObjects;
241
242   for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
243     AttributeSelectionPtr aSelectionAttr = aListAttr->value(anIndex);
244     ResultPtr aContext = aSelectionAttr->context();
245     if(!aContext.get()) {
246       theError = "Error: Empty context.";
247       return false;
248     }
249
250     ResultConstructionPtr aResultConstruction =
251       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
252     if(!aResultConstruction.get()) {
253       // It is not a result construction.
254       continue;
255     }
256
257     GeomShapePtr aShape = aSelectionAttr->value();
258     GeomShapePtr aContextShape = aResultConstruction->shape();
259     if(!aShape.get()) {
260       // Whole sketch selected.
261       aSelectedSketches.insert(aResultConstruction);
262     } else {
263       // Object from sketch selected.
264       aSelectedSketchesFromObjects.insert(aResultConstruction);
265     }
266   }
267
268
269   for(std::set<ResultConstructionPtr>::const_iterator anIt = aSelectedSketches.cbegin();
270       anIt != aSelectedSketches.cend();
271       ++anIt) {
272     ResultConstructionPtr aResultConstruction = *anIt;
273     if(aSelectedSketchesFromObjects.find(aResultConstruction) !=
274         aSelectedSketchesFromObjects.cend()) {
275       theError = "Sketch and objects from it can not be selected at the same time.";
276       return false;
277     }
278   }
279
280   return true;
281 }
282
283 //==================================================================================================
284 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
285                                                         const std::list<std::string>& theArguments,
286                                                         Events_InfoMessage& theError) const
287 {
288   if(!theAttribute.get()) {
289     theError = "Error: Empty attribute.";
290     return false;
291   }
292
293   std::string anAttributeType = theAttribute->attributeType();
294   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
295     AttributeSelectionListPtr aListAttr =
296       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
297     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
298       // If at least one attribute is invalid, the result is false.
299       if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
300         return false;
301       }
302     }
303   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
304     // Getting context.
305     AttributeSelectionPtr anAttr =
306       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
307     ResultPtr aContext = anAttr->context();
308     if(!aContext.get()) {
309       theError = "Error: Attribute have empty context.";
310       return false;
311     }
312
313     GeomShapePtr aShape = anAttr->value();
314     GeomShapePtr aContextShape = aContext->shape();
315     if(!aShape.get()) {
316       aShape = aContextShape;
317     }
318     if(!aShape.get()) {
319       theError = "Error: Empty shape selected";
320       return false;
321     }
322
323     ResultConstructionPtr aConstruction =
324       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
325     if(aConstruction.get()) {
326       // Construciotn selected. Check that is is not infinite.
327       if(aConstruction->isInfinite()) {
328         theError = "Error: Infinite constructions is not allowed as base.";
329         return false;
330       }
331
332       if(aShape->isEqual(aContextShape)) {
333         // Whole construction selected. Check that it have faces.
334         if(aConstruction->facesNum() > 0) {
335           return true;
336         }
337       } else {
338         // Shape on construction selected. Check that it is a face or wire.
339         if(aShape->shapeType() == GeomAPI_Shape::WIRE ||
340            aShape->shapeType() == GeomAPI_Shape::FACE) {
341           return true;
342         }
343       }
344
345       return false;
346     }
347
348     if(!aShape->isEqual(aContextShape)) {
349       // Local selection on body does not allowed.
350       theError =
351         "Error: Selected shape is in the local selection. Only global selection is allowed.";
352       return false;
353     }
354
355     // Check that object is a shape with allowed type.
356     GeomValidators_ShapeType aShapeTypeValidator;
357     if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
358       theError = "Error: Selected shape has unacceptable type. Acceptable types are: faces or "
359                  "wires on sketch, whole sketch(if it has at least one face), "
360                  "and whole objects with shape types: %1";
361       std::string anArgumentString;
362       for(auto anIt = theArguments.cbegin(); anIt != theArguments.cend(); ++anIt) {
363         if (!anArgumentString.empty())
364           anArgumentString += ", ";
365         anArgumentString += *anIt;
366       }
367       theError.arg(anArgumentString);
368       return false;
369     }
370
371   } else {
372     theError = "Error: Attribute \"%1\" does not supported by this validator.";
373     theError.arg(anAttributeType);
374     return false;
375   }
376
377   return true;
378 }
379
380 //==================================================================================================
381 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
382                                                         const std::list<std::string>& theArguments,
383                                                         Events_InfoMessage& theError) const
384 {
385   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
386     theError = "Error: The attribute with the %1 type is not processed";
387     theError.arg(theAttribute->attributeType());
388     return false;
389   }
390   if (theArguments.size() != 2) {
391     theError = "Error: Wrong parameters in XML definition for %1 type";
392     theError.arg(theAttribute->attributeType());
393     return false;
394   }
395   // first argument is for the base attribute, second - for skipping feature kind
396   std::list<std::string>::const_iterator anIt = theArguments.begin();
397   std::string aBaseAttributeId = *anIt;
398   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
399   AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
400   if (!aBaseAttribute.get()) {
401     theError = "Wrong parameters in XML definition for %1 type";
402     theError.arg(theAttribute->attributeType());
403     return false;
404   }
405   if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
406     // this validator is not necessary anymore
407     return true;
408
409   anIt++;
410   std::string aFeatureAttributeKind = *anIt;
411   GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
412   // check whether the selection is on the sketch
413   std::list<std::string> anArguments;
414   anArguments.push_back(aFeatureAttributeKind);
415
416   bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
417   bool aPlanarFace = false;
418   // check if selection has Face selected
419   GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
420   anArguments.clear();
421   anArguments.push_back("face");
422   aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
423
424   bool aValid = !aFeatureKind && aPlanarFace;
425   return aValid;
426 }
427
428 //==================================================================================================
429 bool FeaturesPlugin_ValidatorExtrusionDir::isValid(
430                                                const std::shared_ptr<ModelAPI_Feature>& theFeature,
431                                                const std::list<std::string>& theArguments,
432                                                Events_InfoMessage& theError) const
433 {
434   if(theArguments.size() != 2) {
435     theError = "Error: Validator should be used with 2 parameters for extrusion.";
436     return false;
437   }
438
439   std::list<std::string>::const_iterator
440     anArgsIt = theArguments.begin(), aLast = theArguments.end();
441
442   AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
443   ++anArgsIt;
444
445   GeomShapePtr aDirShape;
446   AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
447   if(aSelAttr.get()) {
448     aDirShape = aSelAttr->value();
449     if(!aDirShape.get()) {
450       ResultPtr aContext = aSelAttr->context();
451       if(aContext.get()) {
452         aDirShape = aContext->shape();
453       }
454     }
455   }
456
457   if(!aDirShape.get()) {
458     // Check that dir can be empty.
459     if(!isShapesCanBeEmpty(aCheckAttribute, theError)) {
460       theError = "Error: Base objects list contains vertex or edge, so attribute \"%1\" "
461                  "can not be used with default value. Select direction for extrusion.";
462       theError.arg(*anArgsIt);
463       return false;
464     } else {
465       return true;
466     }
467   }
468
469   std::shared_ptr<GeomAPI_Edge> aDirEdge(new GeomAPI_Edge(aDirShape));
470
471   // If faces selected check that direction not parallel with them.
472   AttributeSelectionListPtr aListAttr =
473     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aCheckAttribute);
474   for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
475     AttributeSelectionPtr anAttr = aListAttr->value(anIndex);
476     GeomShapePtr aShapeInList = anAttr->value();
477     if(!aShapeInList.get()) {
478       aShapeInList = anAttr->context()->shape();
479     }
480     bool isParallel = true;
481     if(aShapeInList->shapeType() == GeomAPI_Shape::FACE ||
482        aShapeInList->shapeType() == GeomAPI_Shape::SHELL) {
483       for(GeomAPI_ShapeExplorer
484           anExp(aShapeInList, GeomAPI_Shape::FACE); anExp.more(); anExp.next()) {
485         std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(anExp.current()));
486         isParallel = GeomAlgoAPI_ShapeTools::isParallel(aDirEdge, aFace);
487         if(isParallel) {
488           break;
489         }
490       }
491     } else if(aShapeInList->shapeType() == GeomAPI_Shape::COMPOUND) {
492       std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges =
493         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aShapeInList);
494       if(aPlanarEdges.get()) {
495         std::shared_ptr<GeomAPI_Dir> aSketchDir = aPlanarEdges->norm();
496         if(aDirEdge->isLine()) {
497           std::shared_ptr<GeomAPI_Dir> aDir = aDirEdge->line()->direction();
498           isParallel = fabs(aSketchDir->angle(aDir) - M_PI / 2.0) < 10e-7;
499         } else {
500           isParallel = false;
501         }
502       } else {
503         isParallel = false;
504       }
505     } else {
506       isParallel = false;
507     }
508     if(isParallel) {
509       theError =
510         "Error: Direction is parallel to one of the selected face or face on selected shell.";
511       return false;
512     }
513   }
514
515   return true;
516 }
517
518 //==================================================================================================
519 bool FeaturesPlugin_ValidatorExtrusionDir::isShapesCanBeEmpty(const AttributePtr& theAttribute,
520                                                               Events_InfoMessage& theError) const
521 {
522   if(!theAttribute.get()) {
523     return true;
524   }
525
526   std::string anAttributeType = theAttribute->attributeType();
527   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
528     AttributeSelectionListPtr aListAttr =
529       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
530     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
531       // If at least one attribute is invalid, the result is false.
532       if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
533         return false;
534       }
535     }
536   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
537     // Getting context.
538     AttributeSelectionPtr anAttr =
539       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
540     ResultPtr aContext = anAttr->context();
541     if(!aContext.get()) {
542       return false;
543     }
544
545     GeomShapePtr aShape = anAttr->value();
546     GeomShapePtr aContextShape = aContext->shape();
547     if(!aShape.get()) {
548       aShape = aContextShape;
549     }
550     if(!aShape.get()) {
551       return false;
552     }
553
554     if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
555        aShape->shapeType() == GeomAPI_Shape::EDGE ||
556        !aShape->isPlanar()) {
557       return false;
558     }
559   } else {
560     return false;
561   }
562
563   return true;
564 }
565
566 //==================================================================================================
567 bool FeaturesPlugin_ValidatorBooleanSelection::isValid(const AttributePtr& theAttribute,
568                                                        const std::list<std::string>& theArguments,
569                                                        Events_InfoMessage& theError) const
570 {
571   AttributeSelectionListPtr anAttrSelectionList =
572     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
573   if(!anAttrSelectionList.get()) {
574     theError =
575       "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
576     return false;
577   }
578   std::shared_ptr<FeaturesPlugin_Boolean> aFeature =
579     std::dynamic_pointer_cast<FeaturesPlugin_Boolean>(theAttribute->owner());
580   FeaturesPlugin_Boolean::OperationType anOperationType = aFeature->operationType();
581
582   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
583     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
584     if(!anAttrSelection.get()) {
585       theError = "Error: Empty attribute selection.";
586       return false;
587     }
588     ResultPtr aContext = anAttrSelection->context();
589     if(!aContext.get()) {
590       FeaturePtr aContFeat = anAttrSelection->contextFeature();
591       if (!aContFeat.get()) {
592         theError = "Error: Empty selection context.";
593         return false;
594       }
595     }
596     ResultConstructionPtr aResultConstruction =
597       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
598     if(aResultConstruction.get()) {
599       if (anOperationType != FeaturesPlugin_Boolean::BOOL_FILL
600           || theAttribute->id() != FeaturesPlugin_Boolean::TOOL_LIST_ID()) {
601         theError = "Error: Result construction not allowed for selection.";
602         return false;
603       }
604     }
605     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
606     if(!aShape.get()) {
607       GeomShapePtr aContextShape = aContext->shape();
608       aShape = aContextShape;
609     }
610     if(!aShape.get()) {
611       theError = "Error: Empty shape.";
612       return false;
613     }
614     if (aContext.get() && !aShape->isEqual(aContext->shape())) {
615       theError = "Error: Local selection not allowed.";
616       return false;
617     }
618
619     GeomAPI_Shape::ShapeType aShapeType = aShape->shapeType();
620     std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
621     if(anOperationType == FeaturesPlugin_Boolean::BOOL_FUSE) {
622       anAllowedTypes.insert(GeomAPI_Shape::EDGE);
623       anAllowedTypes.insert(GeomAPI_Shape::FACE);
624       anAllowedTypes.insert(GeomAPI_Shape::SOLID);
625       anAllowedTypes.insert(GeomAPI_Shape::COMPSOLID);
626       anAllowedTypes.insert(GeomAPI_Shape::COMPOUND);
627     } else if (anOperationType == FeaturesPlugin_Boolean::BOOL_FILL
628                || anOperationType == FeaturesPlugin_Boolean::BOOL_CUT)
629     {
630       anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
631       anAllowedTypes.insert(GeomAPI_Shape::EDGE);
632       anAllowedTypes.insert(GeomAPI_Shape::WIRE);
633       anAllowedTypes.insert(GeomAPI_Shape::FACE);
634       anAllowedTypes.insert(GeomAPI_Shape::SHELL);
635       anAllowedTypes.insert(GeomAPI_Shape::SOLID);
636       anAllowedTypes.insert(GeomAPI_Shape::COMPSOLID);
637       anAllowedTypes.insert(GeomAPI_Shape::COMPOUND);
638     } else {
639       anAllowedTypes.insert(GeomAPI_Shape::SOLID);
640       anAllowedTypes.insert(GeomAPI_Shape::COMPSOLID);
641       anAllowedTypes.insert(GeomAPI_Shape::COMPOUND);
642     }
643
644     if(anAllowedTypes.find(aShapeType) == anAllowedTypes.end()
645       || (aResultConstruction.get() && aShapeType != GeomAPI_Shape::FACE)) {
646       theError = "Error: Selected shape has the wrong type.";
647       return false;
648     }
649
650   }
651
652   return true;
653 }
654
655 //==================================================================================================
656 bool FeaturesPlugin_ValidatorFilletSelection::isValid(const AttributePtr& theAttribute,
657                                                       const std::list<std::string>& theArguments,
658                                                       Events_InfoMessage& theError) const
659 {
660   AttributeSelectionListPtr anAttrSelectionList =
661     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
662   if(!anAttrSelectionList.get()) {
663     theError =
664       "Error: This validator can only work with selection list attributes in \"Fillet\" feature.";
665     return false;
666   }
667
668   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
669   // Check all selected entities are sub-shapes of single solid
670   GeomShapePtr aBaseSolid;
671   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
672     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
673     if(!anAttrSelection.get()) {
674       theError = "Error: Empty attribute selection.";
675       return false;
676     }
677     ResultPtr aContext = anAttrSelection->context();
678     if(!aContext.get()) {
679       theError = "Error: Empty selection context.";
680       return false;
681     }
682
683     ResultBodyPtr aContextOwner = ModelAPI_Tools::bodyOwner(aContext);
684     GeomShapePtr anOwner = aContextOwner.get() ? aContextOwner->shape() : aContext->shape();
685
686     if (anOwner->shapeType() != GeomAPI_Shape::SOLID &&
687         anOwner->shapeType() != GeomAPI_Shape::COMPSOLID) {
688       theError = "Error: Not all selected shapes are sub-shapes of solids";
689       return false;
690     }
691
692     if (!aBaseSolid)
693       aBaseSolid = anOwner;
694     else if (!aBaseSolid->isEqual(anOwner)) {
695       theError = "Error: Sub-shapes of different solids have been selected.";
696       return false;
697     }
698   }
699
700   return true;
701 }
702
703 //==================================================================================================
704 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
705                                                        const std::list<std::string>& theArguments,
706                                                        Events_InfoMessage& theError) const
707 {
708   AttributeSelectionListPtr anAttrSelectionList =
709     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
710   if(!anAttrSelectionList.get()) {
711     theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
712     return false;
713   }
714
715   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
716     AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
717
718     //GeomValidators_BodyShapes aBodyValidator;
719     //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
720     //  continue;
721     //}
722
723     GeomValidators_FeatureKind aFeatureKindValidator;
724     if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
725       continue;
726     }
727
728     ResultPtr aContext = aSelectAttr->context();
729     ResultConstructionPtr aResultConstruction =
730       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
731     if(aResultConstruction.get()) {
732       theError = "Error: Only body shapes and construction planes are allowed for selection.";
733       return false;
734     }
735
736     ResultBodyPtr aResultBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
737     if(aResultBody.get()) {
738       continue;
739     }
740     FeaturePtr aResultFeature = aSelectAttr->contextFeature();
741     if(aResultFeature.get()) {
742       bool aOkRes = false;
743       std::list<ResultPtr>::const_iterator aFRes = aResultFeature->results().cbegin();
744       for(; aFRes != aResultFeature->results().cend() && !aOkRes; aFRes++) {
745         ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aFRes);
746         if (aBody.get() && !aBody->isDisabled())
747           aOkRes = true;
748       }
749       if (aOkRes)
750         continue;
751     }
752
753     theError = "Error: Only body shapes and construction planes are allowed for selection.";
754     return false;
755   }
756
757   theError = "";
758   return true;
759 }
760
761 //==================================================================================================
762 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
763                                                      const std::list<std::string>& theArguments,
764                                                      Events_InfoMessage& theError) const
765 {
766   AttributeSelectionListPtr aSubShapesAttrList =
767     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
768   if(!aSubShapesAttrList.get()) {
769     theError =
770       "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
771     return false;
772   }
773
774   static const std::string aBaseShapeID = "base_shape";
775   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
776   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
777
778   if(!aShapeAttrSelection.get()) {
779     theError = "Error: Could not get \"%1\" attribute.";
780     theError.arg(aBaseShapeID);
781     return false;
782   }
783
784   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
785   ResultPtr aContext = aShapeAttrSelection->context();
786   if(!aContext.get()) {
787     theError = "Error: Empty context.";
788     return false;
789   }
790   if(!aBaseShape.get()) {
791     aBaseShape = aContext->shape();
792   }
793   if(!aBaseShape.get()) {
794     theError = "Error: Empty base shape.";
795     return false;
796   }
797
798   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
799     bool isSameFound = false;
800     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
801     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
802     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
803       if(anIt.current()->isEqual(aShapeToAdd)) {
804         isSameFound = true;
805         break;
806       }
807     }
808     if(!isSameFound) {
809       theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
810       return false;
811     }
812   }
813
814   return true;
815 }
816
817 //==================================================================================================
818 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(
819   const std::shared_ptr<ModelAPI_Feature>& theFeature,
820   const std::list<std::string>& theArguments,
821   Events_InfoMessage& theError) const
822 {
823   static const std::string aBaseShapeID = "base_shape";
824   static const std::string aSubShapesID = "subshapes_to_keep";
825
826   if(theFeature->getKind() != "Remove_SubShapes") {
827     theError = "Error: Feature \"%1\" does not supported by this validator.";
828     theError.arg(theFeature->getKind());
829     return false;
830   }
831
832   AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
833   if(!aShapeAttrSelection.get()) {
834     theError = "Error: Could not get \"%1\" attribute.";
835     theError.arg(aBaseShapeID);
836     return false;
837   }
838
839   AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
840   if(!aSubShapesAttrList.get()) {
841     theError = "Error: Could not get \"%1\" attribute.";
842     theError.arg(aSubShapesID);
843     return false;
844   }
845
846   // Copy base shape.
847   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
848   if(!aBaseShape.get()) {
849     theError = "Error: Base shape is empty.";
850     return false;
851   }
852   GeomShapePtr aResultShape = aBaseShape->emptyCopied();
853
854   if (aSubShapesAttrList->size() == 0) {
855     theError = "Error: Resulting shape is not valid.";
856     return false;
857   }
858
859   // Copy sub-shapes from list to new shape.
860   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
861     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
862     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
863     GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
864   }
865
866   // Check new shape.
867   if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
868     theError = "Error: Resulting shape is not valid.";
869     return false;
870   }
871
872   return true;
873 }
874
875 //==================================================================================================
876 bool FeaturesPlugin_ValidatorUnionSelection::isValid(const AttributePtr& theAttribute,
877                                                      const std::list<std::string>& theArguments,
878                                                      Events_InfoMessage& theError) const
879 {
880   AttributeSelectionListPtr aBaseObjectsAttrList =
881     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
882   if(!aBaseObjectsAttrList.get()) {
883     theError = "Error: This validator can only work with selection list in \"%1\" feature.";
884     theError.arg(FeaturesPlugin_Union::ID());
885     return false;
886   }
887
888   for(int anIndex = 0; anIndex < aBaseObjectsAttrList->size(); ++anIndex) {
889     bool isSameFound = false;
890     AttributeSelectionPtr anAttrSelectionInList = aBaseObjectsAttrList->value(anIndex);
891     ResultPtr aContext = anAttrSelectionInList->context();
892     if (!aContext.get()) {
893       theError = "Error: selection is invalid.";
894       return false;
895     }
896
897     ResultConstructionPtr aConstruction =
898       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
899     if(aConstruction.get()) {
900       theError = "Error: Result construction not allowed for selection.";
901       return false;
902     }
903
904     GeomShapePtr aShape = anAttrSelectionInList->value();
905     GeomShapePtr aContextShape = aContext->shape();
906     if (aShape.get() && aContextShape.get() && !aContextShape->isEqual(aShape)) {
907       theError = "Error: Local selection not allowed.";
908       return false;
909     }
910
911     ResultBodyPtr aResult =
912       std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
913     if(!aResult.get()) {
914       continue;
915     }
916
917     if(aResult->numberOfSubs() > 0) {
918       theError = "Error: Whole compsolids not allowed for selection.";
919       return false;
920     }
921   }
922
923   return true;
924 }
925
926 //==================================================================================================
927 bool FeaturesPlugin_ValidatorUnionArguments::isValid(
928   const std::shared_ptr<ModelAPI_Feature>& theFeature,
929   const std::list<std::string>& theArguments,
930   Events_InfoMessage& theError) const
931 {
932   // Check feature kind.
933   if(theFeature->getKind() != FeaturesPlugin_Union::ID()) {
934     theError = "Error: This validator supports only \"%1\" feature.";
935     theError.arg(FeaturesPlugin_Union::ID());
936     return false;
937   }
938
939   // Get base objects attribute list.
940   AttributeSelectionListPtr aBaseObejctsAttrList =
941     theFeature->selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
942   if(!aBaseObejctsAttrList.get()) {
943     theError = "Error: Could not get \"%1\" attribute.";
944     theError.arg(FeaturesPlugin_Union::BASE_OBJECTS_ID());
945     return false;
946   }
947
948   // Get all shapes.
949   GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::COMPSOLID;
950   ListOfShape aBaseShapesList;
951   for(int anIndex = 0; anIndex < aBaseObejctsAttrList->size(); ++anIndex) {
952     AttributeSelectionPtr anAttrSelectionInList = aBaseObejctsAttrList->value(anIndex);
953     GeomShapePtr aShape = anAttrSelectionInList->value();
954     if (!aShape.get()) {
955       continue;
956     }
957     aBaseShapesList.push_back(aShape);
958     aType = aShape->shapeType() == GeomAPI_Shape::FACE ? GeomAPI_Shape::SHELL :
959                                                          GeomAPI_Shape::COMPSOLID;
960   }
961
962   // Make compound and find connected.
963   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseShapesList);
964   ListOfShape aCombined, aFree;
965   GeomAlgoAPI_ShapeTools::combineShapes(
966     aCompound,
967     aType,
968     aCombined,
969     aFree);
970
971   if(aFree.size() > 0 || aCombined.size() > 1) {
972     theError = "Error: Not all shapes have shared topology.";
973     return false;
974   }
975
976   return true;
977 }
978
979 bool FeaturesPlugin_ValidatorConcealedResult::isValid(const AttributePtr& theAttribute,
980                                             const std::list<std::string>& theArguments,
981                                             Events_InfoMessage& theError) const
982 {
983   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
984     theError = "Error: The attribute with the %1 type is not processed";
985     theError.arg(theAttribute->attributeType());
986     return false;
987   }
988
989   AttributeReferencePtr aRefAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeReference>
990                                                                                (theAttribute);
991   ObjectPtr aRefObject = aRefAttribute->value();
992   if (!aRefObject.get()) {
993     theError = "Error: Empty feature.";
994     return false;
995   }
996
997   FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefObject);
998   if (!aRefFeature.get()) {
999     theError = "Error: Empty feature.";
1000     return false;
1001   }
1002   std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1003   ModelAPI_Tools::getConcealedResults(aRefFeature, aResults);
1004
1005   size_t aConcealedResults = aResults.size();
1006   if (!aConcealedResults && !theArguments.empty()) {
1007     // find if these results are touched by the feature in another attribute
1008     std::list<std::string>::const_iterator anIt = theArguments.begin();
1009     std::string aRecoveredList = *anIt;
1010     if (!aRecoveredList.empty()) {
1011       std::shared_ptr<ModelAPI_AttributeRefList> aParameterList =
1012                                  theAttribute->owner()->data()->reflist(aRecoveredList);
1013       if (aParameterList.get())
1014         aConcealedResults = aParameterList->size();
1015     }
1016   }
1017
1018   if (aConcealedResults == 0)
1019     theError = "Error: No concealed results.";
1020
1021   return theError.empty();
1022 }
1023
1024 bool FeaturesPlugin_ValidatorCircular::isValid(const AttributePtr& theAttribute,
1025                                                const std::list<std::string>& theArguments,
1026                                                Events_InfoMessage& theError) const
1027 {
1028   static std::list<std::string> aEdgeArg(1, "circle");
1029   static std::list<std::string> aFaceArg(1, "cylinder");
1030
1031   Events_InfoMessage aError;
1032   bool isValid = GeomValidators_ShapeType().isValid(theAttribute, aEdgeArg, aError);
1033   if (!isValid) {
1034     isValid = GeomValidators_Face().isValid(theAttribute, aFaceArg, aError);
1035     if (!isValid)
1036       theError = "The shape neither circle nor cylinder";
1037   }
1038   return isValid;
1039 }
1040
1041 //=================================================================================================
1042 bool FeaturesPlugin_ValidatorBooleanArguments::isValid(
1043   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1044   const std::list<std::string>& theArguments,
1045   Events_InfoMessage& theError) const
1046 {
1047   if (theArguments.size() != 2)
1048   {
1049     theError = "Wrong number of arguments (expected 2).";
1050     return false;
1051   }
1052
1053   int anObjectsNb = 0, aToolsNb = 0;
1054   //int anOperationType = 0;
1055
1056   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1057
1058   bool isAllInSameCompSolid = true;
1059   ResultBodyPtr aCompSolid;
1060
1061   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1062   if (anAttrSelList)
1063   {
1064     anObjectsNb = anAttrSelList->size();
1065     for (int anIndex = 0; anIndex < anObjectsNb; ++anIndex)
1066     {
1067       AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1068       ResultPtr aContext = anAttr->context();
1069       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1070       if (aResCompSolidPtr.get())
1071       {
1072         if (aCompSolid.get())
1073         {
1074           isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1075         }
1076         else
1077         {
1078           aCompSolid = aResCompSolidPtr;
1079         }
1080       }
1081       else
1082       {
1083         isAllInSameCompSolid = false;
1084         break;
1085       }
1086     }
1087   }
1088   anIt++;
1089
1090
1091   anAttrSelList = theFeature->selectionList(*anIt);
1092   if (anAttrSelList)
1093   {
1094     aToolsNb = anAttrSelList->size();
1095     if (isAllInSameCompSolid)
1096     {
1097       for (int anIndex = 0; anIndex < aToolsNb; ++anIndex)
1098       {
1099         AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1100         ResultPtr aContext = anAttr->context();
1101         ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1102         if (aResCompSolidPtr.get())
1103         {
1104           if (aCompSolid.get())
1105           {
1106             isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1107           }
1108           else
1109           {
1110             aCompSolid = aResCompSolidPtr;
1111           }
1112         }
1113         else
1114         {
1115           isAllInSameCompSolid = false;
1116           break;
1117         }
1118       }
1119     }
1120   }
1121   anIt++;
1122
1123   std::shared_ptr<FeaturesPlugin_Boolean> aFeature =
1124     std::dynamic_pointer_cast<FeaturesPlugin_Boolean>(theFeature);
1125   FeaturesPlugin_Boolean::OperationType anOperationType = aFeature->operationType();
1126
1127   if (anOperationType == FeaturesPlugin_Boolean::BOOL_FUSE)
1128   {
1129     // Fuse operation
1130     if (anObjectsNb + aToolsNb < 2)
1131     {
1132       theError = "Not enough arguments for Fuse operation.";
1133       return false;
1134     }
1135     else if (isAllInSameCompSolid)
1136     {
1137       theError = "Operations only between sub-shapes of the same shape not allowed.";
1138       return false;
1139     }
1140   }
1141   else
1142   {
1143     if (anObjectsNb < 1)
1144     {
1145       theError = "Objects not selected.";
1146       return false;
1147     }
1148     if (aToolsNb < 1)
1149     {
1150       theError = "Tools not selected.";
1151       return false;
1152     }
1153     if (isAllInSameCompSolid)
1154     {
1155       theError = "Operations only between sub-shapes of the same shape not allowed.";
1156       return false;
1157     }
1158   }
1159
1160   return true;
1161 }
1162
1163 //=================================================================================================
1164 bool FeaturesPlugin_ValidatorBooleanArguments::isNotObligatory(std::string theFeature,
1165                                                                std::string theAttribute)
1166 {
1167   if (theAttribute == "main_objects" || theAttribute == "tool_objects")
1168   {
1169     return true;
1170   }
1171
1172   return false;
1173 }
1174
1175 //==================================================================================================
1176 bool FeaturesPlugin_ValidatorBooleanSmashSelection::isValid(
1177   const AttributePtr& theAttribute,
1178   const std::list<std::string>& theArguments,
1179   Events_InfoMessage& theError) const
1180 {
1181   std::shared_ptr<FeaturesPlugin_BooleanSmash> aFeature =
1182     std::dynamic_pointer_cast<FeaturesPlugin_BooleanSmash>(theAttribute->owner());
1183
1184   AttributeSelectionListPtr anAttrSelectionList =
1185     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1186   if (!aFeature.get() || !anAttrSelectionList.get()) {
1187     theError =
1188       "Error: Validator used in wrong feature or attribute";
1189     return false;
1190   }
1191
1192   AttributeSelectionListPtr anOtherAttrSelectionList;
1193   if (theAttribute->id() == FeaturesPlugin_BooleanSmash::OBJECT_LIST_ID()) {
1194     anOtherAttrSelectionList =
1195       aFeature->selectionList(FeaturesPlugin_BooleanSmash::TOOL_LIST_ID());
1196   } else {
1197     anOtherAttrSelectionList =
1198       aFeature->selectionList(FeaturesPlugin_BooleanSmash::OBJECT_LIST_ID());
1199   }
1200
1201   GeomAPI_Shape::ShapeType aSelectedShapesType = GeomAPI_Shape::SHAPE;
1202   GeomAPI_DataMapOfShapeShape aSelectedCompSolidsInOtherList;
1203   GeomPlanePtr aFacesPln;
1204
1205   for (int anIndex = 0; anIndex < anOtherAttrSelectionList->size(); ++anIndex) {
1206     AttributeSelectionPtr anAttrSelection = anOtherAttrSelectionList->value(anIndex);
1207     ResultPtr aContext = anAttrSelection->context();
1208     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1209     GeomShapePtr aContextShape = aContext->shape();
1210     if (!aShape.get()) {
1211       aShape = aContextShape;
1212     }
1213
1214     if (aShape->isSolid() || aShape->isCompSolid()) {
1215       aSelectedShapesType = GeomAPI_Shape::SOLID;
1216       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1217       if (aResCompSolidPtr.get()) {
1218         GeomShapePtr aCompSolidShape = aResCompSolidPtr->shape();
1219         aSelectedCompSolidsInOtherList.bind(aCompSolidShape, aCompSolidShape);
1220       }
1221     } else {
1222       aSelectedShapesType = GeomAPI_Shape::FACE;
1223       GeomAPI_Face aFace(aShape);
1224       aFacesPln = aFace.getPlane();
1225       break;
1226     }
1227   }
1228
1229   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1230     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1231     if (!anAttrSelection.get()) {
1232       theError = "Error: Empty attribute selection.";
1233       return false;
1234     }
1235     ResultPtr aContext = anAttrSelection->context();
1236     if (!aContext.get()) {
1237       theError = "Error: Empty selection context.";
1238       return false;
1239     }
1240     ResultConstructionPtr aResultConstruction =
1241       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1242     if (aResultConstruction.get()) {
1243       theError = "Error: Result construction not allowed for selection.";
1244       return false;
1245     }
1246     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1247     GeomShapePtr aContextShape = aContext->shape();
1248     if (!aShape.get()) {
1249       aShape = aContextShape;
1250     }
1251     if (!aShape.get()) {
1252       theError = "Error: Empty shape.";
1253       return false;
1254     }
1255     if (!aShape->isEqual(aContextShape)) {
1256       theError = "Error: Local selection not allowed.";
1257       return false;
1258     }
1259
1260     if (aSelectedShapesType == GeomAPI_Shape::SHAPE) {
1261       // Other list is empty.
1262       if (aShape->isSolid() || aShape->isCompSolid()) {
1263         aSelectedShapesType = GeomAPI_Shape::SOLID;
1264       } else {
1265         aSelectedShapesType = GeomAPI_Shape::FACE;
1266         GeomAPI_Face aFace(aShape);
1267         aFacesPln = aFace.getPlane();
1268
1269         if (!aFacesPln.get()) {
1270           theError = "Error: Only planar faces allowed.";
1271           return false;
1272         }
1273       }
1274
1275       continue;
1276     } else if (aSelectedShapesType == GeomAPI_Shape::SOLID) {
1277       if (!aShape->isSolid() && !aShape->isCompSolid()) {
1278         theError = "Error: Selected shapes should have the same type.";
1279         return false;
1280       }
1281
1282       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1283       if (aResCompSolidPtr.get()) {
1284         GeomShapePtr aCompSolidShape = aResCompSolidPtr->shape();
1285         if (aSelectedCompSolidsInOtherList.isBound(aCompSolidShape)) {
1286           theError = "Error: Solids from compsolid in other list not allowed.";
1287           return false;
1288         }
1289       }
1290     } else {
1291       GeomAPI_Face aFace(aShape);
1292       GeomPlanePtr aPln = aFace.getPlane();
1293
1294       if (!aPln.get()) {
1295         theError = "Error: Only planar faces allowed.";
1296         return false;
1297       }
1298
1299       if (!aFacesPln->isCoincident(aPln)) {
1300         theError = "Error: Only coincident faces allowed.";
1301         return false;
1302       }
1303     }
1304   }
1305
1306   return true;
1307 }
1308
1309 //==================================================================================================
1310 bool FeaturesPlugin_IntersectionSelection::isValid(const AttributePtr& theAttribute,
1311                                                    const std::list<std::string>& theArguments,
1312                                                    Events_InfoMessage& theError) const
1313 {
1314   if (!theAttribute.get()) {
1315     theError = "Error: empty selection.";
1316     return false;
1317   }
1318   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
1319   AttributeSelectionListPtr anAttrSelectionList =
1320     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1321   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1322     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1323     if (!anAttrSelection.get()) {
1324       theError = "Error: empty attribute selection.";
1325       return false;
1326     }
1327     ResultPtr aContext = anAttrSelection->context();
1328     if (!aContext.get()) {
1329       theError = "Error: empty selection context.";
1330       return false;
1331     }
1332     FeaturePtr aFeature = ModelAPI_Feature::feature(aContext);
1333     if (!aFeature.get()) {
1334       theError = "Error: empty feature.";
1335       return false;
1336     }
1337     std::string aFeatureKind = aFeature->getKind();
1338     if (aFeatureKind == "Sketch" ||
1339         aFeatureKind == "Plane" ||
1340         aFeatureKind == "Axis") {
1341       theError = "Error: %1 shape is not allowed for selection.";
1342       theError.arg(aFeatureKind);
1343       return false;
1344     }
1345     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1346     GeomShapePtr aContextShape = aContext->shape();
1347     if (!aShape.get()) {
1348       aShape = aContextShape;
1349     }
1350     if (!aShape.get()) {
1351       theError = "Error: empty shape.";
1352       return false;
1353     }
1354     if (!aShape->isEqual(aContextShape)) {
1355       theError = "Error: Local selection not allowed.";
1356       return false;
1357     }
1358
1359     int aShapeType = aShape->shapeType();
1360     // Allow to select edges, faces and solids.
1361     if (aShapeType != GeomAPI_Shape::EDGE &&
1362         aShapeType != GeomAPI_Shape::FACE &&
1363         aShapeType != GeomAPI_Shape::SOLID &&
1364         aShapeType != GeomAPI_Shape::COMPSOLID &&
1365         aShapeType != GeomAPI_Shape::COMPOUND) {
1366       theError = "Error: selected shape has the wrong type.";
1367       return false;
1368     }
1369   }
1370
1371   return true;
1372 }
1373
1374 //==================================================================================================
1375 bool FeaturesPlugin_ValidatorBooleanFuseSelection::isValid(
1376   const AttributePtr& theAttribute,
1377   const std::list<std::string>& theArguments,
1378   Events_InfoMessage& theError) const
1379 {
1380   AttributeSelectionListPtr anAttrSelectionList =
1381     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1382   if (!anAttrSelectionList.get()) {
1383     theError =
1384       "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
1385     return false;
1386   }
1387
1388   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1389     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1390     if (!anAttrSelection.get()) {
1391       theError = "Error: Empty attribute selection.";
1392       return false;
1393     }
1394     ResultPtr aContext = anAttrSelection->context();
1395     if (!aContext.get()) {
1396       theError = "Error: Empty selection context.";
1397       return false;
1398     }
1399     ResultConstructionPtr aResultConstruction =
1400       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1401     if (aResultConstruction.get()) {
1402       theError = "Error: Result construction not allowed for selection.";
1403       return false;
1404     }
1405     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1406     GeomShapePtr aContextShape = aContext->shape();
1407     if (!aShape.get()) {
1408       aShape = aContextShape;
1409     }
1410     if (!aShape.get()) {
1411       theError = "Error: Empty shape.";
1412       return false;
1413     }
1414     if (!aShape->isEqual(aContextShape)) {
1415       theError = "Error: Local selection not allowed.";
1416       return false;
1417     }
1418   }
1419
1420   return true;
1421 }
1422
1423 //=================================================================================================
1424 bool FeaturesPlugin_ValidatorBooleanFuseArguments::isValid(
1425   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1426   const std::list<std::string>& theArguments,
1427   Events_InfoMessage& theError) const
1428 {
1429   if (theArguments.size() != 2) {
1430     theError = "Wrong number of arguments (expected 2).";
1431     return false;
1432   }
1433
1434   std::shared_ptr<FeaturesPlugin_BooleanFuse> aFeature =
1435     std::dynamic_pointer_cast<FeaturesPlugin_BooleanFuse>(theFeature);
1436
1437   int anObjectsNb = 0, aToolsNb = 0;
1438
1439   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1440
1441   bool isAllInSameCompSolid = true;
1442   ResultBodyPtr aCompSolid;
1443
1444   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1445   if (anAttrSelList) {
1446     anObjectsNb = anAttrSelList->size();
1447     for (int anIndex = 0; anIndex < anObjectsNb; ++anIndex) {
1448       AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1449       ResultPtr aContext = anAttr->context();
1450       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1451       if (aResCompSolidPtr.get()) {
1452         if (aCompSolid.get()) {
1453           isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1454         } else {
1455           aCompSolid = aResCompSolidPtr;
1456         }
1457       } else {
1458         isAllInSameCompSolid = false;
1459         break;
1460       }
1461     }
1462   }
1463   anIt++;
1464
1465   if (aFeature->string(FeaturesPlugin_BooleanFuse::CREATION_METHOD())->value()
1466       == FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED()) {
1467     anAttrSelList = theFeature->selectionList(*anIt);
1468     if (anAttrSelList) {
1469       aToolsNb = anAttrSelList->size();
1470       if (isAllInSameCompSolid) {
1471         for (int anIndex = 0; anIndex < aToolsNb; ++anIndex) {
1472           AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1473           ResultPtr aContext = anAttr->context();
1474           ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1475           if (aResCompSolidPtr.get()) {
1476             if (aCompSolid.get()) {
1477               isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1478             } else {
1479               aCompSolid = aResCompSolidPtr;
1480             }
1481           } else {
1482             isAllInSameCompSolid = false;
1483             break;
1484           }
1485         }
1486       }
1487     }
1488   }
1489
1490   anIt++;
1491
1492   if (anObjectsNb + aToolsNb < 2) {
1493     theError = "Not enough arguments for Fuse operation.";
1494     return false;
1495   } else if (isAllInSameCompSolid) {
1496     theError = "Operations only between sub-shapes of the same shape not allowed.";
1497     return false;
1498   }
1499
1500   return true;
1501 }
1502
1503 //=================================================================================================
1504 bool FeaturesPlugin_ValidatorBooleanFuseArguments::isNotObligatory(
1505   std::string theFeature,
1506   std::string theAttribute)
1507 {
1508   if (theAttribute == "main_objects" || theAttribute == "tool_objects") {
1509     return true;
1510   }
1511
1512   return false;
1513 }
1514
1515 //==================================================================================================
1516 bool FeaturesPlugin_ValidatorBooleanCommonSelection::isValid(
1517   const AttributePtr& theAttribute,
1518   const std::list<std::string>& theArguments,
1519   Events_InfoMessage& theError) const
1520 {
1521   AttributeSelectionListPtr anAttrSelectionList =
1522     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1523   if (!anAttrSelectionList.get()) {
1524     theError =
1525       "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
1526     return false;
1527   }
1528
1529   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1530     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1531     if (!anAttrSelection.get()) {
1532       theError = "Error: Empty attribute selection.";
1533       return false;
1534     }
1535     ResultPtr aContext = anAttrSelection->context();
1536     if (!aContext.get()) {
1537       theError = "Error: Empty selection context.";
1538       return false;
1539     }
1540     ResultConstructionPtr aResultConstruction =
1541       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1542     if (aResultConstruction.get()) {
1543       if (theAttribute->id() != FeaturesPlugin_BooleanCommon::TOOL_LIST_ID()) {
1544         theError = "Error: Result construction not allowed for selection.";
1545         return false;
1546       }
1547     }
1548     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1549     GeomShapePtr aContextShape = aContext->shape();
1550     if (!aShape.get()) {
1551       aShape = aContextShape;
1552     }
1553     if (!aShape.get()) {
1554       theError = "Error: Empty shape.";
1555       return false;
1556     }
1557     if (!aShape->isEqual(aContextShape)) {
1558       theError = "Error: Local selection not allowed.";
1559       return false;
1560     }
1561
1562     if (aResultConstruction.get() && aShape->shapeType() != GeomAPI_Shape::FACE) {
1563       theError = "Error: Result construction should be plane.";
1564       return false;
1565     }
1566   }
1567
1568   return true;
1569 }
1570
1571 //=================================================================================================
1572 bool FeaturesPlugin_ValidatorBooleanCommonArguments::isValid(
1573   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1574   const std::list<std::string>& theArguments,
1575   Events_InfoMessage& theError) const
1576 {
1577   if (theArguments.size() != 2) {
1578     theError = "Wrong number of arguments (expected 2).";
1579     return false;
1580   }
1581
1582   std::shared_ptr<FeaturesPlugin_BooleanCommon> aFeature =
1583     std::dynamic_pointer_cast<FeaturesPlugin_BooleanCommon>(theFeature);
1584
1585   int anObjectsNb = 0, aToolsNb = 0;
1586
1587   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1588
1589   bool isAllInSameCompSolid = true;
1590   ResultBodyPtr aCompSolid;
1591
1592   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1593   if (anAttrSelList) {
1594     anObjectsNb = anAttrSelList->size();
1595   }
1596
1597   bool isSimpleMode = aFeature->string(FeaturesPlugin_BooleanCommon::CREATION_METHOD())->value()
1598                       == FeaturesPlugin_BooleanCommon::CREATION_METHOD_SIMPLE();
1599
1600   if (!isSimpleMode) {
1601     anAttrSelList = theFeature->selectionList(*anIt);
1602     if (anAttrSelList) {
1603       aToolsNb = anAttrSelList->size();
1604     }
1605   }
1606
1607   if ((isSimpleMode && anObjectsNb < 2)
1608       || (!isSimpleMode && (anObjectsNb == 0 || aToolsNb == 0))) {
1609     theError = "Not enough arguments for Fuse operation.";
1610     return false;
1611   }
1612 }
1613
1614 //=================================================================================================
1615 bool FeaturesPlugin_ValidatorBooleanCommonArguments::isNotObligatory(
1616   std::string theFeature,
1617   std::string theAttribute)
1618 {
1619   return false;
1620 }