Salome HOME
Fix for the issue #1794: can't create a plane by a vertex and an axis if the vertex...
[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 #include <ModelAPI_AttributeBoolean.h>
18
19 #include <Events_InfoMessage.h>
20
21 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
22 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
23 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
24
25 //==================================================================================================
26 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
27                                                      const std::list<std::string>& theArguments,
28                                                      Events_InfoMessage& theError) const
29 {
30   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
31
32   AttributeSelectionPtr aLineAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
33   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
34
35   GeomShapePtr aLineShape1 = aLineAttribute1->value();
36   ResultPtr aContext1 = aLineAttribute1->context();
37   if(!aContext1.get()) {
38     theError = "One of the attribute not initialized.";
39     return false;
40   }
41   if(!aLineShape1.get()) {
42     aLineShape1 = aContext1->shape();
43   }
44   if(!aLineShape1->isEdge()) {
45     theError = "One of the selected shapes not an edge.";
46     return false;
47   }
48
49   GeomShapePtr aLineShape2 = aLineAttribute2->value();
50   ResultPtr aContext2 = aLineAttribute2->context();
51   if(!aContext2.get()) {
52     return true;
53   }
54   if(!aLineShape2.get()) {
55     aLineShape2 = aContext2->shape();
56   }
57   if(!aLineShape2->isEdge()) {
58     theError = "One of the selected shapes not an edge.";
59     return false;
60   }
61
62   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
63   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
64
65   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
66   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
67
68   if(!aLine1->isCoplanar(aLine2)) {
69     theError = "Selected lines not coplanar.";
70     return false;
71   }
72
73   if(aLine1->isParallel(aLine2)) {
74     theError = "Selected lines are parallel.";
75     return false;
76   }
77
78   return true;
79 }
80
81 //==================================================================================================
82 bool ConstructionPlugin_ValidatorPointLineAndPlaneNotParallel::isValid(
83     const AttributePtr& theAttribute,
84     const std::list<std::string>& theArguments,
85     Events_InfoMessage& theError) const
86 {
87   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
88
89   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
90   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
91
92   std::shared_ptr<GeomAPI_Lin> aLin;
93   std::shared_ptr<GeomAPI_Pln> aPln;
94
95   GeomShapePtr aShape1 = anAttribute1->value();
96   ResultPtr aContext1 = anAttribute1->context();
97   if(!aContext1.get()) {
98     theError = "One of the attribute not initialized.";
99     return false;
100   }
101   if(!aShape1.get()) {
102     aShape1 = aContext1->shape();
103   }
104
105   GeomShapePtr aShape2 = anAttribute2->value();
106   ResultPtr aContext2 = anAttribute2->context();
107   if(!aContext2.get()) {
108     return true;
109   }
110   if(!aShape2.get()) {
111     aShape2 = aContext2->shape();
112   }
113
114   aLin = getLin(aShape1);
115   aPln = getPln(aShape2);
116   if(!aLin.get() || !aPln.get()) {
117     aLin = getLin(aShape2);
118     aPln = getPln(aShape1);
119   }
120
121   if(!aLin.get() || !aPln.get()) {
122     theError = "Wrong shape types selected.";
123     return false;
124   }
125
126   if(aPln->isParallel(aLin)) {
127     theError = "Plane and line are parallel.";
128     return false;
129   }
130
131   return true;
132 }
133
134 //==================================================================================================
135 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
136                                                            const std::list<std::string>& theArguments,
137                                                            Events_InfoMessage& theError) const
138 {
139   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
140
141   AttributeSelectionPtr aPointAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
142   AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
143   AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
144
145   GeomShapePtr aPointShape1 = aPointAttribute1->value();
146   ResultPtr aContext1 = aPointAttribute1->context();
147   if(!aContext1.get()) {
148     theError = "One of the attribute not initialized.";
149     return false;
150   }
151   if(!aPointShape1.get()) {
152     aPointShape1 = aContext1->shape();
153   }
154   if(!aPointShape1->isVertex()) {
155     theError = "One of the selected shapes not a vertex.";
156     return false;
157   }
158
159   GeomShapePtr aPointShape2 = aPointAttribute2->value();
160   ResultPtr aContext2 = aPointAttribute2->context();
161   if(!aContext2.get()) {
162     return true;
163   }
164   if(!aPointShape2.get()) {
165     aPointShape2 = aContext2->shape();
166   }
167   if(!aPointShape2->isVertex()) {
168     theError = "One of the selected shapes not a vertex.";
169     return false;
170   }
171
172   GeomShapePtr aPointShape3 = aPointAttribute3->value();
173   ResultPtr aContext3 = aPointAttribute3->context();
174   if(!aContext3.get()) {
175     return true;
176   }
177   if(!aPointShape3.get()) {
178     aPointShape3 = aContext3->shape();
179   }
180   if(!aPointShape3->isVertex()) {
181     theError = "One of the selected shapes not a vertex.";
182     return false;
183   }
184
185   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
186   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
187   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
188
189   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
190   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
191   std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
192
193   std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
194
195   if(aLin->contains(aPnt3)) {
196     theError = "Selected points lie on a line.";
197     return false;
198   }
199
200   return true;
201 }
202
203 //==================================================================================================
204 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
205     const AttributePtr& theAttribute,
206     const std::list<std::string>& theArguments,
207     Events_InfoMessage& theError) const
208 {
209   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
210
211   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
212   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
213
214   std::shared_ptr<GeomAPI_Lin> aLin;
215   std::shared_ptr<GeomAPI_Pnt> aPnt;
216
217   GeomShapePtr aShape1 = anAttribute1->value();
218   ResultPtr aContext1 = anAttribute1->context();
219   if(!aContext1.get()) {
220     theError = "One of the attribute not initialized.";
221     return false;
222   }
223   if(!aShape1.get()) {
224     aShape1 = aContext1->shape();
225   }
226
227   GeomShapePtr aShape2 = anAttribute2->value();
228   ResultPtr aContext2 = anAttribute2->context();
229   if(!aContext2.get()) {
230     return true;
231   }
232   if(!aShape2.get()) {
233     aShape2 = aContext2->shape();
234   }
235
236   aLin = getLin(aShape1);
237   aPnt = getPnt(aShape2);
238   if(!aLin.get() || !aPnt.get()) {
239     aLin = getLin(aShape2);
240     aPnt = getPnt(aShape1);
241   }
242
243   if(!aLin.get() || !aPnt.get()) {
244     theError = "Wrong shape types selected.";
245     return false;
246   }
247
248   // line should not contain point only for not-prependicular case
249   AttributeBooleanPtr aBoolAttr = aFeature->boolean(theArguments.back());
250   if (aBoolAttr.get() && !aBoolAttr->value()) {
251     if(aLin->contains(aPnt)) {
252       theError = "Point lies on the line.";
253       return false;
254     }
255   }
256
257   return true;
258 }
259
260 //==================================================================================================
261 bool ConstructionPlugin_ValidatorPlaneTwoParallelPlanes::isValid(
262     const AttributePtr& theAttribute,
263     const std::list<std::string>& theArguments,
264     Events_InfoMessage& theError) const
265 {
266   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
267
268   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
269   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
270
271   std::shared_ptr<GeomAPI_Pln> aPln1;
272   std::shared_ptr<GeomAPI_Pln> aPln2;
273
274   GeomShapePtr aShape1 = anAttribute1->value();
275   ResultPtr aContext1 = anAttribute1->context();
276   if(!aContext1.get()) {
277     theError = "One of the attribute not initialized.";
278     return false;
279   }
280   if(!aShape1.get()) {
281     aShape1 = aContext1->shape();
282   }
283
284   GeomShapePtr aShape2 = anAttribute2->value();
285   ResultPtr aContext2 = anAttribute2->context();
286   if(!aContext2.get()) {
287     return true;
288   }
289   if(!aShape2.get()) {
290     aShape2 = aContext2->shape();
291   }
292
293   aPln1 = getPln(aShape1);
294   aPln2 = getPln(aShape2);
295
296   if(!aPln1.get() || !aPln2.get()) {
297     theError = "Wrong shape types selected.";
298     return false;
299   }
300
301   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
302   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
303
304   if(!aDir1->isParallel(aDir2)) {
305     theError = "Planes not parallel.";
306     return false;
307   }
308
309   return true;
310 }
311
312 //==================================================================================================
313 bool ConstructionPlugin_ValidatorAxisTwoNotParallelPlanes::isValid(
314     const AttributePtr& theAttribute,
315     const std::list<std::string>& theArguments,
316     Events_InfoMessage& theError) const
317 {
318   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
319
320   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
321   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
322
323   std::shared_ptr<GeomAPI_Pln> aPln1;
324   std::shared_ptr<GeomAPI_Pln> aPln2;
325
326   GeomShapePtr aShape1 = anAttribute1->value();
327   ResultPtr aContext1 = anAttribute1->context();
328   if(!aContext1.get()) {
329     theError = "One of the attribute not initialized.";
330     return false;
331   }
332   if(!aShape1.get()) {
333     aShape1 = aContext1->shape();
334   }
335
336   GeomShapePtr aShape2 = anAttribute2->value();
337   ResultPtr aContext2 = anAttribute2->context();
338   if(!aContext2.get()) {
339     return true;
340   }
341   if(!aShape2.get()) {
342     aShape2 = aContext2->shape();
343   }
344
345   aPln1 = getPln(aShape1);
346   aPln2 = getPln(aShape2);
347
348   if(!aPln1.get() || !aPln2.get()) {
349     theError = "Wrong shape types selected.";
350     return false;
351   }
352
353   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
354   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
355
356   if(aDir1->isParallel(aDir2)) {
357     theError = "Planes are parallel.";
358     return false;
359   }
360
361   return true;
362 }
363
364 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
365 {
366   std::shared_ptr<GeomAPI_Lin> aLin;
367
368   if(!theShape->isEdge()) {
369     return aLin;
370   }
371
372   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
373
374   if(!anEdge->isLine()) {
375     return aLin;
376   }
377
378   aLin = anEdge->line();
379
380   return aLin;
381 }
382
383 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
384 {
385   std::shared_ptr<GeomAPI_Pln> aPln;
386
387   if(!theShape->isFace()) {
388     return aPln;
389   }
390
391   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
392
393   if(!aFace->isPlanar()) {
394     return aPln;
395   }
396
397   aPln = aFace->getPlane();
398
399   return aPln;
400 }
401
402 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
403 {
404   std::shared_ptr<GeomAPI_Pnt> aPnt;
405
406   if(!theShape->isVertex()) {
407     return aPnt;
408   }
409
410   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
411
412   aPnt = aVertex->point();
413
414   return aPnt;
415 }