Salome HOME
Revert deletion of BuildPlugin_ValidatorExpressionInterpolation::isValid()
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Validators.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BuildPlugin_Validators.h"
21 #include "BuildPlugin_Solid.h"
22 #include "BuildPlugin_Face.h"
23 #include "BuildPlugin_Wire.h"
24
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_ResultConstruction.h>
27
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAPI_ShapeIterator.h>
32
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_ShapeBuilder.h>
36 #include <GeomAlgoAPI_ShapeTools.h>
37 #include <GeomAlgoAPI_SketchBuilder.h>
38 #include <GeomAlgoAPI_WireBuilder.h>
39 #include <GeomAlgoAPI_MakeVolume.h>
40 #include <GeomAlgoAPI_Tools.h>
41
42 #include <GeomValidators_FeatureKind.h>
43 #include <GeomValidators_ShapeType.h>
44
45 #include <SketchPlugin_Sketch.h>
46
47 #include <Events_InfoMessage.h>
48
49 //=================================================================================================
50 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
51                                                 const std::list<std::string>& theArguments,
52                                                 Events_InfoMessage& theError) const
53 {
54   // Get base objects list.
55   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
56     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
57                        "not support attribute type '%1'\nOnly '%2' is supported.";
58     Events_InfoMessage("BuildPlugin_Validators", aMsg).
59       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
60     return false;
61   }
62   AttributeSelectionListPtr aSelectionList =
63     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
64   if(!aSelectionList.get()) {
65     theError = "Could not get selection list.";
66     return false;
67   }
68   if(aSelectionList->size() == 0) {
69     theError = "Empty selection list.";
70     return false;
71   }
72
73   // Collect base shapes.
74   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
75     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
76     if(!aSelection.get()) {
77       theError = "Could not get selection.";
78       return false;
79     }
80     ResultPtr aContext = aSelection->context();
81     if(!aContext.get()) {
82       theError = "Attribute have empty context.";
83       return false;
84     }
85
86     GeomShapePtr aShape = aSelection->value();
87     GeomShapePtr aContextShape = aContext->shape();
88     if(!aShape.get()) {
89       aShape = aContextShape;
90     }
91     if(!aShape.get()) {
92       theError = "Empty shape selected.";
93       return false;
94     }
95
96     // Check that shapes has acceptable type.
97     GeomValidators_ShapeType aValidatorShapeType;
98     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
99       return false;
100     }
101
102     // Check that it is shape on sketch.
103     ResultConstructionPtr aConstruction =
104       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
105     if(aConstruction.get()) {
106       if(aConstruction->isInfinite()) {
107         theError = "Infinite objects not acceptable.";
108         return false;
109       }
110     }
111   }
112
113   return true;
114 }
115
116 //=================================================================================================
117 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
118                                                const std::list<std::string>& theArguments,
119                                                Events_InfoMessage& theError) const
120 {
121   // Get attribute.
122   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
123   if(!aSelectionList.get()) {
124     theError = "Empty attribute \"%1\".";
125     theError.arg(theArguments.front());
126     return false;
127   }
128
129   if (theFeature->getKind() == BuildPlugin_Wire::ID()) {
130     /// remove objects of sub-type if ojects of correct type is in List,  in some cases :
131     /// Wire builder: wires and edges selected
132     std::set<int> aRemove;
133     for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
134       AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
135       GeomShapePtr aShape = aSelection->value();
136       if (aShape.get()) {
137         GeomAPI_Shape::ShapeType aType = aShape->shapeType();
138         if (aType == GeomAPI_Shape::WIRE) {
139           // check for edges
140           GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
141           for (; anEdgeExp.more(); anEdgeExp.next()) {
142             GeomShapePtr aEdge = anEdgeExp.current();
143             for (int i = 0; i < aSelectionList->size(); ++i) {
144               AttributeSelectionPtr aSel = aSelectionList->value(i);
145               GeomShapePtr aShp = aSel->value();
146               if (aShp.get()) {
147                 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
148                   if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
149                     aRemove.insert(i);
150                 }
151               }
152               else {
153                 aRemove.insert(anIndex);
154               }
155             }
156           }
157         }
158       }
159     }
160     if (aRemove.size() > 0)
161       aSelectionList->remove(aRemove);
162   }
163
164   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::shapeTypeByStr(theArguments.back());
165   // Collect base shapes.
166   ListOfShape aListOfShapes;
167   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
168     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
169     GeomShapePtr aShape = aSelection->value();
170     ResultPtr aContext = aSelection->context();
171     if (!aShape.get() && aContext.get())
172       aShape = aContext->shape();
173
174     bool isProper = aShape.get() &&
175         (aShape->shapeType() == GeomAPI_Shape::EDGE || aShape->shapeType() == aShapeType);
176
177     if (isProper)
178       aListOfShapes.push_back(aShape);
179     else {
180       // is it a sketch?
181       FeaturePtr aFeature = aSelection->contextFeature();
182       if (!aFeature.get()) {
183         GeomShapePtr aValue = aSelection->value();
184         // whole sketch is allowed only
185         if (aContext.get() && !aValue.get()) {
186           aFeature = ModelAPI_Feature::feature(aContext);
187         }
188       }
189
190       if (!aFeature.get()) {
191         theError = "Error: Incorrect selection.";
192         return false;
193       }
194
195       if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
196         theError = "Error: %1 shape is not allowed for selection.";
197         theError.arg(aFeature->getKind());
198         return false;
199       }
200     }
201   }
202
203   if (aShapeType == GeomAPI_Shape::WIRE) {
204     // Create wire.
205     GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
206     if (!aWire.get() && !aListOfShapes.empty()) {
207       theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
208       return false;
209     }
210   }
211
212   return true;
213 }
214
215 //=================================================================================================
216 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
217                                                const std::list<std::string>& theArguments,
218                                                Events_InfoMessage& theError) const
219 {
220   // Get attribute.
221   if(theArguments.size() != 1) {
222     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
223       "1 parameter (ID of base objects list).";
224     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
225     return false;
226   }
227   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
228   if(!aSelectionList.get()) {
229     theError = "Empty attribute \"%1\".";
230     theError.arg(theArguments.front());
231     return false;
232   }
233
234   if (theFeature->getKind() == BuildPlugin_Face::ID()) {
235     /// remove objects of sub-type if ojects of correct type is in List,  in some cases :
236     /// - Face builder: edges, faces and wires selected
237     ///                 --> remove edges and wires
238     std::set<int> aRemove;
239     for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
240       AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
241       GeomShapePtr aShape = aSelection->value();
242       if (aShape.get()) {
243         GeomAPI_Shape::ShapeType aType = aShape->shapeType();
244         if (aType == GeomAPI_Shape::FACE) {
245           // Check for wires
246           GeomAPI_ShapeExplorer anWireExp(aShape, GeomAPI_Shape::WIRE);
247           for (; anWireExp.more(); anWireExp.next()) {
248             GeomShapePtr aWire = anWireExp.current();
249             for (int i = 0; i < aSelectionList->size(); ++i) {
250               AttributeSelectionPtr aSel = aSelectionList->value(i);
251               GeomShapePtr aShp = aSel->value();
252               if (aShp.get()) {
253                 if (aShp->shapeType() == GeomAPI_Shape::WIRE) {
254                   if (aShp->isEqual(aWire) || aShp->isSameGeometry(aWire))
255                     aRemove.insert(i);
256                 }
257               }
258               else {
259                 aRemove.insert(anIndex);
260               }
261             }
262           }
263
264           // check for edges
265           GeomAPI_ShapeExplorer anEdgeExp(aShape, GeomAPI_Shape::EDGE);
266           for (; anEdgeExp.more(); anEdgeExp.next()) {
267             GeomShapePtr aEdge = anEdgeExp.current();
268             for (int i = 0; i < aSelectionList->size(); ++i) {
269               AttributeSelectionPtr aSel = aSelectionList->value(i);
270               GeomShapePtr aShp = aSel->value();
271               if (aShp.get()) {
272                 if (aShp->shapeType() == GeomAPI_Shape::EDGE) {
273                   if (aShp->isEqual(aEdge) || aShp->isSameGeometry(aEdge))
274                     aRemove.insert(i);
275                 }
276               }
277               else {
278                 aRemove.insert(anIndex);
279               }
280             }
281           }
282         }
283       }
284     }
285     if (aRemove.size() > 0)
286       aSelectionList->remove(aRemove);
287   }
288   bool hasEdgesOrWires = false;
289   bool hasFaces = false;
290
291   // Collect base shapes.
292   ListOfShape anEdges;
293   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
294     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
295     GeomShapePtr aShape = aSelection->value();
296     if(!aShape.get()) {
297       if (!aSelection->context()) {
298         theError = "Objects are not selected.";
299         return false;
300       }
301       aShape = aSelection->context()->shape();
302     }
303     ResultConstructionPtr aSketchRes =
304         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
305
306     if (aShape->shapeType() == GeomAPI_Shape::FACE ||
307         (!aSelection->value() && aSketchRes && aSketchRes->facesNum() > 0)) {
308       // skip faces exploding
309       hasFaces = true;
310       continue;
311     }
312
313     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
314       hasEdgesOrWires = true;
315       GeomShapePtr anEdge = anExp.current();
316       anEdges.push_back(anEdge);
317     }
318   }
319
320   if (hasFaces && hasEdgesOrWires) {
321     theError = "Faces and edges/wires should be selected together.";
322     return false;
323   } else if (hasEdgesOrWires && anEdges.empty()) {
324     theError = "Objects are not selected.";
325     return false;
326   }
327
328   // Check that edges does not have intersections.
329   if(anEdges.size() > 1) {
330     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
331     if(!aPaveFiller.isDone()) {
332       theError = "Error while checking if edges intersects.";
333       return false;
334     }
335     GeomShapePtr aSectedEdges = aPaveFiller.shape();
336
337     size_t anEdgesNum = 0;
338     for(GeomAPI_ShapeExplorer
339         anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
340       anEdgesNum++;
341     }
342     if(anEdgesNum != anEdges.size()) {
343       theError = "Selected objects have intersections.";
344       return false;
345     }
346   }
347
348   if (!anEdges.empty()) {
349     // Check that they are planar.
350     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
351     if(!aPln.get()) {
352       theError = "Selected object(s) should belong to only one plane.";
353       return false;
354     }
355
356     // Check that selected objects have closed contours.
357     GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
358     const ListOfShape& aFaces = aBuilder.faces();
359     if(aFaces.empty()) {
360       theError = "Selected objects do not generate closed contour.";
361       return false;
362     }
363   }
364
365   return true;
366 }
367
368 //=================================================================================================
369 bool BuildPlugin_ValidatorBaseForSolids::isValid(
370   const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
371   Events_InfoMessage& theError) const
372 {
373   // Get base objects list.
374   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
375   if (!aSelectionList.get()) {
376     theError = "Could not get selection list.";
377     return false;
378   }
379   if (aSelectionList->size() == 0) {
380     theError = "Empty selection list.";
381     return false;
382   }
383
384   if (theFeature->getKind() == BuildPlugin_Solid::ID()) {
385     /// remove objects of sub-type if ojects of correct type is in List,  in some cases :
386     /// Solid builder: faces and shapes shells or solids seleted
387     ///                --> remove faces
388
389     std::set<int> aRemove;
390     for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
391       AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
392       GeomShapePtr aShape = aSelection->value();
393       if (aShape.get()) {
394         GeomAPI_Shape::ShapeType aType = aShape->shapeType();
395         if ((aType == GeomAPI_Shape::SHAPE) ||
396           (aType == GeomAPI_Shape::SOLID) ||
397           (aType == GeomAPI_Shape::SHELL)) {
398
399           GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::FACE);
400           for (; anExp.more(); anExp.next()) {
401             GeomShapePtr aFace = anExp.current();
402             for (int i = 0; i < aSelectionList->size(); ++i) {
403               AttributeSelectionPtr aSel = aSelectionList->value(i);
404               GeomShapePtr aShp = aSel->value();
405               if (aShp.get()) {
406                 if (aShp->shapeType() == GeomAPI_Shape::FACE) {
407                   if (aShp->isEqual(aFace))
408                     aRemove.insert(i);
409                 }
410               }
411               else {
412                 aRemove.insert(anIndex);
413               }
414             }
415           }
416         }
417       }
418     }
419     if (aRemove.size() > 0)
420       aSelectionList->remove(aRemove);
421   }
422
423   // Collect base shapes.
424   ListOfShape anOriginalShapes;
425   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
426     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
427     if (!aSelection->context().get()) {
428       theError = "Invalid selection.";
429       return false;
430     }
431     GeomShapePtr aShape = aSelection->value();
432     if (!aShape.get())
433       aShape = aSelection->context()->shape();
434     anOriginalShapes.push_back(aShape);
435   }
436
437   std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
438     new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
439
440   std::string anErr;
441   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
442     theError = anErr;
443     return false;
444   }
445
446   // set of allowed types of results
447   std::set<GeomAPI_Shape::ShapeType> aResultType;
448   std::string aType = theArguments.back();
449   if (aType == "solid")
450     aResultType.insert(GeomAPI_Shape::SOLID);
451   else if (aType == "compsolid") {
452     aResultType.insert(GeomAPI_Shape::COMPSOLID);
453     aResultType.insert(GeomAPI_Shape::SOLID);
454   }
455
456   GeomShapePtr aCompound = anAlgorithm->shape();
457   if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
458     GeomAPI_ShapeIterator anIt(aCompound);
459     GeomShapePtr aFoundSub;
460     for (; anIt.more() && !aFoundSub; anIt.next()) {
461       aFoundSub = anIt.current();
462       if (aResultType.count(aFoundSub->shapeType()) == 0) {
463         theError = "Unable to build a solid";
464         return false;
465       }
466     }
467     if (anIt.more() || !aFoundSub.get()) {
468       theError = "Unable to build a solid";
469       return false;
470     }
471   } else if (aResultType.count(aCompound->shapeType()) == 0) {
472     theError = "Unable to build a solid";
473     return false;
474   }
475   // check the internal faces presence
476   for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
477     if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
478       theError = "Internal faces are not allowed in the resulting solid";
479       return false;
480     }
481   }
482
483   return true;
484 }
485
486
487 //=================================================================================================
488 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
489                                                       const std::list<std::string>& theArguments,
490                                                       Events_InfoMessage& theError) const
491 {
492   if(theArguments.size() != 1) {
493     // LCOV_EXCL_START
494     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
495       "1 parameter(Sketch feature id).";
496     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
497     return false;
498     // LCOV_EXCL_STOP
499   }
500
501   // Get base objects list.
502   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
503     // LCOV_EXCL_START
504     std::string aMsg =
505       "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
506       "%1\"\n Only \"%2\" supported.";
507     Events_InfoMessage("BuildPlugin_Validators", aMsg).
508       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
509     return false;
510     // LCOV_EXCL_STOP
511   }
512   AttributeSelectionListPtr aSelectionList =
513     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
514   if(!aSelectionList.get()) {
515     theError = "Could not get selection list.";
516     return false;
517   }
518
519   // Get base shape.
520   const std::string aBaseShapeId = "base_shape";
521   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
522   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
523
524   if(!aShapeAttrSelection.get()) {
525     theError = "Base shape is empty.";
526     return false;
527   }
528
529   ResultPtr aBaseContext = aShapeAttrSelection->context();
530
531   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
532   if(!aBaseShape.get()) {
533     theError = "Base shape is empty.";
534     return false;
535   }
536
537   GeomAlgoAPI_ShapeBuilder aBuilder;
538   aBuilder.removeInternal(aBaseShape);
539   aBaseShape = aBuilder.shape();
540
541   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
542   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
543   switch(aBaseShape->shapeType()) {
544     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
545     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
546     default: break;
547   }
548
549   // Check selected shapes.
550   GeomValidators_FeatureKind aFeatureKindValidator;
551   std::list<std::string> anArguments;
552   anArguments.push_back(theArguments.front());
553   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
554     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
555     if(!aSelectionAttrInList.get()) {
556       theError = "Empty attribute in list.";
557       return false;
558     }
559
560     // If context of selection same skip.
561     if(aBaseContext == aSelectionAttrInList->context()) {
562       continue;
563     }
564
565     // Check that it is a selection on Sketch.
566     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
567       return false;
568     }
569
570     // Check shape type.
571     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
572     if(!aShapeInList.get()) {
573       aShapeInList = aSelectionAttrInList->context()->shape();
574     }
575     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
576       theError = "Selected shape has unacceptable type.";
577       return false;
578     }
579
580     // Check that shape inside wire or face.
581     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
582       theError = "Selected shape is not inside base face.";
583       return false;
584     }
585   }
586
587   return true;
588 }
589
590
591 //=================================================================================================
592 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
593                                                     const std::list<std::string>& /*theArguments*/,
594                                                     Events_InfoMessage& theError) const
595 {
596   // Get base objects list.
597   if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
598     // LCOV_EXCL_START
599     std::string aMsg =
600       "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
601       "%1\"\n Only \"%2\" supported.";
602     Events_InfoMessage("BuildPlugin_Validators", aMsg).
603       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
604     return false;
605     // LCOV_EXCL_STOP
606   }
607   AttributeSelectionListPtr aSelectionList =
608     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
609   if (!aSelectionList.get()) {
610     theError = "Could not get selection list.";
611     return false;
612   }
613
614   //FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
615
616   // Check selected shapes.
617   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
618     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
619     if (!aSelectionAttrInList.get()) {
620       theError = "Empty attribute in list.";
621       return false;
622     }
623
624     // Check shape exists.
625     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
626     if (!aShapeInList.get()) {
627       theError = "Object has no shape";
628       return false;
629     }
630
631     // Check shape type.
632     GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
633     if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
634       theError = "Incorrect objects selected";
635       return false;
636     }
637   }
638
639   return true;
640 }
641
642
643 //=================================================================================================
644 bool BuildPlugin_ValidatorBaseForVertex::isValid(const AttributePtr& theAttribute,
645                                                  const std::list<std::string>& /*theArguments*/,
646                                                  Events_InfoMessage& theError) const
647 {
648   if (!theAttribute.get()) {
649     theError = "Error: empty selection.";
650     return false;
651   }
652
653   AttributeSelectionListPtr aSelectionList =
654     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
655   if (!aSelectionList.get()) {
656     theError = "Could not get selection list.";
657     return false;
658   }
659
660   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
661     AttributeSelectionPtr aSelectionAttr = aSelectionList->value(anIndex);
662     if (!aSelectionAttr.get()) {
663       theError = "Empty attribute in list.";
664       return false;
665     }
666
667     // Vertex?
668     bool isVertex = false;
669     GeomShapePtr aShape = aSelectionAttr->value();
670     ResultPtr aContext = aSelectionAttr->context();
671     if (!aShape.get() && aContext.get())
672       aShape = aContext->shape();
673     if (aShape.get())
674       isVertex = (aShape->shapeType() == GeomAPI_Shape::VERTEX);
675
676     if (!isVertex) {
677       // Sketch?
678       FeaturePtr aFeature = aSelectionAttr->contextFeature();
679       if (!aFeature.get()) {
680         GeomShapePtr aValue = aSelectionAttr->value();
681         // whole sketch is allowed only
682         if (aContext.get() && !aValue.get()) {
683           aFeature = ModelAPI_Feature::feature(aContext);
684         }
685       }
686
687       if (!aFeature.get()) {
688         theError = "Error: Incorrect selection.";
689         return false;
690       }
691
692       if (aFeature->getKind() != SketchPlugin_Sketch::ID()) {
693         theError = "Error: %1 shape is not allowed for selection.";
694         theError.arg(aFeature->getKind());
695         return false;
696       }
697     }
698   }
699
700   return true;
701 }
702
703 //=================================================================================================
704 bool BuildPlugin_ValidatorExpressionInterpolation::isValid(const AttributePtr& theAttribute,
705                                                    const std::list<std::string>& /*theArguments*/,
706                                                    Events_InfoMessage& theError) const
707 {
708   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
709
710   AttributeStringPtr aStrAttr =
711       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
712   if (!aStrAttr->isInitialized()) {
713     theError = "Attribute \"%1\" is not initialized.";
714     theError.arg(aStrAttr->id());
715     return false;
716   }
717   bool isEmptyExpr = aStrAttr->value().empty();
718   if (isEmptyExpr) {
719     theError = "Expression is empty.";
720     return false;
721   }
722
723   theError = aFeature->string(BuildPlugin_Interpolation::EXPRESSION_ERROR_ID())->value();
724   return theError.empty();
725 }