]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Measurement.cpp
Salome HOME
Issue #2551: clear Measurement when selection lost
[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
37 #include <GeomAlgoAPI_ShapeTools.h>
38
39 #include <iomanip>
40 #include <sstream>
41
42 FeaturesPlugin_Measurement::FeaturesPlugin_Measurement()
43 {
44 }
45
46 void FeaturesPlugin_Measurement::initAttributes()
47 {
48   data()->addAttribute(FeaturesPlugin_Measurement::MEASURE_KIND(),
49                        ModelAPI_AttributeString::typeId());
50
51   // attribute for length
52   data()->addAttribute(EDGE_FOR_LENGTH_ID(), ModelAPI_AttributeSelection::typeId());
53   // attributes for distance
54   data()->addAttribute(DISTANCE_FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
55   data()->addAttribute(DISTANCE_TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
56   // attribute for radius
57   data()->addAttribute(CIRCULAR_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
58   // attribute for angle
59   data()->addAttribute(ANGLE_FROM_EDGE_ID(), ModelAPI_AttributeSelection::typeId());
60   data()->addAttribute(ANGLE_TO_EDGE_ID(), ModelAPI_AttributeSelection::typeId());
61   // attribute for result message and values
62   data()->addAttribute(RESULT_ID(), ModelAPI_AttributeString::typeId());
63   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
64 }
65
66 void FeaturesPlugin_Measurement::execute()
67 {
68 }
69
70 void FeaturesPlugin_Measurement::attributeChanged(const std::string& theID)
71 {
72   if (theID == MEASURE_KIND()) {
73     // clear selected objects
74     selection(EDGE_FOR_LENGTH_ID())->reset();
75     selection(DISTANCE_FROM_OBJECT_ID())->reset();
76     selection(DISTANCE_TO_OBJECT_ID())->reset();
77     selection(CIRCULAR_OBJECT_ID())->reset();
78     selection(ANGLE_FROM_EDGE_ID())->reset();
79     selection(ANGLE_TO_EDGE_ID())->reset();
80     string(RESULT_ID())->setValue("");
81     std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
82         attribute(RESULT_VALUES_ID()))->setSize(0);
83   }
84   else if (theID != RESULT_ID()) {
85     std::string aKind = string(MEASURE_KIND())->value();
86     if (aKind == MEASURE_LENGTH())
87       computeLength();
88     else if (aKind == MEASURE_DISTANCE())
89       computeDistance();
90     else if (aKind == MEASURE_RADIUS())
91       computeRadius();
92     else if (aKind == MEASURE_ANGLE())
93       computeAngle();
94   }
95 }
96
97 void FeaturesPlugin_Measurement::computeLength()
98 {
99   AttributeSelectionPtr aSelectedFeature = selection(EDGE_FOR_LENGTH_ID());
100
101   GeomShapePtr aShape;
102   GeomEdgePtr anEdge;
103   if (aSelectedFeature)
104     aShape = aSelectedFeature->value();
105   if (!aShape && aSelectedFeature->context())
106     aShape = aSelectedFeature->context()->shape();
107   if (aShape && aShape->isEdge())
108     anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
109   if (!anEdge) {
110     string(RESULT_ID())->setValue("");
111     return;
112   }
113
114   std::ostringstream anOutput;
115   anOutput << "Length = " << std::setprecision(10) << anEdge->length();
116   string(RESULT_ID())->setValue(anOutput.str());
117
118   AttributeDoubleArrayPtr aValues =
119       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
120   aValues->setSize(1);
121   aValues->setValue(0, anEdge->length());
122 }
123
124 void FeaturesPlugin_Measurement::computeDistance()
125 {
126   AttributeSelectionPtr aFirstFeature = selection(DISTANCE_FROM_OBJECT_ID());
127   GeomShapePtr aShape1;
128   if (aFirstFeature)
129     aShape1 = aFirstFeature->value();
130   if (!aShape1 && aFirstFeature->context())
131     aShape1 = aFirstFeature->context()->shape();
132
133   AttributeSelectionPtr aSecondFeature = selection(DISTANCE_TO_OBJECT_ID());
134   GeomShapePtr aShape2;
135   if (aSecondFeature)
136     aShape2 = aSecondFeature->value();
137   if (!aShape2 && aSecondFeature->context())
138     aShape2 = aSecondFeature->context()->shape();
139
140   if (!aShape1 || !aShape2) {
141     string(RESULT_ID())->setValue("");
142     return;
143   }
144
145   double aDistance = GeomAlgoAPI_ShapeTools::minimalDistance(aShape1, aShape2);
146
147   std::ostringstream anOutput;
148   anOutput << "Distance = " << std::setprecision(10) << aDistance;
149   string(RESULT_ID())->setValue(anOutput.str());
150
151   AttributeDoubleArrayPtr aValues =
152       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
153   aValues->setSize(1);
154   aValues->setValue(0, aDistance);
155 }
156
157 void FeaturesPlugin_Measurement::computeRadius()
158 {
159   AttributeSelectionPtr aSelectedFeature = selection(CIRCULAR_OBJECT_ID());
160
161   GeomShapePtr aShape;
162   if (aSelectedFeature)
163     aShape = aSelectedFeature->value();
164   if (!aShape && aSelectedFeature->context())
165     aShape = aSelectedFeature->context()->shape();
166
167   double aRadius = -1.0;
168   if (aShape) {
169     if (aShape->isEdge()) {
170       GeomEdgePtr anEdge(new GeomAPI_Edge(aShape));
171       if (anEdge->isCircle() || anEdge->isArc()) {
172         aRadius = anEdge->circle()->radius();
173       }
174     } else if (aShape->isFace()) {
175       GeomFacePtr aFace(new GeomAPI_Face(aShape));
176       aRadius = GeomAlgoAPI_ShapeTools::radius(aFace);
177     }
178   }
179
180   if (aRadius < 0.0) {
181     string(RESULT_ID())->setValue("");
182     return;
183   }
184
185   std::ostringstream anOutput;
186   anOutput << "Radius = " << std::setprecision(10) << aRadius;
187   string(RESULT_ID())->setValue(anOutput.str());
188
189   AttributeDoubleArrayPtr aValues =
190       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
191   aValues->setSize(1);
192   aValues->setValue(0, aRadius);
193 }
194
195 void FeaturesPlugin_Measurement::computeAngle()
196 {
197   AttributeSelectionPtr aFirstFeature = selection(ANGLE_FROM_EDGE_ID());
198   GeomShapePtr aShape1;
199   GeomEdgePtr anEdge1;
200   if (aFirstFeature)
201     aShape1 = aFirstFeature->value();
202   if (!aShape1 && aFirstFeature->context())
203     aShape1 = aFirstFeature->context()->shape();
204   if (aShape1 && aShape1->isEdge())
205     anEdge1 = GeomEdgePtr(new GeomAPI_Edge(aShape1));
206
207   AttributeSelectionPtr aSecondFeature = selection(ANGLE_TO_EDGE_ID());
208   GeomShapePtr aShape2;
209   GeomEdgePtr anEdge2;
210   if (aSecondFeature)
211     aShape2 = aSecondFeature->value();
212   if (!aShape2 && aSecondFeature->context())
213     aShape2 = aSecondFeature->context()->shape();
214   if (aShape2 && aShape2->isEdge())
215     anEdge2 = GeomEdgePtr(new GeomAPI_Edge(aShape2));
216
217   if (!anEdge1 || !anEdge2) {
218     string(RESULT_ID())->setValue("");
219     return;
220   }
221
222   GeomShapePtr anInter = anEdge1->intersect(anEdge2);
223
224   std::ostringstream anOutput;
225   anOutput << std::setprecision(10);
226   std::list<double> aValuesList;
227   if (anInter) {
228     if (anInter->isVertex()) {
229       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
230       std::shared_ptr<GeomAPI_Angle> anAngle(
231           new GeomAPI_Angle(anEdge1, anEdge2, aVertex->point()));
232       double anAngleValue = anAngle->angleDegree();
233       anOutput << "Angle = " << anAngleValue << std::endl;
234       aValuesList.push_back(anAngleValue);
235     }
236     else {
237       GeomAPI_ShapeIterator anIt(anInter);
238       for (int anIndex = 1; anIt.more(); anIt.next(), ++anIndex) {
239         GeomShapePtr aCurrent = anIt.current();
240         if (!aCurrent->isVertex())
241           continue;
242         std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
243         std::shared_ptr<GeomAPI_Angle> anAngle(
244             new GeomAPI_Angle(anEdge1, anEdge2, aVertex->point()));
245         double anAngleValue = anAngle->angleDegree();
246         anOutput << "Angle" << anIndex << " = " << anAngleValue << std::endl;
247         aValuesList.push_back(anAngleValue);
248       }
249     }
250   }
251
252   string(RESULT_ID())->setValue(anOutput.str());
253
254   AttributeDoubleArrayPtr aValues =
255       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
256   aValues->setSize((int)aValuesList.size());
257   int anIndex = 0;
258   for (std::list<double>::iterator anIt = aValuesList.begin(); anIt != aValuesList.end(); ++anIt)
259     aValues->setValue(anIndex++, *anIt);
260 }