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