1 // Copyright (C) 2017-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: PrimitivesPlugin_Torus.cpp
21 // Created: 17 Mar 2017
22 // Author: Clarisse Genrault (CEA)
24 #include <PrimitivesPlugin_Torus.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAlgoAPI_PointBuilder.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultConstruction.h>
37 #include <ModelAPI_Session.h>
41 //=================================================================================================
42 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
46 //=================================================================================================
47 void PrimitivesPlugin_Torus::initAttributes()
49 data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
50 ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
52 ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
55 ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
57 ModelAPI_AttributeDouble::typeId());
59 // Initialize the base point of the torus at the origin if the base point is not filled.
60 AttributeSelectionPtr aCenterPoint =
61 data()->selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
62 if (!aCenterPoint->isInitialized()) {
63 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
64 ->objectByName(ModelAPI_ResultConstruction::group(), L"Origin");
65 if (aPointObj.get()) {
66 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
67 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
71 // Initialize the axis at the OZ axis if the axis is not filled.
72 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Torus::AXIS_ID());
73 if (!anAxis->isInitialized()) {
74 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
75 ->objectByName(ModelAPI_ResultConstruction::group(), L"OZ");
76 if (anAxisObj.get()) {
77 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
78 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
83 //=================================================================================================
84 void PrimitivesPlugin_Torus::execute()
86 // Getting base point.
87 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
88 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
89 selection(PrimitivesPlugin_Torus::BASE_POINT_ID());
90 if (aPointRef.get() != NULL) {
91 GeomShapePtr aShape1 = aPointRef->value();
93 aShape1 = aPointRef->context()->shape();
96 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
101 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
102 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef = selection(AXIS_ID());
103 GeomShapePtr aShape = anEdgeRef->value();
105 if (anEdgeRef->context().get()) {
106 aShape = anEdgeRef->context()->shape();
110 setError(aSelectionError);
113 std::shared_ptr<GeomAPI_Edge> anEdge;
114 if (aShape->isEdge())
116 anEdge = aShape->edge();
118 else if (aShape->isCompound())
120 GeomAPI_ShapeIterator anIt(aShape);
121 anEdge = anIt.current()->edge();
125 setError(aSelectionError);
131 setError(aSelectionError);
135 std::shared_ptr<GeomAPI_Ax2> anAxis(new GeomAPI_Ax2(aBasePoint,
136 anEdge->line()->direction()));
138 // Getting radius and ring radius
139 double aRadius = real(PrimitivesPlugin_Torus::RADIUS_ID())->value();
140 double aRingRadius = real(PrimitivesPlugin_Torus::RING_RADIUS_ID())->value();
142 std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
143 std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
147 // These checks should be made to the GUI for the feature but
148 // the corresponding validator does not exist yet.
149 if (!aTorusAlgo->check()) {
150 setError(aTorusAlgo->getError());
157 // Check if the creation of the cylinder
158 if(!aTorusAlgo->isDone()) {
159 setError(aTorusAlgo->getError());
162 if(!aTorusAlgo->checkValid("Torus builder")) {
163 setError(aTorusAlgo->getError());
167 int aResultIndex = 0;
168 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
169 loadNamingDS(aTorusAlgo, aResultBox);
170 setResult(aResultBox, aResultIndex);
173 //=================================================================================================
174 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
175 std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
178 theResultTorus->store(theTorusAlgo->shape());
180 // Prepare the naming
181 theTorusAlgo->prepareNamingFaces();
185 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
186 theTorusAlgo->getCreatedFaces();
187 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
188 it != listOfFaces.end();
191 theResultTorus->generated((*it).second, (*it).first);
195 GeomAPI_DataMapOfShapeShape anEdges;
197 for (GeomAPI_ShapeExplorer anEdgeExp(theTorusAlgo->shape(), GeomAPI_Shape::EDGE);
201 if (!anEdges.isBound(anEdgeExp.current())) {
202 std::ostringstream aStream;
203 aStream<<"Edge_"<<anIndex++;
204 theResultTorus->generated(anEdgeExp.current(), aStream.str());
205 anEdges.bind(anEdgeExp.current(), anEdgeExp.current());