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