]> SALOME platform Git repositories - modules/shaper.git/blob - src/BuildPlugin/BuildPlugin_Validators.cpp
Salome HOME
dfdd20d3990afd28b0e77fdaf2af31db0ab9d11d
[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 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_ShapeExplorer.h>
15
16 #include <GeomAlgoAPI_CompoundBuilder.h>
17 #include <GeomAlgoAPI_PaveFiller.h>
18 #include <GeomAlgoAPI_ShapeTools.h>
19 #include <GeomAlgoAPI_SketchBuilder.h>
20 #include <GeomAlgoAPI_WireBuilder.h>
21
22 #include <GeomValidators_ShapeType.h>
23
24 #include <Events_Error.h>
25
26 //=================================================================================================
27 bool BuildPlugin_ValidatorBaseForBuild::isValid(const AttributePtr& theAttribute,
28                                                 const std::list<std::string>& theArguments,
29                                                 std::string& theError) const
30 {
31   // Get base objects list.
32   if(theAttribute->attributeType() != ModelAPI_AttributeSelectionList::typeId()) {
33     Events_Error::send("Error: BuildPlugin_ValidatorBaseForBuild does not support attribute type \"" + theAttribute->attributeType()
34       + "\"\n Only \"" + ModelAPI_AttributeSelectionList::typeId() + "\" supported.");
35     return false;
36   }
37   AttributeSelectionListPtr aSelectionList = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
38   if(!aSelectionList.get()) {
39     theError = "Could not get selection list.";
40     return false;
41   }
42   if(aSelectionList->size() == 0) {
43     theError = "Empty selection list.";
44     return false;
45   }
46
47   // Collect base shapes.
48   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
49     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
50     if(!aSelection.get()) {
51       theError = "Could not get selection.";
52       return false;
53     }
54     ResultPtr aContext = aSelection->context();
55     if(!aContext.get()) {
56       theError = "Attribute have empty context.";
57       return false;
58     }
59
60     GeomShapePtr aShape = aSelection->value();
61     GeomShapePtr aContextShape = aContext->shape();
62     if(!aShape.get()) {
63       aShape = aContextShape;
64     }
65     if(!aShape.get()) {
66       theError = "Empty shape selected.";
67       return false;
68     }
69
70     // Check that shapes has acceptable type.
71     GeomValidators_ShapeType aValidatorShapeType;
72     if(!aValidatorShapeType.isValid(aSelection, theArguments, theError)) {
73       return false;
74     }
75
76     // Check that it is shape on sketch.
77     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
78     if(aConstruction.get()) {
79       if(aConstruction->isInfinite()) {
80         theError = "Inifinte objects not acceptable.";
81         return false;
82       }
83
84       std::shared_ptr<GeomAPI_PlanarEdges> anEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
85       if(anEdges.get()) {
86         if(aShape->isEqual(aContextShape)) {
87           // It is whole sketch.
88           return false;
89         }
90
91         continue;
92       }
93     }
94
95     if(!aShape->isEqual(aContextShape)) {
96       // Local selection on body does not allowed.
97       theError = "Selected shape is in the local selection. Only global selection is allowed.";
98       return false;
99     }
100   }
101
102   return true;
103 }
104
105 //=================================================================================================
106 bool BuildPlugin_ValidatorBaseForWire::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
107                                                const std::list<std::string>& theArguments,
108                                                std::string& theError) const
109 {
110   // Get attribute.
111   if(theArguments.size() != 1) {
112     Events_Error::send("Error: BuildPlugin_ValidatorBaseForWire should be used only with 1 parameter (ID of base objects list).");
113     return false;
114   }
115   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
116   if(!aSelectionList.get()) {
117     theError = "Empty attribute \"" + theArguments.front() + "\".";
118     return false;
119   }
120
121
122   // Collect base shapes.
123   ListOfShape aListOfShapes;
124   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
125     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
126     GeomShapePtr aShape = aSelection->value();
127     if(!aShape.get()) {
128       aShape = aSelection->context()->shape();
129     }
130     aListOfShapes.push_back(aShape);
131   }
132
133   // Create wire.
134   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
135   if(!aWire.get()) {
136     theError = "Result wire empty. Probably it has disconnected edges or non-manifold.";
137     return false;
138   }
139
140   return true;
141 }
142
143 //=================================================================================================
144 bool BuildPlugin_ValidatorBaseForWire::isNotObligatory(std::string theFeature, std::string theAttribute)
145 {
146   return false;
147 }
148
149 //=================================================================================================
150 bool BuildPlugin_ValidatorBaseForFace::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
151                                                const std::list<std::string>& theArguments,
152                                                std::string& theError) const
153 {
154   // Get attribute.
155   if(theArguments.size() != 1) {
156     Events_Error::send("Error: BuildPlugin_ValidatorBaseForFace should be used only with 1 parameter (ID of base objects list).");
157     return false;
158   }
159   AttributeSelectionListPtr aSelectionList = theFeature->selectionList(theArguments.front());
160   if(!aSelectionList.get()) {
161     theError = "Empty attribute \"" + theArguments.front() + "\".";
162     return false;
163   }
164
165   // Collect base shapes.
166   ListOfShape anEdges;
167   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
168     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
169     GeomShapePtr aShape = aSelection->value();
170     if(!aShape.get()) {
171       aShape = aSelection->context()->shape();
172     }
173     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
174       GeomShapePtr anEdge = anExp.current();
175       anEdges.push_back(anEdge);
176     }
177   }
178
179   // Check that edges does not have intersections.
180   if(anEdges.size() > 1) {
181     GeomAlgoAPI_PaveFiller aPaveFiller(anEdges, false);
182     if(!aPaveFiller.isDone()) {
183       theError = "Error while checking if edges intersects.";
184       return false;
185     }
186     GeomShapePtr aSectedEdges = aPaveFiller.shape();
187
188     int anEdgesNum = 0;
189     for(GeomAPI_ShapeExplorer anExp(aSectedEdges, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
190       anEdgesNum++;
191     }
192     if(anEdgesNum != anEdges.size()) {
193       theError = "Selected objects have intersections.";
194       return false;
195     }
196   }
197
198   // Check that they are planar.
199   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
200   if(!aPln.get()) {
201     theError = "Selected objects are not planar.";
202     return false;
203   }
204
205   // Check that selected objects have closed contours.
206   ListOfShape aFaces;
207   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
208   if(aFaces.empty()) {
209     theError = "Selected objects does not have closed contours.";
210     return false;
211   }
212
213   return true;
214 }
215
216 //=================================================================================================
217 bool BuildPlugin_ValidatorBaseForFace::isNotObligatory(std::string theFeature, std::string theAttribute)
218 {
219   return false;
220 }