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