]> SALOME platform Git repositories - modules/shaper.git/blob - src/PrimitivesPlugin/PrimitivesPlugin_Cone.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[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
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_Cone::PrimitivesPlugin_Cone()
22 {
23 }
24
25 //=================================================================================================
26 void PrimitivesPlugin_Cone::initAttributes()
27 {
28   data()->addAttribute(PrimitivesPlugin_Cone::BASE_POINT_ID(),
29                        ModelAPI_AttributeSelection::typeId());
30   data()->addAttribute(PrimitivesPlugin_Cone::AXIS_ID(),
31                        ModelAPI_AttributeSelection::typeId());
32
33   data()->addAttribute(PrimitivesPlugin_Cone::BASE_RADIUS_ID(),
34                        ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(PrimitivesPlugin_Cone::TOP_RADIUS_ID(),
36                        ModelAPI_AttributeDouble::typeId());
37   data()->addAttribute(PrimitivesPlugin_Cone::HEIGHT_ID(),
38                        ModelAPI_AttributeDouble::typeId());
39
40   // Initialize the base point of the cone at the origin if the base point is not filled.
41   AttributeSelectionPtr aCenterPoint =
42     data()->selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
43   if (!aCenterPoint->isInitialized()) {
44     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
45       ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
46     if (aPointObj.get()) {
47       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
48       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
49     }
50   }
51
52   // Initialize the axis at the OZ axis if the axis is not filled.
53   AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Cone::AXIS_ID());
54   if (!anAxis->isInitialized()) {
55     ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
56       ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
57     if (anAxisObj.get()) {
58       ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
59       anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
60     }
61   }
62 }
63
64 //=================================================================================================
65 void PrimitivesPlugin_Cone::execute()
66 {
67   // Getting base point.
68   std::shared_ptr<GeomAPI_Pnt> aBasePoint;
69   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
70     selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
71   if (aPointRef.get() != NULL) {
72     GeomShapePtr aShape1 = aPointRef->value();
73     if (!aShape1.get()) {
74       aShape1 = aPointRef->context()->shape();
75     }
76     if (aShape1) {
77         aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
78     }
79   }
80
81   // Getting axis.
82   std::shared_ptr<GeomAPI_Ax2> anAxis;
83   std::shared_ptr<GeomAPI_Edge> anEdge;
84   std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
85     selection(PrimitivesPlugin_Cone::AXIS_ID());
86   if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
87     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
88   } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
89              anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
90     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
91   }
92   if(anEdge) {
93     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
94                                                           anEdge->line()->direction()));
95   }
96
97   // Getting base radius, top radius and height
98   double aBaseRadius = real(PrimitivesPlugin_Cone::BASE_RADIUS_ID())->value();
99   double aTopRadius = real(PrimitivesPlugin_Cone::TOP_RADIUS_ID())->value();
100   double aHeight = real(PrimitivesPlugin_Cone::HEIGHT_ID())->value();
101
102   std::shared_ptr<GeomAlgoAPI_Cone> aConeAlgo =
103     std::shared_ptr<GeomAlgoAPI_Cone>(new GeomAlgoAPI_Cone(anAxis,
104                                                            aBaseRadius,
105                                                            aTopRadius,
106                                                            aHeight));
107
108   // These checks should be made to the GUI for the feature but
109   // the corresponding validator does not exist yet.
110   if (!aConeAlgo->check()) {
111     setError(aConeAlgo->getError());
112     return;
113   }
114
115   // Build the sphere
116   aConeAlgo->build();
117
118   // Check if the creation of the cylinder
119   if(!aConeAlgo->isDone()) {
120     setError(aConeAlgo->getError());
121     return;
122   }
123   if(!aConeAlgo->checkValid("Cone builder")) {
124     setError(aConeAlgo->getError());
125     return;
126   }
127
128   int aResultIndex = 0;
129   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
130   loadNamingDS(aConeAlgo, aResultBox);
131   setResult(aResultBox, aResultIndex);
132 }
133
134 //=================================================================================================
135 void PrimitivesPlugin_Cone::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Cone> theConeAlgo,
136                                          std::shared_ptr<ModelAPI_ResultBody> theResultCone)
137 {
138   // Load the result
139   theResultCone->store(theConeAlgo->shape());
140
141   // Prepare the naming
142   theConeAlgo->prepareNamingFaces();
143
144   // Insert to faces
145   int num = 1;
146   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
147       theConeAlgo->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     theResultCone->generated(aFace, (*it).first, num++);
152   }
153 }