Salome HOME
7204aebce1f05fd0f784cde8ee740f70e75a3960
[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_CompoundBuilder.h>
15 #include <GeomAlgoAPI_WireBuilder.h>
16
17 #include <GeomValidators_ShapeType.h>
18
19 #include <Events_Error.h>
20
21 //=================================================================================================
22 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
23                                                 const std::list<std::string>& theArguments,
24                                                 std::string& theError) const
25 {
26   // Get base objects list.
27   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
28     Events_Error::send("Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type \"" + theAttribute->attributeType()
29       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
30     return false;
31   }
32   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
33   if(!aSelectionList.get()) {
34     theError = "Could not get selection list.";
35     return false;
36   }
37   if(aSelectionList->size() == 0) {
38     theError = "Empty selection list.";
39     return false;
40   }
41
42   // Collect base shapes.
43   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
44     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
45     if(!aSelection.get()) {
46       theError = "Could not get selection.";
47       return false;
48     }
49     ResultPtr aContext = aSelection->context();
50     if(!aContext.get()) {
51       theError = "Attribute have empty context.";
52       return false;
53     }
54
55     GeomShapePtr aShape = aSelection->value();
56     GeomShapePtr aContextShape = aContext->shape();
57     if(!aShape.get()) {
58       aShape = aContextShape;
59     }
60     if(!aShape.get()) {
61       theError = "Empty shape selected.";
62       return false;
63     }
64
65     // Check that shapes has acceptable type.
66     GeomValidators_ShapeType aValidatorShapeType;
67     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
68       return false;
69     }
70
71     // Check that it is shape on sketch.
72     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
73     if(aConstruction.get()) {
74       if(aConstruction->isInfinite()) {
75         theError = "Inifinte objects not acceptable.";
76         return false;
77       }
78
79       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
80       if(anEdges.get()) {
81         if(aShape->isEqual(aContextShape)) {
82           // It is whole sketch.
83           return false;
84         }
85       } else if(!aShape->isEqual(aContextShape)) {
86         // Local selection on body does not allowed.
87         theError = "Selected shape is in the local selection. Only global selection is allowed.";
88         return false;
89       }
90     }
91   }
92
93   return true;
94 }
95
96 //=================================================================================================
97 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
98                                                const std::list<std::string>& theArguments,
99                                                std::string& theError) const
100 {
101   // Get attribute.
102   if(theArguments.size() != 1) {
103     Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list)");
104     return false;
105   }
106   AttributePtr anAttribute = theFeature->attribute(theArguments.front());
107
108   // Check base objects list.
109   BuildPlugin_ValidatorBaseForBuild aValidatorBaseForBuild;
110   std::list<std::string> anArguments;
111   anArguments.push_back("edge");
112   anArguments.push_back("wire");
113   if(!aValidatorBaseForBuild.isValid(anAttribute, anArguments, theError)) {
114     return false;
115   }
116
117   // Collect base shapes.
118   AttributeSelectionListPtr aSelectionList =
119     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
120   ListOfShape aListOfShapes;
121   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
122     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
123     GeomShapePtr aShape = aSelection->value();
124     if(!aShape.get()) {
125       aShape = aSelection->context()->shape();
126     }
127     aListOfShapes.push_back(aShape);
128   }
129
130   // Create wire.
131   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
132   if(!aWire.get()) {
133     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
134     return false;
135   }
136
137   return true;
138 }
139
140 //=================================================================================================
141 bool BuildPlugin_ValidatorBaseForWireisNotObligatory(std::string theFeature, std::string theAttribute)
142 {
143   return false;
144 }
145
146 //=================================================================================================
147 bool BuildPlugin_ValidatorBaseForFace::isValid(const AttributePtr& theAttribute,
148                                                const std::list<std::string>& theArguments,
149                                                std::string& theError) const
150 {
151   // Get base objects list.
152   BuildPlugin_ValidatorBaseForBuild aValidatorBaseForBuild;
153   std::list<std::string> anArguments;
154   anArguments.push_back("edge");
155   anArguments.push_back("wire");
156   if(!aValidatorBaseForBuild.isValid(theAttribute, anArguments, theError)) {
157     return false;
158   }
159
160   // Collect base shapes.
161   AttributeSelectionListPtr aSelectionList =
162     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
163   ListOfShape aListOfShapes;
164   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
165     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
166     GeomShapePtr aShape = aSelection->value();
167     if(!aShape.get()) {
168       aShape = aSelection->context()->shape();
169     }
170     aListOfShapes.push_back(aShape);
171   }
172
173   // Check that they are planar.
174   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aListOfShapes);
175
176   return aCompound->isPlanar();
177 }