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