]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Measurement.cpp
Salome HOME
Avoid compilation failure
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Measurement.cpp
1 // Copyright (C) 2018-20xx  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 "FeaturesPlugin_Measurement.h"
22
23 #include <ModelAPI_AttributeDoubleArray.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Session.h>
28
29 #include <GeomAPI_Angle.h>
30 #include <GeomAPI_Circ.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Shape.h>
35 #include <GeomAPI_ShapeIterator.h>
36 #include <GeomAPI_Vertex.h>
37
38 #include <GeomAlgoAPI_ShapeTools.h>
39
40 #include <iomanip>
41 #include <sstream>
42
43 FeaturesPlugin_Measurement::FeaturesPlugin_Measurement()
44 {
45 }
46
47 void FeaturesPlugin_Measurement::initAttributes()
48 {
49   data()->addAttribute(FeaturesPlugin_Measurement::MEASURE_KIND(),
50                        ModelAPI_AttributeString::typeId());
51
52   // attribute for length
53   data()->addAttribute(EDGE_FOR_LENGTH_ID(), ModelAPI_AttributeSelection::typeId());
54   // attributes for distance
55   data()->addAttribute(DISTANCE_FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
56   data()->addAttribute(DISTANCE_TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57   // attribute for radius
58   data()->addAttribute(CIRCULAR_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
59   // attributes for angle
60   data()->addAttribute(ANGLE_FROM_EDGE_ID(), ModelAPI_AttributeSelection::typeId());
61   data()->addAttribute(ANGLE_TO_EDGE_ID(), ModelAPI_AttributeSelection::typeId());
62   // attributes for angle by 3 points
63   data()->addAttribute(ANGLE_POINT1_ID(), ModelAPI_AttributeSelection::typeId());
64   data()->addAttribute(ANGLE_POINT2_ID(), ModelAPI_AttributeSelection::typeId());
65   data()->addAttribute(ANGLE_POINT3_ID(), ModelAPI_AttributeSelection::typeId());
66   // attributes for result message and values
67   data()->addAttribute(RESULT_ID(), ModelAPI_AttributeString::typeId());
68   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
69 }
70
71 void FeaturesPlugin_Measurement::execute()
72 {
73 }
74
75 void FeaturesPlugin_Measurement::attributeChanged(const std::string& theID)
76 {
77   if (theID == MEASURE_KIND()) {
78     // clear selected objects
79     selection(EDGE_FOR_LENGTH_ID())->reset();
80     selection(DISTANCE_FROM_OBJECT_ID())->reset();
81     selection(DISTANCE_TO_OBJECT_ID())->reset();
82     selection(CIRCULAR_OBJECT_ID())->reset();
83     selection(ANGLE_FROM_EDGE_ID())->reset();
84     selection(ANGLE_TO_EDGE_ID())->reset();
85     selection(ANGLE_POINT1_ID())->reset();
86     selection(ANGLE_POINT2_ID())->reset();
87     selection(ANGLE_POINT3_ID())->reset();
88     string(RESULT_ID())->setValue("");
89     std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
90         attribute(RESULT_VALUES_ID()))->setSize(0);
91   }
92   else if (theID != RESULT_ID()) {
93     std::string aKind = string(MEASURE_KIND())->value();
94     if (aKind == MEASURE_LENGTH())
95       computeLength();
96     else if (aKind == MEASURE_DISTANCE())
97       computeDistance();
98     else if (aKind == MEASURE_RADIUS())
99       computeRadius();
100     else if (aKind == MEASURE_ANGLE())
101       computeAngle();
102     else if (aKind == MEASURE_ANGLE_POINTS())
103       computeAngleByPoints();
104   }
105 }
106
107 void FeaturesPlugin_Measurement::computeLength()
108 {
109   AttributeSelectionPtr aSelectedFeature = selection(EDGE_FOR_LENGTH_ID());
110
111   GeomShapePtr aShape;
112   GeomEdgePtr anEdge;
113   if (aSelectedFeature && aSelectedFeature->isInitialized()) {
114     aShape = aSelectedFeature->value();
115     if (!aShape && aSelectedFeature->context())
116       aShape = aSelectedFeature->context()->shape();
117   }
118   if (aShape && aShape->isEdge())
119     anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
120   if (!anEdge) {
121     string(RESULT_ID())->setValue("");
122     return;
123   }
124
125   std::ostringstream anOutput;
126   anOutput << "Length = " << std::setprecision(10) << anEdge->length();
127   string(RESULT_ID())->setValue(anOutput.str());
128
129   AttributeDoubleArrayPtr aValues =
130       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
131   aValues->setSize(1);
132   aValues->setValue(0, anEdge->length());
133 }
134
135 void FeaturesPlugin_Measurement::computeDistance()
136 {
137   AttributeSelectionPtr aFirstFeature = selection(DISTANCE_FROM_OBJECT_ID());
138   GeomShapePtr aShape1;
139   if (aFirstFeature && aFirstFeature->isInitialized()) {
140     aShape1 = aFirstFeature->value();
141     if (!aShape1 && aFirstFeature->context())
142       aShape1 = aFirstFeature->context()->shape();
143   }
144
145   AttributeSelectionPtr aSecondFeature = selection(DISTANCE_TO_OBJECT_ID());
146   GeomShapePtr aShape2;
147   if (aSecondFeature && aSecondFeature->isInitialized()) {
148     aShape2 = aSecondFeature->value();
149     if (!aShape2 && aSecondFeature->context())
150       aShape2 = aSecondFeature->context()->shape();
151   }
152
153   if (!aShape1 || !aShape2) {
154     string(RESULT_ID())->setValue("");
155     return;
156   }
157
158   double aDistance = GeomAlgoAPI_ShapeTools::minimalDistance(aShape1, aShape2);
159
160   std::ostringstream anOutput;
161   anOutput << "Distance = " << std::setprecision(10) << aDistance;
162   string(RESULT_ID())->setValue(anOutput.str());
163
164   AttributeDoubleArrayPtr aValues =
165       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
166   aValues->setSize(1);
167   aValues->setValue(0, aDistance);
168 }
169
170 void FeaturesPlugin_Measurement::computeRadius()
171 {
172   AttributeSelectionPtr aSelectedFeature = selection(CIRCULAR_OBJECT_ID());
173
174   GeomShapePtr aShape;
175   if (aSelectedFeature && aSelectedFeature->isInitialized()) {
176     aShape = aSelectedFeature->value();
177     if (!aShape && aSelectedFeature->context())
178       aShape = aSelectedFeature->context()->shape();
179   }
180
181   double aRadius = -1.0;
182   if (aShape) {
183     if (aShape->isEdge()) {
184       GeomEdgePtr anEdge(new GeomAPI_Edge(aShape));
185       if (anEdge->isCircle() || anEdge->isArc()) {
186         aRadius = anEdge->circle()->radius();
187       }
188     } else if (aShape->isFace()) {
189       GeomFacePtr aFace(new GeomAPI_Face(aShape));
190       aRadius = GeomAlgoAPI_ShapeTools::radius(aFace);
191     }
192   }
193
194   if (aRadius < 0.0) {
195     string(RESULT_ID())->setValue("");
196     return;
197   }
198
199   std::ostringstream anOutput;
200   anOutput << "Radius = " << std::setprecision(10) << aRadius;
201   string(RESULT_ID())->setValue(anOutput.str());
202
203   AttributeDoubleArrayPtr aValues =
204       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
205   aValues->setSize(1);
206   aValues->setValue(0, aRadius);
207 }
208
209 void FeaturesPlugin_Measurement::computeAngle()
210 {
211   AttributeSelectionPtr aFirstFeature = selection(ANGLE_FROM_EDGE_ID());
212   GeomShapePtr aShape1;
213   GeomEdgePtr anEdge1;
214   if (aFirstFeature && aFirstFeature->isInitialized()) {
215     aShape1 = aFirstFeature->value();
216     if (!aShape1 && aFirstFeature->context())
217       aShape1 = aFirstFeature->context()->shape();
218   }
219   if (aShape1 && aShape1->isEdge())
220     anEdge1 = GeomEdgePtr(new GeomAPI_Edge(aShape1));
221
222   AttributeSelectionPtr aSecondFeature = selection(ANGLE_TO_EDGE_ID());
223   GeomShapePtr aShape2;
224   GeomEdgePtr anEdge2;
225   if (aSecondFeature && aSecondFeature->isInitialized()) {
226     aShape2 = aSecondFeature->value();
227     if (!aShape2 && aSecondFeature->context())
228       aShape2 = aSecondFeature->context()->shape();
229   }
230   if (aShape2 && aShape2->isEdge())
231     anEdge2 = GeomEdgePtr(new GeomAPI_Edge(aShape2));
232
233   if (!anEdge1 || !anEdge2) {
234     string(RESULT_ID())->setValue("");
235     return;
236   }
237
238   GeomShapePtr anInter = anEdge1->intersect(anEdge2);
239
240   std::ostringstream anOutput;
241   anOutput << std::setprecision(10);
242   std::list<double> aValuesList;
243   if (anInter) {
244     if (anInter->isVertex()) {
245       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
246       std::shared_ptr<GeomAPI_Angle> anAngle(
247           new GeomAPI_Angle(anEdge1, anEdge2, aVertex->point()));
248       double anAngleValue = anAngle->angleDegree();
249       anOutput << "Angle = " << std::setprecision(10) << anAngleValue << std::endl;
250       aValuesList.push_back(anAngleValue);
251     }
252     else {
253       GeomAPI_ShapeIterator anIt(anInter);
254       for (int anIndex = 1; anIt.more(); anIt.next(), ++anIndex) {
255         GeomShapePtr aCurrent = anIt.current();
256         if (!aCurrent->isVertex())
257           continue;
258         std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
259         std::shared_ptr<GeomAPI_Angle> anAngle(
260             new GeomAPI_Angle(anEdge1, anEdge2, aVertex->point()));
261         double anAngleValue = anAngle->angleDegree();
262         anOutput << "Angle" << anIndex << " = "
263                  << std::setprecision(10) << anAngleValue << std::endl;
264         aValuesList.push_back(anAngleValue);
265       }
266     }
267   }
268
269   string(RESULT_ID())->setValue(anOutput.str());
270
271   AttributeDoubleArrayPtr aValues =
272       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
273   aValues->setSize((int)aValuesList.size());
274   int anIndex = 0;
275   for (std::list<double>::iterator anIt = aValuesList.begin(); anIt != aValuesList.end(); ++anIt)
276     aValues->setValue(anIndex++, *anIt);
277 }
278
279 static GeomVertexPtr selectionToVertex(const AttributeSelectionPtr& aSelection)
280 {
281   GeomShapePtr aShape;
282   GeomVertexPtr aVertex;
283   if (aSelection && aSelection->isInitialized()) {
284     aShape = aSelection->value();
285     if (!aShape && aSelection->context())
286       aShape = aSelection->context()->shape();
287   }
288   if (aShape && aShape->isVertex())
289     aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
290   return aVertex;
291 }
292
293 void FeaturesPlugin_Measurement::computeAngleByPoints()
294 {
295   GeomVertexPtr aVertex1 = selectionToVertex(selection(ANGLE_POINT1_ID()));
296   GeomVertexPtr aVertex2 = selectionToVertex(selection(ANGLE_POINT2_ID()));
297   GeomVertexPtr aVertex3 = selectionToVertex(selection(ANGLE_POINT3_ID()));
298
299   if (!aVertex1 || !aVertex2 || ! aVertex3) {
300     string(RESULT_ID())->setValue("");
301     return;
302   }
303
304   std::shared_ptr<GeomAPI_Angle> anAngle(
305       new GeomAPI_Angle(aVertex1->point(), aVertex2->point(), aVertex3->point()));
306   double anAngleValue = anAngle->angleDegree();
307
308   std::ostringstream anOutput;
309   anOutput << "Angle = " << std::setprecision(10) << anAngleValue;
310   string(RESULT_ID())->setValue(anOutput.str());
311
312   AttributeDoubleArrayPtr aValues =
313     std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
314   aValues->setSize(1);
315   aValues->setValue(0, anAngleValue);
316 }