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>
12 #include <GeomAPI_ShapeIterator.h>
14 #include <GeomAlgoAPI_PointBuilder.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_ResultBody.h>
19 #include <ModelAPI_ResultConstruction.h>
20 #include <ModelAPI_Session.h>
24 //=================================================================================================
25 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
29 //=================================================================================================
30 void PrimitivesPlugin_Torus::initAttributes()
32 data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
33 ModelAPI_AttributeSelection::typeId());
34 data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
35 ModelAPI_AttributeSelection::typeId());
37 data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
38 ModelAPI_AttributeDouble::typeId());
39 data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
40 ModelAPI_AttributeDouble::typeId());
42 // Initialize the base point of the torus at the origin if the base point is not filled.
43 AttributeSelectionPtr aCenterPoint =
44 data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
45 if (!aCenterPoint->isInitialized()) {
46 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
47 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
48 if (aPointObj.get()) {
49 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
50 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
54 // Initialize the axis at the OZ axis if the axis is not filled.
55 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
56 if (!anAxis->isInitialized()) {
57 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
58 ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
59 if (anAxisObj.get()) {
60 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
61 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
66 //=================================================================================================
67 void PrimitivesPlugin_Torus::execute()
69 // Getting base point.
70 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
71 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
72 selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
73 if (aPointRef.get() != NULL) {
74 GeomShapePtr aShape1 = aPointRef->value();
76 aShape1 = aPointRef->context()->shape();
79 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
84 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
85 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef = selection(AXIS_ID());
86 GeomShapePtr aShape = anEdgeRef->value();
88 if (anEdgeRef->context().get()) {
89 aShape = anEdgeRef->context()->shape();
93 setError(aSelectionError);
96 std::shared_ptr<GeomAPI_Edge> anEdge;
99 anEdge = aShape->edge();
101 else if (aShape->isCompound())
103 GeomAPI_ShapeIterator anIt(aShape);
104 anEdge = anIt.current()->edge();
108 setError(aSelectionError);
114 setError(aSelectionError);
118 std::shared_ptr<GeomAPI_Ax2> anAxis(new GeomAPI_Ax2(aBasePoint,
119 anEdge->line()->direction()));
121 // Getting radius and ring radius
122 double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
123 double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
125 std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
126 std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
130 // These checks should be made to the GUI for the feature but
131 // the corresponding validator does not exist yet.
132 if (!aTorusAlgo->check()) {
133 setError(aTorusAlgo->getError());
140 // Check if the creation of the cylinder
141 if(!aTorusAlgo->isDone()) {
142 setError(aTorusAlgo->getError());
145 if(!aTorusAlgo->checkValid("Torus builder")) {
146 setError(aTorusAlgo->getError());
150 int aResultIndex = 0;
151 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
152 loadNamingDS(aTorusAlgo, aResultBox);
153 setResult(aResultBox, aResultIndex);
156 //=================================================================================================
157 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
158 std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
161 theResultTorus->store(theTorusAlgo->shape());
163 // Prepare the naming
164 theTorusAlgo->prepareNamingFaces();
169 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
170 theTorusAlgo->getCreatedFaces();
171 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
172 it != listOfFaces.end();
175 theResultTorus->generated((*it).second, (*it).first);
179 GeomAPI_DataMapOfShapeShape anEdges;
181 for (GeomAPI_ShapeExplorer anEdgeExp(theTorusAlgo->shape(), GeomAPI_Shape::EDGE);
185 if (!anEdges.isBound(anEdgeExp.current())) {
186 std::ostringstream aStream;
187 aStream<<"Edge_"<<anIndex++;
188 theResultTorus->generated(anEdgeExp.current(), aStream.str());
189 anEdges.bind(anEdgeExp.current(), anEdgeExp.current());