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