1 // Copyright (C) 2017-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: PrimitivesPlugin_Sphere.cpp
21 // Created: 15 Mar 2017
22 // Author: Clarisse Genrault (CEA)
24 #include <PrimitivesPlugin_Sphere.h>
26 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAlgoAPI_PointBuilder.h>
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>
39 //=================================================================================================
40 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
44 //=================================================================================================
45 void PrimitivesPlugin_Sphere::initAttributes()
47 data()->addAttribute(PrimitivesPlugin_Sphere::CREATION_METHOD(),
48 ModelAPI_AttributeString::typeId());
50 // data for the first mode : by a point and a radius
51 data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
52 ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
55 ModelAPI_AttributeDouble::typeId());
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>());
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());
78 //=================================================================================================
79 void PrimitivesPlugin_Sphere::execute()
81 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Sphere::CREATION_METHOD());
82 std::string aMethodType = aMethodTypeAttr->value();
84 if (aMethodType == CREATION_METHOD_BY_PT_RADIUS())
85 createSphereByPtRadius();
87 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
88 createShereByDimensions();
92 //=================================================================================================
93 void PrimitivesPlugin_Sphere::createSphereByPtRadius()
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();
105 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
110 double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
112 std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
113 std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
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());
123 aSphereAlgo->build();
125 // Check if the creation of the sphere is OK
126 if(!aSphereAlgo->isDone()) {
127 setError(aSphereAlgo->getError());
130 if(!aSphereAlgo->checkValid("Sphere builder")) {
131 setError(aSphereAlgo->getError());
135 int aResultIndex = 0;
136 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
137 loadNamingDS(aSphereAlgo, aResultBox);
138 setResult(aResultBox, aResultIndex);
141 //=================================================================================================
142 void PrimitivesPlugin_Sphere::createShereByDimensions()
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();
152 std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo = std::shared_ptr<GeomAlgoAPI_Sphere>(
153 new GeomAlgoAPI_Sphere(aRMin, aRMax, aPhiMin, aPhiMax, aThetaMin, aThetaMax));
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());
163 aSphereAlgo->build();
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());
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());
177 int aResultIndex = 0;
178 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
179 loadNamingDS(aSphereAlgo, aResultBox);
180 setResult(aResultBox, aResultIndex);
183 //=================================================================================================
184 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
185 std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
188 theResultSphere->store(theSphereAlgo->shape());
190 // Prepare the naming
191 theSphereAlgo->prepareNamingFaces();
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();
201 theResultSphere->generated((*it).second, (*it).first);
205 GeomAPI_DataMapOfShapeShape aVertices;
207 for (GeomAPI_ShapeExplorer aVertExp(theSphereAlgo->shape(), GeomAPI_Shape::VERTEX);
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());