Salome HOME
Adding tests for "Box" primitive.
[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 #include <iostream>
19
20 //=================================================================================================
21 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
22 {
23 }
24
25 //=================================================================================================
26 void PrimitivesPlugin_Box::initAttributes()
27 {
28   data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
29
30   data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
31   data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
32   data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
33
34   data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
35                        ModelAPI_AttributeSelection::typeId());
36   data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
37                        ModelAPI_AttributeSelection::typeId());
38 }
39
40 //=================================================================================================
41 void PrimitivesPlugin_Box::execute()
42 {
43   AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
44   std::string aMethodType = aMethodTypeAttr->value();
45
46   if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
47     createBoxByDimensions();
48
49   if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
50     createBoxByTwoPoints();
51 }
52
53 //=================================================================================================
54 void PrimitivesPlugin_Box::createBoxByDimensions()
55 {
56   double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
57   double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
58   double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
59
60   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
61
62   // These checks should be made to the GUI for the feature but
63   // the corresponding validator does not exist yet.
64   if (!aBoxAlgo->check()) {
65     setError(aBoxAlgo->getError());
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     setError(aBoxAlgo->getError());
114     return;
115   }
116
117   // Build the box
118   aBoxAlgo->build();
119
120   // Check if the creation of the box
121   if(!aBoxAlgo->isDone()) {
122     // The error is not displayed in a popup window. It must be in the message console.
123     setError(aBoxAlgo->getError());
124     return;
125   }
126   if(!aBoxAlgo->checkValid("Box builder with two points")) {
127     // The error is not displayed in a popup window. It must be in the message console.
128     setError(aBoxAlgo->getError());
129     return;
130   }
131
132   int aResultIndex = 0;
133   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
134   loadNamingDS(aBoxAlgo, aResultBox);
135   setResult(aResultBox, aResultIndex);
136 }
137
138 //=================================================================================================
139 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
140                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
141 {
142   // Load the result
143   theResultBox->store(theBoxAlgo->shape());
144
145   // Prepare the naming
146   theBoxAlgo->prepareNamingFaces();
147
148   // Insert to faces
149   int num = 1;
150   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
151     theBoxAlgo->getCreatedFaces();
152   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
153        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
154     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
155     theResultBox->generated(aFace, (*it).first, num++);
156   }
157 }
158