1 // Copyright (C) 2017-2020 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.h
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_ResultBody.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
38 //=================================================================================================
39 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
43 //=================================================================================================
44 void PrimitivesPlugin_Sphere::initAttributes()
46 data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
47 ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
50 ModelAPI_AttributeDouble::typeId());
52 // Initialize the center point of the sphere at the origin if the center point is not filled.
53 AttributeSelectionPtr aCenterPoint =
54 data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
55 if (!aCenterPoint->isInitialized()) {
56 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
57 ->objectByName(ModelAPI_ResultConstruction::group(), L"Origin");
58 if (aPointObj.get()) {
59 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
60 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
65 //=================================================================================================
66 void PrimitivesPlugin_Sphere::execute()
69 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
70 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
71 selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
72 if (aPointRef.get() != NULL) {
73 GeomShapePtr aShape1 = aPointRef->value();
75 aShape1 = aPointRef->context()->shape();
78 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
83 double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
85 std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
86 std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
88 // These checks should be made to the GUI for the feature but
89 // the corresponding validator does not exist yet.
90 if (!aSphereAlgo->check()) {
91 setError(aSphereAlgo->getError());
98 // Check if the creation of the cylinder
99 if(!aSphereAlgo->isDone()) {
100 setError(aSphereAlgo->getError());
103 if(!aSphereAlgo->checkValid("Sphere builder")) {
104 setError(aSphereAlgo->getError());
108 int aResultIndex = 0;
109 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
110 loadNamingDS(aSphereAlgo, aResultBox);
111 setResult(aResultBox, aResultIndex);
114 //=================================================================================================
115 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
116 std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
119 theResultSphere->store(theSphereAlgo->shape());
121 // Prepare the naming
122 theSphereAlgo->prepareNamingFaces();
125 // Naming for faces and edges
126 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
127 theSphereAlgo->getCreatedFaces();
128 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
129 it != listOfFaces.end();
132 theResultSphere->generated((*it).second, (*it).first);
136 GeomAPI_DataMapOfShapeShape aVertices;
138 for (GeomAPI_ShapeExplorer aVertExp(theSphereAlgo->shape(), GeomAPI_Shape::VERTEX);
142 if (!aVertices.isBound(aVertExp.current())) {
143 std::ostringstream aStream;
144 aStream<<"Vertex_"<<anIndex++;
145 theResultSphere->generated(aVertExp.current(), aStream.str());
146 aVertices.bind(aVertExp.current(), aVertExp.current());