1 // Copyright (C) 2014-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ConstructionPlugin_Validators.h"
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 #include <GeomAPI_Pnt.h>
29 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <ModelAPI_AttributeSelection.h>
33 #include <ModelAPI_AttributeBoolean.h>
35 #include <Events_InfoMessage.h>
37 static std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape);
38 static std::shared_ptr<GeomAPI_Face> getFace(const GeomShapePtr theShape);
39 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
40 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
41 static std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape);
43 //==================================================================================================
44 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
45 const std::list<std::string>& theArguments,
46 Events_InfoMessage& theError) const
48 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
50 AttributeSelectionPtr aLineAttribute1 =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
52 AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
54 GeomShapePtr aLineShape1 = aLineAttribute1->value();
55 ResultPtr aContext1 = aLineAttribute1->context();
56 if(!aContext1.get()) {
57 theError = "One of the attribute is not initialized.";
60 if(!aLineShape1.get()) {
61 aLineShape1 = aContext1->shape();
63 if(!aLineShape1->isEdge()) {
64 theError = "One of the selected shapes not an edge.";
68 GeomShapePtr aLineShape2 = aLineAttribute2->value();
69 ResultPtr aContext2 = aLineAttribute2->context();
70 if(!aContext2.get()) {
73 if(!aLineShape2.get()) {
74 aLineShape2 = aContext2->shape();
76 if(!aLineShape2->isEdge()) {
77 theError = "One of the selected shapes not an edge.";
81 std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
82 std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
84 std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
85 std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
87 if (!aLine1.get() || !aLine2.get()) {
88 theError = "Selected edge is not a line.";
92 if(!aLine1->isCoplanar(aLine2)) {
93 theError = "Selected lines not coplanar.";
97 if(aLine1->isParallel(aLine2)) {
98 theError = "Selected lines are parallel.";
105 //==================================================================================================
106 bool ConstructionPlugin_ValidatorPointEdgeAndPlaneNotParallel::isValid(
107 const AttributePtr& theAttribute,
108 const std::list<std::string>& theArguments,
109 Events_InfoMessage& theError) const
111 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
113 AttributeSelectionPtr anAttribute1 =
114 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
115 AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
117 std::shared_ptr<GeomAPI_Edge> anEdge;
118 std::shared_ptr<GeomAPI_Face> aFace;
120 GeomShapePtr aShape1 = anAttribute1->value();
121 ResultPtr aContext1 = anAttribute1->context();
122 if(!aContext1.get()) {
123 theError = "One of the attribute is not initialized.";
127 aShape1 = aContext1->shape();
130 GeomShapePtr aShape2 = anAttribute2->value();
131 ResultPtr aContext2 = anAttribute2->context();
132 if(!aContext2.get()) {
136 aShape2 = aContext2->shape();
139 anEdge = getEdge(aShape1);
140 aFace = getFace(aShape2);
141 if(!anEdge.get() || !aFace.get()) {
142 anEdge = getEdge(aShape2);
143 aFace = getFace(aShape1);
146 if(!anEdge.get() || !aFace.get()) {
147 theError = "Wrong shape types selected.";
151 if(GeomAlgoAPI_ShapeTools::isParallel(anEdge, aFace)) {
152 theError = "Plane and edge are parallel.";
159 //==================================================================================================
160 bool ConstructionPlugin_ValidatorPlaneThreePoints::isValid(const AttributePtr& theAttribute,
161 const std::list<std::string>& theArguments,
162 Events_InfoMessage& theError) const
164 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
166 AttributeSelectionPtr aPointAttribute1 =
167 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
168 AttributeSelectionPtr aPointAttribute2 = aFeature->selection(theArguments.front());
169 AttributeSelectionPtr aPointAttribute3 = aFeature->selection(theArguments.back());
171 GeomShapePtr aPointShape1 = aPointAttribute1->value();
172 ResultPtr aContext1 = aPointAttribute1->context();
173 if(!aContext1.get()) {
174 theError = "One of the attribute is not initialized.";
177 if(!aPointShape1.get()) {
178 aPointShape1 = aContext1->shape();
180 if(!aPointShape1->isVertex()) {
181 theError = "One of the selected shapes not a vertex.";
185 GeomShapePtr aPointShape2 = aPointAttribute2->value();
186 ResultPtr aContext2 = aPointAttribute2->context();
187 if(!aContext2.get()) {
190 if(!aPointShape2.get()) {
191 aPointShape2 = aContext2->shape();
193 if(!aPointShape2->isVertex()) {
194 theError = "One of the selected shapes not a vertex.";
198 GeomShapePtr aPointShape3 = aPointAttribute3->value();
199 ResultPtr aContext3 = aPointAttribute3->context();
200 if(!aContext3.get()) {
203 if(!aPointShape3.get()) {
204 aPointShape3 = aContext3->shape();
206 if(!aPointShape3->isVertex()) {
207 theError = "One of the selected shapes not a vertex.";
211 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
212 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
213 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
215 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aVertex1->point();
216 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aVertex2->point();
217 std::shared_ptr<GeomAPI_Pnt> aPnt3 = aVertex3->point();
219 if (aPnt1->isEqual(aPnt2)) {
220 theError = "Selected points are equal";
224 std::shared_ptr<GeomAPI_Lin> aLin(new GeomAPI_Lin(aPnt1, aPnt2));
226 if(aLin->contains(aPnt3)) {
227 theError = "Selected points lie on a line.";
234 //==================================================================================================
235 bool ConstructionPlugin_ValidatorPlaneLinePoint::isValid(
236 const AttributePtr& theAttribute,
237 const std::list<std::string>& theArguments,
238 Events_InfoMessage& theError) const
240 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
242 AttributeSelectionPtr anAttribute1 =
243 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
244 AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
246 std::shared_ptr<GeomAPI_Lin> aLin;
247 std::shared_ptr<GeomAPI_Pnt> aPnt;
249 GeomShapePtr aShape1 = anAttribute1->value();
250 ResultPtr aContext1 = anAttribute1->context();
251 if(!aContext1.get()) {
252 theError = "One of the attribute is not initialized.";
256 aShape1 = aContext1->shape();
259 GeomShapePtr aShape2 = anAttribute2->value();
260 ResultPtr aContext2 = anAttribute2->context();
261 if(!aContext2.get()) {
265 aShape2 = aContext2->shape();
268 aLin = getLin(aShape1);
269 aPnt = getPnt(aShape2);
270 if(!aLin.get() || !aPnt.get()) {
271 aLin = getLin(aShape2);
272 aPnt = getPnt(aShape1);
275 if(!aLin.get() || !aPnt.get()) {
276 theError = "Wrong shape types selected.";
280 // line should not contain point only for not-prependicular case
281 AttributeBooleanPtr aBoolAttr = aFeature->boolean(theArguments.back());
282 if (aBoolAttr.get() && !aBoolAttr->value()) {
283 if(aLin->contains(aPnt)) {
284 theError = "Point lies on the line.";
292 //==================================================================================================
293 bool ConstructionPlugin_ValidatorPlaneTwoParallelPlanes::isValid(
294 const AttributePtr& theAttribute,
295 const std::list<std::string>& theArguments,
296 Events_InfoMessage& theError) const
298 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
300 AttributeSelectionPtr anAttribute1 =
301 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
302 AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
304 std::shared_ptr<GeomAPI_Pln> aPln1;
305 std::shared_ptr<GeomAPI_Pln> aPln2;
307 GeomShapePtr aShape1 = anAttribute1->value();
308 ResultPtr aContext1 = anAttribute1->context();
309 if(!aContext1.get()) {
310 theError = "One of the attribute is not initialized.";
314 aShape1 = aContext1->shape();
317 GeomShapePtr aShape2 = anAttribute2->value();
318 ResultPtr aContext2 = anAttribute2->context();
319 if(!aContext2.get()) {
323 aShape2 = aContext2->shape();
326 aPln1 = getPln(aShape1);
327 aPln2 = getPln(aShape2);
329 if(!aPln1.get() || !aPln2.get()) {
330 theError = "Wrong shape types selected.";
334 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
335 std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
337 if(!aDir1->isParallel(aDir2)) {
338 theError = "Planes not parallel.";
345 //==================================================================================================
346 bool ConstructionPlugin_ValidatorAxisTwoNotParallelPlanes::isValid(
347 const AttributePtr& theAttribute,
348 const std::list<std::string>& theArguments,
349 Events_InfoMessage& theError) const
351 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
353 AttributeSelectionPtr anAttribute1 =
354 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
355 AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
357 std::shared_ptr<GeomAPI_Pln> aPln1;
358 std::shared_ptr<GeomAPI_Pln> aPln2;
360 GeomShapePtr aShape1 = anAttribute1->value();
361 ResultPtr aContext1 = anAttribute1->context();
362 if(!aContext1.get()) {
363 theError = "One of the attribute is not initialized.";
367 aShape1 = aContext1->shape();
370 GeomShapePtr aShape2 = anAttribute2->value();
371 ResultPtr aContext2 = anAttribute2->context();
372 if(!aContext2.get()) {
376 aShape2 = aContext2->shape();
379 aPln1 = getPln(aShape1);
380 aPln2 = getPln(aShape2);
382 if(!aPln1.get() || !aPln2.get()) {
383 theError = "Wrong shape types selected.";
387 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
388 std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
390 if(aDir1->isParallel(aDir2)) {
391 theError = "Planes are parallel.";
398 //==================================================================================================
399 bool ConstructionPlugin_ValidatorPointThreeNonParallelPlanes::isValid(
400 const AttributePtr& theAttribute,
401 const std::list<std::string>& theArguments,
402 Events_InfoMessage& theError) const
404 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
406 AttributeSelectionPtr anAttribute1 =
407 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
408 AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
409 AttributeSelectionPtr anAttribute3 = aFeature->selection(theArguments.back());
411 GeomShapePtr aShape1 = anAttribute1->value();
412 ResultPtr aContext1 = anAttribute1->context();
413 if (!aContext1.get()) {
414 theError = "One of the attribute is not initialized.";
417 if (!aShape1.get()) {
418 aShape1 = aContext1->shape();
421 std::shared_ptr<GeomAPI_Pln> aPln1 = getPln(aShape1);
423 theError = "Wrong shape types selected.";
426 std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
428 if (anAttribute2.get()) {
429 GeomShapePtr aShape2 = anAttribute2->value();
430 ResultPtr aContext2 = anAttribute2->context();
431 if (!aShape2.get() && aContext2.get()) {
432 aShape2 = aContext2->shape();
436 std::shared_ptr<GeomAPI_Pln> aPln2 = getPln(aShape2);
438 theError = "Wrong shape types selected.";
441 std::shared_ptr<GeomAPI_Dir> aDir2 = aPln2->direction();
442 if (aDir1->isParallel(aDir2)) {
443 theError = "Planes are parallel.";
449 if (anAttribute3.get()) {
450 GeomShapePtr aShape3 = anAttribute3->value();
451 ResultPtr aContext3 = anAttribute3->context();
452 if (!aShape3.get() && aContext3.get()) {
453 aShape3 = aContext3->shape();
457 std::shared_ptr<GeomAPI_Pln> aPln3 = getPln(aShape3);
459 theError = "Wrong shape types selected.";
462 std::shared_ptr<GeomAPI_Dir> aDir3 = aPln3->direction();
463 if (aDir1->isParallel(aDir3)) {
464 theError = "Planes are parallel.";
473 //==================================================================================================
474 bool ConstructionPlugin_ValidatorNotFeature::isValid(
475 const AttributePtr& theAttribute,
476 const std::list<std::string>& /*theArguments*/,
477 Events_InfoMessage& theError) const
479 AttributeSelectionPtr aSelAttr =
480 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
482 theError = "Wrong attribute";
486 FeaturePtr aContextFeature = aSelAttr->contextFeature();
487 if (aContextFeature) {
488 theError = "Feature should not be selected";
494 //==================================================================================================
496 std::shared_ptr<GeomAPI_Edge> getEdge(const GeomShapePtr theShape)
500 if(theShape->isEdge()) {
501 anEdge = theShape->edge();
503 else if (theShape->isCompound()) {
504 GeomAPI_ShapeIterator anIt(theShape);
505 anEdge = anIt.current()->edge();
511 GeomFacePtr getFace(const GeomShapePtr theShape)
515 if (theShape->isFace()) {
516 aFace = theShape->face();
518 else if (theShape->isCompound()) {
519 GeomAPI_ShapeIterator anIt(theShape);
520 aFace = anIt.current()->face();
526 std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
528 std::shared_ptr<GeomAPI_Lin> aLin;
532 if (theShape->isEdge()) {
533 anEdge = theShape->edge();
535 else if (theShape->isCompound()) {
536 GeomAPI_ShapeIterator anIt(theShape);
537 anEdge = anIt.current()->edge();
543 if(!anEdge->isLine()) {
547 aLin = anEdge->line();
552 std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
554 std::shared_ptr<GeomAPI_Pln> aPln;
558 if(theShape->isFace()) {
559 aFace = theShape->face();
561 else if (theShape->isCompound()) {
562 GeomAPI_ShapeIterator anIt(theShape);
563 aFace = anIt.current()->face();
569 if(!aFace || !aFace->isPlanar()) {
573 aPln = aFace->getPlane();
578 std::shared_ptr<GeomAPI_Pnt> getPnt(const GeomShapePtr theShape)
580 std::shared_ptr<GeomAPI_Pnt> aPnt;
582 if(!theShape->isVertex()) {
586 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(theShape));
588 aPnt = aVertex->point();