Salome HOME
ac429fb80ca0e1fd20843bc58a9c9c15b88baa45
[modules/shaper.git] / src / PrimitivesAPI / PrimitivesAPI_Sphere.cpp
1 // Copyright (C) 2017-2022  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:        PrimitivesAPI_Sphere.h
21 // Created:     16 Mar 2017
22 // Author:      Clarisse Genrault
23
24 #include "PrimitivesAPI_Sphere.h"
25
26 #include <ModelHighAPI_Dumper.h>
27 #include <ModelHighAPI_Selection.h>
28 #include <ModelHighAPI_Tools.h>
29
30 //==================================================================================================
31 PrimitivesAPI_Sphere::PrimitivesAPI_Sphere(const std::shared_ptr<ModelAPI_Feature>& theFeature)
32 : ModelHighAPI_Interface(theFeature)
33 {
34   initialize();
35 }
36
37 //==================================================================================================
38 PrimitivesAPI_Sphere::PrimitivesAPI_Sphere(const std::shared_ptr<ModelAPI_Feature>& theFeature,
39                                            const ModelHighAPI_Selection& theCenterPoint,
40                                            const ModelHighAPI_Double& theRadius)
41 : ModelHighAPI_Interface(theFeature)
42 {
43   if (initialize()) {
44     fillAttribute(PrimitivesPlugin_Sphere::CREATION_METHOD_BY_PT_RADIUS(), creationMethod());
45     fillAttribute(theCenterPoint, centerPoint());
46     setRadius(theRadius);
47   }
48 }
49
50 //==================================================================================================
51 PrimitivesAPI_Sphere::PrimitivesAPI_Sphere(const std::shared_ptr<ModelAPI_Feature>& theFeature,
52                                            const ModelHighAPI_Double& theRMin,
53                                            const ModelHighAPI_Double& theRMax,
54                                            const ModelHighAPI_Double& thePhiMin,
55                                            const ModelHighAPI_Double& thePhiMax,
56                                            const ModelHighAPI_Double& theThetaMin,
57                                            const ModelHighAPI_Double& theThetaMax)
58 : ModelHighAPI_Interface(theFeature)
59 {
60   if (initialize()) {
61     fillAttribute(PrimitivesPlugin_Sphere::CREATION_METHOD_BY_DIMENSIONS(), creationMethod());
62     fillAttribute(theRMin, rmin());
63     fillAttribute(theRMax, rmax());
64     fillAttribute(thePhiMin, phimin());
65     fillAttribute(thePhiMax, phimax());
66     fillAttribute(theThetaMin, thetamin());
67     fillAttribute(theThetaMax, thetamax());
68     execute();
69   }
70 }
71
72 //==================================================================================================
73 PrimitivesAPI_Sphere::~PrimitivesAPI_Sphere()
74 {
75 }
76
77 //==================================================================================================
78 void PrimitivesAPI_Sphere::setCenterPoint(const ModelHighAPI_Selection& theCenterPoint)
79 {
80   fillAttribute(theCenterPoint, centerPoint());
81   execute();
82 }
83
84 //==================================================================================================
85 void PrimitivesAPI_Sphere::setRadius(const ModelHighAPI_Double& theRadius)
86 {
87   fillAttribute(theRadius, radius());
88   execute();
89 }
90
91 //==================================================================================================
92 void PrimitivesAPI_Sphere::setRadius(const ModelHighAPI_Double& theRMin,
93                                      const ModelHighAPI_Double& theRMax)
94 {
95   fillAttribute(theRMin, rmin());
96   fillAttribute(theRMax, rmax());
97   execute();
98 }
99
100 //==================================================================================================
101 void PrimitivesAPI_Sphere::setPhi(const ModelHighAPI_Double& thePhiMin,
102                                   const ModelHighAPI_Double& thePhiMax)
103 {
104   fillAttribute(thePhiMin, phimin());
105   fillAttribute(thePhiMax, phimax());
106   execute();
107 }
108
109 //==================================================================================================
110 void PrimitivesAPI_Sphere::setTheta(const ModelHighAPI_Double& theThetaMin,
111                                     const ModelHighAPI_Double& theThetaMax)
112 {
113   fillAttribute(theThetaMin, thetamin());
114   fillAttribute(theThetaMax, thetamax());
115   execute();
116 }
117
118 //==================================================================================================
119 void PrimitivesAPI_Sphere::dump(ModelHighAPI_Dumper& theDumper) const
120 {
121   FeaturePtr aBase = feature();
122   const std::string& aDocName = theDumper.name(aBase->document());
123
124   theDumper << aBase << " = model.addSphere(" << aDocName;
125
126   std::string aCreationMethod = aBase->string(PrimitivesPlugin_Sphere::CREATION_METHOD())->value();
127
128   if (aCreationMethod == PrimitivesPlugin_Sphere::CREATION_METHOD_BY_DIMENSIONS()) {
129     AttributeDoublePtr anAttrRMin = aBase->real(PrimitivesPlugin_Sphere::RMIN_ID());
130     AttributeDoublePtr anAttrRMax = aBase->real(PrimitivesPlugin_Sphere::RMAX_ID());
131     AttributeDoublePtr anAttrPhiMin = aBase->real(PrimitivesPlugin_Sphere::PHIMIN_ID());
132     AttributeDoublePtr anAttrPhiMax = aBase->real(PrimitivesPlugin_Sphere::PHIMAX_ID());
133     AttributeDoublePtr anAttrThetaMin = aBase->real(PrimitivesPlugin_Sphere::THETAMIN_ID());
134     AttributeDoublePtr anAttrThetaMax = aBase->real(PrimitivesPlugin_Sphere::THETAMAX_ID());
135     theDumper << ", " << anAttrRMin << ", " << anAttrRMax;
136     theDumper << ", " << anAttrPhiMin << ", " << anAttrPhiMax;
137     theDumper << ", " << anAttrThetaMin << ", " << anAttrThetaMax;
138   } else { // CREATION_METHOD_BY_PT_RADIUS by default to support versions with undefined method
139     AttributeSelectionPtr anAttrCenterPoint =
140         aBase->selection(PrimitivesPlugin_Sphere::CENTER_POINT_ID());
141     AttributeDoublePtr anAttrRadius = aBase->real(PrimitivesPlugin_Sphere::RADIUS_ID());
142     // for old versions radius value was located in another place, so, use the default 10
143     double aRadValue = anAttrRadius->isInitialized() ? anAttrRadius->value() : 10;
144     theDumper << ", " << anAttrCenterPoint << ", " << aRadValue;
145   }
146
147   theDumper << ")" << std::endl;
148 }
149
150 //==================================================================================================
151 SpherePtr addSphere(const std::shared_ptr<ModelAPI_Document>& thePart,
152                     const ModelHighAPI_Selection& theCenterPoint,
153                     const ModelHighAPI_Double& theRadius)
154 {
155   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Sphere::ID());
156   return SpherePtr(new PrimitivesAPI_Sphere(aFeature, theCenterPoint, theRadius));
157 }
158
159 //==================================================================================================
160 SpherePtr addSphere(const std::shared_ptr<ModelAPI_Document>& thePart,
161                     const ModelHighAPI_Double& theRadius)
162 {
163   ModelHighAPI_Selection aCenterPoint("VERTEX", L"PartSet/Origin");
164   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Sphere::ID());
165   return SpherePtr(new PrimitivesAPI_Sphere(aFeature, aCenterPoint, theRadius));
166 }
167
168 //==================================================================================================
169 SpherePtr addSphere(const std::shared_ptr<ModelAPI_Document>& thePart,
170                     const ModelHighAPI_Double& theRMin,
171                     const ModelHighAPI_Double& theRMax,
172                     const ModelHighAPI_Double& thePhiMin,
173                     const ModelHighAPI_Double& thePhiMax,
174                     const ModelHighAPI_Double& theThetaMin,
175                     const ModelHighAPI_Double& theThetaMax)
176 {
177   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Sphere::ID());
178   return SpherePtr(new PrimitivesAPI_Sphere(aFeature, theRMin, theRMax, thePhiMin, thePhiMax,
179                                             theThetaMin, theThetaMax));
180 }