1 // Copyright (C) 2018-2021 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_BoundingBox.h"
22 #include <Config_PropManager.h>
24 #include <FeaturesPlugin_CreateBoundingBox.h>
26 #include <GeomAlgoAPI_BoundingBox.h>
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>
39 //=================================================================================================
40 FeaturesPlugin_BoundingBox::FeaturesPlugin_BoundingBox()
44 //=================================================================================================
45 void FeaturesPlugin_BoundingBox::initAttributes()
47 // attribute for object selected
48 data()->addAttribute(OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
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());
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());
68 data()->addAttribute(RESULT_VALUES_ID(), ModelAPI_AttributeDoubleArray::typeId());
70 data()->realArray(RESULT_VALUES_ID())->setSize(6);
74 //=================================================================================================
75 void FeaturesPlugin_BoundingBox::execute()
80 createBoxByTwoPoints();
82 if (boolean(CREATEBOX_ID())->value()) {
83 if (!myCreateFeature.get())
87 if (myCreateFeature.get()) {
88 myCreateFeature->eraseResults();
89 SessionPtr aSession = ModelAPI_Session::get();
90 DocumentPtr aDoc = aSession->activeDocument();
91 aDoc->removeFeature(myCreateFeature);
92 myCreateFeature.reset();
97 //=================================================================================================
98 void FeaturesPlugin_BoundingBox::attributeChanged(const std::string& theID)
100 if (theID == OBJECT_ID()) {
101 if (myCreateFeature.get())
106 //=================================================================================================
107 AttributePtr FeaturesPlugin_BoundingBox::attributResultValues()
109 return attribute(RESULT_VALUES_ID());
112 //=================================================================================================
113 bool FeaturesPlugin_BoundingBox::updateValues()
115 AttributeSelectionPtr aSelection = selection(OBJECT_ID());
116 if (aSelection->isInitialized()) {
117 AttributeDoubleArrayPtr aValues =
118 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
119 std::stringstream streamxmin;
120 std::stringstream streamymin;
121 std::stringstream streamzmin;
122 std::stringstream streamxmax;
123 std::stringstream streamymax;
124 std::stringstream streamzmax;
127 if (aSelection && aSelection->isInitialized()) {
128 aShape = aSelection->value();
129 if (!aShape && aSelection->context())
130 aShape = aSelection->context()->shape();
133 if (aShape && !aShape->isEqual(myShape)) {
134 double aXmin, aXmax, aYmin, aYmax, aZmin, aZmax;
136 if (!GetBoundingBox(aShape,
141 setError("Error in bounding box calculation :" + anError);
146 streamxmin << std::setprecision(14) << aXmin;
147 aValues->setValue(0, aXmin);
148 streamxmax << std::setprecision(14) << aXmax;
149 aValues->setValue(1, aXmax);
150 streamymin << std::setprecision(14) << aYmin;
151 aValues->setValue(2, aYmin);
152 streamymax << std::setprecision(14) << aYmax;
153 aValues->setValue(3, aYmax);
154 streamzmin << std::setprecision(14) << aZmin;
155 aValues->setValue(4, aZmin);
156 streamzmax << std::setprecision(14) << aZmax;
157 aValues->setValue(5, aZmax);
158 string(X_MIN_COORD_ID() )->setValue( "X = " + streamxmin.str() );
159 string(Y_MIN_COORD_ID() )->setValue( "Y = " + streamymin.str() );
160 string(Z_MIN_COORD_ID() )->setValue( "Z = " + streamzmin.str() );
161 string(X_MAX_COORD_ID() )->setValue( "X = " + streamxmax.str() );
162 string(Y_MAX_COORD_ID() )->setValue( "Y = " + streamymax.str() );
163 string(Z_MAX_COORD_ID() )->setValue( "Z = " + streamzmax.str() );
169 //=================================================================================================
170 void FeaturesPlugin_BoundingBox::createBox()
172 SessionPtr aSession = ModelAPI_Session::get();
174 DocumentPtr aDoc = aSession->activeDocument();
177 myCreateFeature = aDoc->addFeature(FeaturesPlugin_CreateBoundingBox::ID());
181 //=================================================================================================
182 void FeaturesPlugin_BoundingBox::updateBox()
184 myCreateFeature->boolean(FeaturesPlugin_CreateBoundingBox::COMPUTE_ID())->setValue(false);
185 myCreateFeature->selection(FeaturesPlugin_CreateBoundingBox::OBJECT_ID())
186 ->setValue(selection(OBJECT_ID())->context(),
187 selection(OBJECT_ID())->value());
189 AttributeDoubleArrayPtr aValuesFeatures =
190 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>
191 (myCreateFeature->attribute(RESULT_VALUES_ID()));
192 AttributeDoubleArrayPtr aValues =
193 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
194 for (int anI=0; anI < 6; anI++)
195 aValuesFeatures->setValue(anI,aValues->value(anI));
197 myCreateFeature->execute();
198 myCreateFeature->boolean(FeaturesPlugin_CreateBoundingBox::COMPUTE_ID())->setValue(true);