Salome HOME
Replace of Events_Error by Events_InfoMessage. Provide storing of non translated...
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        BuildPlugin_Validators.cpp
4 // Created:     22 March 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Validators.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultConstruction.h>
11
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_ShapeExplorer.h>
15
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <GeomAlgoAPI_PaveFiller.h>
18 #include <GeomAlgoAPI_ShapeBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAlgoAPI_SketchBuilder.h>
21 #include <GeomAlgoAPI_WireBuilder.h>
22
23 #include <GeomValidators_FeatureKind.h>
24 #include <GeomValidators_ShapeType.h>
25
26 #include <Events_InfoMessage.h>
27
28 //=================================================================================================
29 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
30                                                 const std::list<std::string>& theArguments,
31                                                 std::string& theError) const
32 {
33   // Get base objects list.
34   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
35     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type '%1'\nOnly '%2' is supported.";
36     Events_InfoMessage("BuildPlugin_Validators", aMsg).
37       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
38     return false;
39   }
40   AttributeSelectionListPtr aSelectionList =
41     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
42   if(!aSelectionList.get()) {
43     theError = "Could not get selection list.";
44     return false;
45   }
46   if(aSelectionList->size() == 0) {
47     theError = "Empty selection list.";
48     return false;
49   }
50
51   // Collect base shapes.
52   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
53     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
54     if(!aSelection.get()) {
55       theError = "Could not get selection.";
56       return false;
57     }
58     ResultPtr aContext = aSelection->context();
59     if(!aContext.get()) {
60       theError = "Attribute have empty context.";
61       return false;
62     }
63
64     GeomShapePtr aShape = aSelection->value();
65     GeomShapePtr aContextShape = aContext->shape();
66     if(!aShape.get()) {
67       aShape = aContextShape;
68     }
69     if(!aShape.get()) {
70       theError = "Empty shape selected.";
71       return false;
72     }
73
74     // Check that shapes has acceptable type.
75     GeomValidators_ShapeType aValidatorShapeType;
76     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
77       return false;
78     }
79
80     // Check that it is shape on sketch.
81     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
82     if(aConstruction.get()) {
83       if(aConstruction->isInfinite()) {
84         theError = "Inifinte objects not acceptable.";
85         return false;
86       }
87
88       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
89       if(anEdges.get()) {
90         if(aShape->isEqual(aContextShape)) {
91           // It is whole sketch.
92           return false;
93         }
94
95         continue;
96       }
97     }
98
99     if(!aShape->isEqual(aContextShape)) {
100       // Local selection on body does not allowed.
101       theError = "Selected shape is in the local selection. Only global selection is allowed.";
102       return false;
103     }
104   }
105
106   return true;
107 }
108
109 //=================================================================================================
110 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
111                                                const std::list<std::string>& theArguments,
112                                                std::string& theError) const
113 {
114   // Get attribute.
115   if(theArguments.size() != 1) {
116     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list).";
117     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
118     return false;
119   }
120   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
121   if(!aSelectionList.get()) {
122     theError = "Empty attribute \"" + theArguments.front() + "\".";
123     return false;
124   }
125
126
127   // Collect base shapes.
128   ListOfShape aListOfShapes;
129   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
130     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
131     GeomShapePtr aShape = aSelection->value();
132     if(!aShape.get()) {
133       aShape = aSelection->context()->shape();
134     }
135     aListOfShapes.push_back(aShape);
136   }
137
138   // Create wire.
139   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
140   if(!aWire.get()) {
141     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
142     return false;
143   }
144
145   return true;
146 }
147
148 //=================================================================================================
149 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature, std::string theAttribute)
150 {
151   return false;
152 }
153
154 //=================================================================================================
155 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
156                                                const std::list<std::string>& theArguments,
157                                                std::string& theError) const
158 {
159   // Get attribute.
160   if(theArguments.size() != 1) {
161     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
162       "1 parameter (ID of base objects list).";
163     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
164     return false;
165   }
166   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
167   if(!aSelectionList.get()) {
168     theError = "Empty attribute \"" + theArguments.front() + "\".";
169     return false;
170   }
171
172   // Collect base shapes.
173   ListOfShape anEdges;
174   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
175     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
176     GeomShapePtr aShape = aSelection->value();
177     if(!aShape.get()) {
178       aShape = aSelection->context()->shape();
179     }
180     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
181       GeomShapePtr anEdge = anExp.current();
182       anEdges.push_back(anEdge);
183     }
184   }
185
186   // Check that edges does not have intersections.
187   if(anEdges.size() > 1) {
188     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
189     if(!aPaveFiller.isDone()) {
190       theError = "Error while checking if edges intersects.";
191       return false;
192     }
193     GeomShapePtr aSectedEdges = aPaveFiller.shape();
194
195     int anEdgesNum = 0;
196     for(GeomAPI_ShapeExplorer anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
197       anEdgesNum++;
198     }
199     if(anEdgesNum != anEdges.size()) {
200       theError = "Selected objects have intersections.";
201       return false;
202     }
203   }
204
205   // Check that they are planar.
206   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
207   if(!aPln.get()) {
208     theError = "Selected objects are not planar.";
209     return false;
210   }
211
212   // Check that selected objects have closed contours.
213   ListOfShape aFaces;
214   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
215   if(aFaces.empty()) {
216     theError = "Selected objects does not have closed contours.";
217     return false;
218   }
219
220   return true;
221 }
222
223 //=================================================================================================
224 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature, std::string theAttribute)
225 {
226   return false;
227 }
228
229 //=================================================================================================
230 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
231                                                       const std::list<std::string>& theArguments,
232                                                       std::string& theError) const
233 {
234   if(theArguments.size() != 1) {
235     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
236       "1 parameter(Sketch feature id).";
237     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
238     return false;
239   }
240
241   // Get base objects list.
242   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
243     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
244       "%1\"\n Only \"%2\" supported.";
245     Events_InfoMessage("BuildPlugin_Validators", aMsg).
246       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
247     return false;
248   }
249   AttributeSelectionListPtr aSelectionList =
250     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
251   if(!aSelectionList.get()) {
252     theError = "Could not get selection list.";
253     return false;
254   }
255
256   // Get base shape.
257   const std::string aBaseShapeId = "base_shape";
258   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
259   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
260
261   if(!aShapeAttrSelection.get()) {
262     theError = "Base shape is empty.";
263     return false;
264   }
265
266   ResultPtr aBaseContext = aShapeAttrSelection->context();
267
268   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
269   if(!aBaseShape.get()) {
270     theError = "Base shape is empty.";
271     return false;
272   }
273
274   GeomAlgoAPI_ShapeBuilder aBuilder;
275   aBuilder.removeInternal(aBaseShape);
276   aBaseShape = aBuilder.shape();
277
278   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
279   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
280   switch(aBaseShape->shapeType()) {
281     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
282     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
283     default: break;
284   }
285
286   // Check selected shapes.
287   GeomValidators_FeatureKind aFeatureKindValidator;
288   std::list<std::string> anArguments;
289   anArguments.push_back(theArguments.front());
290   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
291     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
292     if(!aSelectionAttrInList.get()) {
293       theError = "Empty attribute in list.";
294       return false;
295     }
296
297     // If context of selection same skip.
298     if(aBaseContext == aSelectionAttrInList->context()) {
299       continue;
300     }
301
302     // Check that it is a selection on Sketch.
303     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
304       return false;
305     }
306
307     // Check shape type.
308     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
309     if(!aShapeInList.get()) {
310       aShapeInList = aSelectionAttrInList->context()->shape();
311     }
312     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
313       theError = "Selected shape has unacceptable type.";
314       return false;
315     }
316
317     // Check that shape inside wire or face.
318     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
319       theError = "Selected shape is not inside base face.";
320       return false;
321     }
322   }
323
324   return true;
325 }