]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_BoundingBox.cpp
Salome HOME
CEA : Lot2 - Bounding box
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BoundingBox.cpp
1 // Copyright (C) 2018-2020  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_BoundingBox.h"
21
22 #include <Config_PropManager.h>
23
24 #include <FeaturesPlugin_CreateBoundingBox.h>
25
26 #include <GeomAlgoAPI_BoundingBox.h>
27
28 #include <ModelAPI_AttributeSelection.h>
29 #include <ModelAPI_AttributeDoubleArray.h>
30 #include <ModelAPI_AttributeBoolean.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.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(CREATEBOX_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(), CREATEBOX_ID());
66   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), RESULT_VALUES_ID());
67
68   data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
69
70   data()->realArray(RESULT_VALUES_ID())->setSize(6);
71
72 }
73
74 //=================================================================================================
75 void FeaturesPlugin_BoundingBox::execute()
76 {
77   updateValues();
78   createBoxByTwoPoints();
79
80   if (boolean(CREATEBOX_ID())->value()) {
81     if (!myCreateFeature.get())
82       createBox();
83     updateBox();
84   } else {
85     if (myCreateFeature.get()) {
86       myCreateFeature->eraseResults();
87       SessionPtr aSession = ModelAPI_Session::get();
88       DocumentPtr aDoc =  aSession->activeDocument();
89       aDoc->removeFeature(myCreateFeature);
90       myCreateFeature.reset();
91     }
92   }
93 }
94
95 //=================================================================================================
96 void FeaturesPlugin_BoundingBox::attributeChanged(const std::string& theID)
97 {
98   if (theID == OBJECT_ID()) {
99     if (myCreateFeature.get())
100       updateBox();
101   }
102 }
103
104 //=================================================================================================
105 AttributePtr FeaturesPlugin_BoundingBox::attributResultValues()
106 {
107    return attribute(RESULT_VALUES_ID());
108 }
109
110 //=================================================================================================
111 void FeaturesPlugin_BoundingBox::updateValues()
112 {
113   AttributeSelectionPtr aSelection = selection(OBJECT_ID());
114   if (aSelection->isInitialized()) {
115     AttributeDoubleArrayPtr aValues =
116       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
117     std::stringstream streamxmin;
118     std::stringstream streamymin;
119     std::stringstream streamzmin;
120     std::stringstream streamxmax;
121     std::stringstream streamymax;
122     std::stringstream streamzmax;
123
124     GeomShapePtr aShape;
125     if (aSelection && aSelection->isInitialized()) {
126       aShape = aSelection->value();
127       if (!aShape && aSelection->context())
128         aShape = aSelection->context()->shape();
129     }
130
131     if (aShape && !aShape->isEqual(myShape)) {
132       double aXmin, aXmax, aYmin,aYmax,aZmin,aZmax;
133       std::string aError;
134       if (!GetBoundingBox(aShape,
135                           true,
136                           aXmin, aXmax,
137                           aYmin,aYmax,
138                           aZmin,aZmax,
139                           aError))
140           setError("Error in bounding box calculation :" +  aError);
141
142       myShape = aShape;
143       streamxmin << std::setprecision(14) << aXmin;
144       aValues->setValue(0, aXmin);
145       streamxmax << std::setprecision(14) << aXmax;
146       aValues->setValue(1, aXmax);
147       streamymin << std::setprecision(14) << aYmin;
148       aValues->setValue(2, aYmin);
149       streamymax << std::setprecision(14) << aYmax;
150       aValues->setValue(3, aYmax);
151       streamzmin << std::setprecision(14) << aZmin;
152       aValues->setValue(4, aZmin);
153       streamzmax << std::setprecision(14) << aZmax;
154       aValues->setValue(5, aZmax);
155       string(X_MIN_COORD_ID() )->setValue( "X = " +  streamxmin.str() );
156       string(Y_MIN_COORD_ID() )->setValue( "Y = " +  streamymin.str() );
157       string(Z_MIN_COORD_ID() )->setValue( "Z = " +  streamzmin.str() );
158       string(X_MAX_COORD_ID() )->setValue( "X = " +  streamxmax.str() );
159       string(Y_MAX_COORD_ID() )->setValue( "Y = " +  streamymax.str() );
160       string(Z_MAX_COORD_ID() )->setValue( "Z = " +  streamzmax.str() );
161     }
162   }
163 }
164
165 //=================================================================================================
166 void FeaturesPlugin_BoundingBox::createBox()
167 {
168   SessionPtr aSession = ModelAPI_Session::get();
169
170   DocumentPtr aDoc =  aSession->activeDocument();
171
172   if (aDoc.get()) {
173     myCreateFeature = aDoc->addFeature(FeaturesPlugin_CreateBoundingBox::ID());
174   }
175 }
176
177 //=================================================================================================
178 void FeaturesPlugin_BoundingBox::updateBox()
179 {
180   myCreateFeature->boolean(FeaturesPlugin_CreateBoundingBox::COMPUTE_ID())->setValue(false);
181   myCreateFeature->selection(FeaturesPlugin_CreateBoundingBox::OBJECT_ID())
182                         ->setValue(selection(OBJECT_ID())->context(),
183                                    selection(OBJECT_ID())->value());
184
185   AttributeDoubleArrayPtr aValuesFeatures =
186     std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>
187                               (myCreateFeature->attribute(RESULT_VALUES_ID()));
188   AttributeDoubleArrayPtr aValues =
189     std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
190   for (int anI=0; anI < 6; anI++)
191     aValuesFeatures->setValue(anI,aValues->value(anI));
192
193   myCreateFeature->execute();
194   myCreateFeature->boolean(FeaturesPlugin_CreateBoundingBox::COMPUTE_ID())->setValue(true);
195 }