1 // Copyright (C) 2018-2022 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 email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_GeometryCalculation.h"
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeDoubleArray.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Session.h>
29 #include <Config_PropManager.h>
31 #include <GeomAPI_Shape.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
38 //=================================================================================================
39 FeaturesPlugin_GeometryCalculation::FeaturesPlugin_GeometryCalculation()
43 //=================================================================================================
44 void FeaturesPlugin_GeometryCalculation::initAttributes()
46 // attribute for point selected
47 data()->addAttribute(OBJECT_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
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());
54 data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
55 data()->realArray(RESULT_VALUES_ID())->setSize(3);
59 //=================================================================================================
60 void FeaturesPlugin_GeometryCalculation::execute()
64 //=================================================================================================
65 void FeaturesPlugin_GeometryCalculation::attributeChanged(const std::string& theID)
67 if (theID == OBJECT_SELECTED_ID()) {
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;
77 if (aSelection && aSelection->isInitialized()) {
78 aShape = aSelection->value();
79 if (!aShape && aSelection->context())
80 aShape = aSelection->context()->shape();
88 aLength = GeomAlgoAPI_ShapeTools::length(aShape);
89 aSurfArea = GeomAlgoAPI_ShapeTools::area(aShape);
90 aVolume = GeomAlgoAPI_ShapeTools::volume(aShape);
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);
100 string(LENGTH_ID())->setValue(streamL.str());
101 string(AREA_ID())->setValue(streamA.str());
102 string(VOLUME_ID())->setValue(streamV.str());