Salome HOME
Issue #1834: Fix length of lines
[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_BoxPoints> 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_BoxPoints>(
107                                   new GeomAlgoAPI_BoxPoints(aFirstPoint,aSecondPoint));
108     }
109   }
110   
111   // These checks should be made to the GUI for the feature but 
112   // the corresponding validator does not exist yet.
113   if (!aBoxAlgo->check()) {
114     // The error is not displayed in a popup window. It must be in the message console.
115     setError(aBoxAlgo->getError(), false);
116     return;
117   }
118   
119   // Build the box 
120   aBoxAlgo->build();
121
122   // Check if the creation of the box
123   if(!aBoxAlgo->isDone()) {
124     // The error is not displayed in a popup window. It must be in the message console.
125     setError(aBoxAlgo->getError(), false);
126     return;
127   }
128   if(!aBoxAlgo->checkValid("Box builder with two points")) {
129     // The error is not displayed in a popup window. It must be in the message console.
130     setError(aBoxAlgo->getError(), false);
131     return;
132   }
133   
134   int aResultIndex = 0;
135   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex); 
136   loadNamingDS(aBoxAlgo, aResultBox);
137   setResult(aResultBox, aResultIndex);
138 }
139
140 //=================================================================================================
141 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
142                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
143 {
144   // Load the result
145   theResultBox->store(theBoxAlgo->shape());
146   
147   // Prepare the naming
148   theBoxAlgo->prepareNamingFaces();
149   
150   // Insert to faces
151   int num = 1;
152   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces = 
153     theBoxAlgo->getCreatedFaces();
154   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator 
155        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
156     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
157     theResultBox->generated(aFace, (*it).first, num++);
158   }
159 }
160