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