1 // Copyright (C) 2018-20xx CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Measurement.h"
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>
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>
38 #include <GeomAlgoAPI_ShapeTools.h>
43 FeaturesPlugin_Measurement::FeaturesPlugin_Measurement()
47 void FeaturesPlugin_Measurement::initAttributes()
49 data()->addAttribute(FeaturesPlugin_Measurement::MEASURE_KIND(),
50 ModelAPI_AttributeString::typeId());
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());
71 void FeaturesPlugin_Measurement::execute()
75 void FeaturesPlugin_Measurement::attributeChanged(const std::string& theID)
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);
92 else if (theID != RESULT_ID()) {
93 std::string aKind = string(MEASURE_KIND())->value();
94 if (aKind == MEASURE_LENGTH())
96 else if (aKind == MEASURE_DISTANCE())
98 else if (aKind == MEASURE_RADIUS())
100 else if (aKind == MEASURE_ANGLE())
102 else if (aKind == MEASURE_ANGLE_POINTS())
103 computeAngleByPoints();
107 void FeaturesPlugin_Measurement::computeLength()
109 AttributeSelectionPtr aSelectedFeature = selection(EDGE_FOR_LENGTH_ID());
113 if (aSelectedFeature && aSelectedFeature->isInitialized()) {
114 aShape = aSelectedFeature->value();
115 if (!aShape && aSelectedFeature->context())
116 aShape = aSelectedFeature->context()->shape();
118 if (aShape && aShape->isEdge())
119 anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
121 string(RESULT_ID())->setValue("");
125 std::ostringstream anOutput;
126 anOutput << "Length = " << std::setprecision(10) << anEdge->length();
127 string(RESULT_ID())->setValue(anOutput.str());
129 AttributeDoubleArrayPtr aValues =
130 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
132 aValues->setValue(0, anEdge->length());
135 void FeaturesPlugin_Measurement::computeDistance()
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();
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();
153 if (!aShape1 || !aShape2) {
154 string(RESULT_ID())->setValue("");
158 double aDistance = GeomAlgoAPI_ShapeTools::minimalDistance(aShape1, aShape2);
160 std::ostringstream anOutput;
161 anOutput << "Distance = " << std::setprecision(10) << aDistance;
162 string(RESULT_ID())->setValue(anOutput.str());
164 AttributeDoubleArrayPtr aValues =
165 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
167 aValues->setValue(0, aDistance);
170 void FeaturesPlugin_Measurement::computeRadius()
172 AttributeSelectionPtr aSelectedFeature = selection(CIRCULAR_OBJECT_ID());
175 if (aSelectedFeature && aSelectedFeature->isInitialized()) {
176 aShape = aSelectedFeature->value();
177 if (!aShape && aSelectedFeature->context())
178 aShape = aSelectedFeature->context()->shape();
181 double aRadius = -1.0;
183 if (aShape->isEdge()) {
184 GeomEdgePtr anEdge(new GeomAPI_Edge(aShape));
185 if (anEdge->isCircle() || anEdge->isArc()) {
186 aRadius = anEdge->circle()->radius();
188 } else if (aShape->isFace()) {
189 GeomFacePtr aFace(new GeomAPI_Face(aShape));
190 aRadius = GeomAlgoAPI_ShapeTools::radius(aFace);
195 string(RESULT_ID())->setValue("");
199 std::ostringstream anOutput;
200 anOutput << "Radius = " << std::setprecision(10) << aRadius;
201 string(RESULT_ID())->setValue(anOutput.str());
203 AttributeDoubleArrayPtr aValues =
204 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
206 aValues->setValue(0, aRadius);
209 void FeaturesPlugin_Measurement::computeAngle()
211 AttributeSelectionPtr aFirstFeature = selection(ANGLE_FROM_EDGE_ID());
212 GeomShapePtr aShape1;
214 if (aFirstFeature && aFirstFeature->isInitialized()) {
215 aShape1 = aFirstFeature->value();
216 if (!aShape1 && aFirstFeature->context())
217 aShape1 = aFirstFeature->context()->shape();
219 if (aShape1 && aShape1->isEdge())
220 anEdge1 = GeomEdgePtr(new GeomAPI_Edge(aShape1));
222 AttributeSelectionPtr aSecondFeature = selection(ANGLE_TO_EDGE_ID());
223 GeomShapePtr aShape2;
225 if (aSecondFeature && aSecondFeature->isInitialized()) {
226 aShape2 = aSecondFeature->value();
227 if (!aShape2 && aSecondFeature->context())
228 aShape2 = aSecondFeature->context()->shape();
230 if (aShape2 && aShape2->isEdge())
231 anEdge2 = GeomEdgePtr(new GeomAPI_Edge(aShape2));
233 if (!anEdge1 || !anEdge2) {
234 string(RESULT_ID())->setValue("");
238 GeomShapePtr anInter = anEdge1->intersect(anEdge2);
240 std::ostringstream anOutput;
241 anOutput << std::setprecision(10);
242 std::list<double> aValuesList;
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);
253 GeomAPI_ShapeIterator anIt(anInter);
254 for (int anIndex = 1; anIt.more(); anIt.next(), ++anIndex) {
255 GeomShapePtr aCurrent = anIt.current();
256 if (!aCurrent->isVertex())
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);
269 string(RESULT_ID())->setValue(anOutput.str());
271 AttributeDoubleArrayPtr aValues =
272 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
273 aValues->setSize((int)aValuesList.size());
275 for (std::list<double>::iterator anIt = aValuesList.begin(); anIt != aValuesList.end(); ++anIt)
276 aValues->setValue(anIndex++, *anIt);
279 static GeomVertexPtr selectionToVertex(AttributeSelectionPtr& aSelection)
282 GeomVertexPtr aVertex;
283 if (aSelection && aSelection->isInitialized()) {
284 aShape = aSelection->value();
285 if (!aShape && aSelection->context())
286 aShape = aSelection->context()->shape();
288 if (aShape && aShape->isVertex())
289 aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
293 void FeaturesPlugin_Measurement::computeAngleByPoints()
295 GeomVertexPtr aVertex1 = selectionToVertex(selection(ANGLE_POINT1_ID()));
296 GeomVertexPtr aVertex2 = selectionToVertex(selection(ANGLE_POINT2_ID()));
297 GeomVertexPtr aVertex3 = selectionToVertex(selection(ANGLE_POINT3_ID()));
299 if (!aVertex1 || !aVertex2 || ! aVertex3) {
300 string(RESULT_ID())->setValue("");
304 std::shared_ptr<GeomAPI_Angle> anAngle(
305 new GeomAPI_Angle(aVertex1->point(), aVertex2->point(), aVertex3->point()));
306 double anAngleValue = anAngle->angleDegree();
308 std::ostringstream anOutput;
309 anOutput << "Angle = " << std::setprecision(10) << anAngleValue;
310 string(RESULT_ID())->setValue(anOutput.str());
312 AttributeDoubleArrayPtr aValues =
313 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
315 aValues->setValue(0, anAngleValue);