Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[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
14 #include <GeomAlgoAPI_WireBuilder.h>
15
16 #include <GeomValidators_ShapeType.h>
17
18 #include <Events_Error.h>
19
20 //=================================================================================================
21 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
22                                                 const std::list<std::string>& theArguments,
23                                                 std::string& theError) const
24 {
25   // Get base objects list.
26   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
27     Events_Error::send("Validator does not support attribute type \"" + theAttribute->attributeType()
28       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
29     return false;
30   }
31   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
32   if(!aSelectionList.get()) {
33     theError = "Could not get selection list.";
34     return false;
35   }
36   if(aSelectionList->size() == 0) {
37     theError = "Empty selection list.";
38     return false;
39   }
40
41   // Collect base shapes.
42   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
43     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
44     if(!aSelection.get()) {
45       theError = "Could not get selection.";
46       return false;
47     }
48     ResultPtr aContext = aSelection->context();
49     if(!aContext.get()) {
50       theError = "Attribute have empty context.";
51       return false;
52     }
53
54     GeomShapePtr aShape = aSelection->value();
55     GeomShapePtr aContextShape = aContext->shape();
56     if(!aShape.get()) {
57       aShape = aContextShape;
58     }
59     if(!aShape.get()) {
60       theError = "Empty shape selected.";
61       return false;
62     }
63
64     // Check that shapes has acceptable type.
65     GeomValidators_ShapeType aValidatorShapeType;
66     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
67       return false;
68     }
69
70     // Check that it is shape on sketch.
71     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
72     if(aConstruction.get()) {
73       if(aConstruction->isInfinite()) {
74         theError = "Inifinte objects not acceptable.";
75         return false;
76       }
77
78       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
79       if(anEdges.get()) {
80         if(aShape->isEqual(aContextShape)) {
81           // It is whole sketch.
82           return false;
83         }
84       } else if(!aShape->isEqual(aContextShape)) {
85         // Local selection on body does not allowed.
86         theError = "Selected shape is in the local selection. Only global selection is allowed.";
87         return false;
88       }
89     }
90   }
91
92   return true;
93 }
94
95 //=================================================================================================
96 bool BuildPlugin_ValidatorBaseForWire::isValid(const AttributePtr& theAttribute,
97                                                const std::list<std::string>& theArguments,
98                                                std::string& theError) const
99 {
100   // Get base objects list.
101   BuildPlugin_ValidatorBaseForBuild aValidatorBaseForBuild;
102   if(!aValidatorBaseForBuild.isValid(theAttribute, theArguments, theError)) {
103     return false;
104   }
105
106   // Collect base shapes.
107   AttributeSelectionListPtr aSelectionList =
108     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
109   ListOfShape aListOfShapes;
110   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
111     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
112     ResultPtr aContext = aSelection->context();
113     GeomShapePtr aShape = aSelection->value();
114     GeomShapePtr aContextShape = aContext->shape();
115     if(!aShape.get()) {
116       aShape = aSelection->context()->shape();
117     }
118     aListOfShapes.push_back(aShape);
119   }
120
121   // Create wire.
122   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
123   if(!aWire.get()) {
124     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
125     return false;
126   }
127
128   return true;
129 }