Salome HOME
Naming now stores primitives on tags 11+.
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Box.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <PrimitivesPlugin_Box.h>
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeString.h>
28
29 #include <GeomAlgoAPI_PointBuilder.h>
30
31 #include <memory>
32 #include <iostream>
33
34 //=================================================================================================
35 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
36 {
37 }
38
39 //=================================================================================================
40 void PrimitivesPlugin_Box::initAttributes()
41 {
42   data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43
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()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
49                        ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
51                        ModelAPI_AttributeSelection::typeId());
52 }
53
54 //=================================================================================================
55 void PrimitivesPlugin_Box::execute()
56 {
57   AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
58   std::string aMethodType = aMethodTypeAttr->value();
59
60   if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
61     createBoxByDimensions();
62
63   if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
64     createBoxByTwoPoints();
65 }
66
67 //=================================================================================================
68 void PrimitivesPlugin_Box::createBoxByDimensions()
69 {
70   double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
71   double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
72   double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
73
74   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
75
76   // These checks should be made to the GUI for the feature but
77   // the corresponding validator does not exist yet.
78   if (!aBoxAlgo->check()) {
79     setError(aBoxAlgo->getError());
80     return;
81   }
82
83   // Build the box
84   aBoxAlgo->build();
85
86   // Check if the creation of the box
87   if(!aBoxAlgo->isDone()) {
88     setError(aBoxAlgo->getError());
89     return;
90   }
91   if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
92     setError(aBoxAlgo->getError());
93     return;
94   }
95
96   int aResultIndex = 0;
97   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
98   loadNamingDS(aBoxAlgo, aResultBox);
99   setResult(aResultBox, aResultIndex);
100 }
101
102 //=================================================================================================
103 void PrimitivesPlugin_Box::createBoxByTwoPoints()
104 {
105   AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
106   AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
107
108   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
109
110   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
111     GeomShapePtr aShape1 = aRef1->value();
112     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
113       aShape1 = aRef1->context()->shape();
114     GeomShapePtr aShape2 = aRef2->value();
115     if (!aShape2.get())
116       aShape2 = aRef2->context()->shape();
117     if (aShape1 && aShape2){
118       std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
119       std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
120       aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(aFirstPoint,aSecondPoint));
121     }
122   }
123
124   // These checks should be made to the GUI for the feature but
125   // the corresponding validator does not exist yet.
126   if (!aBoxAlgo->check()) {
127     setError(aBoxAlgo->getError());
128     return;
129   }
130
131   // Build the box
132   aBoxAlgo->build();
133
134   // Check if the creation of the box
135   if(!aBoxAlgo->isDone()) {
136     // The error is not displayed in a popup window. It must be in the message console.
137     setError(aBoxAlgo->getError());
138     return;
139   }
140   if(!aBoxAlgo->checkValid("Box builder with two points")) {
141     // The error is not displayed in a popup window. It must be in the message console.
142     setError(aBoxAlgo->getError());
143     return;
144   }
145
146   int aResultIndex = 0;
147   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
148   loadNamingDS(aBoxAlgo, aResultBox);
149   setResult(aResultBox, aResultIndex);
150 }
151
152 //=================================================================================================
153 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
154                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
155 {
156   // Load the result
157   theResultBox->store(theBoxAlgo->shape());
158
159   // Prepare the naming
160   theBoxAlgo->prepareNamingFaces();
161
162   // Insert to faces
163   int num = 1;
164   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
165     theBoxAlgo->getCreatedFaces();
166   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
167        it != listOfFaces.end();
168        ++it)
169   {
170     theResultBox->generated((*it).second, (*it).first);
171   }
172 }
173