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