]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Validators.cpp
Salome HOME
Issue #1369: add contour functionality
[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
153     if(!aShape->isEqual(aContextShape)) {
154       // Local selection on body does not allowed.
155       theError = "Selected shape is in the local selection. Only global selection is allowed.";
156       return false;
157     }
158
159     // Check that object is a shape with allowed type.
160     GeomValidators_ShapeType aShapeTypeValidator;
161     if(!aShapeTypeValidator.isValid(anAttr, theArguments, theError)) {
162       theError = "Selected shape has unacceptable type. Acceptable types are: faces or wires on sketch, \
163 whole sketch(if it has at least one face), and whole objects with shape types: ";
164       std::list<std::string>::const_iterator anIt = theArguments.cbegin();
165       theError += *anIt;
166       for(++anIt; anIt != theArguments.cend(); ++anIt) {
167         theError += ", " + *anIt;
168       }
169       return false;
170     }
171
172   } else {
173     theError = "Following attribute does not supported: " + anAttributeType + ".";
174     return false;
175   }
176
177   return true;
178 }
179
180 //=================================================================================================
181 bool FeaturesPlugin_ValidatorCompositeLauncher::isValid(const AttributePtr& theAttribute,
182                                                         const std::list<std::string>& theArguments,
183                                                         std::string& theError) const
184 {
185   if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
186     theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
187     return false;
188   }
189   if (theArguments.size() != 2) {
190     theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
191     return false;
192   }
193   // first argument is for the base attribute, second - for skipping feature kind
194   std::list<std::string>::const_iterator anIt = theArguments.begin();
195   std::string aBaseAttributeId = *anIt;
196   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
197   AttributePtr aBaseAttribute = aFeature->attribute(aBaseAttributeId);
198   if (!aBaseAttribute.get()) {
199     theError = "Wrong parameters in XML definition for " + theAttribute->attributeType() + " type";
200     return false;
201   }
202   if (aBaseAttribute->isInitialized()) // when base list of composite feature is already filled,
203     // this validator is not necessary anymore
204     return true;
205
206   anIt++;
207   std::string aFeatureAttributeKind = *anIt;
208   GeomValidators_FeatureKind* aValidator = new GeomValidators_FeatureKind();
209   // check whether the selection is on the sketch
210   std::list<std::string> anArguments;
211   anArguments.push_back(aFeatureAttributeKind);
212
213   bool aFeatureKind = aValidator->isValid(theAttribute, theArguments, theError);
214   bool aPlanarFace = false;
215   // check if selection has Face selected
216   GeomValidators_ShapeType* aShapeType = new GeomValidators_ShapeType();
217   anArguments.clear();
218   anArguments.push_back("face");
219   aPlanarFace = aShapeType->isValid(theAttribute, anArguments, theError);
220
221   bool aValid = !aFeatureKind && aPlanarFace;
222   return aValid;
223 }
224
225 //=================================================================================================
226 bool FeaturesPlugin_ValidatorCanBeEmpty::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
227                                                  const std::list<std::string>& theArguments,
228                                                  std::string& theError) const
229 {
230   if(theArguments.size() != 2) {
231     theError = "Validator should be used with 2 parameters for extrusion.";
232     return false;
233   }
234
235   std::list<std::string>::const_iterator anArgsIt = theArguments.begin(), aLast = theArguments.end();
236
237   AttributePtr aCheckAttribute = theFeature->attribute(*anArgsIt);
238   ++anArgsIt;
239
240   if(isShapesCanBeEmpty(aCheckAttribute, theError)) {
241     return true;
242   }
243
244   AttributeSelectionPtr aSelAttr = theFeature->selection(*anArgsIt);
245   if(!aSelAttr.get()) {
246     theError = "Could not get selection attribute \"" + *anArgsIt + "\".";
247     return false;
248   }
249
250   GeomShapePtr aShape = aSelAttr->value();
251   if(!aShape.get()) {
252     ResultPtr aContext = aSelAttr->context();
253     if(!aContext.get()) {
254       theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
255       return false;
256     }
257
258     aShape = aContext->shape();
259   }
260
261   if(!aShape.get()) {
262     theError = "Selection attribute \"" + *anArgsIt + "\" can not be empty.";
263     return false;
264   }
265
266   return true;
267 }
268
269 //=================================================================================================
270 bool FeaturesPlugin_ValidatorCanBeEmpty::isNotObligatory(std::string theFeature, std::string theAttribute)
271 {
272   return false;
273 }
274
275 //=================================================================================================
276 bool FeaturesPlugin_ValidatorCanBeEmpty::isShapesCanBeEmpty(const AttributePtr& theAttribute,
277                                                             std::string& theError) const
278 {
279   if(!theAttribute.get()) {
280     return true;
281   }
282
283   std::string anAttributeType = theAttribute->attributeType();
284   if(anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
285     AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
286     for(int anIndex = 0; anIndex < aListAttr->size(); ++anIndex) {
287       // If at least one attribute is invalid, the result is false.
288       if(!isShapesCanBeEmpty(aListAttr->value(anIndex), theError)) {
289         return false;
290       }
291     }
292   } else if(anAttributeType == ModelAPI_AttributeSelection::typeId()) {
293     // Getting context.
294     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
295     ResultPtr aContext = anAttr->context();
296     if(!aContext.get()) {
297       return false;
298     }
299
300     GeomShapePtr aShape = anAttr->value();
301     GeomShapePtr aContextShape = aContext->shape();
302     if(!aShape.get()) {
303       aShape = aContextShape;
304     }
305     if(!aShape.get()) {
306       return false;
307     }
308
309     if(aShape->shapeType() == GeomAPI_Shape::VERTEX ||
310        aShape->shapeType() == GeomAPI_Shape::EDGE ||
311        !aShape->isPlanar()) {
312       return false;
313     }
314   } else {
315     return false;
316   }
317
318   return true;
319 }
320
321 //=================================================================================================
322 bool FeaturesPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
323                                                   const std::list<std::string>& theArguments,
324                                                   std::string& theError) const
325 {
326   // Get base objects list.
327   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
328     Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
329       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
330     return false;
331   }
332   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
333   if(!aSelectionList.get()) {
334     theError = "Could not get selection list.";
335     return false;
336   }
337   if(aSelectionList->size() == 0) {
338     theError = "Empty selection list.";
339     return false;
340   }
341
342   // Collect base shapes.
343   ListOfShape aListOfShapes;
344   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
345     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
346     if(!aSelection.get()) {
347       theError = "Could not get selection.";
348       return false;
349     }
350     ResultPtr aContext = aSelection->context();
351     if(!aContext.get()) {
352       theError = "Attribute have empty context.";
353       return false;
354     }
355
356     GeomShapePtr aShape = aSelection->value();
357     GeomShapePtr aContextShape = aContext->shape();
358     if(!aShape.get()) {
359       aShape = aContextShape;
360     }
361     if(!aShape.get()) {
362       theError = "Empty shape selected.";
363       return false;
364     }
365
366     // Check that shape has acceptable type.
367     if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
368       theError = "Selected shape has wrong type. Only edges and wires acceptable.";
369       return false;
370     }
371
372     // Check that it is edge on sketch.
373     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
374     if(aConstruction.get()) {
375       if(aConstruction->isInfinite()) {
376         theError = "Inifinte objects not acceptable.";
377         return false;
378       }
379
380       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
381       if(!anEdges.get()) {
382         // It is not an edge on the sketch.
383         // Check that it is not local selection.
384         if(!aShape->isEqual(aContextShape)) {
385           // Local selection on body does not allowed.
386           theError = "Selected shape is in the local selection. Only global selection is allowed.";
387           return false;
388         }
389       }
390     }
391
392     aListOfShapes.push_back(aShape);
393   }
394
395   // Create wire.
396   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
397   if(!aWire.get()) {
398     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
399     return false;
400   }
401
402   return true;
403 }