Salome HOME
Copyright update 2022
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Box.cpp
1 // Copyright (C) 2014-2022  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 <PrimitivesPlugin_Box.h>
21
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>
27
28 #include <GeomAlgoAPI_PointBuilder.h>
29
30 #include <memory>
31 #include <iostream>
32
33 //=================================================================================================
34 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
35 {
36 }
37
38 //=================================================================================================
39 void PrimitivesPlugin_Box::initAttributes()
40 {
41   data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
42
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());
47
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());
53
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());
61 }
62
63 //=================================================================================================
64 void PrimitivesPlugin_Box::execute()
65 {
66   AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
67   std::string aMethodType = aMethodTypeAttr->value();
68
69   if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
70     createBoxByDimensions();
71
72   if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
73     createBoxByTwoPoints();
74
75   if (aMethodType == CREATION_METHOD_BY_ONE_POINT_AND_DIMS())
76     createBoxByOnePointAndDims();
77 }
78
79 //=================================================================================================
80 void PrimitivesPlugin_Box::createBoxByDimensions()
81 {
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();
85
86   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
87
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());
92     return;
93   }
94
95   // Build the box
96   aBoxAlgo->build();
97
98   // Check if the creation of the box
99   if(!aBoxAlgo->isDone()) {
100     setError(aBoxAlgo->getError());
101     return;
102   }
103   if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
104     setError(aBoxAlgo->getError());
105     return;
106   }
107
108   int aResultIndex = 0;
109   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
110   loadNamingDS(aBoxAlgo, aResultBox);
111   setResult(aResultBox, aResultIndex);
112 }
113
114 //=================================================================================================
115 void PrimitivesPlugin_Box::createBoxByTwoPoints()
116 {
117   AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
118   AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
119
120   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
121
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();
127     if (!aShape2.get())
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));
133     }
134   }
135
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());
140     return;
141   }
142
143   // Build the box
144   aBoxAlgo->build();
145
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());
150     return;
151   }
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());
155     return;
156   }
157
158   int aResultIndex = 0;
159   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
160   loadNamingDS(aBoxAlgo, aResultBox);
161   setResult(aResultBox, aResultIndex);
162 }
163
164 //=================================================================================================
165 void PrimitivesPlugin_Box::createBoxByOnePointAndDims()
166 {
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();
171
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();
176
177   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
178   aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(x,y,z,aDx,aDy,aDz));
179
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());
184     return;
185   }
186
187   // Build the box
188   aBoxAlgo->build();
189
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());
194     return;
195   }
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());
199     return;
200   }
201
202   int aResultIndex = 0;
203   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
204   loadNamingDS(aBoxAlgo, aResultBox);
205   setResult(aResultBox, aResultIndex);
206 }
207
208 //=================================================================================================
209 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
210                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
211 {
212   // Load the result
213   theResultBox->store(theBoxAlgo->shape());
214
215   // Prepare the naming
216   theBoxAlgo->prepareNamingFaces();
217
218   // Insert to faces
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();
223        ++it)
224   {
225     theResultBox->generated((*it).second, (*it).first);
226   }
227 }
228