Salome HOME
Correcting ggoglestyle warnings.
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Cone.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        PrimitivesPlugin_Cone.cpp
4 // Created:     17 Mar 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include <PrimitivesPlugin_Cone.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_Cone::PrimitivesPlugin_Cone()
25 {
26 }
27
28 //=================================================================================================
29 void PrimitivesPlugin_Cone::initAttributes()
30 {
31   data()->addAttribute(PrimitivesPlugin_Cone::BASE_POINT_ID(),
32                        ModelAPI_AttributeSelection::typeId());
33   data()->addAttribute(PrimitivesPlugin_Cone::AXIS_ID(),
34                        ModelAPI_AttributeSelection::typeId());
35
36   data()->addAttribute(PrimitivesPlugin_Cone::BASE_RADIUS_ID(),
37                        ModelAPI_AttributeDouble::typeId());
38   data()->addAttribute(PrimitivesPlugin_Cone::TOP_RADIUS_ID(),
39                        ModelAPI_AttributeDouble::typeId());
40   data()->addAttribute(PrimitivesPlugin_Cone::HEIGHT_ID(),
41                        ModelAPI_AttributeDouble::typeId());
42
43   // Initialize the base point of the cone at the origin if the base point is not filled.
44   AttributeSelectionPtr aCenterPoint =
45     data()->selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
46   if (!aCenterPoint->isInitialized()) {
47     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
48       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
49     if (aPointObj.get()) {
50       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
51       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
52     }
53   }
54
55   // Initialize the axis at the OZ axis if the axis is not filled.
56   AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Cone::AXIS_ID());
57   if (!anAxis->isInitialized()) {
58     ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
59       ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
60     if (anAxisObj.get()) {
61       ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
62       anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
63     }
64   }
65 }
66
67 //=================================================================================================
68 void PrimitivesPlugin_Cone::execute()
69 {
70   // Getting base point.
71   std::shared_ptr<GeomAPI_Pnt> aBasePoint;
72   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
73     selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
74   if (aPointRef.get() != NULL) {
75     GeomShapePtr aShape1 = aPointRef->value();
76     if (!aShape1.get()) {
77       aShape1 = aPointRef->context()->shape();
78     }
79     if (aShape1) {
80         aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
81     }
82   }
83
84   // Getting axis.
85   std::shared_ptr<GeomAPI_Ax2> anAxis;
86   std::shared_ptr<GeomAPI_Edge> anEdge;
87   std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
88     selection(PrimitivesPlugin_Cone::AXIS_ID());
89   if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
90     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
91   } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
92              anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
93     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
94   }
95   if(anEdge) {
96     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
97                                                           anEdge->line()->direction()));
98   }
99
100   // Getting base radius, top radius and height
101   double aBaseRadius = real(PrimitivesPlugin_Cone::BASE_RADIUS_ID())->value();
102   double aTopRadius = real(PrimitivesPlugin_Cone::TOP_RADIUS_ID())->value();
103   double aHeight = real(PrimitivesPlugin_Cone::HEIGHT_ID())->value();
104
105   std::shared_ptr<GeomAlgoAPI_Cone> aConeAlgo =
106     std::shared_ptr<GeomAlgoAPI_Cone>(new GeomAlgoAPI_Cone(anAxis,
107                                                            aBaseRadius,
108                                                            aTopRadius,
109                                                            aHeight));
110
111   // These checks should be made to the GUI for the feature but
112   // the corresponding validator does not exist yet.
113   if (!aConeAlgo->check()) {
114     setError(aConeAlgo->getError());
115     return;
116   }
117
118   // Build the sphere
119   aConeAlgo->build();
120
121   // Check if the creation of the cylinder
122   if(!aConeAlgo->isDone()) {
123     setError(aConeAlgo->getError());
124     return;
125   }
126   if(!aConeAlgo->checkValid("Cone builder")) {
127     setError(aConeAlgo->getError());
128     return;
129   }
130
131   int aResultIndex = 0;
132   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
133   loadNamingDS(aConeAlgo, aResultBox);
134   setResult(aResultBox, aResultIndex);
135 }
136
137 //=================================================================================================
138 void PrimitivesPlugin_Cone::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Cone> theConeAlgo,
139                                          std::shared_ptr<ModelAPI_ResultBody> theResultCone)
140 {
141   // Load the result
142   theResultCone->store(theConeAlgo->shape());
143
144   // Prepare the naming
145   theConeAlgo->prepareNamingFaces();
146
147   // Insert to faces
148   int num = 1;
149   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
150       theConeAlgo->getCreatedFaces();
151   int nbFaces = 0;
152   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
153        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
154     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
155     theResultCone->generated(aFace, (*it).first, num++);
156     nbFaces++;
157   }
158
159   if (nbFaces == 2) {
160     // Naming vertices
161     GeomAPI_DataMapOfShapeShape aVertices;
162     GeomAPI_ShapeExplorer aVertExp(theConeAlgo->shape(), GeomAPI_Shape::VERTEX);
163     for(int anIndex = 1; aVertExp.more(); aVertExp.next()) {
164       if (!aVertices.isBound(aVertExp.current())) {
165         std::ostringstream aStream;
166         aStream<<"Vertex_"<<anIndex++;
167         theResultCone->generated(aVertExp.current(), aStream.str(), num++);
168         aVertices.bind(aVertExp.current(), aVertExp.current());
169       }
170     }
171   }
172 }