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