Salome HOME
Add the primitive 3D "Box".
[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::METHOD(), ModelAPI_AttributeString::typeId());
28
29   data()->addAttribute(PrimitivesPlugin_Box::DX(), ModelAPI_AttributeDouble::typeId());
30   data()->addAttribute(PrimitivesPlugin_Box::DY(), ModelAPI_AttributeDouble::typeId());
31   data()->addAttribute(PrimitivesPlugin_Box::DZ(), ModelAPI_AttributeDouble::typeId());
32
33   data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST(), ModelAPI_AttributeSelection::typeId());
34   data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND(), ModelAPI_AttributeSelection::typeId());
35 }
36
37 //=================================================================================================
38 void PrimitivesPlugin_Box::execute()
39 {
40   AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::METHOD());
41   std::string aMethodType = aMethodTypeAttr->value();
42   
43   if (aMethodType == "BoxByDimensions") 
44     createBoxByDimensions();
45   
46   if (aMethodType == "BoxByTwoPoints") 
47     createBoxByTwoPoints();
48 }
49
50 //=================================================================================================
51 void PrimitivesPlugin_Box::createBoxByDimensions()
52 {
53   double aDx = real(PrimitivesPlugin_Box::DX())->value();
54   double aDy = real(PrimitivesPlugin_Box::DY())->value();
55   double aDz = real(PrimitivesPlugin_Box::DZ())->value();
56
57   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
58   
59   // These checks should be made to the GUI for the feature but the corresponding validator does not exist yet.
60   if (!aBoxAlgo->check()) {
61     // The error is not displayed in a popup window. It must be in the status bar.
62     setError(aBoxAlgo->getError(), false);
63     return;
64   }
65   
66   // Build the box 
67   aBoxAlgo->build();
68
69   // Check if the creation of the box
70   if(!aBoxAlgo->isDone()) {
71     setError(aBoxAlgo->getError());
72     return;
73   }
74   if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
75     setError(aBoxAlgo->getError());
76     return;
77   }
78   
79   int aResultIndex = 0;
80   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex); 
81   loadNamingDS(aBoxAlgo, aResultBox);
82   setResult(aResultBox, aResultIndex);
83 }
84
85 //=================================================================================================
86 void PrimitivesPlugin_Box::createBoxByTwoPoints() 
87 {
88   AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST());
89   AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND());
90   
91   std::shared_ptr<GeomAlgoAPI_BoxPoints> aBoxAlgo;
92   
93   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
94     GeomShapePtr aShape1 = aRef1->value();
95     if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
96       aShape1 = aRef1->context()->shape();
97     GeomShapePtr aShape2 = aRef2->value();
98     if (!aShape2.get())
99       aShape2 = aRef2->context()->shape();
100     if (aShape1 && aShape2){
101       std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
102       std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
103       aBoxAlgo = std::shared_ptr<GeomAlgoAPI_BoxPoints>(new GeomAlgoAPI_BoxPoints(aFirstPoint,aSecondPoint));
104     }
105   }
106   
107   // These checks should be made to the GUI for the feature but the corresponding validator does not exist yet.
108   if (!aBoxAlgo->check()) {
109     // The error is not displayed in a popup window. It must be in the message console.
110     setError(aBoxAlgo->getError(), false);
111     return;
112   }
113   
114   // Build the box 
115   aBoxAlgo->build();
116
117   // Check if the creation of the box
118   if(!aBoxAlgo->isDone()) {
119     // The error is not displayed in a popup window. It must be in the message console.
120     setError(aBoxAlgo->getError(), false);
121     return;
122   }
123   if(!aBoxAlgo->checkValid("Box builder with two points")) {
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   
129   int aResultIndex = 0;
130   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex); 
131   loadNamingDS(aBoxAlgo, aResultBox);
132   setResult(aResultBox, aResultIndex);
133 }
134
135 //=================================================================================================
136 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
137                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
138 {
139   // Load the result
140   theResultBox->store(theBoxAlgo->shape());
141   
142   // Prepare the naming
143   theBoxAlgo->prepareNamingFaces();
144   
145   // Insert to faces
146   int num = 1;
147   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces = theBoxAlgo->getCreatedFaces();
148   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
149     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
150     theResultBox->generated(aFace, (*it).first, num++);
151   }
152 }
153