Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_PointCoordinates.cpp
1 // Copyright (C) 2018-2023  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_PointCoordinates.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_Pnt.h>
32 #include <GeomAPI_Shape.h>
33 #include <GeomAPI_Vertex.h>
34
35 #include <GeomAlgoAPI_PointBuilder.h>
36
37 #include <iomanip>
38 #include <sstream>
39
40 FeaturesPlugin_PointCoordinates::FeaturesPlugin_PointCoordinates()
41 {
42 }
43
44 void FeaturesPlugin_PointCoordinates::initAttributes()
45 {
46   // attribute for point selected
47   data()->addAttribute(POINT_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
48
49   // attribute for x, y and z coordinates
50   data()->addAttribute(X_COORD_ID(), ModelAPI_AttributeString::typeId());
51   data()->addAttribute(Y_COORD_ID(), ModelAPI_AttributeString::typeId());
52   data()->addAttribute(Z_COORD_ID(), ModelAPI_AttributeString::typeId());
53
54   // attributes for result message and values
55   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
56   data()->realArray(RESULT_VALUES_ID())->setSize(3);
57 }
58
59 void FeaturesPlugin_PointCoordinates::execute()
60 {
61 }
62
63 void FeaturesPlugin_PointCoordinates::attributeChanged(const std::string& theID)
64 {
65   if (theID == POINT_SELECTED_ID()) {
66     AttributeSelectionPtr aSelection = selection(POINT_SELECTED_ID());
67     GeomShapePtr aShape;
68     GeomPointPtr aPoint;
69     if (aSelection && aSelection->isInitialized()) {
70       aShape = aSelection->value();
71       if (!aShape && aSelection->context())
72         aShape = aSelection->context()->shape();
73     }
74
75     AttributeDoubleArrayPtr aValues =
76       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
77     std::stringstream streamx;
78     std::stringstream streamy;
79     std::stringstream streamz;
80     if (aShape) {
81       aPoint = GeomAlgoAPI_PointBuilder::point(aShape);
82       streamx << std::setprecision(14) << aPoint->x();
83       aValues->setValue(0, aPoint->x());
84       streamy << std::setprecision(14) << aPoint->y();
85       aValues->setValue(1, aPoint->y());
86       streamz << std::setprecision(14) << aPoint->z();
87       aValues->setValue(2, aPoint->z());
88     }
89
90     string(X_COORD_ID() )->setValue( "X = " +  streamx.str() );
91     string(Y_COORD_ID() )->setValue( "Y = " +  streamy.str() );
92     string(Z_COORD_ID() )->setValue( "Z = " +  streamz.str() );
93   }
94
95 }