Salome HOME
Merge commit 'refs/tags/V9_2_0^{}'
[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 <GeomAPI_ShapeIterator.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_AttributeBoolean.h>
35
36 #include <Events_InfoMessage.h>
37
38 static std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape);
39 static std::shared_ptr<GeomAPI_Face> getFace(const GeomShapePtr theShape);
40 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
41 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
42 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
43
44 //==================================================================================================
45 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
46                                                      const std::list<std::string>& theArguments,
47                                                      Events_InfoMessage& theError) const
48 {
49   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
50
51   AttributeSelectionPtr aLineAttribute1 =
52     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
53   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
54
55   GeomShapePtr aLineShape1 = aLineAttribute1->value();
56   ResultPtr aContext1 = aLineAttribute1->context();
57   if(!aContext1.get()) {
58     theError = "One of the attribute not initialized.";
59     return false;
60   }
61   if(!aLineShape1.get()) {
62     aLineShape1 = aContext1->shape();
63   }
64   if(!aLineShape1->isEdge()) {
65     theError = "One of the selected shapes not an edge.";
66     return false;
67   }
68
69   GeomShapePtr aLineShape2 = aLineAttribute2->value();
70   ResultPtr aContext2 = aLineAttribute2->context();
71   if(!aContext2.get()) {
72     return true;
73   }
74   if(!aLineShape2.get()) {
75     aLineShape2 = aContext2->shape();
76   }
77   if(!aLineShape2->isEdge()) {
78     theError = "One of the selected shapes not an edge.";
79     return false;
80   }
81
82   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
83   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
84
85   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
86   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
87
88   if (!aLine1.get() || !aLine2.get()) {
89     theError = "Selected edge is not a line.";
90     return false;
91   }
92
93   if(!aLine1->isCoplanar(aLine2)) {
94     theError = "Selected lines not coplanar.";
95     return false;
96   }
97
98   if(aLine1->isParallel(aLine2)) {
99     theError = "Selected lines are parallel.";
100     return false;
101   }
102
103   return true;
104 }
105
106 //==================================================================================================
107 bool ConstructionPlugin_ValidatorPointEdgeAndPlaneNotParallel::isValid(
108     const AttributePtr& theAttribute,
109     const std::list<std::string>& theArguments,
110     Events_InfoMessage& theError) const
111 {
112   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
113
114   AttributeSelectionPtr anAttribute1 =
115     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
116   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
117
118   std::shared_ptr<GeomAPI_Edge> anEdge;
119   std::shared_ptr<GeomAPI_Face> aFace;
120
121   GeomShapePtr aShape1 = anAttribute1->value();
122   ResultPtr aContext1 = anAttribute1->context();
123   if(!aContext1.get()) {
124     theError = "One of the attribute not initialized.";
125     return false;
126   }
127   if(!aShape1.get()) {
128     aShape1 = aContext1->shape();
129   }
130
131   GeomShapePtr aShape2 = anAttribute2->value();
132   ResultPtr aContext2 = anAttribute2->context();
133   if(!aContext2.get()) {
134     return true;
135   }
136   if(!aShape2.get()) {
137     aShape2 = aContext2->shape();
138   }
139
140   bool isPlaneFirst = false;
141   anEdge = getEdge(aShape1);
142
143   aFace = getFace(aShape2);
144   if(!anEdge.get() || !aFace.get()) {
145     anEdge = getEdge(aShape2);
146     aFace = getFace(aShape1);
147     isPlaneFirst = true;
148   }
149
150   if(!anEdge.get() || !aFace.get()) {
151     theError = "Wrong shape types selected.";
152     return false;
153   }
154
155   if(GeomAlgoAPI_ShapeTools::isParallel(anEdge, aFace)) {
156     theError = "Plane and edge are parallel.";
157     return false;
158   }
159
160   return true;
161 }
162
163 //==================================================================================================
164 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
165                                                         const std::list<std::string>& theArguments,
166                                                         Events_InfoMessage& theError) const
167 {
168   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
169
170   AttributeSelectionPtr aPointAttribute1 =
171     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
172   AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
173   AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
174
175   GeomShapePtr aPointShape1 = aPointAttribute1->value();
176   ResultPtr aContext1 = aPointAttribute1->context();
177   if(!aContext1.get()) {
178     theError = "One of the attribute not initialized.";
179     return false;
180   }
181   if(!aPointShape1.get()) {
182     aPointShape1 = aContext1->shape();
183   }
184   if(!aPointShape1->isVertex()) {
185     theError = "One of the selected shapes not a vertex.";
186     return false;
187   }
188
189   GeomShapePtr aPointShape2 = aPointAttribute2->value();
190   ResultPtr aContext2 = aPointAttribute2->context();
191   if(!aContext2.get()) {
192     return true;
193   }
194   if(!aPointShape2.get()) {
195     aPointShape2 = aContext2->shape();
196   }
197   if(!aPointShape2->isVertex()) {
198     theError = "One of the selected shapes not a vertex.";
199     return false;
200   }
201
202   GeomShapePtr aPointShape3 = aPointAttribute3->value();
203   ResultPtr aContext3 = aPointAttribute3->context();
204   if(!aContext3.get()) {
205     return true;
206   }
207   if(!aPointShape3.get()) {
208     aPointShape3 = aContext3->shape();
209   }
210   if(!aPointShape3->isVertex()) {
211     theError = "One of the selected shapes not a vertex.";
212     return false;
213   }
214
215   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
216   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
217   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
218
219   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
220   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
221   std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
222
223   if (aPnt1->isEqual(aPnt2)) {
224     theError = "Selected points are equal";
225     return false;
226   }
227
228   std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
229
230   if(aLin->contains(aPnt3)) {
231     theError = "Selected points lie on a line.";
232     return false;
233   }
234
235   return true;
236 }
237
238 //==================================================================================================
239 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
240     const AttributePtr& theAttribute,
241     const std::list<std::string>& theArguments,
242     Events_InfoMessage& theError) const
243 {
244   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
245
246   AttributeSelectionPtr anAttribute1 =
247     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
248   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
249
250   std::shared_ptr<GeomAPI_Lin> aLin;
251   std::shared_ptr<GeomAPI_Pnt> aPnt;
252
253   GeomShapePtr aShape1 = anAttribute1->value();
254   ResultPtr aContext1 = anAttribute1->context();
255   if(!aContext1.get()) {
256     theError = "One of the attribute not initialized.";
257     return false;
258   }
259   if(!aShape1.get()) {
260     aShape1 = aContext1->shape();
261   }
262
263   GeomShapePtr aShape2 = anAttribute2->value();
264   ResultPtr aContext2 = anAttribute2->context();
265   if(!aContext2.get()) {
266     return true;
267   }
268   if(!aShape2.get()) {
269     aShape2 = aContext2->shape();
270   }
271
272   aLin = getLin(aShape1);
273   aPnt = getPnt(aShape2);
274   if(!aLin.get() || !aPnt.get()) {
275     aLin = getLin(aShape2);
276     aPnt = getPnt(aShape1);
277   }
278
279   if(!aLin.get() || !aPnt.get()) {
280     theError = "Wrong shape types selected.";
281     return false;
282   }
283
284   // line should not contain point only for not-prependicular case
285   AttributeBooleanPtr aBoolAttr = aFeature->boolean(theArguments.back());
286   if (aBoolAttr.get() && !aBoolAttr->value()) {
287     if(aLin->contains(aPnt)) {
288       theError = "Point lies on the line.";
289       return false;
290     }
291   }
292
293   return true;
294 }
295
296 //==================================================================================================
297 bool ConstructionPlugin_ValidatorPlaneTwoParallelPlanes::isValid(
298     const AttributePtr& theAttribute,
299     const std::list<std::string>& theArguments,
300     Events_InfoMessage& theError) const
301 {
302   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
303
304   AttributeSelectionPtr anAttribute1 =
305     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
306   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
307
308   std::shared_ptr<GeomAPI_Pln> aPln1;
309   std::shared_ptr<GeomAPI_Pln> aPln2;
310
311   GeomShapePtr aShape1 = anAttribute1->value();
312   ResultPtr aContext1 = anAttribute1->context();
313   if(!aContext1.get()) {
314     theError = "One of the attribute not initialized.";
315     return false;
316   }
317   if(!aShape1.get()) {
318     aShape1 = aContext1->shape();
319   }
320
321   GeomShapePtr aShape2 = anAttribute2->value();
322   ResultPtr aContext2 = anAttribute2->context();
323   if(!aContext2.get()) {
324     return true;
325   }
326   if(!aShape2.get()) {
327     aShape2 = aContext2->shape();
328   }
329
330   aPln1 = getPln(aShape1);
331   aPln2 = getPln(aShape2);
332
333   if(!aPln1.get() || !aPln2.get()) {
334     theError = "Wrong shape types selected.";
335     return false;
336   }
337
338   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
339   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
340
341   if(!aDir1->isParallel(aDir2)) {
342     theError = "Planes not parallel.";
343     return false;
344   }
345
346   return true;
347 }
348
349 //==================================================================================================
350 bool ConstructionPlugin_ValidatorAxisTwoNotParallelPlanes::isValid(
351     const AttributePtr& theAttribute,
352     const std::list<std::string>& theArguments,
353     Events_InfoMessage& theError) const
354 {
355   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
356
357   AttributeSelectionPtr anAttribute1 =
358     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
359   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
360
361   std::shared_ptr<GeomAPI_Pln> aPln1;
362   std::shared_ptr<GeomAPI_Pln> aPln2;
363
364   GeomShapePtr aShape1 = anAttribute1->value();
365   ResultPtr aContext1 = anAttribute1->context();
366   if(!aContext1.get()) {
367     theError = "One of the attribute not initialized.";
368     return false;
369   }
370   if(!aShape1.get()) {
371     aShape1 = aContext1->shape();
372   }
373
374   GeomShapePtr aShape2 = anAttribute2->value();
375   ResultPtr aContext2 = anAttribute2->context();
376   if(!aContext2.get()) {
377     return true;
378   }
379   if(!aShape2.get()) {
380     aShape2 = aContext2->shape();
381   }
382
383   aPln1 = getPln(aShape1);
384   aPln2 = getPln(aShape2);
385
386   if(!aPln1.get() || !aPln2.get()) {
387     theError = "Wrong shape types selected.";
388     return false;
389   }
390
391   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
392   std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
393
394   if(aDir1->isParallel(aDir2)) {
395     theError = "Planes are parallel.";
396     return false;
397   }
398
399   return true;
400 }
401
402 //==================================================================================================
403 bool ConstructionPlugin_ValidatorPointThreeNonParallelPlanes::isValid(
404   const AttributePtr& theAttribute,
405   const std::list<std::string>& theArguments,
406   Events_InfoMessage& theError) const
407 {
408   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
409
410   AttributeSelectionPtr anAttribute1 =
411     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
412   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
413   AttributeSelectionPtr anAttribute3 = aFeature->selection(theArguments.back());
414
415   GeomShapePtr aShape1 = anAttribute1->value();
416   ResultPtr aContext1 = anAttribute1->context();
417   if (!aContext1.get()) {
418     theError = "One of the attribute not initialized.";
419     return false;
420   }
421   if (!aShape1.get()) {
422     aShape1 = aContext1->shape();
423   }
424
425   std::shared_ptr<GeomAPI_Pln> aPln1 = getPln(aShape1);
426   if (!aPln1.get()) {
427     theError = "Wrong shape types selected.";
428     return false;
429   }
430   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
431
432   if (anAttribute2.get()) {
433     GeomShapePtr aShape2 = anAttribute2->value();
434     ResultPtr aContext2 = anAttribute2->context();
435     if (!aShape2.get() && aContext2.get()) {
436       aShape2 = aContext2->shape();
437     }
438
439     if (aShape2.get()) {
440       std::shared_ptr<GeomAPI_Pln> aPln2 = getPln(aShape2);
441       if (!aPln2.get()) {
442         theError = "Wrong shape types selected.";
443         return false;
444       }
445       std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
446       if (aDir1->isParallel(aDir2)) {
447         theError = "Planes are parallel.";
448         return false;
449       }
450     }
451   }
452
453   if (anAttribute3.get()) {
454     GeomShapePtr aShape3 = anAttribute3->value();
455     ResultPtr aContext3 = anAttribute3->context();
456     if (!aShape3.get() && aContext3.get()) {
457       aShape3 = aContext3->shape();
458     }
459
460     if (aShape3.get()) {
461       std::shared_ptr<GeomAPI_Pln> aPln3 = getPln(aShape3);
462       if (!aPln3.get()) {
463         theError = "Wrong shape types selected.";
464         return false;
465       }
466       std::shared_ptr<GeomAPI_Dir> aDir3 = aPln3->direction();
467       if (aDir1->isParallel(aDir3)) {
468         theError = "Planes are parallel.";
469         return false;
470       }
471     }
472   }
473
474   return true;
475 }
476
477 std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape)
478 {
479   GeomEdgePtr anEdge;
480
481   if(theShape->isEdge()) {
482     anEdge = theShape->edge();
483   }
484   else if (theShape->isCompound()) {
485     GeomAPI_ShapeIterator anIt(theShape);
486     anEdge = anIt.current()->edge();
487   }
488
489   return anEdge;
490 }
491
492 GeomFacePtr getFace(const GeomShapePtr theShape)
493 {
494   GeomFacePtr aFace;
495
496   if (theShape->isFace()) {
497     aFace = theShape->face();
498   }
499   else if (theShape->isCompound()) {
500     GeomAPI_ShapeIterator anIt(theShape);
501     aFace = anIt.current()->face();
502   }
503
504   return aFace;
505 }
506
507 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
508 {
509   std::shared_ptr<GeomAPI_Lin> aLin;
510
511   GeomEdgePtr anEdge;
512
513   if (theShape->isEdge()) {
514     anEdge = theShape->edge();
515   }
516   else if (theShape->isCompound()) {
517     GeomAPI_ShapeIterator anIt(theShape);
518     anEdge = anIt.current()->edge();
519   }
520   else {
521     return aLin;
522   }
523
524   if(!anEdge->isLine()) {
525     return aLin;
526   }
527
528   aLin = anEdge->line();
529
530   return aLin;
531 }
532
533 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
534 {
535   std::shared_ptr<GeomAPI_Pln> aPln;
536
537   GeomFacePtr aFace;
538
539   if(theShape->isFace()) {
540     aFace = theShape->face();
541   }
542   else if (theShape->isCompound()) {
543     GeomAPI_ShapeIterator anIt(theShape);
544     aFace = anIt.current()->face();
545   }
546   else {
547     return aPln;
548   }
549
550   if(!aFace || !aFace->isPlanar()) {
551     return aPln;
552   }
553
554   aPln = aFace->getPlane();
555
556   return aPln;
557 }
558
559 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
560 {
561   std::shared_ptr<GeomAPI_Pnt> aPnt;
562
563   if(!theShape->isVertex()) {
564     return aPnt;
565   }
566
567   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
568
569   aPnt = aVertex->point();
570
571   return aPnt;
572 }