Salome HOME
Update copyrights
[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     ListOfShape aFaces;
250     GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
251                                            aPln->direction(), anEdges, aFaces);
252     if(aFaces.empty()) {
253       theError = "Selected objects do not generate closed contour.";
254       return false;
255     }
256   }
257
258   return true;
259 }
260
261 //=================================================================================================
262 bool BuildPlugin_ValidatorBaseForSolids::isValid(
263   const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
264   Events_InfoMessage& theError) const
265 {
266   // Get base objects list.
267   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
268   if (!aSelectionList.get()) {
269     theError = "Could not get selection list.";
270     return false;
271   }
272   if (aSelectionList->size() == 0) {
273     theError = "Empty selection list.";
274     return false;
275   }
276
277   // Collect base shapes.
278   ListOfShape anOriginalShapes;
279   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
280     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
281     if (!aSelection->context().get()) {
282       theError = "Invalid selection.";
283       return false;
284     }
285     GeomShapePtr aShape = aSelection->value();
286     if (!aShape.get())
287       aShape = aSelection->context()->shape();
288     anOriginalShapes.push_back(aShape);
289   }
290
291   std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(
292     new GeomAlgoAPI_MakeVolume(anOriginalShapes, false));
293
294   std::string anErr;
295   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgorithm, "MakeVolume", anErr)) {
296     theError = anErr;
297     return false;
298   }
299
300   // set of allowed types of results
301   std::set<GeomAPI_Shape::ShapeType> aResultType;
302   std::string aType = theArguments.back();
303   if (aType == "solid")
304     aResultType.insert(GeomAPI_Shape::SOLID);
305   else if (aType == "compsolid") {
306     aResultType.insert(GeomAPI_Shape::COMPSOLID);
307     aResultType.insert(GeomAPI_Shape::SOLID);
308   }
309
310   GeomShapePtr aCompound = anAlgorithm->shape();
311   if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
312     GeomAPI_ShapeIterator anIt(aCompound);
313     GeomShapePtr aFoundSub;
314     for (; anIt.more() && !aFoundSub; anIt.next()) {
315       aFoundSub = anIt.current();
316       if (aResultType.count(aFoundSub->shapeType()) == 0) {
317         theError = "Unable to build a solid";
318         return false;
319       }
320     }
321     if (anIt.more() || !aFoundSub.get()) {
322       theError = "Unable to build a solid";
323       return false;
324     }
325   } else if (aResultType.count(aCompound->shapeType()) == 0) {
326     theError = "Unable to build a solid";
327     return false;
328   }
329   // check the internal faces presence
330   for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
331     if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
332       theError = "Internal faces are not allowed in the resulting solid";
333       return false;
334     }
335   }
336
337   return true;
338 }
339
340
341 //=================================================================================================
342 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
343                                                       const std::list<std::string>& theArguments,
344                                                       Events_InfoMessage& theError) const
345 {
346   if(theArguments.size() != 1) {
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   }
352
353   // Get base objects list.
354   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
355     std::string aMsg =
356       "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
357       "%1\"\n Only \"%2\" supported.";
358     Events_InfoMessage("BuildPlugin_Validators", aMsg).
359       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
360     return false;
361   }
362   AttributeSelectionListPtr aSelectionList =
363     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
364   if(!aSelectionList.get()) {
365     theError = "Could not get selection list.";
366     return false;
367   }
368
369   // Get base shape.
370   const std::string aBaseShapeId = "base_shape";
371   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
372   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
373
374   if(!aShapeAttrSelection.get()) {
375     theError = "Base shape is empty.";
376     return false;
377   }
378
379   ResultPtr aBaseContext = aShapeAttrSelection->context();
380
381   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
382   if(!aBaseShape.get()) {
383     theError = "Base shape is empty.";
384     return false;
385   }
386
387   GeomAlgoAPI_ShapeBuilder aBuilder;
388   aBuilder.removeInternal(aBaseShape);
389   aBaseShape = aBuilder.shape();
390
391   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
392   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
393   switch(aBaseShape->shapeType()) {
394     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
395     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
396     default: break;
397   }
398
399   // Check selected shapes.
400   GeomValidators_FeatureKind aFeatureKindValidator;
401   std::list<std::string> anArguments;
402   anArguments.push_back(theArguments.front());
403   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
404     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
405     if(!aSelectionAttrInList.get()) {
406       theError = "Empty attribute in list.";
407       return false;
408     }
409
410     // If context of selection same skip.
411     if(aBaseContext == aSelectionAttrInList->context()) {
412       continue;
413     }
414
415     // Check that it is a selection on Sketch.
416     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
417       return false;
418     }
419
420     // Check shape type.
421     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
422     if(!aShapeInList.get()) {
423       aShapeInList = aSelectionAttrInList->context()->shape();
424     }
425     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
426       theError = "Selected shape has unacceptable type.";
427       return false;
428     }
429
430     // Check that shape inside wire or face.
431     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
432       theError = "Selected shape is not inside base face.";
433       return false;
434     }
435   }
436
437   return true;
438 }
439
440
441 //=================================================================================================
442 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
443                                                       const std::list<std::string>& theArguments,
444                                                       Events_InfoMessage& theError) const
445 {
446   // Get base objects list.
447   if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
448     std::string aMsg =
449       "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
450       "%1\"\n Only \"%2\" supported.";
451     Events_InfoMessage("BuildPlugin_Validators", aMsg).
452       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
453     return false;
454   }
455   AttributeSelectionListPtr aSelectionList =
456     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
457   if (!aSelectionList.get()) {
458     theError = "Could not get selection list.";
459     return false;
460   }
461
462   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
463
464   // Check selected shapes.
465   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
466     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
467     if (!aSelectionAttrInList.get()) {
468       theError = "Empty attribute in list.";
469       return false;
470     }
471
472     // Check shape exists.
473     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
474     if (!aShapeInList.get()) {
475       theError = "Object has no shape";
476       return false;
477     }
478
479     // Check shape type.
480     GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
481     if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
482       theError = "Incorrect objects selected";
483       return false;
484     }
485   }
486
487   return true;
488 }