Salome HOME
Update copyrights
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Sphere.cpp
1 // Copyright (C) 2017-2019  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 // File:        PrimitivesPlugin_Sphere.h
21 // Created:     15 Mar 2017
22 // Author:      Clarisse Genrault (CEA)
23
24 #include <PrimitivesPlugin_Sphere.h>
25
26 #include <GeomAPI_ShapeExplorer.h>
27
28 #include <GeomAlgoAPI_PointBuilder.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_ResultBody.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35
36 #include <sstream>
37
38 //=================================================================================================
39 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
40 {
41 }
42
43 //=================================================================================================
44 void PrimitivesPlugin_Sphere::initAttributes()
45 {
46   data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
47                        ModelAPI_AttributeSelection::typeId());
48
49   data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
50                        ModelAPI_AttributeDouble::typeId());
51
52   // Initialize the center point of the sphere at the origin if the center point is not filled.
53   AttributeSelectionPtr aCenterPoint =
54     data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
55   if (!aCenterPoint->isInitialized()) {
56     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
57       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
58     if (aPointObj.get()) {
59       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
60       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
61     }
62   }
63 }
64
65 //=================================================================================================
66 void PrimitivesPlugin_Sphere::execute()
67 {
68   // Getting point.
69   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
70   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
71     selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
72   if (aPointRef.get() != NULL) {
73     GeomShapePtr aShape1 = aPointRef->value();
74     if (!aShape1.get()) {
75       aShape1 = aPointRef->context()->shape();
76     }
77     if (aShape1) {
78         aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
79     }
80   }
81
82   // Getting radius
83   double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
84
85   std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
86     std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
87
88   // These checks should be made to the GUI for the feature but
89   // the corresponding validator does not exist yet.
90   if (!aSphereAlgo->check()) {
91     setError(aSphereAlgo->getError());
92     return;
93   }
94
95   // Build the sphere
96   aSphereAlgo->build();
97
98   // Check if the creation of the cylinder
99   if(!aSphereAlgo->isDone()) {
100     setError(aSphereAlgo->getError());
101     return;
102   }
103   if(!aSphereAlgo->checkValid("Sphere builder")) {
104     setError(aSphereAlgo->getError());
105     return;
106   }
107
108   int aResultIndex = 0;
109   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
110   loadNamingDS(aSphereAlgo, aResultBox);
111   setResult(aResultBox, aResultIndex);
112 }
113
114 //=================================================================================================
115 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
116                                            std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
117 {
118   // Load the result
119   theResultSphere->store(theSphereAlgo->shape());
120
121   // Prepare the naming
122   theSphereAlgo->prepareNamingFaces();
123
124   // Insert to faces
125   // Naming for faces and edges
126   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
127       theSphereAlgo->getCreatedFaces();
128   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
129        it != listOfFaces.end();
130        ++it)
131   {
132     theResultSphere->generated((*it).second, (*it).first);
133   }
134
135   // Naming vertices
136   GeomAPI_DataMapOfShapeShape aVertices;
137   int anIndex = 1;
138   for (GeomAPI_ShapeExplorer aVertExp(theSphereAlgo->shape(), GeomAPI_Shape::VERTEX);
139        aVertExp.more();
140        aVertExp.next())
141   {
142     if (!aVertices.isBound(aVertExp.current())) {
143       std::ostringstream aStream;
144       aStream<<"Vertex_"<<anIndex++;
145       theResultSphere->generated(aVertExp.current(), aStream.str());
146       aVertices.bind(aVertExp.current(), aVertExp.current());
147     }
148   }
149 }