Salome HOME
Merge branch 'BR_EDF_2018_Lot1'
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Validators.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ConstructionPlugin_Validators.h"
22
23 #include <GeomAPI_Dir.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Face.h>
26 #include <GeomAPI_Lin.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_Vertex.h>
29 #include <GeomAPI_Pnt.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31
32 #include <ModelAPI_AttributeSelection.h>
33 #include <ModelAPI_AttributeBoolean.h>
34
35 #include <Events_InfoMessage.h>
36
37 static std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape);
38 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
39 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
40 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
41
42 //==================================================================================================
43 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
44                                                      const std::list<std::string>& theArguments,
45                                                      Events_InfoMessage& theError) const
46 {
47   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
48
49   AttributeSelectionPtr aLineAttribute1 =
50     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
51   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
52
53   GeomShapePtr aLineShape1 = aLineAttribute1->value();
54   ResultPtr aContext1 = aLineAttribute1->context();
55   if(!aContext1.get()) {
56     theError = "One of the attribute not initialized.";
57     return false;
58   }
59   if(!aLineShape1.get()) {
60     aLineShape1 = aContext1->shape();
61   }
62   if(!aLineShape1->isEdge()) {
63     theError = "One of the selected shapes not an edge.";
64     return false;
65   }
66
67   GeomShapePtr aLineShape2 = aLineAttribute2->value();
68   ResultPtr aContext2 = aLineAttribute2->context();
69   if(!aContext2.get()) {
70     return true;
71   }
72   if(!aLineShape2.get()) {
73     aLineShape2 = aContext2->shape();
74   }
75   if(!aLineShape2->isEdge()) {
76     theError = "One of the selected shapes not an edge.";
77     return false;
78   }
79
80   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
81   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
82
83   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
84   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
85
86   if (!aLine1.get() || !aLine2.get()) {
87     theError = "Selected edge is not a line.";
88     return false;
89   }
90
91   if(!aLine1->isCoplanar(aLine2)) {
92     theError = "Selected lines not coplanar.";
93     return false;
94   }
95
96   if(aLine1->isParallel(aLine2)) {
97     theError = "Selected lines are parallel.";
98     return false;
99   }
100
101   return true;
102 }
103
104 //==================================================================================================
105 bool ConstructionPlugin_ValidatorPointEdgeAndPlaneNotParallel::isValid(
106     const AttributePtr& theAttribute,
107     const std::list<std::string>& theArguments,
108     Events_InfoMessage& theError) const
109 {
110   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
111
112   AttributeSelectionPtr anAttribute1 =
113     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
114   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
115
116   std::shared_ptr<GeomAPI_Edge> anEdge;
117   std::shared_ptr<GeomAPI_Pln> aPln;
118
119   GeomShapePtr aShape1 = anAttribute1->value();
120   ResultPtr aContext1 = anAttribute1->context();
121   if(!aContext1.get()) {
122     theError = "One of the attribute not initialized.";
123     return false;
124   }
125   if(!aShape1.get()) {
126     aShape1 = aContext1->shape();
127   }
128
129   GeomShapePtr aShape2 = anAttribute2->value();
130   ResultPtr aContext2 = anAttribute2->context();
131   if(!aContext2.get()) {
132     return true;
133   }
134   if(!aShape2.get()) {
135     aShape2 = aContext2->shape();
136   }
137
138   bool isPlaneFirst = false;
139   anEdge = getEdge(aShape1);
140   aPln = getPln(aShape2);
141   if(!anEdge.get() || !aPln.get()) {
142     anEdge = getEdge(aShape2);
143     aPln = getPln(aShape1);
144     isPlaneFirst = true;
145   }
146
147   if(!anEdge.get() || !aPln.get()) {
148     theError = "Wrong shape types selected.";
149     return false;
150   }
151
152   std::shared_ptr<GeomAPI_Face> aPlaneFace(new GeomAPI_Face(isPlaneFirst ? aShape1 : aShape2));
153   if(GeomAlgoAPI_ShapeTools::isParallel(anEdge, aPlaneFace)) {
154     theError = "Plane and edge are parallel.";
155     return false;
156   }
157
158   return true;
159 }
160
161 //==================================================================================================
162 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
163                                                         const std::list<std::string>& theArguments,
164                                                         Events_InfoMessage& theError) const
165 {
166   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
167
168   AttributeSelectionPtr aPointAttribute1 =
169     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
170   AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
171   AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
172
173   GeomShapePtr aPointShape1 = aPointAttribute1->value();
174   ResultPtr aContext1 = aPointAttribute1->context();
175   if(!aContext1.get()) {
176     theError = "One of the attribute not initialized.";
177     return false;
178   }
179   if(!aPointShape1.get()) {
180     aPointShape1 = aContext1->shape();
181   }
182   if(!aPointShape1->isVertex()) {
183     theError = "One of the selected shapes not a vertex.";
184     return false;
185   }
186
187   GeomShapePtr aPointShape2 = aPointAttribute2->value();
188   ResultPtr aContext2 = aPointAttribute2->context();
189   if(!aContext2.get()) {
190     return true;
191   }
192   if(!aPointShape2.get()) {
193     aPointShape2 = aContext2->shape();
194   }
195   if(!aPointShape2->isVertex()) {
196     theError = "One of the selected shapes not a vertex.";
197     return false;
198   }
199
200   GeomShapePtr aPointShape3 = aPointAttribute3->value();
201   ResultPtr aContext3 = aPointAttribute3->context();
202   if(!aContext3.get()) {
203     return true;
204   }
205   if(!aPointShape3.get()) {
206     aPointShape3 = aContext3->shape();
207   }
208   if(!aPointShape3->isVertex()) {
209     theError = "One of the selected shapes not a vertex.";
210     return false;
211   }
212
213   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
214   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
215   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
216
217   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
218   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
219   std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
220
221   if (aPnt1->isEqual(aPnt2)) {
222     theError = "Selected points are equal";
223     return false;
224   }
225
226   std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
227
228   if(aLin->contains(aPnt3)) {
229     theError = "Selected points lie on a line.";
230     return false;
231   }
232
233   return true;
234 }
235
236 //==================================================================================================
237 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
238     const AttributePtr& theAttribute,
239     const std::list<std::string>& theArguments,
240     Events_InfoMessage& theError) const
241 {
242   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
243
244   AttributeSelectionPtr anAttribute1 =
245     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
246   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
247
248   std::shared_ptr<GeomAPI_Lin> aLin;
249   std::shared_ptr<GeomAPI_Pnt> aPnt;
250
251   GeomShapePtr aShape1 = anAttribute1->value();
252   ResultPtr aContext1 = anAttribute1->context();
253   if(!aContext1.get()) {
254     theError = "One of the attribute not initialized.";
255     return false;
256   }
257   if(!aShape1.get()) {
258     aShape1 = aContext1->shape();
259   }
260
261   GeomShapePtr aShape2 = anAttribute2->value();
262   ResultPtr aContext2 = anAttribute2->context();
263   if(!aContext2.get()) {
264     return true;
265   }
266   if(!aShape2.get()) {
267     aShape2 = aContext2->shape();
268   }
269
270   aLin = getLin(aShape1);
271   aPnt = getPnt(aShape2);
272   if(!aLin.get() || !aPnt.get()) {
273     aLin = getLin(aShape2);
274     aPnt = getPnt(aShape1);
275   }
276
277   if(!aLin.get() || !aPnt.get()) {
278     theError = "Wrong shape types selected.";
279     return false;
280   }
281
282   // line should not contain point only for not-prependicular case
283   AttributeBooleanPtr aBoolAttr = aFeature->boolean(theArguments.back());
284   if (aBoolAttr.get() && !aBoolAttr->value()) {
285     if(aLin->contains(aPnt)) {
286       theError = "Point lies on the line.";
287       return false;
288     }
289   }
290
291   return true;
292 }
293
294 //==================================================================================================
295 bool ConstructionPlugin_ValidatorPlaneTwoParallelPlanes::isValid(
296     const AttributePtr& theAttribute,
297     const std::list<std::string>& theArguments,
298     Events_InfoMessage& theError) const
299 {
300   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
301
302   AttributeSelectionPtr anAttribute1 =
303     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
304   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
305
306   std::shared_ptr<GeomAPI_Pln> aPln1;
307   std::shared_ptr<GeomAPI_Pln> aPln2;
308
309   GeomShapePtr aShape1 = anAttribute1->value();
310   ResultPtr aContext1 = anAttribute1->context();
311   if(!aContext1.get()) {
312     theError = "One of the attribute not initialized.";
313     return false;
314   }
315   if(!aShape1.get()) {
316     aShape1 = aContext1->shape();
317   }
318
319   GeomShapePtr aShape2 = anAttribute2->value();
320   ResultPtr aContext2 = anAttribute2->context();
321   if(!aContext2.get()) {
322     return true;
323   }
324   if(!aShape2.get()) {
325     aShape2 = aContext2->shape();
326   }
327
328   aPln1 = getPln(aShape1);
329   aPln2 = getPln(aShape2);
330
331   if(!aPln1.get() || !aPln2.get()) {
332     theError = "Wrong shape types selected.";
333     return false;
334   }
335
336   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
337   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
338
339   if(!aDir1->isParallel(aDir2)) {
340     theError = "Planes not parallel.";
341     return false;
342   }
343
344   return true;
345 }
346
347 //==================================================================================================
348 bool ConstructionPlugin_ValidatorAxisTwoNotParallelPlanes::isValid(
349     const AttributePtr& theAttribute,
350     const std::list<std::string>& theArguments,
351     Events_InfoMessage& theError) const
352 {
353   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
354
355   AttributeSelectionPtr anAttribute1 =
356     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
357   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
358
359   std::shared_ptr<GeomAPI_Pln> aPln1;
360   std::shared_ptr<GeomAPI_Pln> aPln2;
361
362   GeomShapePtr aShape1 = anAttribute1->value();
363   ResultPtr aContext1 = anAttribute1->context();
364   if(!aContext1.get()) {
365     theError = "One of the attribute not initialized.";
366     return false;
367   }
368   if(!aShape1.get()) {
369     aShape1 = aContext1->shape();
370   }
371
372   GeomShapePtr aShape2 = anAttribute2->value();
373   ResultPtr aContext2 = anAttribute2->context();
374   if(!aContext2.get()) {
375     return true;
376   }
377   if(!aShape2.get()) {
378     aShape2 = aContext2->shape();
379   }
380
381   aPln1 = getPln(aShape1);
382   aPln2 = getPln(aShape2);
383
384   if(!aPln1.get() || !aPln2.get()) {
385     theError = "Wrong shape types selected.";
386     return false;
387   }
388
389   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
390   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
391
392   if(aDir1->isParallel(aDir2)) {
393     theError = "Planes are parallel.";
394     return false;
395   }
396
397   return true;
398 }
399
400 //==================================================================================================
401 bool ConstructionPlugin_ValidatorPointThreeNonParallelPlanes::isValid(
402   const AttributePtr& theAttribute,
403   const std::list<std::string>& theArguments,
404   Events_InfoMessage& theError) const
405 {
406   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
407
408   AttributeSelectionPtr anAttribute1 =
409     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
410   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
411   AttributeSelectionPtr anAttribute3 = aFeature->selection(theArguments.back());
412
413   GeomShapePtr aShape1 = anAttribute1->value();
414   ResultPtr aContext1 = anAttribute1->context();
415   if (!aContext1.get()) {
416     theError = "One of the attribute not initialized.";
417     return false;
418   }
419   if (!aShape1.get()) {
420     aShape1 = aContext1->shape();
421   }
422
423   std::shared_ptr<GeomAPI_Pln> aPln1 = getPln(aShape1);
424   if (!aPln1.get()) {
425     theError = "Wrong shape types selected.";
426     return false;
427   }
428   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
429
430   if (anAttribute2.get()) {
431     GeomShapePtr aShape2 = anAttribute2->value();
432     ResultPtr aContext2 = anAttribute2->context();
433     if (!aShape2.get() && aContext2.get()) {
434       aShape2 = aContext2->shape();
435     }
436
437     if (aShape2.get()) {
438       std::shared_ptr<GeomAPI_Pln> aPln2 = getPln(aShape2);
439       if (!aPln2.get()) {
440         theError = "Wrong shape types selected.";
441         return false;
442       }
443       std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
444       if (aDir1->isParallel(aDir2)) {
445         theError = "Planes are parallel.";
446         return false;
447       }
448     }
449   }
450
451   if (anAttribute3.get()) {
452     GeomShapePtr aShape3 = anAttribute3->value();
453     ResultPtr aContext3 = anAttribute3->context();
454     if (!aShape3.get() && aContext3.get()) {
455       aShape3 = aContext3->shape();
456     }
457
458     if (aShape3.get()) {
459       std::shared_ptr<GeomAPI_Pln> aPln3 = getPln(aShape3);
460       if (!aPln3.get()) {
461         theError = "Wrong shape types selected.";
462         return false;
463       }
464       std::shared_ptr<GeomAPI_Dir> aDir3 = aPln3->direction();
465       if (aDir1->isParallel(aDir3)) {
466         theError = "Planes are parallel.";
467         return false;
468       }
469     }
470   }
471
472   return true;
473 }
474
475 std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape)
476 {
477   if(!theShape->isEdge()) {
478     return std::shared_ptr<GeomAPI_Edge>();
479   }
480
481   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
482
483   return anEdge;
484 }
485
486 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
487 {
488   std::shared_ptr<GeomAPI_Lin> aLin;
489
490   if(!theShape->isEdge()) {
491     return aLin;
492   }
493
494   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
495
496   if(!anEdge->isLine()) {
497     return aLin;
498   }
499
500   aLin = anEdge->line();
501
502   return aLin;
503 }
504
505 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
506 {
507   std::shared_ptr<GeomAPI_Pln> aPln;
508
509   if(!theShape->isFace()) {
510     return aPln;
511   }
512
513   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
514
515   if(!aFace->isPlanar()) {
516     return aPln;
517   }
518
519   aPln = aFace->getPlane();
520
521   return aPln;
522 }
523
524 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
525 {
526   std::shared_ptr<GeomAPI_Pnt> aPnt;
527
528   if(!theShape->isVertex()) {
529     return aPnt;
530   }
531
532   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
533
534   aPnt = aVertex->point();
535
536   return aPnt;
537 }