Salome HOME
Merge branch 'master' into cgt/devCEA
[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 <GeomAlgoAPI_PointBuilder.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_Session.h>
16
17 //=================================================================================================
18 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
19 {
20 }
21
22 //=================================================================================================
23 void PrimitivesPlugin_Sphere::initAttributes()
24 {
25   data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
26                        ModelAPI_AttributeSelection::typeId());
27
28   data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
29                        ModelAPI_AttributeDouble::typeId());
30
31   // Initialize the center point of the sphere at the origin if the center point is not filled.
32   AttributeSelectionPtr aCenterPoint =
33     data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
34   if (!aCenterPoint->isInitialized()) {
35     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
36       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
37     if (aPointObj.get()) {
38       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
39       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
40     }
41   }
42 }
43
44 //=================================================================================================
45 void PrimitivesPlugin_Sphere::execute()
46 {
47   // Getting point.
48   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
49   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
50     selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
51   if (aPointRef.get() != NULL) {
52     GeomShapePtr aShape1 = aPointRef->value();
53     if (!aShape1.get()) {
54       aShape1 = aPointRef->context()->shape();
55     }
56     if (aShape1) {
57         aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
58     }
59   }
60
61   // Getting radius
62   double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
63
64   std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
65     std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
66
67   // These checks should be made to the GUI for the feature but
68   // the corresponding validator does not exist yet.
69   if (!aSphereAlgo->check()) {
70     setError(aSphereAlgo->getError());
71     return;
72   }
73
74   // Build the sphere
75   aSphereAlgo->build();
76
77   // Check if the creation of the cylinder
78   if(!aSphereAlgo->isDone()) {
79     setError(aSphereAlgo->getError());
80     return;
81   }
82   if(!aSphereAlgo->checkValid("Sphere builder")) {
83     setError(aSphereAlgo->getError());
84     return;
85   }
86
87   int aResultIndex = 0;
88   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
89   loadNamingDS(aSphereAlgo, aResultBox);
90   setResult(aResultBox, aResultIndex);
91 }
92
93 //=================================================================================================
94 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
95                                            std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
96 {
97   // Load the result
98   theResultSphere->store(theSphereAlgo->shape());
99
100   // Prepare the naming
101   theSphereAlgo->prepareNamingFaces();
102
103   // Insert to faces
104   int num = 1;
105   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
106       theSphereAlgo->getCreatedFaces();
107   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
108        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
109     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
110     theResultSphere->generated(aFace, (*it).first, num++);
111   }
112 }