1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: PrimitivesPlugin_Torus.cpp
4 // Created: 17 Mar 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <PrimitivesPlugin_Torus.h>
9 #include <GeomAPI_Edge.h>
10 #include <GeomAPI_Lin.h>
12 #include <GeomAlgoAPI_PointBuilder.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultConstruction.h>
18 #include <ModelAPI_Session.h>
20 //=================================================================================================
21 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
25 //=================================================================================================
26 void PrimitivesPlugin_Torus::initAttributes()
28 data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
29 ModelAPI_AttributeSelection::typeId());
30 data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
31 ModelAPI_AttributeSelection::typeId());
33 data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
34 ModelAPI_AttributeDouble::typeId());
35 data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
36 ModelAPI_AttributeDouble::typeId());
38 // Initialize the base point of the torus at the origin if the base point is not filled.
39 AttributeSelectionPtr aCenterPoint =
40 data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
41 if (!aCenterPoint->isInitialized()) {
42 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
43 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
44 if (aPointObj.get()) {
45 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
46 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
50 // Initialize the axis at the OZ axis if the axis is not filled.
51 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
52 if (!anAxis->isInitialized()) {
53 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
54 ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
55 if (anAxisObj.get()) {
56 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
57 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
62 //=================================================================================================
63 void PrimitivesPlugin_Torus::execute()
65 // Getting base point.
66 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
67 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
68 selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
69 if (aPointRef.get() != NULL) {
70 GeomShapePtr aShape1 = aPointRef->value();
72 aShape1 = aPointRef->context()->shape();
75 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
80 std::shared_ptr<GeomAPI_Ax2> anAxis;
81 std::shared_ptr<GeomAPI_Edge> anEdge;
82 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
83 selection(PrimitivesPlugin_Torus::AXIS_ID());
84 if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
85 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
86 } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
87 anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
88 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
91 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
92 anEdge->line()->direction()));
95 // Getting radius and ring radius
96 double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
97 double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
99 std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
100 std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
104 // These checks should be made to the GUI for the feature but
105 // the corresponding validator does not exist yet.
106 if (!aTorusAlgo->check()) {
107 setError(aTorusAlgo->getError());
114 // Check if the creation of the cylinder
115 if(!aTorusAlgo->isDone()) {
116 setError(aTorusAlgo->getError());
119 if(!aTorusAlgo->checkValid("Torus builder")) {
120 setError(aTorusAlgo->getError());
124 int aResultIndex = 0;
125 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
126 loadNamingDS(aTorusAlgo, aResultBox);
127 setResult(aResultBox, aResultIndex);
130 //=================================================================================================
131 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
132 std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
135 theResultTorus->store(theTorusAlgo->shape());
137 // Prepare the naming
138 theTorusAlgo->prepareNamingFaces();
142 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
143 theTorusAlgo->getCreatedFaces();
144 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
145 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
146 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
147 theResultTorus->generated(aFace, (*it).first, num++);