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