Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomData / GeomData_Point2D.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_Point2D.h"
21
22 #include <GeomAPI_Pnt2d.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_Point2D::GeomData_Point2D()
32 {
33   myIsInitialized = false;
34 }
35
36 void GeomData_Point2D::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_Point2D::reset()
46 {
47   myIsInitialized = false;
48   for(int aComponent = 0; aComponent < NUM_COMPONENTS; ++aComponent) {
49     myExpression[aComponent]->reset();
50   }
51 }
52
53 void GeomData_Point2D::setCalculatedValue(const double theX, const double theY)
54 {
55   if (!myIsInitialized || x() != theX || y() != theY) {
56     myExpression[0]->setValue(theX);
57     myExpression[1]->setValue(theY);
58     owner()->data()->sendAttributeUpdated(this);
59   }
60 }
61
62 void GeomData_Point2D::setValue(const double theX, const double theY)
63 {
64   setCalculatedValue(textX().empty() ? theX : x(),
65                      textY().empty() ? theY : y());
66 }
67
68 void GeomData_Point2D::setValue(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
69 {
70   setValue(thePoint->x(), thePoint->y());
71 }
72
73 double GeomData_Point2D::x() const
74 {
75   return myExpression[0]->value();
76 }
77
78 double GeomData_Point2D::y() const
79 {
80   return myExpression[1]->value();
81 }
82
83 std::shared_ptr<GeomAPI_Pnt2d> GeomData_Point2D::pnt()
84 {
85   std::shared_ptr<GeomAPI_Pnt2d> aResult(new GeomAPI_Pnt2d(x(), y()));
86   return aResult;
87 }
88
89 void GeomData_Point2D::setText(const std::wstring& theX,
90                                const std::wstring& theY)
91 {
92   if (!myIsInitialized && theX.empty() && theY.empty())
93     return; // empty strings are not good initializers
94   if (!myIsInitialized || textX() != theX || textY() != theY) {
95     myExpression[0]->setText(theX);
96     myExpression[1]->setText(theY);
97     // Send it to evaluator to convert into the double and store in the attribute
98     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
99     owner()->data()->sendAttributeUpdated(this);
100   }
101 }
102
103 std::wstring GeomData_Point2D::textX()
104 {
105   return myExpression[0]->text();
106 }
107 std::wstring GeomData_Point2D::textY()
108 {
109   return myExpression[1]->text();
110 }
111
112 void GeomData_Point2D::setExpressionInvalid(int theComponent, bool theFlag)
113 {
114   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
115   if (!myIsInitialized || expressionInvalid(theComponent) != theFlag)
116     myExpression[theComponent]->setInvalid(theFlag);
117 }
118
119 bool GeomData_Point2D::expressionInvalid(int theComponent)
120 {
121   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
122   return myExpression[theComponent]->isInvalid();
123 }
124
125 void GeomData_Point2D::setExpressionError(int theComponent, const std::string& theError)
126 {
127   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
128   if (expressionError(theComponent) != theError)
129     myExpression[theComponent]->setError(theError);
130 }
131
132 std::string GeomData_Point2D::expressionError(int theComponent)
133 {
134   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
135   return myExpression[theComponent]->error();
136 }
137
138 void GeomData_Point2D::setUsedParameters(int theComponent,
139                                          const std::set<std::wstring>& theUsedParameters)
140 {
141   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
142   myExpression[theComponent]->setUsedParameters(theUsedParameters);
143 }
144
145 std::set<std::wstring> GeomData_Point2D::usedParameters(int theComponent) const
146 {
147   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
148   return myExpression[theComponent]->usedParameters();
149 }