Salome HOME
f1d7cb043d2059fa0464db8722cad1fa00604656
[modules/shaper.git] / src / Model / Model_AttributeDoubleArray.cpp
1 // Copyright (C) 2014-2023  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "Model_AttributeDoubleArray.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Object.h>
24
25 #include <TColStd_HArray1OfReal.hxx>
26
27 #include <string>
28
29 //==================================================================================================
30 int Model_AttributeDoubleArray::size()
31 {
32   if (myArray.IsNull() || !myArray->IsValid()) {
33     // this could be on undo and then redo creation of the attribute
34     // in result creation it may be uninitialized
35     myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
36   }
37   // checking the validity because attribute (as a field) may be presented,
38   // but without label: it is undoed
39   return (myArray.IsNull() || !myArray->IsValid()) ? 0 : myArray->Length();
40 }
41
42 //==================================================================================================
43 void Model_AttributeDoubleArray::setSize(const int theSize)
44 {
45   if (myArray.IsNull() || !myArray->IsValid()) { // create array if it is not done yet
46     if (theSize != 0) { // if size is zero, nothing to do (null array means there is no array)
47       myArray = TDataStd_RealArray::Set(myLab, 0, theSize - 1);
48       owner()->data()->sendAttributeUpdated(this);
49     }
50   } else { // reset the old array
51     if (theSize) {
52       if (theSize != myArray->Length()) { // old data is not keept, a new array is created
53         Handle(TColStd_HArray1OfReal) aNewArray = new TColStd_HArray1OfReal(0, theSize - 1);
54         myArray->ChangeArray(aNewArray);
55         owner()->data()->sendAttributeUpdated(this);
56       }
57     } else { // size is zero => array must be erased
58       if (!myArray.IsNull()) {
59         myArray.Nullify();
60         myLab.ForgetAttribute(TDataStd_RealArray::GetID());
61         owner()->data()->sendAttributeUpdated(this);
62       }
63     }
64   }
65 }
66
67 //==================================================================================================
68 void Model_AttributeDoubleArray::setValue(const int theIndex,
69                                           const double theValue)
70 {
71   if (myArray->Value(theIndex) != theValue) {
72     myArray->SetValue(theIndex, theValue);
73     owner()->data()->sendAttributeUpdated(this);
74   }
75 }
76
77 //==================================================================================================
78 double Model_AttributeDoubleArray::value(const int theIndex)
79 {
80   return myArray->Value(theIndex);
81 }
82
83 //==================================================================================================
84 Model_AttributeDoubleArray::Model_AttributeDoubleArray(TDF_Label& theLabel)
85 {
86   myLab = theLabel;
87   reinit();
88 }
89
90 void Model_AttributeDoubleArray::reinit()
91 {
92   // check the attribute could be already presented in this doc (after load document)
93   myIsInitialized =
94     myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
95 }