Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Validators.cpp
4 // Created:     22 March 2016
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_Validators.h"
8
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_AttributeReference.h>
14
15 #include <Events_Error.h>
16
17 #include <GeomValidators_FeatureKind.h>
18 #include <GeomValidators_ShapeType.h>
19
20 #include <GeomAPI_PlanarEdges.h>
21 #include <GeomAlgoAPI_WireBuilder.h>
22
23 //=================================================================================================
24 bool FeaturesPlugin_ValidatorPipeLocations::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
25                                                     const std::list<std::string>& theArguments,
26                                                     std::string& theError) const
27 {
28   static const std::string aCreationMethodID = "creation_method";
29   static const std::string aBaseObjectsID = "base_objects";
30   static const std::string aLocationsID = "locations_objects";
31
32   if(theFeature->getKind() != "Pipe") {
33     theError = "Feature \"" + theFeature->getKind() + "\" does not supported by this validator.";
34     return false;
35   }
36
37   AttributeStringPtr aCreationMethodAttr = theFeature->string(aCreationMethodID);
38   if(!aCreationMethodAttr.get()) {
39     theError = "Could not get \"" + aCreationMethodID + "\" attribute.";
40     return false;
41   }
42
43   if(aCreationMethodAttr->value() != "locations") {
44     return true;
45   }
46
47   AttributeSelectionListPtr aBaseObjectsSelectionList = theFeature->selectionList(aBaseObjectsID);
48   if(!aBaseObjectsSelectionList.get()) {
49     theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
50     return false;
51   }
52
53   AttributeSelectionListPtr aLocationsSelectionList = theFeature->selectionList(aLocationsID);
54   if(!aLocationsSelectionList.get()) {
55     theError = "Could not get \"" + aBaseObjectsID + "\" attribute.";
56     return false;
57   }
58
59   if(aLocationsSelectionList->size() > 0 && aLocationsSelectionList->size() != aBaseObjectsSelectionList->size()) {
60     theError = "Number of locations should be the same as base objects.";
61     return false;
62   }
63
64   return true;
65 }
66
67 //=================================================================================================
68 bool FeaturesPlugin_ValidatorPipeLocations::isNotObligatory(std::string theFeature, std::string theAttribute)
69 {
70   return false;
71 }
72
73 //=================================================================================================
74 bool FeaturesPlugin_ValidatorBaseForGeneration::isValid(const AttributePtr& theAttribute,
75                                                         const std::list<std::string>& theArguments,
76                                                         std::string& theError) const
77 {
78   if(theArguments.empty()) {
79     theError = "Validator parameters is empty.";
80     return false;
81   }
82
83   // Checking attribute.
84   if(!isValidAttribute(theAttribute, theArguments, theError)) {
85     if(theError.empty()) {
86       theError = "Attribute contains unacceptable shape.";
87     }
88     return false;
89   }
90
91   return true;
92 }
93
94 //=================================================================================================
95 bool FeaturesPlugin_ValidatorBaseForGeneration::isValidAttribute(const AttributePtr& theAttribute,
96                                                                  const std::list<std::string>& theArguments,
97                                                                  std::string& theError) const
98 {
99   if(!theAttribute.get()) {
100     theError = "Empty attribute.";
101     return false;
102   }
103
104   std::string anAttributeType = theAttribute->attributeType();
105   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
106     AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
107     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
108       // If at least one attribute is invalid, the result is false.
109       if(!isValidAttribute(aListAttr->value(anIndex), theArguments, theError)) {
110         return false;
111       }
112     }
113   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
114     // Getting context.
115     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
116     ResultPtr aContext = anAttr->context();
117     if(!aContext.get()) {
118       theError = "Attribute have empty context.";
119       return false;
120     }
121
122     GeomShapePtr aShape = anAttr->value();
123     GeomShapePtr aContextShape = aContext->shape();
124     if(!aShape.get()) {
125       aShape = aContextShape;
126     }
127     if(!aShape.get()) {
128       theError = "Empty shape selected";
129       return false;
130     }
131
132     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
133     if(aConstruction.get()) {
134       // Construciotn selected. Check that is is not infinite.
135       if(aConstruction->isInfinite()) {
136         theError = "Infinite constructions is not allowed as base.";
137         return false;
138       }
139
140       if(aShape->isEqual(aContextShape)) {
141         // Whole construction selected. Check that it have faces.
142         if(aConstruction->facesNum() > 0) {
143           return true;
144         }
145       } else {
146         // Shape on construction selected. Check that it is a face or wire.
147         if(aShape->shapeType() == GeomAPI_Shape::WIRE || aShape->shapeType() == GeomAPI_Shape::FACE) {
148           return true;
149         }
150       }
151
152       return false;
153     }
154
155     if(!aShape->isEqual(aContextShape)) {
156       // Local selection on body does not allowed.
157       theError = "Selected shape is in the local selection. Only global selection is allowed.";
158       return false;
159     }
160
161     // Check that object is a shape with allowed type.
162     GeomValidators_ShapeType aShapeTypeValidator;
163     if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
164       theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
165 whole sketch(if it has at least one face), and whole objects with shape types: ";
166       std::list<std::string>::const_iterator anIt = theArguments.cbegin();
167       theError += *anIt;
168       for(++anIt; anIt != theArguments.cend(); ++anIt) {
169         theError += ", " + *anIt;
170       }
171       return false;
172     }
173
174   } else {
175     theError = "Following attribute does not supported: " + anAttributeType + ".";
176     return false;
177   }
178
179   return true;
180 }
181
182 //=================================================================================================
183 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
184                                                         const std::list<std::string>& theArguments,
185                                                         std::string& theError) const
186 {
187   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
188     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
189     return false;
190   }
191   if (theArguments.size() != 2) {
192     theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
193     return false;
194   }
195   // first argument is for the base attribute, second - for skipping feature kind
196   std::list<std::string>::const_iterator anIt = theArguments.begin();
197   std::string aBaseAttributeId = *anIt;
198   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
199   AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
200   if (!aBaseAttribute.get()) {
201     theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
202     return false;
203   }
204   if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
205     // this validator is not necessary anymore
206     return true;
207
208   anIt++;
209   std::string aFeatureAttributeKind = *anIt;
210   GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
211   // check whether the selection is on the sketch
212   std::list<std::string> anArguments;
213   anArguments.push_back(aFeatureAttributeKind);
214
215   bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
216   bool aPlanarFace = false;
217   // check if selection has Face selected
218   GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
219   anArguments.clear();
220   anArguments.push_back("face");
221   aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
222
223   bool aValid = !aFeatureKind && aPlanarFace;
224   return aValid;
225 }
226
227 //=================================================================================================
228 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
229                                                  const std::list<std::string>& theArguments,
230                                                  std::string& theError) const
231 {
232   if(theArguments.size() != 2) {
233     theError = "Validator should be used with 2 parameters for extrusion.";
234     return false;
235   }
236
237   std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
238
239   AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
240   ++anArgsIt;
241
242   if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
243     return true;
244   }
245
246   AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
247   if(!aSelAttr.get()) {
248     theError = "Could not get selection attribute \"" + *anArgsIt + "\".";
249     return false;
250   }
251
252   GeomShapePtr aShape = aSelAttr->value();
253   if(!aShape.get()) {
254     ResultPtr aContext = aSelAttr->context();
255     if(!aContext.get()) {
256       theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
257       return false;
258     }
259
260     aShape = aContext->shape();
261   }
262
263   if(!aShape.get()) {
264     theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
265     return false;
266   }
267
268   return true;
269 }
270
271 //=================================================================================================
272 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
273 {
274   return false;
275 }
276
277 //=================================================================================================
278 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
279                                                             std::string& theError) const
280 {
281   if(!theAttribute.get()) {
282     return true;
283   }
284
285   std::string anAttributeType = theAttribute->attributeType();
286   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
287     AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
288     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
289       // If at least one attribute is invalid, the result is false.
290       if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
291         return false;
292       }
293     }
294   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
295     // Getting context.
296     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
297     ResultPtr aContext = anAttr->context();
298     if(!aContext.get()) {
299       return false;
300     }
301
302     GeomShapePtr aShape = anAttr->value();
303     GeomShapePtr aContextShape = aContext->shape();
304     if(!aShape.get()) {
305       aShape = aContextShape;
306     }
307     if(!aShape.get()) {
308       return false;
309     }
310
311     if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
312        aShape->shapeType() == GeomAPI_Shape::EDGE ||
313        !aShape->isPlanar()) {
314       return false;
315     }
316   } else {
317     return false;
318   }
319
320   return true;
321 }
322
323 //=================================================================================================
324 bool FeaturesPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
325                                                   const std::list<std::string>& theArguments,
326                                                   std::string& theError) const
327 {
328   // Get base objects list.
329   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
330     Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
331       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
332     return false;
333   }
334   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
335   if(!aSelectionList.get()) {
336     theError = "Could not get selection list.";
337     return false;
338   }
339   if(aSelectionList->size() == 0) {
340     theError = "Empty selection list.";
341     return false;
342   }
343
344   // Collect base shapes.
345   ListOfShape aListOfShapes;
346   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
347     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
348     if(!aSelection.get()) {
349       theError = "Could not get selection.";
350       return false;
351     }
352     ResultPtr aContext = aSelection->context();
353     if(!aContext.get()) {
354       theError = "Attribute have empty context.";
355       return false;
356     }
357
358     GeomShapePtr aShape = aSelection->value();
359     GeomShapePtr aContextShape = aContext->shape();
360     if(!aShape.get()) {
361       aShape = aContextShape;
362     }
363     if(!aShape.get()) {
364       theError = "Empty shape selected.";
365       return false;
366     }
367
368     // Check that shape has acceptable type.
369     if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
370       theError = "Selected shape has wrong type. Only edges and wires acceptable.";
371       return false;
372     }
373
374     // Check that it is edge on sketch.
375     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
376     if(aConstruction.get()) {
377       if(aConstruction->isInfinite()) {
378         theError = "Inifinte objects not acceptable.";
379         return false;
380       }
381
382       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
383       if(!anEdges.get()) {
384         // It is not an edge on the sketch.
385         // Check that it is not local selection.
386         if(!aShape->isEqual(aContextShape)) {
387           // Local selection on body does not allowed.
388           theError = "Selected shape is in the local selection. Only global selection is allowed.";
389           return false;
390         }
391       }
392     }
393
394     aListOfShapes.push_back(aShape);
395   }
396
397   // Create wire.
398   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
399   if(!aWire.get()) {
400     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
401     return false;
402   }
403
404   return true;
405 }