Salome HOME
Added option to create Construction Point by intersection of line and plane.
[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
14 #include <ModelAPI_AttributeSelection.h>
15
16 #include <Events_InfoMessage.h>
17
18 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape);
19 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape);
20
21 //==================================================================================================
22 bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute,
23                                                      const std::list<std::string>& theArguments,
24                                                      Events_InfoMessage& theError) const
25 {
26   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
27
28   AttributeSelectionPtr aLineAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
29   AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front());
30
31   GeomShapePtr aLineShape1 = aLineAttribute1->value();
32   ResultPtr aContext1 = aLineAttribute1->context();
33   if(!aContext1.get()) {
34     theError = "One of the attribute not initialized.";
35     return false;
36   }
37   if(!aLineShape1.get()) {
38     aLineShape1 = aContext1->shape();
39   }
40   if(!aLineShape1->isEdge()) {
41     theError = "One of the selected shapes not an edge.";
42     return false;
43   }
44
45   GeomShapePtr aLineShape2 = aLineAttribute2->value();
46   ResultPtr aContext2 = aLineAttribute2->context();
47   if(!aContext2.get()) {
48     return true;
49   }
50   if(!aLineShape2.get()) {
51     aLineShape2 = aContext2->shape();
52   }
53   if(!aLineShape2->isEdge()) {
54     theError = "One of the selected shapes not an edge.";
55     return false;
56   }
57
58   std::shared_ptr<GeomAPI_Edge> aLineEdge1(new GeomAPI_Edge(aLineShape1));
59   std::shared_ptr<GeomAPI_Edge> aLineEdge2(new GeomAPI_Edge(aLineShape2));
60
61   std::shared_ptr<GeomAPI_Lin> aLine1 = aLineEdge1->line();
62   std::shared_ptr<GeomAPI_Lin> aLine2 = aLineEdge2->line();
63
64   if(!aLine1->isCoplanar(aLine2)) {
65     theError = "Selected lines not coplanar.";
66     return false;
67   }
68
69   if(aLine1->isParallel(aLine2)) {
70     theError = "Selected lines are parallel.";
71     return false;
72   }
73
74   return true;
75 }
76
77 //==================================================================================================
78 bool ConstructionPlugin_ValidatorPointLineAndPlaneNotParallel::isValid(
79     const AttributePtr& theAttribute,
80     const std::list<std::string>& theArguments,
81     Events_InfoMessage& theError) const
82 {
83   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
84
85   AttributeSelectionPtr anAttribute1 = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
86   AttributeSelectionPtr anAttribute2 = aFeature->selection(theArguments.front());
87
88   std::shared_ptr<GeomAPI_Lin> aLin;
89   std::shared_ptr<GeomAPI_Pln> aPln;
90
91   GeomShapePtr aShape1 = anAttribute1->value();
92   ResultPtr aContext1 = anAttribute1->context();
93   if(!aContext1.get()) {
94     theError = "One of the attribute not initialized.";
95     return false;
96   }
97   if(!aShape1.get()) {
98     aShape1 = aContext1->shape();
99   }
100
101   GeomShapePtr aShape2 = anAttribute2->value();
102   ResultPtr aContext2 = anAttribute2->context();
103   if(!aContext2.get()) {
104     return true;
105   }
106   if(!aShape2.get()) {
107     aShape2 = aContext2->shape();
108   }
109
110   aLin = getLin(aShape1);
111   aPln = getPln(aShape2);
112   if(!aLin.get() || !aPln.get()) {
113     aLin = getLin(aShape2);
114     aPln = getPln(aShape1);
115   }
116
117   if(!aLin.get() || !aPln.get()) {
118     theError = "Wrong shape types selected.";
119     return false;
120   }
121
122   if(aPln->isParallel(aLin)) {
123     theError = "Plane and line are parallel.";
124     return false;
125   }
126
127   return true;
128 }
129
130 static std::shared_ptr<GeomAPI_Lin> getLin(const GeomShapePtr theShape)
131 {
132   std::shared_ptr<GeomAPI_Lin> aLin;
133
134   if(!theShape->isEdge()) {
135     return aLin;
136   }
137
138   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
139
140   if(!anEdge->isLine()) {
141     return aLin;
142   }
143
144   aLin = anEdge->line();
145
146   return aLin;
147 }
148
149 static std::shared_ptr<GeomAPI_Pln> getPln(const GeomShapePtr theShape)
150 {
151   std::shared_ptr<GeomAPI_Pln> aPln;
152
153   if(!theShape->isFace()) {
154     return aPln;
155   }
156
157   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
158
159   if(!aFace->isPlanar()) {
160     return aPln;
161   }
162
163   aPln = aFace->getPlane();
164
165   return aPln;
166 }