Salome HOME
Task 2.8. Measurement functions
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_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 "FeaturesAPI_Measurement.h"
22
23 #include <FeaturesPlugin_Measurement.h>
24 #include <ModelAPI_AttributeDoubleArray.h>
25 #include <ModelHighAPI_Services.h>
26 #include <ModelHighAPI_Tools.h>
27
28 double measureLength(const std::shared_ptr<ModelAPI_Document>& thePart,
29                      const ModelHighAPI_Selection& theEdge)
30 {
31   FeaturePtr aMeasure = thePart->addFeature(FeaturesPlugin_Measurement::ID());
32   fillAttribute(FeaturesPlugin_Measurement::MEASURE_LENGTH(),
33                 aMeasure->string(FeaturesPlugin_Measurement::MEASURE_KIND()));
34   fillAttribute(theEdge, aMeasure->selection(FeaturesPlugin_Measurement::EDGE_FOR_LENGTH_ID()));
35   aMeasure->execute();
36
37   // obtain result
38   AttributeDoubleArrayPtr aResult = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
39       aMeasure->attribute(FeaturesPlugin_Measurement::RESULT_VALUES_ID()));
40   double aValue = aResult->size() ? aResult->value(0) : -1.0;
41
42   // perform removing macro feature Measurement
43   thePart->removeFeature(aMeasure);
44   apply();
45
46   return aValue;
47 }
48
49 double measureDistance(const std::shared_ptr<ModelAPI_Document>& thePart,
50                        const ModelHighAPI_Selection& theFrom,
51                        const ModelHighAPI_Selection& theTo)
52 {
53   FeaturePtr aMeasure = thePart->addFeature(FeaturesPlugin_Measurement::ID());
54   fillAttribute(FeaturesPlugin_Measurement::MEASURE_DISTANCE(),
55                 aMeasure->string(FeaturesPlugin_Measurement::MEASURE_KIND()));
56   fillAttribute(theFrom,
57                 aMeasure->selection(FeaturesPlugin_Measurement::DISTANCE_FROM_OBJECT_ID()));
58   fillAttribute(theTo, aMeasure->selection(FeaturesPlugin_Measurement::DISTANCE_TO_OBJECT_ID()));
59   aMeasure->execute();
60
61   // obtain result
62   AttributeDoubleArrayPtr aResult = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
63       aMeasure->attribute(FeaturesPlugin_Measurement::RESULT_VALUES_ID()));
64   double aValue = aResult->size() ? aResult->value(0) : -1.0;
65
66   // perform removing macro feature Measurement
67   thePart->removeFeature(aMeasure);
68   apply();
69
70   return aValue;
71 }
72
73 double measureRadius(const std::shared_ptr<ModelAPI_Document>& thePart,
74                      const ModelHighAPI_Selection& theObject)
75 {
76   FeaturePtr aMeasure = thePart->addFeature(FeaturesPlugin_Measurement::ID());
77   fillAttribute(FeaturesPlugin_Measurement::MEASURE_RADIUS(),
78                 aMeasure->string(FeaturesPlugin_Measurement::MEASURE_KIND()));
79   fillAttribute(theObject, aMeasure->selection(FeaturesPlugin_Measurement::CIRCULAR_OBJECT_ID()));
80   aMeasure->execute();
81
82   // obtain result
83   AttributeDoubleArrayPtr aResult = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
84       aMeasure->attribute(FeaturesPlugin_Measurement::RESULT_VALUES_ID()));
85   double aValue = aResult->size() ? aResult->value(0) : -1.0;
86
87   // perform removing macro feature Measurement
88   thePart->removeFeature(aMeasure);
89   apply();
90
91   return aValue;
92 }
93
94 std::list<double> measureAngle(const std::shared_ptr<ModelAPI_Document>& thePart,
95                                const ModelHighAPI_Selection& theFrom,
96                                const ModelHighAPI_Selection& theTo)
97 {
98   FeaturePtr aMeasure = thePart->addFeature(FeaturesPlugin_Measurement::ID());
99   fillAttribute(FeaturesPlugin_Measurement::MEASURE_ANGLE(),
100                 aMeasure->string(FeaturesPlugin_Measurement::MEASURE_KIND()));
101   fillAttribute(theFrom, aMeasure->selection(FeaturesPlugin_Measurement::ANGLE_FROM_EDGE_ID()));
102   fillAttribute(theTo, aMeasure->selection(FeaturesPlugin_Measurement::ANGLE_TO_EDGE_ID()));
103   aMeasure->execute();
104
105   // obtain result
106   AttributeDoubleArrayPtr aResult = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
107       aMeasure->attribute(FeaturesPlugin_Measurement::RESULT_VALUES_ID()));
108   std::list<double> aValues;
109   for (int i = 0, n = aResult->size(); i < n; ++i)
110     aValues.push_back(aResult->value(i));
111
112   // perform removing macro feature Measurement
113   thePart->removeFeature(aMeasure);
114   apply();
115
116   return aValues;
117 }
118
119 double measureAngle(const std::shared_ptr<ModelAPI_Document>& thePart,
120                     const ModelHighAPI_Selection& thePoint1,
121                     const ModelHighAPI_Selection& thePoint2,
122                     const ModelHighAPI_Selection& thePoint3)
123 {
124   FeaturePtr aMeasure = thePart->addFeature(FeaturesPlugin_Measurement::ID());
125   fillAttribute(FeaturesPlugin_Measurement::MEASURE_ANGLE_POINTS(),
126                 aMeasure->string(FeaturesPlugin_Measurement::MEASURE_KIND()));
127   fillAttribute(thePoint1, aMeasure->selection(FeaturesPlugin_Measurement::ANGLE_POINT1_ID()));
128   fillAttribute(thePoint2, aMeasure->selection(FeaturesPlugin_Measurement::ANGLE_POINT2_ID()));
129   fillAttribute(thePoint3, aMeasure->selection(FeaturesPlugin_Measurement::ANGLE_POINT3_ID()));
130   aMeasure->execute();
131
132   // obtain result
133   AttributeDoubleArrayPtr aResult = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(
134       aMeasure->attribute(FeaturesPlugin_Measurement::RESULT_VALUES_ID()));
135   double aValue = aResult->size() ? aResult->value(0) : -1.0;
136
137   // perform removing macro feature Measurement
138   thePart->removeFeature(aMeasure);
139   apply();
140
141   return aValue;
142 }