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>
11 #include <GeomAPI_ShapeExplorer.h>
13 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeSelection.h>
17 #include <ModelAPI_ResultBody.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
23 //=================================================================================================
24 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
28 //=================================================================================================
29 void PrimitivesPlugin_Torus::initAttributes()
31 data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
32 ModelAPI_AttributeSelection::typeId());
33 data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
34 ModelAPI_AttributeSelection::typeId());
36 data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
37 ModelAPI_AttributeDouble::typeId());
38 data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
39 ModelAPI_AttributeDouble::typeId());
41 // Initialize the base point of the torus at the origin if the base point is not filled.
42 AttributeSelectionPtr aCenterPoint =
43 data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
44 if (!aCenterPoint->isInitialized()) {
45 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
46 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
47 if (aPointObj.get()) {
48 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
49 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
53 // Initialize the axis at the OZ axis if the axis is not filled.
54 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
55 if (!anAxis->isInitialized()) {
56 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
57 ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
58 if (anAxisObj.get()) {
59 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
60 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
65 //=================================================================================================
66 void PrimitivesPlugin_Torus::execute()
68 // Getting base point.
69 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
70 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
71 selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
72 if (aPointRef.get() != NULL) {
73 GeomShapePtr aShape1 = aPointRef->value();
75 aShape1 = aPointRef->context()->shape();
78 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
83 std::shared_ptr<GeomAPI_Ax2> anAxis;
84 std::shared_ptr<GeomAPI_Edge> anEdge;
85 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
86 selection(PrimitivesPlugin_Torus::AXIS_ID());
87 if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
88 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
89 } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
90 anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
91 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
94 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
95 anEdge->line()->direction()));
98 // Getting radius and ring radius
99 double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
100 double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
102 std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
103 std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
107 // These checks should be made to the GUI for the feature but
108 // the corresponding validator does not exist yet.
109 if (!aTorusAlgo->check()) {
110 setError(aTorusAlgo->getError());
117 // Check if the creation of the cylinder
118 if(!aTorusAlgo->isDone()) {
119 setError(aTorusAlgo->getError());
122 if(!aTorusAlgo->checkValid("Torus builder")) {
123 setError(aTorusAlgo->getError());
127 int aResultIndex = 0;
128 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
129 loadNamingDS(aTorusAlgo, aResultBox);
130 setResult(aResultBox, aResultIndex);
133 //=================================================================================================
134 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
135 std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
138 theResultTorus->store(theTorusAlgo->shape());
140 // Prepare the naming
141 theTorusAlgo->prepareNamingFaces();
146 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
147 theTorusAlgo->getCreatedFaces();
148 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
149 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
150 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
151 theResultTorus->generated(aFace, (*it).first, num++);
155 GeomAPI_DataMapOfShapeShape anEdges;
156 GeomAPI_ShapeExplorer anEdgeExp(theTorusAlgo->shape(), GeomAPI_Shape::EDGE);
157 for(int anIndex = 1; anEdgeExp.more(); anEdgeExp.next()) {
158 if (!anEdges.isBound(anEdgeExp.current())) {
159 std::ostringstream aStream;
160 aStream<<"Edge_"<<anIndex++;
161 theResultTorus->generated(anEdgeExp.current(), aStream.str(), num++);
162 anEdges.bind(anEdgeExp.current(), anEdgeExp.current());