Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / GeomData / GeomData_Point2D.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomData_Point2D.cxx
4 // Created:     24 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomData_Point2D.h"
8
9 #include <GeomAPI_Pnt2d.h>
10
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_Expression.h>
14 #include <ModelAPI_Feature.h>
15
16 #include <cassert>
17
18 GeomData_Point2D::GeomData_Point2D()
19 {
20   myIsInitialized = false;
21 }
22
23 void GeomData_Point2D::reinit()
24 {
25   myIsInitialized = true;
26   for (int aComponent = 0; aComponent < NUM_COMPONENTS; ++aComponent) {
27     myExpression[aComponent]->reinit();
28     myIsInitialized = myIsInitialized && myExpression[aComponent]->isInitialized();
29   }
30 }
31
32 void GeomData_Point2D::setCalculatedValue(const double theX, const double theY)
33 {
34   if (!myIsInitialized || x() != theX || y() != theY) {
35     myExpression[0]->setValue(theX);
36     myExpression[1]->setValue(theY);
37     owner()->data()->sendAttributeUpdated(this);
38   }
39 }
40
41 void GeomData_Point2D::setValue(const double theX, const double theY)
42 {
43   setCalculatedValue(textX().empty() ? theX : x(),
44                      textY().empty() ? theY : y());
45 }
46
47 void GeomData_Point2D::setValue(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
48 {
49   setValue(thePoint->x(), thePoint->y());
50 }
51
52 double GeomData_Point2D::x() const
53 {
54   return myExpression[0]->value();
55 }
56
57 double GeomData_Point2D::y() const
58 {
59   return myExpression[1]->value();
60 }
61
62 std::shared_ptr<GeomAPI_Pnt2d> GeomData_Point2D::pnt()
63 {
64   std::shared_ptr<GeomAPI_Pnt2d> aResult(new GeomAPI_Pnt2d(x(), y()));
65   return aResult;
66 }
67
68 void GeomData_Point2D::setText(const std::string& theX,
69                                const std::string& theY)
70 {
71   if (!myIsInitialized && theX.empty() && theY.empty())
72     return; // empty strings are not good initializers
73   if (!myIsInitialized || textX() != theX || textY() != theY) {
74     myExpression[0]->setText(theX);
75     myExpression[1]->setText(theY);
76     // Send it to evaluator to convert into the double and store in the attribute
77     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
78     owner()->data()->sendAttributeUpdated(this);
79   }
80 }
81
82 std::string GeomData_Point2D::textX()
83 {
84   return myExpression[0]->text();
85 }
86 std::string GeomData_Point2D::textY()
87 {
88   return myExpression[1]->text();
89 }
90
91 void GeomData_Point2D::setExpressionInvalid(int theComponent, bool theFlag)
92 {
93   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
94   if (!myIsInitialized || expressionInvalid(theComponent) != theFlag)
95     myExpression[theComponent]->setInvalid(theFlag);
96 }
97
98 bool GeomData_Point2D::expressionInvalid(int theComponent)
99 {
100   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
101   return myExpression[theComponent]->isInvalid();
102 }
103
104 void GeomData_Point2D::setExpressionError(int theComponent, const std::string& theError)
105 {
106   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
107   if (expressionError(theComponent) != theError)
108     myExpression[theComponent]->setError(theError);
109 }
110
111 std::string GeomData_Point2D::expressionError(int theComponent)
112 {
113   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
114   return myExpression[theComponent]->error();
115 }
116
117 void GeomData_Point2D::setUsedParameters(int theComponent, 
118                                          const std::set<std::string>& theUsedParameters)
119 {
120   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
121   myExpression[theComponent]->setUsedParameters(theUsedParameters);
122 }
123
124 std::set<std::string> GeomData_Point2D::usedParameters(int theComponent) const
125 {
126   assert(theComponent >= 0 && theComponent < NUM_COMPONENTS);
127   return myExpression[theComponent]->usedParameters();
128 }