Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BoundingBox.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 "FeaturesPlugin_BoundingBox.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeDoubleArray.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_AttributeBoolean.h>
26
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <GeomAlgoAPI_BoundingBox.h>
32
33 #include <Config_PropManager.h>
34 #include <ModelAPI_ResultBody.h>
35
36 #include <iomanip>
37 #include <sstream>
38
39 //=================================================================================================
40 FeaturesPlugin_BoundingBox::FeaturesPlugin_BoundingBox()
41 {
42 }
43
44 //=================================================================================================
45 void FeaturesPlugin_BoundingBox::initAttributes()
46 {
47   // attribute for object selected
48   data()->addAttribute(OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49
50   // attributes for result message and values
51   data()->addAttribute(X_MIN_COORD_ID(), ModelAPI_AttributeString::typeId());
52   data()->addAttribute(Y_MIN_COORD_ID(), ModelAPI_AttributeString::typeId());
53   data()->addAttribute(Z_MIN_COORD_ID(), ModelAPI_AttributeString::typeId());
54   data()->addAttribute(X_MAX_COORD_ID(), ModelAPI_AttributeString::typeId());
55   data()->addAttribute(Y_MAX_COORD_ID(), ModelAPI_AttributeString::typeId());
56   data()->addAttribute(Z_MAX_COORD_ID(), ModelAPI_AttributeString::typeId());
57   data()->addAttribute(COMPUTE_ID(), ModelAPI_AttributeBoolean::typeId());
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), X_MIN_COORD_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), Y_MIN_COORD_ID());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), Z_MIN_COORD_ID());
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), X_MAX_COORD_ID());
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), Y_MAX_COORD_ID());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), Z_MAX_COORD_ID());
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMPUTE_ID());
66   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_ID());
67   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
68
69   data()->realArray(RESULT_VALUES_ID())->setSize(6);
70   data()->boolean(COMPUTE_ID())->setValue(true);
71 }
72
73 //=================================================================================================
74 void FeaturesPlugin_BoundingBox::execute()
75 {
76   if (!updateValues())
77     return;
78
79   createBoxByTwoPoints();
80 }
81
82 //=================================================================================================
83 void FeaturesPlugin_BoundingBox::attributeChanged(const std::string&)
84 {
85 }
86
87 //=================================================================================================
88 bool FeaturesPlugin_BoundingBox::updateValues()
89 {
90   AttributeSelectionPtr aSelection = selection(OBJECT_ID());
91   AttributeDoubleArrayPtr aValues =
92       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
93
94   if (aSelection->isInitialized()) {
95     std::stringstream streamxmin;
96     std::stringstream streamymin;
97     std::stringstream streamzmin;
98     std::stringstream streamxmax;
99     std::stringstream streamymax;
100     std::stringstream streamzmax;
101
102     GeomShapePtr aShape;
103     if (aSelection && aSelection->isInitialized()) {
104       aShape = aSelection->value();
105       if (!aShape && aSelection->context())
106         aShape = aSelection->context()->shape();
107     }
108
109     AttributeBooleanPtr anIsCompute = boolean(COMPUTE_ID());
110     if (!anIsCompute->value()) {
111       myShape = aShape;
112       anIsCompute->setValue(true);
113     }
114
115     if (aShape && !aShape->isEqual(myShape)) {
116       double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
117       std::string anError;
118       if (!GetBoundingBox(aShape,
119                           aXmin, aXmax,
120                           aYmin, aYmax,
121                           aZmin, aZmax,
122                           anError)) {
123         setError("Error in bounding box calculation :" +  anError);
124         return false;
125       }
126       myShape = aShape;
127       streamxmin << std::setprecision(14) << aXmin;
128       aValues->setValue(0, aXmin);
129       streamxmax << std::setprecision(14) << aXmax;
130       aValues->setValue(1, aXmax);
131       streamymin << std::setprecision(14) << aYmin;
132       aValues->setValue(2, aYmin);
133       streamymax << std::setprecision(14) << aYmax;
134       aValues->setValue(3, aYmax);
135       streamzmin << std::setprecision(14) << aZmin;
136       aValues->setValue(4, aZmin);
137       streamzmax << std::setprecision(14) << aZmax;
138       aValues->setValue(5, aZmax);
139     } else {
140       streamxmin << std::setprecision(14) << aValues->value(0);
141       streamxmax << std::setprecision(14) << aValues->value(1);
142       streamymin << std::setprecision(14) << aValues->value(2);
143       streamymax << std::setprecision(14) << aValues->value(3);
144       streamzmin << std::setprecision(14) << aValues->value(4);
145       streamzmax << std::setprecision(14) << aValues->value(5);
146     }
147
148     string(X_MIN_COORD_ID() )->setValue( "X = " +  streamxmin.str() );
149     string(Y_MIN_COORD_ID() )->setValue( "Y = " +  streamymin.str() );
150     string(Z_MIN_COORD_ID() )->setValue( "Z = " +  streamzmin.str() );
151     string(X_MAX_COORD_ID() )->setValue( "X = " +  streamxmax.str() );
152     string(Y_MAX_COORD_ID() )->setValue( "Y = " +  streamymax.str() );
153     string(Z_MAX_COORD_ID() )->setValue( "Z = " +  streamzmax.str() );
154   }
155   return true;
156 }
157
158 //=================================================================================================
159 AttributePtr FeaturesPlugin_BoundingBox::attributResultValues()
160 {
161    return attribute(RESULT_VALUES_ID());
162 }