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