Salome HOME
updated copyright message
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Sphere.cpp
1 // Copyright (C) 2017-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        PrimitivesPlugin_Sphere.cpp
21 // Created:     15 Mar 2017
22 // Author:      Clarisse Genrault (CEA)
23
24 #include <PrimitivesPlugin_Sphere.h>
25
26 #include <GeomAPI_ShapeExplorer.h>
27
28 #include <GeomAlgoAPI_PointBuilder.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_Session.h>
36
37 #include <sstream>
38
39 //=================================================================================================
40 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
41 {
42 }
43
44 //=================================================================================================
45 void PrimitivesPlugin_Sphere::initAttributes()
46 {
47   data()->addAttribute(PrimitivesPlugin_Sphere::CREATION_METHOD(),
48                        ModelAPI_AttributeString::typeId());
49
50   // data for the first mode : by a point and a radius
51   data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
52                        ModelAPI_AttributeSelection::typeId());
53
54   data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
55                        ModelAPI_AttributeDouble::typeId());
56
57   // Initialize the center point of the sphere at the origin if the center point is not filled.
58   AttributeSelectionPtr aCenterPoint =
59     data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
60   if (!aCenterPoint->isInitialized()) {
61     ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
62       ->objectByName(ModelAPI_ResultConstruction::group(), L"Origin");
63     if (aPointObj.get()) {
64       ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
65       aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
66     }
67   }
68
69   // data for the second mode : by dimensions
70   data()->addAttribute(PrimitivesPlugin_Sphere::RMIN_ID(), ModelAPI_AttributeDouble::typeId());
71   data()->addAttribute(PrimitivesPlugin_Sphere::RMAX_ID(), ModelAPI_AttributeDouble::typeId());
72   data()->addAttribute(PrimitivesPlugin_Sphere::PHIMIN_ID(), ModelAPI_AttributeDouble::typeId());
73   data()->addAttribute(PrimitivesPlugin_Sphere::PHIMAX_ID(), ModelAPI_AttributeDouble::typeId());
74   data()->addAttribute(PrimitivesPlugin_Sphere::THETAMIN_ID(), ModelAPI_AttributeDouble::typeId());
75   data()->addAttribute(PrimitivesPlugin_Sphere::THETAMAX_ID(), ModelAPI_AttributeDouble::typeId());
76 }
77
78 //=================================================================================================
79 void PrimitivesPlugin_Sphere::execute()
80 {
81   AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Sphere::CREATION_METHOD());
82   std::string aMethodType = aMethodTypeAttr->value();
83
84   if (aMethodType == CREATION_METHOD_BY_PT_RADIUS())
85     createSphereByPtRadius();
86
87   if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
88     createShereByDimensions();
89 }
90
91
92 //=================================================================================================
93 void PrimitivesPlugin_Sphere::createSphereByPtRadius()
94 {
95   // Getting point.
96   std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
97   std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
98     selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
99   if (aPointRef.get() != NULL) {
100     GeomShapePtr aShape1 = aPointRef->value();
101     if (!aShape1.get()) {
102       aShape1 = aPointRef->context()->shape();
103     }
104     if (aShape1) {
105         aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
106     }
107   }
108
109   // Getting radius
110   double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
111
112   std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
113     std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
114
115   // These checks should be made to the GUI for the feature but
116   // the corresponding validator does not exist yet.
117   if (!aSphereAlgo->check()) {
118     setError(aSphereAlgo->getError());
119     return;
120   }
121
122   // Build the sphere
123   aSphereAlgo->build();
124
125   // Check if the creation of the sphere is OK
126   if(!aSphereAlgo->isDone()) {
127     setError(aSphereAlgo->getError());
128     return;
129   }
130   if(!aSphereAlgo->checkValid("Sphere builder")) {
131     setError(aSphereAlgo->getError());
132     return;
133   }
134
135   int aResultIndex = 0;
136   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
137   loadNamingDS(aSphereAlgo, aResultBox);
138   setResult(aResultBox, aResultIndex);
139 }
140
141 //=================================================================================================
142 void PrimitivesPlugin_Sphere::createShereByDimensions()
143 {
144   // Getting rmin, rmax, phimin, phimax, thetamin et thetamax
145   double aRMin = real(PrimitivesPlugin_Sphere::RMIN_ID())->value();
146   double aRMax = real(PrimitivesPlugin_Sphere::RMAX_ID())->value();
147   double aPhiMin = real(PrimitivesPlugin_Sphere::PHIMIN_ID())->value();
148   double aPhiMax = real(PrimitivesPlugin_Sphere::PHIMAX_ID())->value();
149   double aThetaMin = real(PrimitivesPlugin_Sphere::THETAMIN_ID())->value();
150   double aThetaMax = real(PrimitivesPlugin_Sphere::THETAMAX_ID())->value();
151
152   std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo = std::shared_ptr<GeomAlgoAPI_Sphere>(
153       new GeomAlgoAPI_Sphere(aRMin, aRMax, aPhiMin, aPhiMax, aThetaMin, aThetaMax));
154
155   // These checks should be made to the GUI for the feature but
156   // the corresponding validator does not exist yet.
157   if (!aSphereAlgo->check()) {
158     setError(aSphereAlgo->getError());
159     return;
160   }
161
162   // Build the sphere
163   aSphereAlgo->build();
164
165   // Check if the creation of the sphere is OK
166   if(!aSphereAlgo->isDone()) {
167     // The error is not displayed in a popup window. It must be in the message console.
168     setError(aSphereAlgo->getError());
169     return;
170   }
171   if(!aSphereAlgo->checkValid("Sphere Builder")) {
172     // The error is not displayed in a popup window. It must be in the message console.
173     setError(aSphereAlgo->getError());
174     return;
175   }
176
177   int aResultIndex = 0;
178   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
179   loadNamingDS(aSphereAlgo, aResultBox);
180   setResult(aResultBox, aResultIndex);
181 }
182
183 //=================================================================================================
184 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
185                                            std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
186 {
187   // Load the result
188   theResultSphere->store(theSphereAlgo->shape());
189
190   // Prepare the naming
191   theSphereAlgo->prepareNamingFaces();
192
193   // Insert to faces
194   // Naming for faces and edges
195   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
196       theSphereAlgo->getCreatedFaces();
197   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
198        it != listOfFaces.end();
199        ++it)
200   {
201     theResultSphere->generated((*it).second, (*it).first);
202   }
203
204   // Naming vertices
205   GeomAPI_DataMapOfShapeShape aVertices;
206   int anIndex = 1;
207   for (GeomAPI_ShapeExplorer aVertExp(theSphereAlgo->shape(), GeomAPI_Shape::VERTEX);
208        aVertExp.more();
209        aVertExp.next())
210   {
211     if (!aVertices.isBound(aVertExp.current())) {
212       std::ostringstream aStream;
213       aStream<<"Vertex_"<<anIndex++;
214       theResultSphere->generated(aVertExp.current(), aStream.str());
215       aVertices.bind(aVertExp.current(), aVertExp.current());
216     }
217   }
218 }