Salome HOME
updated copyright message
[modules/shaper.git] / src / PrimitivesPlugin / PrimitivesPlugin_Torus.cpp
1 // Copyright (C) 2017-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        PrimitivesPlugin_Torus.cpp
21 // Created:     17 Mar 2017
22 // Author:      Clarisse Genrault (CEA)
23
24 #include <PrimitivesPlugin_Torus.h>
25
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
30
31 #include <GeomAlgoAPI_PointBuilder.h>
32
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>
38
39 #include <sstream>
40
41 //=================================================================================================
42 PrimitivesPlugin_Torus::PrimitivesPlugin_Torus()
43 {
44 }
45
46 //=================================================================================================
47 void PrimitivesPlugin_Torus::initAttributes()
48 {
49   data()->addAttribute(PrimitivesPlugin_Torus::BASE_POINT_ID(),
50                        ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(PrimitivesPlugin_Torus::AXIS_ID(),
52                        ModelAPI_AttributeSelection::typeId());
53
54   data()->addAttribute(PrimitivesPlugin_Torus::RADIUS_ID(),
55                        ModelAPI_AttributeDouble::typeId());
56   data()->addAttribute(PrimitivesPlugin_Torus::RING_RADIUS_ID(),
57                        ModelAPI_AttributeDouble::typeId());
58
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>());
68     }
69   }
70
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>());
79     }
80   }
81 }
82
83 //=================================================================================================
84 void PrimitivesPlugin_Torus::execute()
85 {
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();
92     if (!aShape1.get()) {
93       aShape1 = aPointRef->context()->shape();
94     }
95     if (aShape1) {
96         aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
97     }
98   }
99
100   // Getting axis.
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();
104   if (!aShape.get()) {
105     if (anEdgeRef->context().get()) {
106       aShape = anEdgeRef->context()->shape();
107     }
108   }
109   if (!aShape.get()) {
110     setError(aSelectionError);
111     return;
112   }
113   std::shared_ptr<GeomAPI_Edge> anEdge;
114   if (aShape->isEdge())
115   {
116     anEdge = aShape->edge();
117   }
118   else if (aShape->isCompound())
119   {
120     GeomAPI_ShapeIterator anIt(aShape);
121     anEdge = anIt.current()->edge();
122   }
123   else
124   {
125     setError(aSelectionError);
126     return;
127   }
128
129   if (!anEdge.get())
130   {
131     setError(aSelectionError);
132     return;
133   }
134
135   std::shared_ptr<GeomAPI_Ax2> anAxis(new GeomAPI_Ax2(aBasePoint,
136                                                       anEdge->line()->direction()));
137
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();
141
142   std::shared_ptr<GeomAlgoAPI_Torus> aTorusAlgo =
143     std::shared_ptr<GeomAlgoAPI_Torus>(new GeomAlgoAPI_Torus(anAxis,
144                                                              aRadius,
145                                                              aRingRadius));
146
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());
151     return;
152   }
153
154   // Build the sphere
155   aTorusAlgo->build();
156
157   // Check if the creation of the cylinder
158   if(!aTorusAlgo->isDone()) {
159     setError(aTorusAlgo->getError());
160     return;
161   }
162   if(!aTorusAlgo->checkValid("Torus builder")) {
163     setError(aTorusAlgo->getError());
164     return;
165   }
166
167   int aResultIndex = 0;
168   ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
169   loadNamingDS(aTorusAlgo, aResultBox);
170   setResult(aResultBox, aResultIndex);
171 }
172
173 //=================================================================================================
174 void PrimitivesPlugin_Torus::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Torus> theTorusAlgo,
175                                           std::shared_ptr<ModelAPI_ResultBody> theResultTorus)
176 {
177   // Load the result
178   theResultTorus->store(theTorusAlgo->shape());
179
180   // Prepare the naming
181   theTorusAlgo->prepareNamingFaces();
182
183   // Insert to faces
184   // Naming for faces
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();
189        ++it)
190   {
191     theResultTorus->generated((*it).second, (*it).first);
192   }
193
194   // Naming of edges
195   GeomAPI_DataMapOfShapeShape anEdges;
196   int anIndex = 1;
197   for (GeomAPI_ShapeExplorer anEdgeExp(theTorusAlgo->shape(), GeomAPI_Shape::EDGE);
198        anEdgeExp.more();
199        anEdgeExp.next())
200   {
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());
206     }
207   }
208 }