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