]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConstructionPlugin/ConstructionPlugin_Validators.cpp
Salome HOME
Issue #1649: Added options to create plane by three points;
[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_Edge.h>
10 #include <GeomAPI_Face.h>
11 #include <GeomAPI_Lin.h>
12 #include <GeomAPI_Pln.h>
13 #include <GeomAPI_Vertex.h>
14
15 #include <ModelAPI_AttributeSelection.h>
16
17 #include <Events_InfoMessage.h>
18
19 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
20 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
21 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
22
23 //==================================================================================================
24 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
25                                                      const std::list<std::string>& theArguments,
26                                                      Events_InfoMessage& theError) const
27 {
28   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
29
30   AttributeSelectionPtr aLineAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
31   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
32
33   GeomShapePtr aLineShape1 = aLineAttribute1->value();
34   ResultPtr aContext1 = aLineAttribute1->context();
35   if(!aContext1.get()) {
36     theError = "One of the attribute not initialized.";
37     return false;
38   }
39   if(!aLineShape1.get()) {
40     aLineShape1 = aContext1->shape();
41   }
42   if(!aLineShape1->isEdge()) {
43     theError = "One of the selected shapes not an edge.";
44     return false;
45   }
46
47   GeomShapePtr aLineShape2 = aLineAttribute2->value();
48   ResultPtr aContext2 = aLineAttribute2->context();
49   if(!aContext2.get()) {
50     return true;
51   }
52   if(!aLineShape2.get()) {
53     aLineShape2 = aContext2->shape();
54   }
55   if(!aLineShape2->isEdge()) {
56     theError = "One of the selected shapes not an edge.";
57     return false;
58   }
59
60   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
61   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
62
63   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
64   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
65
66   if(!aLine1->isCoplanar(aLine2)) {
67     theError = "Selected lines not coplanar.";
68     return false;
69   }
70
71   if(aLine1->isParallel(aLine2)) {
72     theError = "Selected lines are parallel.";
73     return false;
74   }
75
76   return true;
77 }
78
79 //==================================================================================================
80 bool ConstructionPlugin_ValidatorPointLineAndPlaneNotParallel::isValid(
81     const AttributePtr& theAttribute,
82     const std::list<std::string>& theArguments,
83     Events_InfoMessage& theError) const
84 {
85   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
86
87   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
88   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
89
90   std::shared_ptr<GeomAPI_Lin> aLin;
91   std::shared_ptr<GeomAPI_Pln> aPln;
92
93   GeomShapePtr aShape1 = anAttribute1->value();
94   ResultPtr aContext1 = anAttribute1->context();
95   if(!aContext1.get()) {
96     theError = "One of the attribute not initialized.";
97     return false;
98   }
99   if(!aShape1.get()) {
100     aShape1 = aContext1->shape();
101   }
102
103   GeomShapePtr aShape2 = anAttribute2->value();
104   ResultPtr aContext2 = anAttribute2->context();
105   if(!aContext2.get()) {
106     return true;
107   }
108   if(!aShape2.get()) {
109     aShape2 = aContext2->shape();
110   }
111
112   aLin = getLin(aShape1);
113   aPln = getPln(aShape2);
114   if(!aLin.get() || !aPln.get()) {
115     aLin = getLin(aShape2);
116     aPln = getPln(aShape1);
117   }
118
119   if(!aLin.get() || !aPln.get()) {
120     theError = "Wrong shape types selected.";
121     return false;
122   }
123
124   if(aPln->isParallel(aLin)) {
125     theError = "Plane and line are parallel.";
126     return false;
127   }
128
129   return true;
130 }
131
132 //==================================================================================================
133 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
134                                                            const std::list<std::string>& theArguments,
135                                                            Events_InfoMessage& theError) const
136 {
137   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
138
139   AttributeSelectionPtr aPointAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
140   AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
141   AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
142
143   GeomShapePtr aPointShape1 = aPointAttribute1->value();
144   ResultPtr aContext1 = aPointAttribute1->context();
145   if(!aContext1.get()) {
146     theError = "One of the attribute not initialized.";
147     return false;
148   }
149   if(!aPointShape1.get()) {
150     aPointShape1 = aContext1->shape();
151   }
152   if(!aPointShape1->isVertex()) {
153     theError = "One of the selected shapes not a vertex.";
154     return false;
155   }
156
157   GeomShapePtr aPointShape2 = aPointAttribute2->value();
158   ResultPtr aContext2 = aPointAttribute2->context();
159   if(!aContext2.get()) {
160     return true;
161   }
162   if(!aPointShape2.get()) {
163     aPointShape2 = aContext2->shape();
164   }
165   if(!aPointShape2->isVertex()) {
166     theError = "One of the selected shapes not a vertex.";
167     return false;
168   }
169
170   GeomShapePtr aPointShape3 = aPointAttribute3->value();
171   ResultPtr aContext3 = aPointAttribute3->context();
172   if(!aContext3.get()) {
173     return true;
174   }
175   if(!aPointShape3.get()) {
176     aPointShape3 = aContext3->shape();
177   }
178   if(!aPointShape3->isVertex()) {
179     theError = "One of the selected shapes not a vertex.";
180     return false;
181   }
182
183   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
184   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
185   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
186
187   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
188   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
189   std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
190
191   std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
192
193   if(aLin->contains(aPnt3)) {
194     theError = "Selected points lie on a line.";
195     return false;
196   }
197
198   return true;
199 }
200
201 //==================================================================================================
202 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
203     const AttributePtr& theAttribute,
204     const std::list<std::string>& theArguments,
205     Events_InfoMessage& theError) const
206 {
207   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
208
209   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
210   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
211
212   std::shared_ptr<GeomAPI_Lin> aLin;
213   std::shared_ptr<GeomAPI_Pnt> aPnt;
214
215   GeomShapePtr aShape1 = anAttribute1->value();
216   ResultPtr aContext1 = anAttribute1->context();
217   if(!aContext1.get()) {
218     theError = "One of the attribute not initialized.";
219     return false;
220   }
221   if(!aShape1.get()) {
222     aShape1 = aContext1->shape();
223   }
224
225   GeomShapePtr aShape2 = anAttribute2->value();
226   ResultPtr aContext2 = anAttribute2->context();
227   if(!aContext2.get()) {
228     return true;
229   }
230   if(!aShape2.get()) {
231     aShape2 = aContext2->shape();
232   }
233
234   aLin = getLin(aShape1);
235   aPnt = getPnt(aShape2);
236   if(!aLin.get() || !aPnt.get()) {
237     aLin = getLin(aShape2);
238     aPnt = getPnt(aShape1);
239   }
240
241   if(!aLin.get() || !aPnt.get()) {
242     theError = "Wrong shape types selected.";
243     return false;
244   }
245
246   if(aLin->contains(aPnt)) {
247     theError = "Point lies on the line.";
248     return false;
249   }
250
251   return true;
252 }
253
254 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
255 {
256   std::shared_ptr<GeomAPI_Lin> aLin;
257
258   if(!theShape->isEdge()) {
259     return aLin;
260   }
261
262   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
263
264   if(!anEdge->isLine()) {
265     return aLin;
266   }
267
268   aLin = anEdge->line();
269
270   return aLin;
271 }
272
273 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
274 {
275   std::shared_ptr<GeomAPI_Pln> aPln;
276
277   if(!theShape->isFace()) {
278     return aPln;
279   }
280
281   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
282
283   if(!aFace->isPlanar()) {
284     return aPln;
285   }
286
287   aPln = aFace->getPlane();
288
289   return aPln;
290 }
291
292 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
293 {
294   std::shared_ptr<GeomAPI_Pnt> aPnt;
295
296   if(!theShape->isVertex()) {
297     return aPnt;
298   }
299
300   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
301
302   aPnt = aVertex->point();
303
304   return aPnt;
305 }