]> SALOME platform Git repositories - modules/shaper.git/blob - src/BuildPlugin/BuildPlugin_Validators.cpp
Salome HOME
High level objects history implementation for BuildPlugin features.
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_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 "BuildPlugin_Validators.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultConstruction.h>
25
26 #include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
30
31 #include <GeomAlgoAPI_CompoundBuilder.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeBuilder.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35 #include <GeomAlgoAPI_SketchBuilder.h>
36 #include <GeomAlgoAPI_WireBuilder.h>
37 #include <GeomAlgoAPI_MakeVolume.h>
38 #include <GeomAlgoAPI_Tools.h>
39
40 #include <GeomValidators_FeatureKind.h>
41 #include <GeomValidators_ShapeType.h>
42
43 #include <Events_InfoMessage.h>
44
45 //=================================================================================================
46 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
47                                                 const std::list<std::string>& theArguments,
48                                                 Events_InfoMessage& theError) const
49 {
50   // Get base objects list.
51   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
52     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
53                        "not support attribute type '%1'\nOnly '%2' is supported.";
54     Events_InfoMessage("BuildPlugin_Validators", aMsg).
55       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
56     return false;
57   }
58   AttributeSelectionListPtr aSelectionList =
59     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
60   if(!aSelectionList.get()) {
61     theError = "Could not get selection list.";
62     return false;
63   }
64   if(aSelectionList->size() == 0) {
65     theError = "Empty selection list.";
66     return false;
67   }
68
69   // Collect base shapes.
70   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
71     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
72     if(!aSelection.get()) {
73       theError = "Could not get selection.";
74       return false;
75     }
76     ResultPtr aContext = aSelection->context();
77     if(!aContext.get()) {
78       theError = "Attribute have empty context.";
79       return false;
80     }
81
82     GeomShapePtr aShape = aSelection->value();
83     GeomShapePtr aContextShape = aContext->shape();
84     if(!aShape.get()) {
85       aShape = aContextShape;
86     }
87     if(!aShape.get()) {
88       theError = "Empty shape selected.";
89       return false;
90     }
91
92     // Check that shapes has acceptable type.
93     GeomValidators_ShapeType aValidatorShapeType;
94     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
95       return false;
96     }
97
98     // Check that it is shape on sketch.
99     ResultConstructionPtr aConstruction =
100       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
101     if(aConstruction.get()) {
102       if(aConstruction->isInfinite()) {
103         theError = "Inifinte objects not acceptable.";
104         return false;
105       }
106
107       std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
108         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
109       if(anEdges.get()) {
110         if(aShape->isEqual(aContextShape)) {
111           // It is whole sketch.
112           return false;
113         }
114
115         continue;
116       }
117     }
118   }
119
120   return true;
121 }
122
123 //=================================================================================================
124 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
125                                                const std::list<std::string>& theArguments,
126                                                Events_InfoMessage& theError) const
127 {
128   // Get attribute.
129   if(theArguments.size() != 1) {
130     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
131                        "with 1 parameter (ID of base objects list).";
132     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
133     return false;
134   }
135   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
136   if(!aSelectionList.get()) {
137     theError = "Empty attribute \"%1\".";
138     theError.arg(theArguments.front());
139     return false;
140   }
141
142
143   // Collect base shapes.
144   ListOfShape aListOfShapes;
145   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
146     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
147     GeomShapePtr aShape = aSelection->value();
148     if(!aShape.get()) {
149       if (aSelection->context().get())
150         aShape = aSelection->context()->shape();
151     }
152     if (aShape.get())
153       aListOfShapes.push_back(aShape);
154   }
155
156   // Create wire.
157   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
158   if(!aWire.get()) {
159     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
160     return false;
161   }
162
163   return true;
164 }
165
166 //=================================================================================================
167 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
168                                                const std::list<std::string>& theArguments,
169                                                Events_InfoMessage& theError) const
170 {
171   // Get attribute.
172   if(theArguments.size() != 1) {
173     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
174       "1 parameter (ID of base objects list).";
175     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
176     return false;
177   }
178   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
179   if(!aSelectionList.get()) {
180     theError = "Empty attribute \"%1\".";
181     theError.arg(theArguments.front());
182     return false;
183   }
184
185   bool hasEdgesOrWires = false;
186   bool hasFaces = false;
187
188   // Collect base shapes.
189   ListOfShape anEdges;
190   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
191     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
192     GeomShapePtr aShape = aSelection->value();
193     if(!aShape.get()) {
194       if (!aSelection->context()) {
195         theError = "Objects are not selected.";
196         return false;
197       }
198       aShape = aSelection->context()->shape();
199     }
200     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
201       // skip faces exploding
202       hasFaces = true;
203       continue;
204     }
205
206     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
207       hasEdgesOrWires = true;
208       GeomShapePtr anEdge = anExp.current();
209       anEdges.push_back(anEdge);
210     }
211   }
212
213   if (hasFaces && hasEdgesOrWires) {
214     theError = "Faces and edges/wires should be selected together.";
215     return false;
216   } else if (hasEdgesOrWires && anEdges.empty()) {
217     theError = "Objects are not selected.";
218     return false;
219   }
220
221   // Check that edges does not have intersections.
222   if(anEdges.size() > 1) {
223     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
224     if(!aPaveFiller.isDone()) {
225       theError = "Error while checking if edges intersects.";
226       return false;
227     }
228     GeomShapePtr aSectedEdges = aPaveFiller.shape();
229
230     int anEdgesNum = 0;
231     for(GeomAPI_ShapeExplorer
232         anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
233       anEdgesNum++;
234     }
235     if(anEdgesNum != anEdges.size()) {
236       theError = "Selected objects have intersections.";
237       return false;
238     }
239   }
240
241   if (!anEdges.empty()) {
242     // Check that they are planar.
243     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
244     if(!aPln.get()) {
245       theError = "Selected object(s) should belong to only one plane.";
246       return false;
247     }
248
249     // Check that selected objects have closed contours.
250     GeomAlgoAPI_SketchBuilder aBuilder(aPln, anEdges);
251     const ListOfShape& aFaces = aBuilder.faces();
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 }