Salome HOME
High level objects history implementation for Intersection and Compound features...
[modules/shaper.git] / src / Model / Model_AttributeStringArray.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Model_AttributeStringArray.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Object.h>
25
26 #include <TColStd_HArray1OfExtendedString.hxx>
27
28 //==================================================================================================
29 int Model_AttributeStringArray::size()
30 {
31   if (myArray.IsNull() || !myArray->IsValid()) {
32     // this could be on undo and then redo creation of the attribute
33     // in result creation it may be uninitialized
34     myIsInitialized =
35       myLab.FindAttribute(TDataStd_ExtStringArray::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_AttributeStringArray::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_ExtStringArray::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_HArray1OfExtendedString) aNewArray =
54           new TColStd_HArray1OfExtendedString(0, theSize - 1);
55         myArray->ChangeArray(aNewArray);
56         owner()->data()->sendAttributeUpdated(this);
57       }
58     } else { // size is zero => array must be erased
59       if (!myArray.IsNull()) {
60         myArray.Nullify();
61         myLab.ForgetAttribute(TDataStd_ExtStringArray::GetID());
62         owner()->data()->sendAttributeUpdated(this);
63       }
64     }
65   }
66 }
67
68 //==================================================================================================
69 void Model_AttributeStringArray::setValue(const int theIndex,
70                                           const std::string theValue)
71 {
72   if (!myArray->Value(theIndex).IsEqual(theValue.c_str())) {
73     myArray->SetValue(theIndex, theValue.c_str());
74     owner()->data()->sendAttributeUpdated(this);
75   }
76 }
77
78 //==================================================================================================
79 std::string Model_AttributeStringArray::value(const int theIndex)
80 {
81   return TCollection_AsciiString(myArray->Value(theIndex)).ToCString();
82 }
83
84 //==================================================================================================
85 Model_AttributeStringArray::Model_AttributeStringArray(TDF_Label& theLabel)
86 {
87   myLab = theLabel;
88   myIsInitialized =
89     myLab.FindAttribute(TDataStd_ExtStringArray::GetID(), myArray) == Standard_True;
90 }