Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_GeometryCalculation.cpp
1 // Copyright (C) 2018-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_GeometryCalculation.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeDoubleArray.h>
24
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Session.h>
28
29 #include <Config_PropManager.h>
30
31 #include <GeomAPI_Shape.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33
34
35 #include <iomanip>
36 #include <sstream>
37
38 //=================================================================================================
39 FeaturesPlugin_GeometryCalculation::FeaturesPlugin_GeometryCalculation()
40 {
41 }
42
43 //=================================================================================================
44 void FeaturesPlugin_GeometryCalculation::initAttributes()
45 {
46   // attribute for point selected
47   data()->addAttribute(OBJECT_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
48
49   // attributes for result message and values
50   data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeString::typeId());
51   data()->addAttribute(AREA_ID(), ModelAPI_AttributeString::typeId());
52   data()->addAttribute(VOLUME_ID(), ModelAPI_AttributeString::typeId());
53
54   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
55   data()->realArray(RESULT_VALUES_ID())->setSize(3);
56
57 }
58
59 //=================================================================================================
60 void FeaturesPlugin_GeometryCalculation::execute()
61 {
62 }
63
64 //=================================================================================================
65 void FeaturesPlugin_GeometryCalculation::attributeChanged(const std::string& theID)
66 {
67   if (theID == OBJECT_SELECTED_ID()) {
68
69     AttributeSelectionPtr aSelection = selection(OBJECT_SELECTED_ID());
70     AttributeDoubleArrayPtr aValues =
71       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
72     std::stringstream streamL;
73     std::stringstream streamA;
74     std::stringstream streamV;
75     GeomShapePtr aShape;
76
77     if (aSelection && aSelection->isInitialized()) {
78       aShape = aSelection->value();
79       if (!aShape && aSelection->context())
80         aShape = aSelection->context()->shape();
81     }
82
83     if (aShape) {
84       double aLength;
85       double aSurfArea;
86       double aVolume;
87
88       aLength = GeomAlgoAPI_ShapeTools::length(aShape);
89       aSurfArea = GeomAlgoAPI_ShapeTools::area(aShape);
90       aVolume = GeomAlgoAPI_ShapeTools::volume(aShape);
91
92       streamL << std::setprecision(14) << aLength;
93       aValues->setValue(0, aLength);
94       streamA << std::setprecision(14) << aSurfArea;
95       aValues->setValue(1, aSurfArea);
96       streamV << std::setprecision(14) << aVolume;
97       aValues->setValue(2, aVolume);
98     }
99
100     string(LENGTH_ID())->setValue(streamL.str());
101     string(AREA_ID())->setValue(streamA.str());
102     string(VOLUME_ID())->setValue(streamV.str());
103   }
104 }