Salome HOME
6e028ff1c929de901006e0be3324bdbce933c9c1
[modules/shaper.git] / src / GeomData / GeomData_Point.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 "GeomData_Point.h"
21
22 #include <GeomAPI_Pnt.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Expression.h>
27 #include <ModelAPI_Feature.h>
28
29 #include <cassert>
30
31 GeomData_Point::GeomData_Point()
32 {
33   myIsInitialized = false;
34 }
35
36 void GeomData_Point::reinit()
37 {
38   myIsInitialized = true;
39   for (int aComponent = 0; aComponent < NUM_COMPONENTS; ++aComponent) {
40     myExpression[aComponent]->reinit();
41     myIsInitialized = myIsInitialized && myExpression[aComponent]->isInitialized();
42   }
43 }
44
45 void GeomData_Point::setCalculatedValue(const double theX, const double theY, const double theZ)
46 {
47   if (!myIsInitialized || x() != theX || y() != theY || z() != theZ) {
48     myExpression[0]->setValue(theX);
49     myExpression[1]->setValue(theY);
50     myExpression[2]->setValue(theZ);
51     owner()->data()->sendAttributeUpdated(this);
52   }
53 }
54
55 void GeomData_Point::setValue(const double theX, const double theY, const double theZ)
56 {
57   setCalculatedValue(textX().empty() ? theX : x(),
58                      textY().empty() ? theY : y(),
59                      textZ().empty() ? theZ : z());
60 }
61
62 void GeomData_Point::setValue(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
63 {
64   setValue(thePoint->x(), thePoint->y(), thePoint->z());
65 }
66
67 double GeomData_Point::x() const
68 {
69   return myExpression[0]->value();
70 }
71
72 double GeomData_Point::y() const
73 {
74   return myExpression[1]->value();
75 }
76
77 double GeomData_Point::z() const
78 {
79   return myExpression[2]->value();
80 }
81
82 void GeomData_Point::setX(const double theX)
83 {
84   if (!myIsInitialized) {
85     setCalculatedValue(theX, 0, 0);
86   } else if (x() != theX) {
87     myExpression[0]->setValue(theX);
88     myExpression[0]->setText(L""); // uninitialize the text
89     owner()->data()->sendAttributeUpdated(this);
90   }
91 }
92
93 void GeomData_Point::setY(const double theY)
94 {
95   if (!myIsInitialized) {
96     setCalculatedValue(0, theY, 0);
97   } else if (y() != theY) {
98     myExpression[1]->setValue(theY);
99     myExpression[1]->setText(L""); // uninitialize the text
100     owner()->data()->sendAttributeUpdated(this);
101   }
102 }
103
104 void GeomData_Point::setZ(const double theZ)
105 {
106   if (!myIsInitialized) {
107     setCalculatedValue(0, 0, theZ);
108   }
109   else if (z() != theZ) {
110     myExpression[2]->setValue(theZ);
111     myExpression[2]->setText(L""); // uninitialize the text
112     owner()->data()->sendAttributeUpdated(this);
113   }
114 }
115
116
117 std::shared_ptr<GeomAPI_Pnt> GeomData_Point::pnt()
118 {
119   std::shared_ptr<GeomAPI_Pnt> aResult(new GeomAPI_Pnt(x(), y(), z()));
120   return aResult;
121 }
122
123 void GeomData_Point::setText(const std::wstring& theX,
124                              const std::wstring& theY,
125                              const std::wstring& theZ)
126 {
127   if (!myIsInitialized || textX() != theX || textY() != theY || textZ() != theZ) {
128     myExpression[0]->setText(theX);
129     myExpression[1]->setText(theY);
130     myExpression[2]->setText(theZ);
131     // Send it to evaluator to convert into the double and store in the attribute
132     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
133     owner()->data()->sendAttributeUpdated(this);
134   }
135 }
136
137 void GeomData_Point::setTextX(const std::wstring& theX)
138 {
139   if (!myIsInitialized) {
140     static const std::wstring aDefaultText = L"0";
141     setText(theX, aDefaultText, aDefaultText);
142   }
143   else if (textX() != theX) {
144     myExpression[0]->setText(theX);
145     // Send it to evaluator to convert into the double and store in the attribute
146     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
147     owner()->data()->sendAttributeUpdated(this);
148   }
149 }
150
151 void GeomData_Point::setTextY(const std::wstring& theY)
152 {
153   if (!myIsInitialized) {
154     static const std::wstring aDefaultText = L"0";
155     setText(aDefaultText, theY, aDefaultText);
156   }
157   else if (textY() != theY) {
158     myExpression[1]->setText(theY);
159     // Send it to evaluator to convert into the double and store in the attribute
160     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
161     owner()->data()->sendAttributeUpdated(this);
162   }
163 }
164
165 void GeomData_Point::setTextZ(const std::wstring& theZ)
166 {
167   if (!myIsInitialized) {
168     static const std::wstring aDefaultText = L"0";
169     setText(aDefaultText, aDefaultText, theZ);
170   }
171   else if (textZ() != theZ) {
172     myExpression[2]->setText(theZ);
173     // Send it to evaluator to convert into the double and store in the attribute
174     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
175     owner()->data()->sendAttributeUpdated(this);
176   }
177 }
178
179 std::wstring GeomData_Point::textX()
180 {
181   return myExpression[0]->text();
182 }
183 std::wstring GeomData_Point::textY()
184 {
185   return myExpression[1]->text();
186 }
187 std::wstring GeomData_Point::textZ()
188 {
189   return myExpression[2]->text();
190 }
191
192 void GeomData_Point::setExpressionInvalid(int theComponent, bool theFlag)
193 {
194   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
195   if (!myIsInitialized || expressionInvalid(theComponent) != theFlag)
196     myExpression[theComponent]->setInvalid(theFlag);
197 }
198
199 bool GeomData_Point::expressionInvalid(int theComponent)
200 {
201   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
202   return myExpression[theComponent]->isInvalid();
203 }
204
205 void GeomData_Point::setExpressionError(int theComponent, const std::string& theError)
206 {
207   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
208   if (expressionError(theComponent) != theError)
209     myExpression[theComponent]->setError(theError);
210 }
211
212 std::string GeomData_Point::expressionError(int theComponent)
213 {
214   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
215   return myExpression[theComponent]->error();
216 }
217
218 void GeomData_Point::setUsedParameters(int theComponent,
219                                        const std::set<std::wstring>& theUsedParameters)
220 {
221   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
222   myExpression[theComponent]->setUsedParameters(theUsedParameters);
223 }
224
225 std::set<std::wstring> GeomData_Point::usedParameters(int theComponent) const
226 {
227   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
228   return myExpression[theComponent]->usedParameters();
229 }