]> SALOME platform Git repositories - modules/shaper.git/blob - src/BuildPlugin/BuildPlugin_Validators.cpp
Salome HOME
Fix for the issue #2415 : generation of wide naming structure for the Compound feature
[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 #include <GeomAlgoAPI_MakeVolume.h>
37 #include <GeomAPI_ShapeIterator.h>
38 #include <GeomAPI_ShapeExplorer.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     ListOfShape aFaces;
251     GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
252                                            aPln->direction(), anEdges, aFaces);
253     if(aFaces.empty()) {
254       theError = "Selected objects do not generate closed contour.";
255       return false;
256     }
257   }
258
259   return true;
260 }
261
262 //=================================================================================================
263 bool BuildPlugin_ValidatorBaseForSolids::isValid(
264   const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::list<std::string>& theArguments,
265   Events_InfoMessage& theError) const
266 {
267   // Get base objects list.
268   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
269   if (!aSelectionList.get()) {
270     theError = "Could not get selection list.";
271     return false;
272   }
273   if (aSelectionList->size() == 0) {
274     theError = "Empty selection list.";
275     return false;
276   }
277
278   // Collect base shapes.
279   ListOfShape anOriginalShapes;
280   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
281     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
282     if (!aSelection->context().get()) {
283       theError = "Invalid selection.";
284       return false;
285     }
286     GeomShapePtr aShape = aSelection->value();
287     if (!aShape.get())
288       aShape = aSelection->context()->shape();
289     anOriginalShapes.push_back(aShape);
290   }
291
292   std::shared_ptr<GeomAlgoAPI_MakeVolume> anAlgorithm(new GeomAlgoAPI_MakeVolume(anOriginalShapes));
293
294   if (!anAlgorithm->isDone()) {
295     theError = "MakeVolume algorithm failed.";
296     return false;
297   }
298   if (anAlgorithm->shape()->isNull()) {
299     theError = "Resulting shape of MakeVolume is Null.";
300     return false;
301   }
302   if (!anAlgorithm->isValid()) {
303     theError = "Resulting shape of MakeVolume is not valid.";
304     return false;
305   }
306
307   // set of allowed types of results
308   std::set<GeomAPI_Shape::ShapeType> aResultType;
309   std::string aType = theArguments.back();
310   if (aType == "solid")
311     aResultType.insert(GeomAPI_Shape::SOLID);
312   else if (aType == "compsolid") {
313     aResultType.insert(GeomAPI_Shape::COMPSOLID);
314     aResultType.insert(GeomAPI_Shape::SOLID);
315   }
316
317   GeomShapePtr aCompound = anAlgorithm->shape();
318   if (aCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
319     GeomAPI_ShapeIterator anIt(aCompound);
320     GeomShapePtr aFoundSub;
321     for (; anIt.more() && !aFoundSub; anIt.next()) {
322       aFoundSub = anIt.current();
323       if (aResultType.count(aFoundSub->shapeType()) == 0) {
324         theError = "Unable to build a solid";
325         return false;
326       }
327     }
328     if (anIt.more() || !aFoundSub.get()) {
329       theError = "Unable to build a solid";
330       return false;
331     }
332   } else if (aResultType.count(aCompound->shapeType()) == 0) {
333     theError = "Unable to build a solid";
334     return false;
335   }
336   // check the internal faces presence
337   for(GeomAPI_ShapeExplorer aFaces(aCompound, GeomAPI_Shape::FACE); aFaces.more(); aFaces.next()) {
338     if (aFaces.current()->orientation() == GeomAPI_Shape::INTERNAL) {
339       theError = "Internal faces are not allowed in the resulting solid";
340       return false;
341     }
342   }
343
344   return true;
345 }
346
347
348 //=================================================================================================
349 bool BuildPlugin_ValidatorSubShapesSelection::isValid(const AttributePtr& theAttribute,
350                                                       const std::list<std::string>& theArguments,
351                                                       Events_InfoMessage& theError) const
352 {
353   if(theArguments.size() != 1) {
354     std::string aMsg = "Error: BuildPlugin_ValidatorSubShapesSelection should be used only with "
355       "1 parameter(Sketch feature id).";
356     Events_InfoMessage("BuildPlugin_Validators", aMsg).send();
357     return false;
358   }
359
360   // Get base objects list.
361   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
362     std::string aMsg =
363       "Error: BuildPlugin_ValidatorSubShapesSelection does not support attribute type \""
364       "%1\"\n Only \"%2\" supported.";
365     Events_InfoMessage("BuildPlugin_Validators", aMsg).
366       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
367     return false;
368   }
369   AttributeSelectionListPtr aSelectionList =
370     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
371   if(!aSelectionList.get()) {
372     theError = "Could not get selection list.";
373     return false;
374   }
375
376   // Get base shape.
377   const std::string aBaseShapeId = "base_shape";
378   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
379   AttributeSelectionPtr aShapeAttrSelection = aFeature->selection(aBaseShapeId);
380
381   if(!aShapeAttrSelection.get()) {
382     theError = "Base shape is empty.";
383     return false;
384   }
385
386   ResultPtr aBaseContext = aShapeAttrSelection->context();
387
388   GeomShapePtr aBaseShape  = aShapeAttrSelection->value();
389   if(!aBaseShape.get()) {
390     theError = "Base shape is empty.";
391     return false;
392   }
393
394   GeomAlgoAPI_ShapeBuilder aBuilder;
395   aBuilder.removeInternal(aBaseShape);
396   aBaseShape = aBuilder.shape();
397
398   // If selected shape is wire allow to select only vertices. If face - allow vertices and edges.
399   std::set<GeomAPI_Shape::ShapeType> anAllowedTypes;
400   switch(aBaseShape->shapeType()) {
401     case GeomAPI_Shape::FACE: anAllowedTypes.insert(GeomAPI_Shape::EDGE);
402     case GeomAPI_Shape::WIRE: anAllowedTypes.insert(GeomAPI_Shape::VERTEX);
403     default: break;
404   }
405
406   // Check selected shapes.
407   GeomValidators_FeatureKind aFeatureKindValidator;
408   std::list<std::string> anArguments;
409   anArguments.push_back(theArguments.front());
410   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
411     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
412     if(!aSelectionAttrInList.get()) {
413       theError = "Empty attribute in list.";
414       return false;
415     }
416
417     // If context of selection same skip.
418     if(aBaseContext == aSelectionAttrInList->context()) {
419       continue;
420     }
421
422     // Check that it is a selection on Sketch.
423     if(!aFeatureKindValidator.isValid(aSelectionAttrInList, anArguments, theError)) {
424       return false;
425     }
426
427     // Check shape type.
428     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
429     if(!aShapeInList.get()) {
430       aShapeInList = aSelectionAttrInList->context()->shape();
431     }
432     if(anAllowedTypes.find(aShapeInList->shapeType()) == anAllowedTypes.cend()) {
433       theError = "Selected shape has unacceptable type.";
434       return false;
435     }
436
437     // Check that shape inside wire or face.
438     if(!GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aShapeInList, aBaseShape)) {
439       theError = "Selected shape is not inside base face.";
440       return false;
441     }
442   }
443
444   return true;
445 }
446
447
448 //=================================================================================================
449 bool BuildPlugin_ValidatorFillingSelection::isValid(const AttributePtr& theAttribute,
450                                                       const std::list<std::string>& theArguments,
451                                                       Events_InfoMessage& theError) const
452 {
453   // Get base objects list.
454   if (theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
455     std::string aMsg =
456       "Error: BuildPlugin_ValidatorFillingSelection does not support attribute type \""
457       "%1\"\n Only \"%2\" supported.";
458     Events_InfoMessage("BuildPlugin_Validators", aMsg).
459       arg(theAttribute->attributeType()).arg(ModelAPI_AttributeSelectionList::typeId()).send();
460     return false;
461   }
462   AttributeSelectionListPtr aSelectionList =
463     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
464   if (!aSelectionList.get()) {
465     theError = "Could not get selection list.";
466     return false;
467   }
468
469   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
470
471   // Check selected shapes.
472   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
473     AttributeSelectionPtr aSelectionAttrInList = aSelectionList->value(anIndex);
474     if (!aSelectionAttrInList.get()) {
475       theError = "Empty attribute in list.";
476       return false;
477     }
478
479     // Check shape exists.
480     GeomShapePtr aShapeInList = aSelectionAttrInList->value();
481     if (!aShapeInList.get()) {
482       theError = "Object has no shape";
483       return false;
484     }
485
486     // Check shape type.
487     GeomAPI_Shape::ShapeType aType = aShapeInList->shapeType();
488     if (aType != GeomAPI_Shape::EDGE && aType != GeomAPI_Shape::WIRE) {
489       theError = "Incorrect objects selected";
490       return false;
491     }
492   }
493
494   return true;
495 }