Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Torus.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        PrimitivesPlugin_Torus.cpp
4 // Created:     17 Mar 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <PrimitivesPlugin_Torus.h>
8
9 #include <GeomAPI_Edge.h>
10 #include <GeomAPI_Lin.h>
11
12 #include <GeomAlgoAPI_PointBuilder.h>
13
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultConstruction.h>
18 #include <ModelAPI_Session.h>
19
20 //=================================================================================================
21 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
22 {
23 }
24
25 //=================================================================================================
26 void PrimitivesPlugin_Torus::initAttributes()
27 {
28   data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
29                        ModelAPI_AttributeSelection::typeId());
30   data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
31                        ModelAPI_AttributeSelection::typeId());
32
33   data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
34                        ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
36                        ModelAPI_AttributeDouble::typeId());
37
38   // Initialize the base point of the torus at the origin if the base point is not filled.
39   AttributeSelectionPtr aCenterPoint =
40     data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
41   if (!aCenterPoint->isInitialized()) {
42     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
43       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
44     if (aPointObj.get()) {
45       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
46       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
47     }
48   }
49
50   // Initialize the axis at the OZ axis if the axis is not filled.
51   AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
52   if (!anAxis->isInitialized()) {
53     ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
54       ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
55     if (anAxisObj.get()) {
56       ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
57       anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
58     }
59   }
60 }
61
62 //=================================================================================================
63 void PrimitivesPlugin_Torus::execute()
64 {
65   // Getting base point.
66   std::shared_ptr<GeomAPI_Pnt> aBasePoint;
67   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
68     selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
69   if (aPointRef.get() != NULL) {
70     GeomShapePtr aShape1 = aPointRef->value();
71     if (!aShape1.get()) {
72       aShape1 = aPointRef->context()->shape();
73     }
74     if (aShape1) {
75         aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
76     }
77   }
78
79   // Getting axis.
80   std::shared_ptr<GeomAPI_Ax2> anAxis;
81   std::shared_ptr<GeomAPI_Edge> anEdge;
82   std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
83     selection(PrimitivesPlugin_Torus::AXIS_ID());
84   if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
85     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
86   } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
87              anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
88     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
89   }
90   if(anEdge) {
91     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
92                                                           anEdge->line()->direction()));
93   }
94
95   // Getting radius and ring radius
96   double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
97   double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
98
99   std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
100     std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
101                                                              aRadius,
102                                                              aRingRadius));
103
104   // These checks should be made to the GUI for the feature but
105   // the corresponding validator does not exist yet.
106   if (!aTorusAlgo->check()) {
107     setError(aTorusAlgo->getError());
108     return;
109   }
110
111   // Build the sphere
112   aTorusAlgo->build();
113
114   // Check if the creation of the cylinder
115   if(!aTorusAlgo->isDone()) {
116     setError(aTorusAlgo->getError());
117     return;
118   }
119   if(!aTorusAlgo->checkValid("Torus builder")) {
120     setError(aTorusAlgo->getError());
121     return;
122   }
123
124   int aResultIndex = 0;
125   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
126   loadNamingDS(aTorusAlgo, aResultBox);
127   setResult(aResultBox, aResultIndex);
128 }
129
130 //=================================================================================================
131 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
132                                           std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
133 {
134   // Load the result
135   theResultTorus->store(theTorusAlgo->shape());
136
137   // Prepare the naming
138   theTorusAlgo->prepareNamingFaces();
139
140   // Insert to faces
141   int num = 1;
142   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
143       theTorusAlgo->getCreatedFaces();
144   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
145        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
146     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
147     theResultTorus->generated(aFace, (*it).first, num++);
148   }
149 }