Salome HOME
Issue #1659 New widget for supporting optional inputs : correction for enclosed cases.
[modules/shaper.git] / src / Model / Model_AttributeDoubleArray.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeDoubleArray.cpp
4 // Created:     08 July 2016
5 // Author:      Dmitry Bobylev
6
7 #include "Model_AttributeDoubleArray.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11
12 #include <TColStd_HArray1OfReal.hxx>
13
14 #include <string>
15
16 //==================================================================================================
17 int Model_AttributeDoubleArray::size()
18 {
19   if (myArray.IsNull() || !myArray->IsValid()) {
20     // this could be on undo and then redo creation of the attribute
21     // in result creation it may be uninitialized
22     myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
23   }
24   // checking the validity because attribute (as a field) may be presented,
25   // but without label: it is undoed
26   return (myArray.IsNull() || !myArray->IsValid()) ? 0 : myArray->Length();
27 }
28
29 //==================================================================================================
30 void Model_AttributeDoubleArray::setSize(const int theSize)
31 {
32   if (myArray.IsNull() || !myArray->IsValid()) { // create array if it is not done yet
33     if (theSize != 0) { // if size is zero, nothing to do (null array means there is no array)
34       myArray = TDataStd_RealArray::Set(myLab, 0, theSize - 1);
35       owner()->data()->sendAttributeUpdated(this);
36     }
37   } else { // reset the old array
38     if (theSize) {
39       if (theSize != myArray->Length()) { // old data is not keept, a new array is created
40         Handle(TColStd_HArray1OfReal) aNewArray = new TColStd_HArray1OfReal(0, theSize - 1);
41         myArray->ChangeArray(aNewArray);
42         owner()->data()->sendAttributeUpdated(this);
43       }
44     } else { // size is zero => array must be erased
45       if (!myArray.IsNull()) {
46         myArray.Nullify();
47         myLab.ForgetAttribute(TDataStd_RealArray::GetID());
48         owner()->data()->sendAttributeUpdated(this);
49       }
50     }
51   }
52 }
53
54 //==================================================================================================
55 void Model_AttributeDoubleArray::setValue(const int theIndex,
56                                           const double theValue)
57 {
58   if (myArray->Value(theIndex) != theValue) {
59     myArray->SetValue(theIndex, theValue);
60     owner()->data()->sendAttributeUpdated(this);
61   }
62 }
63
64 //==================================================================================================
65 double Model_AttributeDoubleArray::value(const int theIndex)
66 {
67   return myArray->Value(theIndex);
68 }
69
70 //==================================================================================================
71 Model_AttributeDoubleArray::Model_AttributeDoubleArray(TDF_Label& theLabel)
72 {
73   myLab = theLabel;
74   // check the attribute could be already presented in this doc (after load document)
75   myIsInitialized =
76     myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
77 }