Salome HOME
Issue #2608: Invalid shape when select compound as a tool object for cut
[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() || !aContFeat->results().size()) {
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() && aContext.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       FeaturePtr aContFeat = anAttrSelection->contextFeature();
680       if (!aContFeat.get() || !aContFeat->results().size() ||
681           aContFeat->firstResult()->groupName() != ModelAPI_ResultBody::group()) {
682         theError = "Error: Empty selection context.";
683         return false;
684       }
685       if (aContFeat->results().size() == 1)
686         aContext = aContFeat->firstResult();
687       else {
688         theError = "Error: Too many shapes selected.";
689         return false;
690       }
691     }
692
693     ResultBodyPtr aContextOwner = ModelAPI_Tools::bodyOwner(aContext);
694     GeomShapePtr anOwner = aContextOwner.get() ? aContextOwner->shape() : aContext->shape();
695
696     if (anOwner->shapeType() != GeomAPI_Shape::SOLID &&
697         anOwner->shapeType() != GeomAPI_Shape::COMPSOLID) {
698       theError = "Error: Not all selected shapes are sub-shapes of solids.";
699       return false;
700     }
701
702     if (!aBaseSolid)
703       aBaseSolid = anOwner;
704     else if (!aBaseSolid->isEqual(anOwner)) {
705       theError = "Error: Sub-shapes of different solids have been selected.";
706       return false;
707     }
708   }
709
710   return true;
711 }
712
713 //==================================================================================================
714 bool FeaturesPlugin_ValidatorPartitionSelection::isValid(const AttributePtr& theAttribute,
715                                                        const std::list<std::string>& theArguments,
716                                                        Events_InfoMessage& theError) const
717 {
718   AttributeSelectionListPtr anAttrSelectionList =
719     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
720   if(!anAttrSelectionList.get()) {
721     theError = "Error: This validator can only work with selection list in \"Partition\" feature.";
722     return false;
723   }
724
725   for(int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
726     AttributeSelectionPtr aSelectAttr = anAttrSelectionList->value(anIndex);
727
728     //GeomValidators_BodyShapes aBodyValidator;
729     //if(aBodyValidator.isValid(aSelectAttr, theArguments, theError)) {
730     //  continue;
731     //}
732
733     GeomValidators_FeatureKind aFeatureKindValidator;
734     if(aFeatureKindValidator.isValid(aSelectAttr, theArguments, theError)) {
735       continue;
736     }
737
738     ResultPtr aContext = aSelectAttr->context();
739     ResultConstructionPtr aResultConstruction =
740       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
741     if(aResultConstruction.get()) {
742       theError = "Error: Only body shapes and construction planes are allowed for selection.";
743       return false;
744     }
745
746     ResultBodyPtr aResultBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
747     if(aResultBody.get()) {
748       continue;
749     }
750     FeaturePtr aResultFeature = aSelectAttr->contextFeature();
751     if(aResultFeature.get()) {
752       bool aOkRes = false;
753       std::list<ResultPtr>::const_iterator aFRes = aResultFeature->results().cbegin();
754       for(; aFRes != aResultFeature->results().cend() && !aOkRes; aFRes++) {
755         ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aFRes);
756         if (aBody.get() && !aBody->isDisabled())
757           aOkRes = true;
758       }
759       if (aOkRes)
760         continue;
761     }
762
763     theError = "Error: Only body shapes and construction planes are allowed for selection.";
764     return false;
765   }
766
767   theError = "";
768   return true;
769 }
770
771 //==================================================================================================
772 bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePtr& theAttribute,
773                                                      const std::list<std::string>& theArguments,
774                                                      Events_InfoMessage& theError) const
775 {
776   AttributeSelectionListPtr aSubShapesAttrList =
777     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
778   if(!aSubShapesAttrList.get()) {
779     theError =
780       "Error: This validator can only work with selection list in \"Remove Sub-Shapes\" feature.";
781     return false;
782   }
783
784   static const std::string aBaseShapeID = "base_shape";
785   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
786   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeID);
787
788   if(!aShapeAttrSelection.get()) {
789     theError = "Error: Could not get \"%1\" attribute.";
790     theError.arg(aBaseShapeID);
791     return false;
792   }
793
794   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
795   ResultPtr aContext = aShapeAttrSelection->context();
796   if(!aContext.get()) {
797     theError = "Error: Empty context.";
798     return false;
799   }
800   if(!aBaseShape.get()) {
801     aBaseShape = aContext->shape();
802   }
803   if(!aBaseShape.get()) {
804     theError = "Error: Empty base shape.";
805     return false;
806   }
807
808   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
809     bool isSameFound = false;
810     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
811     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
812     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
813       if(anIt.current()->isEqual(aShapeToAdd)) {
814         isSameFound = true;
815         break;
816       }
817     }
818     if(!isSameFound) {
819       theError = "Error: Only sub-shapes of selected shape is allowed for selection.";
820       return false;
821     }
822   }
823
824   return true;
825 }
826
827 //==================================================================================================
828 bool FeaturesPlugin_ValidatorRemoveSubShapesResult::isValid(
829   const std::shared_ptr<ModelAPI_Feature>& theFeature,
830   const std::list<std::string>& theArguments,
831   Events_InfoMessage& theError) const
832 {
833   static const std::string aBaseShapeID = "base_shape";
834   static const std::string aSubShapesID = "subshapes_to_keep";
835
836   if(theFeature->getKind() != "Remove_SubShapes") {
837     theError = "Error: Feature \"%1\" does not supported by this validator.";
838     theError.arg(theFeature->getKind());
839     return false;
840   }
841
842   AttributeSelectionPtr aShapeAttrSelection = theFeature->selection(aBaseShapeID);
843   if(!aShapeAttrSelection.get()) {
844     theError = "Error: Could not get \"%1\" attribute.";
845     theError.arg(aBaseShapeID);
846     return false;
847   }
848
849   AttributeSelectionListPtr aSubShapesAttrList = theFeature->selectionList(aSubShapesID);
850   if(!aSubShapesAttrList.get()) {
851     theError = "Error: Could not get \"%1\" attribute.";
852     theError.arg(aSubShapesID);
853     return false;
854   }
855
856   // Copy base shape.
857   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
858   if(!aBaseShape.get()) {
859     theError = "Error: Base shape is empty.";
860     return false;
861   }
862   GeomShapePtr aResultShape = aBaseShape->emptyCopied();
863
864   if (aSubShapesAttrList->size() == 0) {
865     theError = "Error: Resulting shape is not valid.";
866     return false;
867   }
868
869   // Copy sub-shapes from list to new shape.
870   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
871     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
872     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
873     GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
874   }
875
876   // Check new shape.
877   if(!GeomAlgoAPI_ShapeTools::isShapeValid(aResultShape)) {
878     theError = "Error: Resulting shape is not valid.";
879     return false;
880   }
881
882   return true;
883 }
884
885 //==================================================================================================
886 bool FeaturesPlugin_ValidatorUnionSelection::isValid(const AttributePtr& theAttribute,
887                                                      const std::list<std::string>& theArguments,
888                                                      Events_InfoMessage& theError) const
889 {
890   AttributeSelectionListPtr aBaseObjectsAttrList =
891     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
892   if(!aBaseObjectsAttrList.get()) {
893     theError = "Error: This validator can only work with selection list in \"%1\" feature.";
894     theError.arg(FeaturesPlugin_Union::ID());
895     return false;
896   }
897
898   for(int anIndex = 0; anIndex < aBaseObjectsAttrList->size(); ++anIndex) {
899     bool isSameFound = false;
900     AttributeSelectionPtr anAttrSelectionInList = aBaseObjectsAttrList->value(anIndex);
901     ResultPtr aContext = anAttrSelectionInList->context();
902     if (!aContext.get()) {
903       theError = "Error: selection is invalid.";
904       return false;
905     }
906
907     ResultConstructionPtr aConstruction =
908       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
909     if(aConstruction.get()) {
910       theError = "Error: Result construction not allowed for selection.";
911       return false;
912     }
913
914     GeomShapePtr aShape = anAttrSelectionInList->value();
915     GeomShapePtr aContextShape = aContext->shape();
916     if (aShape.get() && aContextShape.get() && !aContextShape->isEqual(aShape)) {
917       theError = "Error: Local selection not allowed.";
918       return false;
919     }
920
921     ResultBodyPtr aResult =
922       std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
923     if(!aResult.get()) {
924       continue;
925     }
926
927     if(aResult->numberOfSubs() > 0) {
928       theError = "Error: Whole compsolids not allowed for selection.";
929       return false;
930     }
931   }
932
933   return true;
934 }
935
936 //==================================================================================================
937 bool FeaturesPlugin_ValidatorUnionArguments::isValid(
938   const std::shared_ptr<ModelAPI_Feature>& theFeature,
939   const std::list<std::string>& theArguments,
940   Events_InfoMessage& theError) const
941 {
942   // Check feature kind.
943   if(theFeature->getKind() != FeaturesPlugin_Union::ID()) {
944     theError = "Error: This validator supports only \"%1\" feature.";
945     theError.arg(FeaturesPlugin_Union::ID());
946     return false;
947   }
948
949   // Get base objects attribute list.
950   AttributeSelectionListPtr aBaseObejctsAttrList =
951     theFeature->selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
952   if(!aBaseObejctsAttrList.get()) {
953     theError = "Error: Could not get \"%1\" attribute.";
954     theError.arg(FeaturesPlugin_Union::BASE_OBJECTS_ID());
955     return false;
956   }
957
958   // Get all shapes.
959   GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::COMPSOLID;
960   ListOfShape aBaseShapesList;
961   for(int anIndex = 0; anIndex < aBaseObejctsAttrList->size(); ++anIndex) {
962     AttributeSelectionPtr anAttrSelectionInList = aBaseObejctsAttrList->value(anIndex);
963     GeomShapePtr aShape = anAttrSelectionInList->value();
964     if (!aShape.get()) {
965       continue;
966     }
967     aBaseShapesList.push_back(aShape);
968     aType = aShape->shapeType() == GeomAPI_Shape::FACE ? GeomAPI_Shape::SHELL :
969                                                          GeomAPI_Shape::COMPSOLID;
970   }
971
972   // Make compound and find connected.
973   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseShapesList);
974   ListOfShape aCombined, aFree;
975   GeomAlgoAPI_ShapeTools::combineShapes(
976     aCompound,
977     aType,
978     aCombined,
979     aFree);
980
981   if(aFree.size() > 0 || aCombined.size() > 1) {
982     theError = "Error: Not all shapes have shared topology.";
983     return false;
984   }
985
986   return true;
987 }
988
989 bool FeaturesPlugin_ValidatorConcealedResult::isValid(const AttributePtr& theAttribute,
990                                             const std::list<std::string>& theArguments,
991                                             Events_InfoMessage& theError) const
992 {
993   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
994     theError = "Error: The attribute with the %1 type is not processed";
995     theError.arg(theAttribute->attributeType());
996     return false;
997   }
998
999   AttributeReferencePtr aRefAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeReference>
1000                                                                                (theAttribute);
1001   ObjectPtr aRefObject = aRefAttribute->value();
1002   if (!aRefObject.get()) {
1003     theError = "Error: Empty feature.";
1004     return false;
1005   }
1006
1007   FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefObject);
1008   if (!aRefFeature.get()) {
1009     theError = "Error: Empty feature.";
1010     return false;
1011   }
1012   std::list<std::shared_ptr<ModelAPI_Result> > aResults;
1013   ModelAPI_Tools::getConcealedResults(aRefFeature, aResults);
1014
1015   size_t aConcealedResults = aResults.size();
1016   if (!aConcealedResults && !theArguments.empty()) {
1017     // find if these results are touched by the feature in another attribute
1018     std::list<std::string>::const_iterator anIt = theArguments.begin();
1019     std::string aRecoveredList = *anIt;
1020     if (!aRecoveredList.empty()) {
1021       std::shared_ptr<ModelAPI_AttributeRefList> aParameterList =
1022                                  theAttribute->owner()->data()->reflist(aRecoveredList);
1023       if (aParameterList.get())
1024         aConcealedResults = aParameterList->size();
1025     }
1026   }
1027
1028   if (aConcealedResults == 0)
1029     theError = "Error: No concealed results.";
1030
1031   return theError.empty();
1032 }
1033
1034 bool FeaturesPlugin_ValidatorCircular::isValid(const AttributePtr& theAttribute,
1035                                                const std::list<std::string>& theArguments,
1036                                                Events_InfoMessage& theError) const
1037 {
1038   static std::list<std::string> aEdgeArg(1, "circle");
1039   static std::list<std::string> aFaceArg(1, "cylinder");
1040
1041   Events_InfoMessage aError;
1042   bool isValid = GeomValidators_ShapeType().isValid(theAttribute, aEdgeArg, aError);
1043   if (!isValid) {
1044     isValid = GeomValidators_Face().isValid(theAttribute, aFaceArg, aError);
1045     if (!isValid)
1046       theError = "The shape neither circle nor cylinder";
1047   }
1048   return isValid;
1049 }
1050
1051 //=================================================================================================
1052 bool FeaturesPlugin_ValidatorBooleanArguments::isValid(
1053   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1054   const std::list<std::string>& theArguments,
1055   Events_InfoMessage& theError) const
1056 {
1057   if (theArguments.size() != 2)
1058   {
1059     theError = "Wrong number of arguments (expected 2).";
1060     return false;
1061   }
1062
1063   int anObjectsNb = 0, aToolsNb = 0;
1064   //int anOperationType = 0;
1065
1066   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1067
1068   bool isAllInSameCompSolid = true;
1069   ResultBodyPtr aCompSolid;
1070
1071   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1072   if (anAttrSelList)
1073   {
1074     anObjectsNb = anAttrSelList->size();
1075     for (int anIndex = 0; anIndex < anObjectsNb; ++anIndex)
1076     {
1077       AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1078       ResultPtr aContext = anAttr->context();
1079       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1080       if (aResCompSolidPtr.get())
1081       {
1082         if (aCompSolid.get())
1083         {
1084           isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1085         }
1086         else
1087         {
1088           aCompSolid = aResCompSolidPtr;
1089         }
1090       }
1091       else
1092       {
1093         isAllInSameCompSolid = false;
1094         break;
1095       }
1096     }
1097   }
1098   anIt++;
1099
1100
1101   anAttrSelList = theFeature->selectionList(*anIt);
1102   if (anAttrSelList)
1103   {
1104     aToolsNb = anAttrSelList->size();
1105     if (isAllInSameCompSolid)
1106     {
1107       for (int anIndex = 0; anIndex < aToolsNb; ++anIndex)
1108       {
1109         AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1110         ResultPtr aContext = anAttr->context();
1111         ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1112         if (aResCompSolidPtr.get())
1113         {
1114           if (aCompSolid.get())
1115           {
1116             isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1117           }
1118           else
1119           {
1120             aCompSolid = aResCompSolidPtr;
1121           }
1122         }
1123         else
1124         {
1125           isAllInSameCompSolid = false;
1126           break;
1127         }
1128       }
1129     }
1130   }
1131   anIt++;
1132
1133   std::shared_ptr<FeaturesPlugin_Boolean> aFeature =
1134     std::dynamic_pointer_cast<FeaturesPlugin_Boolean>(theFeature);
1135   FeaturesPlugin_Boolean::OperationType anOperationType = aFeature->operationType();
1136
1137   if (anOperationType == FeaturesPlugin_Boolean::BOOL_FUSE)
1138   {
1139     // Fuse operation
1140     if (anObjectsNb + aToolsNb < 2)
1141     {
1142       theError = "Not enough arguments for Fuse operation.";
1143       return false;
1144     }
1145     else if (isAllInSameCompSolid)
1146     {
1147       theError = "Operations only between sub-shapes of the same shape not allowed.";
1148       return false;
1149     }
1150   }
1151   else
1152   {
1153     if (anObjectsNb < 1)
1154     {
1155       theError = "Objects not selected.";
1156       return false;
1157     }
1158     if (aToolsNb < 1)
1159     {
1160       theError = "Tools not selected.";
1161       return false;
1162     }
1163     if (isAllInSameCompSolid)
1164     {
1165       theError = "Operations only between sub-shapes of the same shape not allowed.";
1166       return false;
1167     }
1168   }
1169
1170   return true;
1171 }
1172
1173 //=================================================================================================
1174 bool FeaturesPlugin_ValidatorBooleanArguments::isNotObligatory(std::string theFeature,
1175                                                                std::string theAttribute)
1176 {
1177   if (theAttribute == "main_objects" || theAttribute == "tool_objects")
1178   {
1179     return true;
1180   }
1181
1182   return false;
1183 }
1184
1185 //==================================================================================================
1186 bool FeaturesPlugin_ValidatorBooleanSmashSelection::isValid(
1187   const AttributePtr& theAttribute,
1188   const std::list<std::string>& theArguments,
1189   Events_InfoMessage& theError) const
1190 {
1191   std::shared_ptr<FeaturesPlugin_BooleanSmash> aFeature =
1192     std::dynamic_pointer_cast<FeaturesPlugin_BooleanSmash>(theAttribute->owner());
1193
1194   AttributeSelectionListPtr anAttrSelectionList =
1195     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1196   if (!aFeature.get() || !anAttrSelectionList.get()) {
1197     theError =
1198       "Error: Validator used in wrong feature or attribute";
1199     return false;
1200   }
1201
1202   AttributeSelectionListPtr anOtherAttrSelectionList;
1203   if (theAttribute->id() == FeaturesPlugin_BooleanSmash::OBJECT_LIST_ID()) {
1204     anOtherAttrSelectionList =
1205       aFeature->selectionList(FeaturesPlugin_BooleanSmash::TOOL_LIST_ID());
1206   } else {
1207     anOtherAttrSelectionList =
1208       aFeature->selectionList(FeaturesPlugin_BooleanSmash::OBJECT_LIST_ID());
1209   }
1210
1211   GeomAPI_Shape::ShapeType aSelectedShapesType = GeomAPI_Shape::SHAPE;
1212   GeomAPI_DataMapOfShapeShape aSelectedCompSolidsInOtherList;
1213   GeomPlanePtr aFacesPln;
1214
1215   for (int anIndex = 0; anIndex < anOtherAttrSelectionList->size(); ++anIndex) {
1216     AttributeSelectionPtr anAttrSelection = anOtherAttrSelectionList->value(anIndex);
1217
1218     if (anAttrSelection->contextFeature().get()) {
1219       theError = "Error: Features not allowed for selection.";
1220       return false;
1221     }
1222
1223     ResultPtr aContext = anAttrSelection->context();
1224     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1225     GeomShapePtr aContextShape = aContext->shape();
1226     if (!aShape.get()) {
1227       aShape = aContextShape;
1228     }
1229
1230     if (aShape->isSolid() || aShape->isCompSolid()) {
1231       aSelectedShapesType = GeomAPI_Shape::SOLID;
1232       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1233       if (aResCompSolidPtr.get()) {
1234         GeomShapePtr aCompSolidShape = aResCompSolidPtr->shape();
1235         aSelectedCompSolidsInOtherList.bind(aCompSolidShape, aCompSolidShape);
1236       }
1237     } else {
1238       aSelectedShapesType = GeomAPI_Shape::FACE;
1239       GeomAPI_Face aFace(aShape);
1240       aFacesPln = aFace.getPlane();
1241       break;
1242     }
1243   }
1244
1245   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1246     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1247     if (!anAttrSelection.get()) {
1248       theError = "Error: Empty attribute selection.";
1249       return false;
1250     }
1251
1252     if (anAttrSelection->contextFeature().get()) {
1253       theError = "Error: Features not allowed for selection.";
1254       return false;
1255     }
1256
1257     ResultPtr aContext = anAttrSelection->context();
1258     if(!aContext.get()) {
1259       FeaturePtr aContFeat = anAttrSelection->contextFeature();
1260       if (!aContFeat.get() || !aContFeat->results().size() ||
1261           aContFeat->firstResult()->groupName() != ModelAPI_ResultBody::group()) {
1262         theError = "Error: Empty selection context.";
1263         return false;
1264       }
1265     }
1266     ResultConstructionPtr aResultConstruction =
1267       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1268     if (aResultConstruction.get()) {
1269       theError = "Error: Result construction not allowed for selection.";
1270       return false;
1271     }
1272     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1273     GeomShapePtr aContextShape = aContext->shape();
1274     if (!aShape.get()) {
1275       aShape = aContextShape;
1276     }
1277     if (!aShape.get()) {
1278       theError = "Error: Empty shape.";
1279       return false;
1280     }
1281     if (!aShape->isEqual(aContextShape)) {
1282       theError = "Error: Local selection not allowed.";
1283       return false;
1284     }
1285
1286     if (aSelectedShapesType == GeomAPI_Shape::SHAPE) {
1287       // Other list is empty.
1288       if (aShape->isSolid() || aShape->isCompSolid()) {
1289         aSelectedShapesType = GeomAPI_Shape::SOLID;
1290       } else {
1291         aSelectedShapesType = GeomAPI_Shape::FACE;
1292         GeomAPI_Face aFace(aShape);
1293         aFacesPln = aFace.getPlane();
1294
1295         if (!aFacesPln.get()) {
1296           theError = "Error: Only planar faces allowed.";
1297           return false;
1298         }
1299       }
1300
1301       continue;
1302     } else if (aSelectedShapesType == GeomAPI_Shape::SOLID) {
1303       if (!aShape->isSolid() && !aShape->isCompSolid()) {
1304         theError = "Error: Selected shapes should have the same type.";
1305         return false;
1306       }
1307
1308       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1309       if (aResCompSolidPtr.get()) {
1310         GeomShapePtr aCompSolidShape = aResCompSolidPtr->shape();
1311         if (aSelectedCompSolidsInOtherList.isBound(aCompSolidShape)) {
1312           theError = "Error: Solids from compsolid in other list not allowed.";
1313           return false;
1314         }
1315       }
1316     } else {
1317       GeomAPI_Face aFace(aShape);
1318       GeomPlanePtr aPln = aFace.getPlane();
1319
1320       if (!aPln.get()) {
1321         theError = "Error: Only planar faces allowed.";
1322         return false;
1323       }
1324
1325       if (!aFacesPln->isCoincident(aPln)) {
1326         theError = "Error: Only coincident faces allowed.";
1327         return false;
1328       }
1329     }
1330   }
1331
1332   return true;
1333 }
1334
1335 //==================================================================================================
1336 bool FeaturesPlugin_IntersectionSelection::isValid(const AttributePtr& theAttribute,
1337                                                    const std::list<std::string>& theArguments,
1338                                                    Events_InfoMessage& theError) const
1339 {
1340   if (!theAttribute.get()) {
1341     theError = "Error: empty selection.";
1342     return false;
1343   }
1344   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
1345   AttributeSelectionListPtr anAttrSelectionList =
1346     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1347   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1348     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1349     if (!anAttrSelection.get()) {
1350       theError = "Error: empty attribute selection.";
1351       return false;
1352     }
1353     ResultPtr aContext = anAttrSelection->context();
1354     if(!aContext.get()) {
1355       FeaturePtr aContFeat = anAttrSelection->contextFeature();
1356       if (!aContFeat.get() || !aContFeat->results().size() ||
1357           aContFeat->firstResult()->groupName() != ModelAPI_ResultBody::group()) {
1358         theError = "Error: Empty selection context.";
1359         return false;
1360       }
1361     }
1362     FeaturePtr aFeature = anAttrSelection->contextFeature().get() ?
1363       anAttrSelection->contextFeature() : ModelAPI_Feature::feature(aContext);
1364     if (!aFeature.get()) {
1365       theError = "Error: empty feature.";
1366       return false;
1367     }
1368     std::string aFeatureKind = aFeature->getKind();
1369     if (aFeatureKind == "Sketch" ||
1370         aFeatureKind == "Plane" ||
1371         aFeatureKind == "Axis") {
1372       theError = "Error: %1 shape is not allowed for selection.";
1373       theError.arg(aFeatureKind);
1374       return false;
1375     }
1376     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1377     if (!aShape.get()) {
1378       GeomShapePtr aContextShape = aContext->shape();
1379       aShape = aContextShape;
1380     }
1381     if (!aShape.get()) {
1382       theError = "Error: empty shape.";
1383       return false;
1384     }
1385     if (aContext.get() && !aShape->isEqual(aContext->shape())) {
1386       theError = "Error: Local selection not allowed.";
1387       return false;
1388     }
1389
1390     int aShapeType = aShape->shapeType();
1391     // Allow to select edges, faces and solids.
1392     if (aShapeType != GeomAPI_Shape::EDGE &&
1393         aShapeType != GeomAPI_Shape::FACE &&
1394         aShapeType != GeomAPI_Shape::SOLID &&
1395         aShapeType != GeomAPI_Shape::COMPSOLID &&
1396         aShapeType != GeomAPI_Shape::COMPOUND) {
1397       theError = "Error: selected shape has the wrong type.";
1398       return false;
1399     }
1400   }
1401
1402   return true;
1403 }
1404
1405 //==================================================================================================
1406 bool FeaturesPlugin_ValidatorBooleanFuseSelection::isValid(
1407   const AttributePtr& theAttribute,
1408   const std::list<std::string>& theArguments,
1409   Events_InfoMessage& theError) const
1410 {
1411   AttributeSelectionListPtr anAttrSelectionList =
1412     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1413   if (!anAttrSelectionList.get()) {
1414     theError =
1415       "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
1416     return false;
1417   }
1418
1419   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1420     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1421     if (!anAttrSelection.get()) {
1422       theError = "Error: Empty attribute selection.";
1423       return false;
1424     }
1425     ResultPtr aContext = anAttrSelection->context();
1426     if(!aContext.get()) {
1427       FeaturePtr aContFeat = anAttrSelection->contextFeature();
1428       if (!aContFeat.get() || !aContFeat->results().size() ||
1429           aContFeat->firstResult()->groupName() != ModelAPI_ResultBody::group()) {
1430         theError = "Error: Empty selection context.";
1431         return false;
1432       }
1433     }
1434     ResultConstructionPtr aResultConstruction =
1435       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1436     if (aResultConstruction.get()) {
1437       theError = "Error: Result construction not allowed for selection.";
1438       return false;
1439     }
1440     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1441     if (!aShape.get()) {
1442       GeomShapePtr aContextShape = aContext->shape();
1443       aShape = aContextShape;
1444     }
1445     if (!aShape.get()) {
1446       theError = "Error: Empty shape.";
1447       return false;
1448     }
1449     if (aContext.get() && !aShape->isEqual(aContext->shape())) {
1450       theError = "Error: Local selection not allowed.";
1451       return false;
1452     }
1453   }
1454
1455   return true;
1456 }
1457
1458 //=================================================================================================
1459 bool FeaturesPlugin_ValidatorBooleanFuseArguments::isValid(
1460   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1461   const std::list<std::string>& theArguments,
1462   Events_InfoMessage& theError) const
1463 {
1464   if (theArguments.size() != 2) {
1465     theError = "Wrong number of arguments (expected 2).";
1466     return false;
1467   }
1468
1469   std::shared_ptr<FeaturesPlugin_BooleanFuse> aFeature =
1470     std::dynamic_pointer_cast<FeaturesPlugin_BooleanFuse>(theFeature);
1471
1472   int anObjectsNb = 0, aToolsNb = 0;
1473
1474   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1475
1476   bool isAllInSameCompSolid = true;
1477   ResultBodyPtr aCompSolid;
1478
1479   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1480   if (anAttrSelList) {
1481     anObjectsNb = anAttrSelList->size();
1482     for (int anIndex = 0; anIndex < anObjectsNb; ++anIndex) {
1483       AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1484       ResultPtr aContext = anAttr->context();
1485       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1486       if (aResCompSolidPtr.get()) {
1487         if (aCompSolid.get()) {
1488           isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1489         } else {
1490           aCompSolid = aResCompSolidPtr;
1491         }
1492       } else {
1493         isAllInSameCompSolid = false;
1494         break;
1495       }
1496     }
1497   }
1498   anIt++;
1499
1500   if (aFeature->string(FeaturesPlugin_BooleanFuse::CREATION_METHOD())->value()
1501       == FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED()) {
1502     anAttrSelList = theFeature->selectionList(*anIt);
1503     if (anAttrSelList) {
1504       aToolsNb = anAttrSelList->size();
1505       if (isAllInSameCompSolid) {
1506         for (int anIndex = 0; anIndex < aToolsNb; ++anIndex) {
1507           AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
1508           ResultPtr aContext = anAttr->context();
1509           ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
1510           if (aResCompSolidPtr.get()) {
1511             if (aCompSolid.get()) {
1512               isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
1513             } else {
1514               aCompSolid = aResCompSolidPtr;
1515             }
1516           } else {
1517             isAllInSameCompSolid = false;
1518             break;
1519           }
1520         }
1521       }
1522     }
1523   }
1524
1525   anIt++;
1526
1527   if (anObjectsNb + aToolsNb < 2) {
1528     theError = "Not enough arguments for Fuse operation.";
1529     return false;
1530   } else if (isAllInSameCompSolid) {
1531     theError = "Operations only between sub-shapes of the same shape not allowed.";
1532     return false;
1533   }
1534
1535   return true;
1536 }
1537
1538 //=================================================================================================
1539 bool FeaturesPlugin_ValidatorBooleanFuseArguments::isNotObligatory(
1540   std::string theFeature,
1541   std::string theAttribute)
1542 {
1543   if (theAttribute == "main_objects" || theAttribute == "tool_objects") {
1544     return true;
1545   }
1546
1547   return false;
1548 }
1549
1550 //==================================================================================================
1551 bool FeaturesPlugin_ValidatorBooleanCommonSelection::isValid(
1552   const AttributePtr& theAttribute,
1553   const std::list<std::string>& theArguments,
1554   Events_InfoMessage& theError) const
1555 {
1556   AttributeSelectionListPtr anAttrSelectionList =
1557     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
1558   if (!anAttrSelectionList.get()) {
1559     theError =
1560       "Error: This validator can only work with selection list attributes in \"Boolean\" feature.";
1561     return false;
1562   }
1563
1564   for (int anIndex = 0; anIndex < anAttrSelectionList->size(); ++anIndex) {
1565     AttributeSelectionPtr anAttrSelection = anAttrSelectionList->value(anIndex);
1566     if (!anAttrSelection.get()) {
1567       theError = "Error: Empty attribute selection.";
1568       return false;
1569     }
1570     ResultPtr aContext = anAttrSelection->context();
1571     if (!aContext.get()) {
1572       theError = "Error: Empty selection context.";
1573       return false;
1574     }
1575     ResultConstructionPtr aResultConstruction =
1576       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
1577     if (aResultConstruction.get()) {
1578       if (theAttribute->id() != FeaturesPlugin_BooleanCommon::TOOL_LIST_ID()) {
1579         theError = "Error: Result construction not allowed for selection.";
1580         return false;
1581       }
1582     }
1583     std::shared_ptr<GeomAPI_Shape> aShape = anAttrSelection->value();
1584     GeomShapePtr aContextShape = aContext->shape();
1585     if (!aShape.get()) {
1586       aShape = aContextShape;
1587     }
1588     if (!aShape.get()) {
1589       theError = "Error: Empty shape.";
1590       return false;
1591     }
1592     if (!aShape->isEqual(aContextShape)) {
1593       theError = "Error: Local selection not allowed.";
1594       return false;
1595     }
1596
1597     if (aResultConstruction.get() && aShape->shapeType() != GeomAPI_Shape::FACE) {
1598       theError = "Error: Result construction should be plane.";
1599       return false;
1600     }
1601   }
1602
1603   return true;
1604 }
1605
1606 //=================================================================================================
1607 bool FeaturesPlugin_ValidatorBooleanCommonArguments::isValid(
1608   const std::shared_ptr<ModelAPI_Feature>& theFeature,
1609   const std::list<std::string>& theArguments,
1610   Events_InfoMessage& theError) const
1611 {
1612   if (theArguments.size() != 2) {
1613     theError = "Wrong number of arguments (expected 2).";
1614     return false;
1615   }
1616
1617   std::shared_ptr<FeaturesPlugin_BooleanCommon> aFeature =
1618     std::dynamic_pointer_cast<FeaturesPlugin_BooleanCommon>(theFeature);
1619
1620   int anObjectsNb = 0, aToolsNb = 0;
1621
1622   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
1623
1624   bool isAllInSameCompSolid = true;
1625   ResultBodyPtr aCompSolid;
1626
1627   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
1628   if (anAttrSelList) {
1629     anObjectsNb = anAttrSelList->size();
1630   }
1631
1632   bool isSimpleMode = aFeature->string(FeaturesPlugin_BooleanCommon::CREATION_METHOD())->value()
1633                       == FeaturesPlugin_BooleanCommon::CREATION_METHOD_SIMPLE();
1634
1635   if (!isSimpleMode) {
1636     anAttrSelList = theFeature->selectionList(*anIt);
1637     if (anAttrSelList) {
1638       aToolsNb = anAttrSelList->size();
1639     }
1640   }
1641
1642   if ((isSimpleMode && anObjectsNb < 2)
1643       || (!isSimpleMode && (anObjectsNb == 0 || aToolsNb == 0))) {
1644     theError = "Not enough arguments for Fuse operation.";
1645     return false;
1646   }
1647 }
1648
1649 //=================================================================================================
1650 bool FeaturesPlugin_ValidatorBooleanCommonArguments::isNotObligatory(
1651   std::string theFeature,
1652   std::string theAttribute)
1653 {
1654   return false;
1655 }