Salome HOME
Issue #1649: Added options to create plane by two parallel planes;
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ConstructionPlugin_Validators.cpp
4 // Created:     04 July 2016
5 // Author:      Dmitry Bobylev
6
7 #include "ConstructionPlugin_Validators.h"
8
9 #include <GeomAPI_Dir.h>
10 #include <GeomAPI_Edge.h>
11 #include <GeomAPI_Face.h>
12 #include <GeomAPI_Lin.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Vertex.h>
15
16 #include <ModelAPI_AttributeSelection.h>
17
18 #include <Events_InfoMessage.h>
19
20 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
21 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
22 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
23
24 //==================================================================================================
25 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
26                                                      const std::list<std::string>& theArguments,
27                                                      Events_InfoMessage& theError) const
28 {
29   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
30
31   AttributeSelectionPtr aLineAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
32   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
33
34   GeomShapePtr aLineShape1 = aLineAttribute1->value();
35   ResultPtr aContext1 = aLineAttribute1->context();
36   if(!aContext1.get()) {
37     theError = "One of the attribute not initialized.";
38     return false;
39   }
40   if(!aLineShape1.get()) {
41     aLineShape1 = aContext1->shape();
42   }
43   if(!aLineShape1->isEdge()) {
44     theError = "One of the selected shapes not an edge.";
45     return false;
46   }
47
48   GeomShapePtr aLineShape2 = aLineAttribute2->value();
49   ResultPtr aContext2 = aLineAttribute2->context();
50   if(!aContext2.get()) {
51     return true;
52   }
53   if(!aLineShape2.get()) {
54     aLineShape2 = aContext2->shape();
55   }
56   if(!aLineShape2->isEdge()) {
57     theError = "One of the selected shapes not an edge.";
58     return false;
59   }
60
61   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
62   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
63
64   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
65   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
66
67   if(!aLine1->isCoplanar(aLine2)) {
68     theError = "Selected lines not coplanar.";
69     return false;
70   }
71
72   if(aLine1->isParallel(aLine2)) {
73     theError = "Selected lines are parallel.";
74     return false;
75   }
76
77   return true;
78 }
79
80 //==================================================================================================
81 bool ConstructionPlugin_ValidatorPointLineAndPlaneNotParallel::isValid(
82     const AttributePtr& theAttribute,
83     const std::list<std::string>& theArguments,
84     Events_InfoMessage& theError) const
85 {
86   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
87
88   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
89   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
90
91   std::shared_ptr<GeomAPI_Lin> aLin;
92   std::shared_ptr<GeomAPI_Pln> aPln;
93
94   GeomShapePtr aShape1 = anAttribute1->value();
95   ResultPtr aContext1 = anAttribute1->context();
96   if(!aContext1.get()) {
97     theError = "One of the attribute not initialized.";
98     return false;
99   }
100   if(!aShape1.get()) {
101     aShape1 = aContext1->shape();
102   }
103
104   GeomShapePtr aShape2 = anAttribute2->value();
105   ResultPtr aContext2 = anAttribute2->context();
106   if(!aContext2.get()) {
107     return true;
108   }
109   if(!aShape2.get()) {
110     aShape2 = aContext2->shape();
111   }
112
113   aLin = getLin(aShape1);
114   aPln = getPln(aShape2);
115   if(!aLin.get() || !aPln.get()) {
116     aLin = getLin(aShape2);
117     aPln = getPln(aShape1);
118   }
119
120   if(!aLin.get() || !aPln.get()) {
121     theError = "Wrong shape types selected.";
122     return false;
123   }
124
125   if(aPln->isParallel(aLin)) {
126     theError = "Plane and line are parallel.";
127     return false;
128   }
129
130   return true;
131 }
132
133 //==================================================================================================
134 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
135                                                            const std::list<std::string>& theArguments,
136                                                            Events_InfoMessage& theError) const
137 {
138   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
139
140   AttributeSelectionPtr aPointAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
141   AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
142   AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
143
144   GeomShapePtr aPointShape1 = aPointAttribute1->value();
145   ResultPtr aContext1 = aPointAttribute1->context();
146   if(!aContext1.get()) {
147     theError = "One of the attribute not initialized.";
148     return false;
149   }
150   if(!aPointShape1.get()) {
151     aPointShape1 = aContext1->shape();
152   }
153   if(!aPointShape1->isVertex()) {
154     theError = "One of the selected shapes not a vertex.";
155     return false;
156   }
157
158   GeomShapePtr aPointShape2 = aPointAttribute2->value();
159   ResultPtr aContext2 = aPointAttribute2->context();
160   if(!aContext2.get()) {
161     return true;
162   }
163   if(!aPointShape2.get()) {
164     aPointShape2 = aContext2->shape();
165   }
166   if(!aPointShape2->isVertex()) {
167     theError = "One of the selected shapes not a vertex.";
168     return false;
169   }
170
171   GeomShapePtr aPointShape3 = aPointAttribute3->value();
172   ResultPtr aContext3 = aPointAttribute3->context();
173   if(!aContext3.get()) {
174     return true;
175   }
176   if(!aPointShape3.get()) {
177     aPointShape3 = aContext3->shape();
178   }
179   if(!aPointShape3->isVertex()) {
180     theError = "One of the selected shapes not a vertex.";
181     return false;
182   }
183
184   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
185   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
186   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
187
188   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
189   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
190   std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
191
192   std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
193
194   if(aLin->contains(aPnt3)) {
195     theError = "Selected points lie on a line.";
196     return false;
197   }
198
199   return true;
200 }
201
202 //==================================================================================================
203 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
204     const AttributePtr& theAttribute,
205     const std::list<std::string>& theArguments,
206     Events_InfoMessage& theError) const
207 {
208   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
209
210   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
211   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
212
213   std::shared_ptr<GeomAPI_Lin> aLin;
214   std::shared_ptr<GeomAPI_Pnt> aPnt;
215
216   GeomShapePtr aShape1 = anAttribute1->value();
217   ResultPtr aContext1 = anAttribute1->context();
218   if(!aContext1.get()) {
219     theError = "One of the attribute not initialized.";
220     return false;
221   }
222   if(!aShape1.get()) {
223     aShape1 = aContext1->shape();
224   }
225
226   GeomShapePtr aShape2 = anAttribute2->value();
227   ResultPtr aContext2 = anAttribute2->context();
228   if(!aContext2.get()) {
229     return true;
230   }
231   if(!aShape2.get()) {
232     aShape2 = aContext2->shape();
233   }
234
235   aLin = getLin(aShape1);
236   aPnt = getPnt(aShape2);
237   if(!aLin.get() || !aPnt.get()) {
238     aLin = getLin(aShape2);
239     aPnt = getPnt(aShape1);
240   }
241
242   if(!aLin.get() || !aPnt.get()) {
243     theError = "Wrong shape types selected.";
244     return false;
245   }
246
247   if(aLin->contains(aPnt)) {
248     theError = "Point lies on the line.";
249     return false;
250   }
251
252   return true;
253 }
254
255 //==================================================================================================
256 bool ConstructionPlugin_ValidatorPlaneTwoParallelPlanes::isValid(
257     const AttributePtr& theAttribute,
258     const std::list<std::string>& theArguments,
259     Events_InfoMessage& theError) const
260 {
261   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
262
263   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
264   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
265
266   std::shared_ptr<GeomAPI_Pln> aPln1;
267   std::shared_ptr<GeomAPI_Pln> aPln2;
268
269   GeomShapePtr aShape1 = anAttribute1->value();
270   ResultPtr aContext1 = anAttribute1->context();
271   if(!aContext1.get()) {
272     theError = "One of the attribute not initialized.";
273     return false;
274   }
275   if(!aShape1.get()) {
276     aShape1 = aContext1->shape();
277   }
278
279   GeomShapePtr aShape2 = anAttribute2->value();
280   ResultPtr aContext2 = anAttribute2->context();
281   if(!aContext2.get()) {
282     return true;
283   }
284   if(!aShape2.get()) {
285     aShape2 = aContext2->shape();
286   }
287
288   aPln1 = getPln(aShape1);
289   aPln2 = getPln(aShape2);
290
291   if(!aPln1.get() || !aPln2.get()) {
292     theError = "Wrong shape types selected.";
293     return false;
294   }
295
296   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
297   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
298
299   if(!aDir1->isParallel(aDir2)) {
300     theError = "Planes not parallel.";
301     return false;
302   }
303
304   return true;
305 }
306
307 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
308 {
309   std::shared_ptr<GeomAPI_Lin> aLin;
310
311   if(!theShape->isEdge()) {
312     return aLin;
313   }
314
315   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
316
317   if(!anEdge->isLine()) {
318     return aLin;
319   }
320
321   aLin = anEdge->line();
322
323   return aLin;
324 }
325
326 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
327 {
328   std::shared_ptr<GeomAPI_Pln> aPln;
329
330   if(!theShape->isFace()) {
331     return aPln;
332   }
333
334   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
335
336   if(!aFace->isPlanar()) {
337     return aPln;
338   }
339
340   aPln = aFace->getPlane();
341
342   return aPln;
343 }
344
345 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
346 {
347   std::shared_ptr<GeomAPI_Pnt> aPnt;
348
349   if(!theShape->isVertex()) {
350     return aPnt;
351   }
352
353   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
354
355   aPnt = aVertex->point();
356
357   return aPnt;
358 }