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