1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: PrimitivesPlugin_Sphere.h
4 // Created: 15 Mar 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <PrimitivesPlugin_Sphere.h>
9 #include <GeomAPI_ShapeExplorer.h>
11 #include <GeomAlgoAPI_PointBuilder.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_Session.h>
21 //=================================================================================================
22 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
26 //=================================================================================================
27 void PrimitivesPlugin_Sphere::initAttributes()
29 data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
30 ModelAPI_AttributeSelection::typeId());
32 data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
33 ModelAPI_AttributeDouble::typeId());
35 // Initialize the center point of the sphere at the origin if the center point is not filled.
36 AttributeSelectionPtr aCenterPoint =
37 data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
38 if (!aCenterPoint->isInitialized()) {
39 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
40 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
41 if (aPointObj.get()) {
42 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
43 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
48 //=================================================================================================
49 void PrimitivesPlugin_Sphere::execute()
52 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
53 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
54 selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
55 if (aPointRef.get() != NULL) {
56 GeomShapePtr aShape1 = aPointRef->value();
58 aShape1 = aPointRef->context()->shape();
61 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
66 double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
68 std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
69 std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
71 // These checks should be made to the GUI for the feature but
72 // the corresponding validator does not exist yet.
73 if (!aSphereAlgo->check()) {
74 setError(aSphereAlgo->getError());
81 // Check if the creation of the cylinder
82 if(!aSphereAlgo->isDone()) {
83 setError(aSphereAlgo->getError());
86 if(!aSphereAlgo->checkValid("Sphere builder")) {
87 setError(aSphereAlgo->getError());
92 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
93 loadNamingDS(aSphereAlgo, aResultBox);
94 setResult(aResultBox, aResultIndex);
97 //=================================================================================================
98 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
99 std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
102 theResultSphere->store(theSphereAlgo->shape());
104 // Prepare the naming
105 theSphereAlgo->prepareNamingFaces();
108 // Naming for faces and edges
109 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
110 theSphereAlgo->getCreatedFaces();
111 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
112 it != listOfFaces.end();
115 theResultSphere->generated((*it).second, (*it).first);
119 GeomAPI_DataMapOfShapeShape aVertices;
121 for (GeomAPI_ShapeExplorer aVertExp(theSphereAlgo->shape(), GeomAPI_Shape::VERTEX);
125 if (!aVertices.isBound(aVertExp.current())) {
126 std::ostringstream aStream;
127 aStream<<"Vertex_"<<anIndex++;
128 theResultSphere->generated(aVertExp.current(), aStream.str());
129 aVertices.bind(aVertExp.current(), aVertExp.current());