Salome HOME
Issue #2811: Update content of Object node on creation moment
[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 #include <GeomAPI_ShapeExplorer.h>
12 #include <GeomAPI_ShapeIterator.h>
13
14 #include <GeomAlgoAPI_PointBuilder.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_ResultBody.h>
19 #include <ModelAPI_ResultConstruction.h>
20 #include <ModelAPI_Session.h>
21
22 #include <sstream>
23
24 //=================================================================================================
25 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
26 {
27 }
28
29 //=================================================================================================
30 void PrimitivesPlugin_Torus::initAttributes()
31 {
32   data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
33                        ModelAPI_AttributeSelection::typeId());
34   data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
35                        ModelAPI_AttributeSelection::typeId());
36
37   data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
38                        ModelAPI_AttributeDouble::typeId());
39   data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
40                        ModelAPI_AttributeDouble::typeId());
41
42   // Initialize the base point of the torus at the origin if the base point is not filled.
43   AttributeSelectionPtr aCenterPoint =
44     data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
45   if (!aCenterPoint->isInitialized()) {
46     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
47       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
48     if (aPointObj.get()) {
49       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
50       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
51     }
52   }
53
54   // Initialize the axis at the OZ axis if the axis is not filled.
55   AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
56   if (!anAxis->isInitialized()) {
57     ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
58       ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
59     if (anAxisObj.get()) {
60       ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
61       anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
62     }
63   }
64 }
65
66 //=================================================================================================
67 void PrimitivesPlugin_Torus::execute()
68 {
69   // Getting base point.
70   std::shared_ptr<GeomAPI_Pnt> aBasePoint;
71   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
72     selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
73   if (aPointRef.get() != NULL) {
74     GeomShapePtr aShape1 = aPointRef->value();
75     if (!aShape1.get()) {
76       aShape1 = aPointRef->context()->shape();
77     }
78     if (aShape1) {
79         aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
80     }
81   }
82
83   // Getting axis.
84   static const std::string aSelectionError = "Error: The axis shape selection is bad.";
85   std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef = selection(AXIS_ID());
86   GeomShapePtr aShape = anEdgeRef->value();
87   if (!aShape.get()) {
88     if (anEdgeRef->context().get()) {
89       aShape = anEdgeRef->context()->shape();
90     }
91   }
92   if (!aShape.get()) {
93     setError(aSelectionError);
94     return;
95   }
96   std::shared_ptr<GeomAPI_Edge> anEdge;
97   if (aShape->isEdge())
98   {
99     anEdge = aShape->edge();
100   }
101   else if (aShape->isCompound())
102   {
103     GeomAPI_ShapeIterator anIt(aShape);
104     anEdge = anIt.current()->edge();
105   }
106   else
107   {
108     setError(aSelectionError);
109     return;
110   }
111
112   if (!anEdge.get())
113   {
114     setError(aSelectionError);
115     return;
116   }
117
118   std::shared_ptr<GeomAPI_Ax2> anAxis(new GeomAPI_Ax2(aBasePoint,
119                                                       anEdge->line()->direction()));
120
121   // Getting radius and ring radius
122   double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
123   double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
124
125   std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
126     std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
127                                                              aRadius,
128                                                              aRingRadius));
129
130   // These checks should be made to the GUI for the feature but
131   // the corresponding validator does not exist yet.
132   if (!aTorusAlgo->check()) {
133     setError(aTorusAlgo->getError());
134     return;
135   }
136
137   // Build the sphere
138   aTorusAlgo->build();
139
140   // Check if the creation of the cylinder
141   if(!aTorusAlgo->isDone()) {
142     setError(aTorusAlgo->getError());
143     return;
144   }
145   if(!aTorusAlgo->checkValid("Torus builder")) {
146     setError(aTorusAlgo->getError());
147     return;
148   }
149
150   int aResultIndex = 0;
151   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
152   loadNamingDS(aTorusAlgo, aResultBox);
153   setResult(aResultBox, aResultIndex);
154 }
155
156 //=================================================================================================
157 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
158                                           std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
159 {
160   // Load the result
161   theResultTorus->store(theTorusAlgo->shape());
162
163   // Prepare the naming
164   theTorusAlgo->prepareNamingFaces();
165
166   // Insert to faces
167   // Naming for faces
168   int num = 1;
169   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
170       theTorusAlgo->getCreatedFaces();
171   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
172        it != listOfFaces.end();
173        ++it)
174   {
175     theResultTorus->generated((*it).second, (*it).first);
176   }
177
178   // Naming of edges
179   GeomAPI_DataMapOfShapeShape anEdges;
180   int anIndex = 1;
181   for (GeomAPI_ShapeExplorer anEdgeExp(theTorusAlgo->shape(), GeomAPI_Shape::EDGE);
182        anEdgeExp.more();
183        anEdgeExp.next())
184   {
185     if (!anEdges.isBound(anEdgeExp.current())) {
186       std::ostringstream aStream;
187       aStream<<"Edge_"<<anIndex++;
188       theResultTorus->generated(anEdgeExp.current(), aStream.str());
189       anEdges.bind(anEdgeExp.current(), anEdgeExp.current());
190     }
191   }
192 }