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