Salome HOME
updated copyright message
[modules/shaper.git] / src / PrimitivesAPI / PrimitivesAPI_Box.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 #include "PrimitivesAPI_Box.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 //==================================================================================================
26 PrimitivesAPI_Box::PrimitivesAPI_Box(const std::shared_ptr<ModelAPI_Feature>& theFeature)
27 : ModelHighAPI_Interface(theFeature)
28 {
29   initialize();
30 }
31
32 //==================================================================================================
33 PrimitivesAPI_Box::PrimitivesAPI_Box(const std::shared_ptr<ModelAPI_Feature>& theFeature,
34                                      const ModelHighAPI_Double & theDx,
35                                      const ModelHighAPI_Double & theDy,
36                                      const ModelHighAPI_Double & theDz)
37 : ModelHighAPI_Interface(theFeature)
38 {
39   if (initialize())
40     setDimensions(theDx, theDy, theDz);
41 }
42
43 //==================================================================================================
44 PrimitivesAPI_Box::PrimitivesAPI_Box(const std::shared_ptr<ModelAPI_Feature>& theFeature,
45                                      const ModelHighAPI_Selection& theFirstPoint,
46                                      const ModelHighAPI_Selection& theSecondPoint)
47 : ModelHighAPI_Interface(theFeature)
48 {
49   if (initialize())
50     setPoints(theFirstPoint, theSecondPoint);
51 }
52
53 //==================================================================================================
54 PrimitivesAPI_Box::PrimitivesAPI_Box(const std::shared_ptr<ModelAPI_Feature>& theFeature,
55                                      const ModelHighAPI_Double& theOx,
56                                      const ModelHighAPI_Double& theOy,
57                                      const ModelHighAPI_Double& theOz,
58                                      const ModelHighAPI_Double& theHalfX,
59                                      const ModelHighAPI_Double& theHalfY,
60                                      const ModelHighAPI_Double& theHalfZ)
61 : ModelHighAPI_Interface(theFeature)
62 {
63   if (initialize())
64   {
65     fillAttribute(PrimitivesPlugin_Box::CREATION_METHOD_BY_ONE_POINT_AND_DIMS(), creationMethod());
66     fillAttribute(theOx, ox());
67     fillAttribute(theOy, oy());
68     fillAttribute(theOz, oz());
69     fillAttribute(theHalfX, halfdx());
70     fillAttribute(theHalfY, halfdy());
71     fillAttribute(theHalfZ, halfdz());
72     execute();
73   }
74 }
75
76 //==================================================================================================
77 PrimitivesAPI_Box::~PrimitivesAPI_Box()
78 {
79
80 }
81
82 //==================================================================================================
83 void PrimitivesAPI_Box::setDimensions(const ModelHighAPI_Double& theDx,
84                                       const ModelHighAPI_Double& theDy,
85                                       const ModelHighAPI_Double& theDz)
86 {
87   fillAttribute(PrimitivesPlugin_Box::CREATION_METHOD_BY_DIMENSIONS(), creationMethod());
88   fillAttribute(theDx, dx());
89   fillAttribute(theDy, dy());
90   fillAttribute(theDz, dz());
91
92   execute();
93 }
94
95 //==================================================================================================
96 void PrimitivesAPI_Box::setPoints(const ModelHighAPI_Selection& theFirstPoint,
97                                   const ModelHighAPI_Selection& theSecondPoint)
98 {
99   fillAttribute(PrimitivesPlugin_Box::CREATION_METHOD_BY_TWO_POINTS(), creationMethod());
100   fillAttribute(theFirstPoint, firstPoint());
101   fillAttribute(theSecondPoint, secondPoint());
102
103   execute();
104 }
105
106 //==================================================================================================
107 void PrimitivesAPI_Box::setOrigin(const ModelHighAPI_Double& theOx,
108                                   const ModelHighAPI_Double& theOy,
109                                   const ModelHighAPI_Double& theOz)
110 {
111   fillAttribute(theOx, ox());
112   fillAttribute(theOy, oy());
113   fillAttribute(theOz, oz());
114
115   execute();
116 }
117
118 //==================================================================================================
119 void PrimitivesAPI_Box::setHalfLengths(const ModelHighAPI_Double& theHalfLengthX,
120                                        const ModelHighAPI_Double& theHalfLengthY,
121                                        const ModelHighAPI_Double& theHalfLengthZ)
122 {
123   fillAttribute(theHalfLengthX, halfdx());
124   fillAttribute(theHalfLengthY, halfdy());
125   fillAttribute(theHalfLengthZ, halfdz());
126
127   execute();
128 }
129
130 //==================================================================================================
131 void PrimitivesAPI_Box::dump(ModelHighAPI_Dumper& theDumper) const
132 {
133   FeaturePtr aBase = feature();
134   const std::string& aDocName = theDumper.name(aBase->document());
135
136   theDumper << aBase << " = model.addBox(" << aDocName;
137
138   std::string aCreationMethod = aBase->string(PrimitivesPlugin_Box::CREATION_METHOD())->value();
139
140   if(aCreationMethod == PrimitivesPlugin_Box::CREATION_METHOD_BY_DIMENSIONS()) {
141     AttributeDoublePtr anAttrDx = aBase->real(PrimitivesPlugin_Box::DX_ID());
142     AttributeDoublePtr anAttrDy = aBase->real(PrimitivesPlugin_Box::DY_ID());
143     AttributeDoublePtr anAttrDz = aBase->real(PrimitivesPlugin_Box::DZ_ID());
144     theDumper << ", " << anAttrDx << ", " << anAttrDy << ", " << anAttrDz;
145   } else if (aCreationMethod == PrimitivesPlugin_Box::CREATION_METHOD_BY_TWO_POINTS()) {
146     AttributeSelectionPtr anAttrFirstPnt =
147       aBase->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
148     AttributeSelectionPtr anAttrSecondPnt =
149       aBase->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
150     theDumper << ", " << anAttrFirstPnt << ", " << anAttrSecondPnt;
151   } else if (aCreationMethod == PrimitivesPlugin_Box::CREATION_METHOD_BY_ONE_POINT_AND_DIMS()) {
152     AttributeDoublePtr anAttrOx = aBase->real(PrimitivesPlugin_Box::OX_ID());
153     AttributeDoublePtr anAttrOy = aBase->real(PrimitivesPlugin_Box::OY_ID());
154     AttributeDoublePtr anAttrOz = aBase->real(PrimitivesPlugin_Box::OZ_ID());
155     AttributeDoublePtr anAttrHalfLengthX = aBase->real(PrimitivesPlugin_Box::HALF_DX_ID());
156     AttributeDoublePtr anAttrHalfLengthY = aBase->real(PrimitivesPlugin_Box::HALF_DY_ID());
157     AttributeDoublePtr anAttrHalfLengthZ = aBase->real(PrimitivesPlugin_Box::HALF_DZ_ID());
158     theDumper << ", " << anAttrOx << ", " << anAttrOy << ", " << anAttrOz;
159     theDumper << ", " << anAttrHalfLengthX << ", " << anAttrHalfLengthY;
160     theDumper << ", " << anAttrHalfLengthZ;
161   }
162
163   theDumper << ")" << std::endl;
164 }
165
166 //==================================================================================================
167 BoxPtr addBox(const std::shared_ptr<ModelAPI_Document>& thePart,
168               const ModelHighAPI_Double& theDx,
169               const ModelHighAPI_Double& theDy,
170               const ModelHighAPI_Double& theDz)
171 {
172   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Box::ID());
173   return BoxPtr(new PrimitivesAPI_Box(aFeature, theDx, theDy, theDz));
174 }
175
176 //==================================================================================================
177 BoxPtr addBox(const std::shared_ptr<ModelAPI_Document>& thePart,
178               const ModelHighAPI_Selection& theFirstPoint,
179               const ModelHighAPI_Selection& theSecondPoint)
180 {
181   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Box::ID());
182   return BoxPtr(new PrimitivesAPI_Box(aFeature, theFirstPoint, theSecondPoint));
183 }
184
185 //==================================================================================================
186 BoxPtr addBox(const std::shared_ptr<ModelAPI_Document>& thePart,
187               const ModelHighAPI_Double& theOx,
188               const ModelHighAPI_Double& theOy,
189               const ModelHighAPI_Double& theOz,
190               const ModelHighAPI_Double& theHalfLengthX,
191               const ModelHighAPI_Double& theHalfLengthY,
192               const ModelHighAPI_Double& theHalfLengthZ)
193 {
194   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PrimitivesAPI_Box::ID());
195   return BoxPtr(new PrimitivesAPI_Box(aFeature, theOx, theOy, theOz, theHalfLengthX,
196                                       theHalfLengthY, theHalfLengthZ));
197 }