Salome HOME
Improve the code coverage level
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Validators.cpp
1 // Copyright (C) 2014-2019  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
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultConstruction.h>
24
25 #include <GeomAPI_PlanarEdges.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
29
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeBuilder.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_SketchBuilder.h>
35 #include <GeomAlgoAPI_WireBuilder.h>
36 #include <GeomAlgoAPI_MakeVolume.h>
37 #include <GeomAlgoAPI_Tools.h>
38
39 #include <GeomValidators_FeatureKind.h>
40 #include <GeomValidators_ShapeType.h>
41
42 #include <Events_InfoMessage.h>
43
44 //=================================================================================================
45 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
46                                                 const std::list<std::string>& theArguments,
47                                                 Events_InfoMessage& theError) const
48 {
49   // Get base objects list.
50   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
51     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
52                        "not support attribute type '%1'\nOnly '%2' is supported.";
53     Events_InfoMessage("BuildPlugin_Validators", aMsg).
54       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
55     return false;
56   }
57   AttributeSelectionListPtr aSelectionList =
58     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
59   if(!aSelectionList.get()) {
60     theError = "Could not get selection list.";
61     return false;
62   }
63   if(aSelectionList->size() == 0) {
64     theError = "Empty selection list.";
65     return false;
66   }
67
68   // Collect base shapes.
69   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
70     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
71     if(!aSelection.get()) {
72       theError = "Could not get selection.";
73       return false;
74     }
75     ResultPtr aContext = aSelection->context();
76     if(!aContext.get()) {
77       theError = "Attribute have empty context.";
78       return false;
79     }
80
81     GeomShapePtr aShape = aSelection->value();
82     GeomShapePtr aContextShape = aContext->shape();
83     if(!aShape.get()) {
84       aShape = aContextShape;
85     }
86     if(!aShape.get()) {
87       theError = "Empty shape selected.";
88       return false;
89     }
90
91     // Check that shapes has acceptable type.
92     GeomValidators_ShapeType aValidatorShapeType;
93     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
94       return false;
95     }
96
97     // Check that it is shape on sketch.
98     ResultConstructionPtr aConstruction =
99       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
100     if(aConstruction.get()) {
101       if(aConstruction->isInfinite()) {
102         theError = "Inifinte objects not acceptable.";
103         return false;
104       }
105
106       std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
107         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
108       if(anEdges.get()) {
109         if(aShape->isEqual(aContextShape)) {
110           // It is whole sketch.
111           return false;
112         }
113
114         continue;
115       }
116     }
117   }
118
119   return true;
120 }
121
122 //=================================================================================================
123 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
124                                                const std::list<std::string>& theArguments,
125                                                Events_InfoMessage& theError) const
126 {
127   // Get attribute.
128   if(theArguments.size() != 1) {
129     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
130                        "with 1 parameter (ID of base objects list).";
131     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
132     return false;
133   }
134   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
135   if(!aSelectionList.get()) {
136     theError = "Empty attribute \"%1\".";
137     theError.arg(theArguments.front());
138     return false;
139   }
140
141
142   // Collect base shapes.
143   ListOfShape aListOfShapes;
144   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
145     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
146     GeomShapePtr aShape = aSelection->value();
147     if(!aShape.get()) {
148       if (aSelection->context().get())
149         aShape = aSelection->context()->shape();
150     }
151     if (aShape.get())
152       aListOfShapes.push_back(aShape);
153   }
154
155   // Create wire.
156   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
157   if(!aWire.get()) {
158     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
159     return false;
160   }
161
162   return true;
163 }
164
165 //=================================================================================================
166 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
167                                                const std::list<std::string>& theArguments,
168                                                Events_InfoMessage& theError) const
169 {
170   // Get attribute.
171   if(theArguments.size() != 1) {
172     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
173       "1 parameter (ID of base objects list).";
174     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
175     return false;
176   }
177   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
178   if(!aSelectionList.get()) {
179     theError = "Empty attribute \"%1\".";
180     theError.arg(theArguments.front());
181     return false;
182   }
183
184   bool hasEdgesOrWires = false;
185   bool hasFaces = false;
186
187   // Collect base shapes.
188   ListOfShape anEdges;
189   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
190     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
191     GeomShapePtr aShape = aSelection->value();
192     if(!aShape.get()) {
193       if (!aSelection->context()) {
194         theError = "Objects are not selected.";
195         return false;
196       }
197       aShape = aSelection->context()->shape();
198     }
199     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
200       // skip faces exploding
201       hasFaces = true;
202       continue;
203     }
204
205     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
206       hasEdgesOrWires = true;
207       GeomShapePtr anEdge = anExp.current();
208       anEdges.push_back(anEdge);
209     }
210   }
211
212   if (hasFaces && hasEdgesOrWires) {
213     theError = "Faces and edges/wires should be selected together.";
214     return false;
215   } else if (hasEdgesOrWires && anEdges.empty()) {
216     theError = "Objects are not selected.";
217     return false;
218   }
219
220   // Check that edges does not have intersections.
221   if(anEdges.size() > 1) {
222     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
223     if(!aPaveFiller.isDone()) {
224       theError = "Error while checking if edges intersects.";
225       return false;
226     }
227     GeomShapePtr aSectedEdges = aPaveFiller.shape();
228
229     int anEdgesNum = 0;
230     for(GeomAPI_ShapeExplorer
231         anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
232       anEdgesNum++;
233     }
234     if(anEdgesNum != anEdges.size()) {
235       theError = "Selected objects have intersections.";
236       return false;
237     }
238   }
239
240   if (!anEdges.empty()) {
241     // Check that they are planar.
242     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
243     if(!aPln.get()) {
244       theError = "Selected object(s) should belong to only one plane.";
245       return false;
246     }
247
248     // Check that selected objects have closed contours.
249     GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
250     const ListOfShape& aFaces = aBuilder.faces();
251     if(aFaces.empty()) {
252       theError = "Selected objects do not generate closed contour.";
253       return false;
254     }
255   }
256
257   return true;
258 }
259
260 //=================================================================================================
261 bool BuildPlugin_ValidatorBaseForSolids::isValid(
262   const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
263   Events_InfoMessage& theError) const
264 {
265   // Get base objects list.
266   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
267   if (!aSelectionList.get()) {
268     theError = "Could not get selection list.";
269     return false;
270   }
271   if (aSelectionList->size() == 0) {
272     theError = "Empty selection list.";
273     return false;
274   }
275
276   // Collect base shapes.
277   ListOfShape anOriginalShapes;
278   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
279     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
280     if (!aSelection->context().get()) {
281       theError = "Invalid selection.";
282       return false;
283     }
284     GeomShapePtr aShape = aSelection->value();
285     if (!aShape.get())
286       aShape = aSelection->context()->shape();
287     anOriginalShapes.push_back(aShape);
288   }
289
290   std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
291     new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
292
293   std::string anErr;
294   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
295     theError = anErr;
296     return false;
297   }
298
299   // set of allowed types of results
300   std::set<GeomAPI_Shape::ShapeType> aResultType;
301   std::string aType = theArguments.back();
302   if (aType == "solid")
303     aResultType.insert(GeomAPI_Shape::SOLID);
304   else if (aType == "compsolid") {
305     aResultType.insert(GeomAPI_Shape::COMPSOLID);
306     aResultType.insert(GeomAPI_Shape::SOLID);
307   }
308
309   GeomShapePtr aCompound = anAlgorithm->shape();
310   if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
311     GeomAPI_ShapeIterator anIt(aCompound);
312     GeomShapePtr aFoundSub;
313     for (; anIt.more() && !aFoundSub; anIt.next()) {
314       aFoundSub = anIt.current();
315       if (aResultType.count(aFoundSub->shapeType()) == 0) {
316         theError = "Unable to build a solid";
317         return false;
318       }
319     }
320     if (anIt.more() || !aFoundSub.get()) {
321       theError = "Unable to build a solid";
322       return false;
323     }
324   } else if (aResultType.count(aCompound->shapeType()) == 0) {
325     theError = "Unable to build a solid";
326     return false;
327   }
328   // check the internal faces presence
329   for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
330     if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
331       theError = "Internal faces are not allowed in the resulting solid";
332       return false;
333     }
334   }
335
336   return true;
337 }
338
339
340 //=================================================================================================
341 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
342                                                       const std::list<std::string>& theArguments,
343                                                       Events_InfoMessage& theError) const
344 {
345   if(theArguments.size() != 1) {
346     // LCOV_EXCL_START
347     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
348       "1 parameter(Sketch feature id).";
349     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
350     return false;
351     // LCOV_EXCL_STOP
352   }
353
354   // Get base objects list.
355   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
356     // LCOV_EXCL_START
357     std::string aMsg =
358       "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
359       "%1\"\n Only \"%2\" supported.";
360     Events_InfoMessage("BuildPlugin_Validators", aMsg).
361       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
362     return false;
363     // LCOV_EXCL_STOP
364   }
365   AttributeSelectionListPtr aSelectionList =
366     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
367   if(!aSelectionList.get()) {
368     theError = "Could not get selection list.";
369     return false;
370   }
371
372   // Get base shape.
373   const std::string aBaseShapeId = "base_shape";
374   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
375   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
376
377   if(!aShapeAttrSelection.get()) {
378     theError = "Base shape is empty.";
379     return false;
380   }
381
382   ResultPtr aBaseContext = aShapeAttrSelection->context();
383
384   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
385   if(!aBaseShape.get()) {
386     theError = "Base shape is empty.";
387     return false;
388   }
389
390   GeomAlgoAPI_ShapeBuilder aBuilder;
391   aBuilder.removeInternal(aBaseShape);
392   aBaseShape = aBuilder.shape();
393
394   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
395   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
396   switch(aBaseShape->shapeType()) {
397     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
398     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
399     default: break;
400   }
401
402   // Check selected shapes.
403   GeomValidators_FeatureKind aFeatureKindValidator;
404   std::list<std::string> anArguments;
405   anArguments.push_back(theArguments.front());
406   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
407     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
408     if(!aSelectionAttrInList.get()) {
409       theError = "Empty attribute in list.";
410       return false;
411     }
412
413     // If context of selection same skip.
414     if(aBaseContext == aSelectionAttrInList->context()) {
415       continue;
416     }
417
418     // Check that it is a selection on Sketch.
419     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
420       return false;
421     }
422
423     // Check shape type.
424     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
425     if(!aShapeInList.get()) {
426       aShapeInList = aSelectionAttrInList->context()->shape();
427     }
428     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
429       theError = "Selected shape has unacceptable type.";
430       return false;
431     }
432
433     // Check that shape inside wire or face.
434     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
435       theError = "Selected shape is not inside base face.";
436       return false;
437     }
438   }
439
440   return true;
441 }
442
443
444 //=================================================================================================
445 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
446                                                       const std::list<std::string>& theArguments,
447                                                       Events_InfoMessage& theError) const
448 {
449   // Get base objects list.
450   if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
451     // LCOV_EXCL_START
452     std::string aMsg =
453       "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
454       "%1\"\n Only \"%2\" supported.";
455     Events_InfoMessage("BuildPlugin_Validators", aMsg).
456       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
457     return false;
458     // LCOV_EXCL_STOP
459   }
460   AttributeSelectionListPtr aSelectionList =
461     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
462   if (!aSelectionList.get()) {
463     theError = "Could not get selection list.";
464     return false;
465   }
466
467   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
468
469   // Check selected shapes.
470   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
471     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
472     if (!aSelectionAttrInList.get()) {
473       theError = "Empty attribute in list.";
474       return false;
475     }
476
477     // Check shape exists.
478     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
479     if (!aShapeInList.get()) {
480       theError = "Object has no shape";
481       return false;
482     }
483
484     // Check shape type.
485     GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
486     if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
487       theError = "Incorrect objects selected";
488       return false;
489     }
490   }
491
492   return true;
493 }