Salome HOME
833536d400a170fd6f1d89cc6b12cab67d8e8baa
[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
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
37 #include <GeomValidators_FeatureKind.h>
38 #include <GeomValidators_ShapeType.h>
39
40 #include <Events_InfoMessage.h>
41
42 //=================================================================================================
43 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
44                                                 const std::list<std::string>& theArguments,
45                                                 Events_InfoMessage& theError) const
46 {
47   // Get base objects list.
48   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
49     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForBuild does "
50                        "not support attribute type '%1'\nOnly '%2' is supported.";
51     Events_InfoMessage("BuildPlugin_Validators", aMsg).
52       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
53     return false;
54   }
55   AttributeSelectionListPtr aSelectionList =
56     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
57   if(!aSelectionList.get()) {
58     theError = "Could not get selection list.";
59     return false;
60   }
61   if(aSelectionList->size() == 0) {
62     theError = "Empty selection list.";
63     return false;
64   }
65
66   // Collect base shapes.
67   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
68     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
69     if(!aSelection.get()) {
70       theError = "Could not get selection.";
71       return false;
72     }
73     ResultPtr aContext = aSelection->context();
74     if(!aContext.get()) {
75       theError = "Attribute have empty context.";
76       return false;
77     }
78
79     GeomShapePtr aShape = aSelection->value();
80     GeomShapePtr aContextShape = aContext->shape();
81     if(!aShape.get()) {
82       aShape = aContextShape;
83     }
84     if(!aShape.get()) {
85       theError = "Empty shape selected.";
86       return false;
87     }
88
89     // Check that shapes has acceptable type.
90     GeomValidators_ShapeType aValidatorShapeType;
91     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
92       return false;
93     }
94
95     // Check that it is shape on sketch.
96     ResultConstructionPtr aConstruction =
97       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
98     if(aConstruction.get()) {
99       if(aConstruction->isInfinite()) {
100         theError = "Inifinte objects not acceptable.";
101         return false;
102       }
103
104       std::shared_ptr<GeomAPI_PlanarEdges> anEdges =
105         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
106       if(anEdges.get()) {
107         if(aShape->isEqual(aContextShape)) {
108           // It is whole sketch.
109           return false;
110         }
111
112         continue;
113       }
114     }
115   }
116
117   return true;
118 }
119
120 //=================================================================================================
121 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
122                                                const std::list<std::string>& theArguments,
123                                                Events_InfoMessage& theError) const
124 {
125   // Get attribute.
126   if(theArguments.size() != 1) {
127     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForWire should be used only "
128                        "with 1 parameter (ID of base objects list).";
129     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
130     return false;
131   }
132   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
133   if(!aSelectionList.get()) {
134     theError = "Empty attribute \"%1\".";
135     theError.arg(theArguments.front());
136     return false;
137   }
138
139
140   // Collect base shapes.
141   ListOfShape aListOfShapes;
142   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
143     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
144     GeomShapePtr aShape = aSelection->value();
145     if(!aShape.get()) {
146       if (aSelection->context().get())
147         aShape = aSelection->context()->shape();
148     }
149     if (aShape.get())
150       aListOfShapes.push_back(aShape);
151   }
152
153   // Create wire.
154   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
155   if(!aWire.get()) {
156     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
157     return false;
158   }
159
160   return true;
161 }
162
163 //=================================================================================================
164 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature,
165                                                        std::string theAttribute)
166 {
167   return false;
168 }
169
170 //=================================================================================================
171 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
172                                                const std::list<std::string>& theArguments,
173                                                Events_InfoMessage& theError) const
174 {
175   // Get attribute.
176   if(theArguments.size() != 1) {
177     std::string aMsg = "Error: BuildPlugin_ValidatorBaseForFace should be used only with "
178       "1 parameter (ID of base objects list).";
179     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
180     return false;
181   }
182   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
183   if(!aSelectionList.get()) {
184     theError = "Empty attribute \"%1\".";
185     theError.arg(theArguments.front());
186     return false;
187   }
188
189   bool hasEdgesOrWires = false;
190   bool hasFaces = false;
191
192   // Collect base shapes.
193   ListOfShape anEdges;
194   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
195     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
196     GeomShapePtr aShape = aSelection->value();
197     if(!aShape.get()) {
198       if (!aSelection->context()) {
199         theError = "Objects are not selected.";
200         return false;
201       }
202       aShape = aSelection->context()->shape();
203     }
204     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
205       // skip faces exploding
206       hasFaces = true;
207       continue;
208     }
209
210     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
211       hasEdgesOrWires = true;
212       GeomShapePtr anEdge = anExp.current();
213       anEdges.push_back(anEdge);
214     }
215   }
216
217   if (hasFaces && hasEdgesOrWires) {
218     theError = "Faces and edges/wires should be selected together.";
219     return false;
220   } else if (hasEdgesOrWires && anEdges.empty()) {
221     theError = "Objects are not selected.";
222     return false;
223   }
224
225   // Check that edges does not have intersections.
226   if(anEdges.size() > 1) {
227     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
228     if(!aPaveFiller.isDone()) {
229       theError = "Error while checking if edges intersects.";
230       return false;
231     }
232     GeomShapePtr aSectedEdges = aPaveFiller.shape();
233
234     int anEdgesNum = 0;
235     for(GeomAPI_ShapeExplorer
236         anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
237       anEdgesNum++;
238     }
239     if(anEdgesNum != anEdges.size()) {
240       theError = "Selected objects have intersections.";
241       return false;
242     }
243   }
244
245   if (!anEdges.empty()) {
246     // Check that they are planar.
247     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
248     if(!aPln.get()) {
249       theError = "Selected object(s) should belong to only one plane.";
250       return false;
251     }
252
253     // Check that selected objects have closed contours.
254     ListOfShape aFaces;
255     GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
256                                            aPln->direction(), anEdges, aFaces);
257     if(aFaces.empty()) {
258       theError = "Selected objects do not generate closed contour.";
259       return false;
260     }
261   }
262
263   return true;
264 }
265
266 //=================================================================================================
267 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature,
268                                                        std::string theAttribute)
269 {
270   return false;
271 }
272
273 //=================================================================================================
274 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
275                                                       const std::list<std::string>& theArguments,
276                                                       Events_InfoMessage& theError) const
277 {
278   if(theArguments.size() != 1) {
279     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
280       "1 parameter(Sketch feature id).";
281     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
282     return false;
283   }
284
285   // Get base objects list.
286   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
287     std::string aMsg =
288       "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
289       "%1\"\n Only \"%2\" supported.";
290     Events_InfoMessage("BuildPlugin_Validators", aMsg).
291       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
292     return false;
293   }
294   AttributeSelectionListPtr aSelectionList =
295     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
296   if(!aSelectionList.get()) {
297     theError = "Could not get selection list.";
298     return false;
299   }
300
301   // Get base shape.
302   const std::string aBaseShapeId = "base_shape";
303   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
304   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
305
306   if(!aShapeAttrSelection.get()) {
307     theError = "Base shape is empty.";
308     return false;
309   }
310
311   ResultPtr aBaseContext = aShapeAttrSelection->context();
312
313   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
314   if(!aBaseShape.get()) {
315     theError = "Base shape is empty.";
316     return false;
317   }
318
319   GeomAlgoAPI_ShapeBuilder aBuilder;
320   aBuilder.removeInternal(aBaseShape);
321   aBaseShape = aBuilder.shape();
322
323   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
324   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
325   switch(aBaseShape->shapeType()) {
326     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
327     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
328     default: break;
329   }
330
331   // Check selected shapes.
332   GeomValidators_FeatureKind aFeatureKindValidator;
333   std::list<std::string> anArguments;
334   anArguments.push_back(theArguments.front());
335   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
336     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
337     if(!aSelectionAttrInList.get()) {
338       theError = "Empty attribute in list.";
339       return false;
340     }
341
342     // If context of selection same skip.
343     if(aBaseContext == aSelectionAttrInList->context()) {
344       continue;
345     }
346
347     // Check that it is a selection on Sketch.
348     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
349       return false;
350     }
351
352     // Check shape type.
353     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
354     if(!aShapeInList.get()) {
355       aShapeInList = aSelectionAttrInList->context()->shape();
356     }
357     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
358       theError = "Selected shape has unacceptable type.";
359       return false;
360     }
361
362     // Check that shape inside wire or face.
363     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
364       theError = "Selected shape is not inside base face.";
365       return false;
366     }
367   }
368
369   return true;
370 }
371
372
373 //=================================================================================================
374 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
375                                                       const std::list<std::string>& theArguments,
376                                                       Events_InfoMessage& theError) const
377 {
378   // Get base objects list.
379   if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
380     std::string aMsg =
381       "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
382       "%1\"\n Only \"%2\" supported.";
383     Events_InfoMessage("BuildPlugin_Validators", aMsg).
384       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
385     return false;
386   }
387   AttributeSelectionListPtr aSelectionList =
388     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
389   if (!aSelectionList.get()) {
390     theError = "Could not get selection list.";
391     return false;
392   }
393
394   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
395
396   // Check selected shapes.
397   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
398     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
399     if (!aSelectionAttrInList.get()) {
400       theError = "Empty attribute in list.";
401       return false;
402     }
403
404     // Check shape exists.
405     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
406     if (!aShapeInList.get()) {
407       theError = "Object has no shape";
408       return false;
409     }
410
411     // Check shape type.
412     GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
413     if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
414       theError = "Incorrect objects selected";
415       return false;
416     }
417   }
418
419   return true;
420 }