1 // Copyright (C) 2014-2023 CEA, EDF
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 <PrimitivesPlugin_Box.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
28 #include <GeomAlgoAPI_PointBuilder.h>
33 //=================================================================================================
34 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
38 //=================================================================================================
39 void PrimitivesPlugin_Box::initAttributes()
41 data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43 // Data for the first mode
44 data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
48 // Data for the second mode
49 data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
50 ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
52 ModelAPI_AttributeSelection::typeId());
54 // Data for the third mode
55 data()->addAttribute(PrimitivesPlugin_Box::OX_ID(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(PrimitivesPlugin_Box::OY_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(PrimitivesPlugin_Box::OZ_ID(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(PrimitivesPlugin_Box::HALF_DX_ID(), ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(PrimitivesPlugin_Box::HALF_DY_ID(), ModelAPI_AttributeDouble::typeId());
60 data()->addAttribute(PrimitivesPlugin_Box::HALF_DZ_ID(), ModelAPI_AttributeDouble::typeId());
63 //=================================================================================================
64 void PrimitivesPlugin_Box::execute()
66 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
67 std::string aMethodType = aMethodTypeAttr->value();
69 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
70 createBoxByDimensions();
72 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
73 createBoxByTwoPoints();
75 if (aMethodType == CREATION_METHOD_BY_ONE_POINT_AND_DIMS())
76 createBoxByOnePointAndDims();
79 //=================================================================================================
80 void PrimitivesPlugin_Box::createBoxByDimensions()
82 double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
83 double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
84 double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
86 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
88 // These checks should be made to the GUI for the feature but
89 // the corresponding validator does not exist yet.
90 if (!aBoxAlgo->check()) {
91 setError(aBoxAlgo->getError());
98 // Check if the creation of the box
99 if(!aBoxAlgo->isDone()) {
100 setError(aBoxAlgo->getError());
103 if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
104 setError(aBoxAlgo->getError());
108 int aResultIndex = 0;
109 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
110 loadNamingDS(aBoxAlgo, aResultBox);
111 setResult(aResultBox, aResultIndex);
114 //=================================================================================================
115 void PrimitivesPlugin_Box::createBoxByTwoPoints()
117 AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
118 AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
120 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
122 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
123 GeomShapePtr aShape1 = aRef1->value();
124 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
125 aShape1 = aRef1->context()->shape();
126 GeomShapePtr aShape2 = aRef2->value();
128 aShape2 = aRef2->context()->shape();
129 if (aShape1 && aShape2){
130 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
131 std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
132 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(aFirstPoint,aSecondPoint));
136 // These checks should be made to the GUI for the feature but
137 // the corresponding validator does not exist yet.
138 if (!aBoxAlgo->check()) {
139 setError(aBoxAlgo->getError());
146 // Check if the creation of the box
147 if(!aBoxAlgo->isDone()) {
148 // The error is not displayed in a popup window. It must be in the message console.
149 setError(aBoxAlgo->getError());
152 if(!aBoxAlgo->checkValid("Box builder with two points")) {
153 // The error is not displayed in a popup window. It must be in the message console.
154 setError(aBoxAlgo->getError());
158 int aResultIndex = 0;
159 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
160 loadNamingDS(aBoxAlgo, aResultBox);
161 setResult(aResultBox, aResultIndex);
164 //=================================================================================================
165 void PrimitivesPlugin_Box::createBoxByOnePointAndDims()
167 // Getting dx, dy and dz
168 double aDx = real(PrimitivesPlugin_Box::HALF_DX_ID())->value();
169 double aDy = real(PrimitivesPlugin_Box::HALF_DY_ID())->value();
170 double aDz = real(PrimitivesPlugin_Box::HALF_DZ_ID())->value();
172 // Getting point coordinates
173 double x = real(PrimitivesPlugin_Box::OX_ID())->value();
174 double y = real(PrimitivesPlugin_Box::OY_ID())->value();
175 double z = real(PrimitivesPlugin_Box::OZ_ID())->value();
177 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
178 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(x,y,z,aDx,aDy,aDz));
180 // These checks should be made to the GUI for the feature but
181 // the corresponding validator does not exist yet.
182 if (!aBoxAlgo->check()) {
183 setError(aBoxAlgo->getError());
190 // Check if the creation of the box
191 if(!aBoxAlgo->isDone()) {
192 // The error is not displayed in a popup window. It must be in the message console.
193 setError(aBoxAlgo->getError());
196 if(!aBoxAlgo->checkValid("Box builder with one point and dimensions")) {
197 // The error is not displayed in a popup window. It must be in the message console.
198 setError(aBoxAlgo->getError());
202 int aResultIndex = 0;
203 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
204 loadNamingDS(aBoxAlgo, aResultBox);
205 setResult(aResultBox, aResultIndex);
208 //=================================================================================================
209 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
210 std::shared_ptr<ModelAPI_ResultBody> theResultBox)
213 theResultBox->store(theBoxAlgo->shape());
215 // Prepare the naming
216 theBoxAlgo->prepareNamingFaces();
219 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
220 theBoxAlgo->getCreatedFaces();
221 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
222 it != listOfFaces.end();
225 theResultBox->generated((*it).second, (*it).first);