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