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 <GeomAlgoAPI_PointBuilder.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_Session.h>
17 //=================================================================================================
18 PrimitivesPlugin_Sphere::PrimitivesPlugin_Sphere()
22 //=================================================================================================
23 void PrimitivesPlugin_Sphere::initAttributes()
25 data()->addAttribute(PrimitivesPlugin_Sphere::CENTER_POINT_ID(),
26 ModelAPI_AttributeSelection::typeId());
28 data()->addAttribute(PrimitivesPlugin_Sphere::RADIUS_ID(),
29 ModelAPI_AttributeDouble::typeId());
31 // Initialize the center point of the sphere at the origin if the center point is not filled.
32 AttributeSelectionPtr aCenterPoint =
33 data()->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
34 if (!aCenterPoint->isInitialized()) {
35 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
36 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
37 if (aPointObj.get()) {
38 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
39 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
44 //=================================================================================================
45 void PrimitivesPlugin_Sphere::execute()
48 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
49 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
50 selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
51 if (aPointRef.get() != NULL) {
52 GeomShapePtr aShape1 = aPointRef->value();
54 aShape1 = aPointRef->context()->shape();
57 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
62 double aRadius = real(PrimitivesPlugin_Sphere::RADIUS_ID())->value();
64 std::shared_ptr<GeomAlgoAPI_Sphere> aSphereAlgo =
65 std::shared_ptr<GeomAlgoAPI_Sphere>(new GeomAlgoAPI_Sphere(aCenterPoint, aRadius));
67 // These checks should be made to the GUI for the feature but
68 // the corresponding validator does not exist yet.
69 if (!aSphereAlgo->check()) {
70 setError(aSphereAlgo->getError());
77 // Check if the creation of the cylinder
78 if(!aSphereAlgo->isDone()) {
79 setError(aSphereAlgo->getError());
82 if(!aSphereAlgo->checkValid("Sphere builder")) {
83 setError(aSphereAlgo->getError());
88 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
89 loadNamingDS(aSphereAlgo, aResultBox);
90 setResult(aResultBox, aResultIndex);
93 //=================================================================================================
94 void PrimitivesPlugin_Sphere::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Sphere> theSphereAlgo,
95 std::shared_ptr<ModelAPI_ResultBody> theResultSphere)
98 theResultSphere->store(theSphereAlgo->shape());
100 // Prepare the naming
101 theSphereAlgo->prepareNamingFaces();
105 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
106 theSphereAlgo->getCreatedFaces();
107 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
108 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
109 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
110 theResultSphere->generated(aFace, (*it).first, num++);