Salome HOME
1e4d4ce2c83341bc55bc4d8bcc39012c8c054b85
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BoundingBoxBase.cpp
1 // Copyright (C) 2018-2023  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 "FeaturesPlugin_BoundingBoxBase.h"
21
22 #include <ModelAPI_AttributeDoubleArray.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28 #include <GeomAPI_Vertex.h>
29
30 #include <GeomAlgoAPI_BoundingBox.h>
31 #include <GeomAlgoAPI_PointBuilder.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33
34 #include <Config_PropManager.h>
35
36 #include <iomanip>
37 #include <sstream>
38
39
40
41 //=================================================================================================
42 void FeaturesPlugin_BoundingBoxBase::createBoxByTwoPoints()
43 {
44   AttributeDoubleArrayPtr aValues =
45       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attributResultValues());
46
47   SessionPtr aSession = ModelAPI_Session::get();
48
49   DocumentPtr aDoc =  aSession->activeDocument();
50
51   GeomVertexPtr vertexFirst =
52           GeomAlgoAPI_PointBuilder::vertex(aValues->value(0),
53                                            aValues->value(2),
54                                            aValues->value(4));
55
56   GeomVertexPtr  vertexSecond =
57           GeomAlgoAPI_PointBuilder::vertex(aValues->value(1),
58                                            aValues->value(3),
59                                            aValues->value(5));
60
61
62   std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
63
64
65   aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(
66                           new GeomAlgoAPI_Box(vertexFirst->point(),vertexSecond->point()));
67
68
69   // These checks should be made to the GUI for the feature but
70   // the corresponding validator does not exist yet.
71   if (!aBoxAlgo->check()) {
72     setError(aBoxAlgo->getError());
73     return;
74   }
75
76   // Build the box
77   aBoxAlgo->build();
78
79   // Check if the creation of the box
80   if (!aBoxAlgo->isDone()) {
81     // The error is not displayed in a popup window. It must be in the message console.
82     setError(aBoxAlgo->getError());
83     return;
84   }
85   if (!aBoxAlgo->checkValid("Box builder with two points")) {
86     // The error is not displayed in a popup window. It must be in the message console.
87     setError(aBoxAlgo->getError());
88     return;
89   }
90
91   int aResultIndex = 0;
92   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
93   loadNamingDS(aBoxAlgo, aResultBox);
94   setResult(aResultBox, aResultIndex);
95 }
96
97 //=================================================================================================
98 void FeaturesPlugin_BoundingBoxBase::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
99                                         std::shared_ptr<ModelAPI_ResultBody> theResultBox)
100 {
101   // Load the result
102   theResultBox->store(theBoxAlgo->shape());
103
104   // Prepare the naming
105   theBoxAlgo->prepareNamingFaces();
106
107   // Insert to faces
108   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
109     theBoxAlgo->getCreatedFaces();
110   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
111        it != listOfFaces.end();
112        ++it) {
113     theResultBox->generated((*it).second, (*it).first);
114   }
115 }