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