Salome HOME
Issue #2155 Trim removes multi-rotation constraint, undo leads to wrong DOF
[modules/shaper.git] / src / Model / Model_AttributeStringArray.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_AttributeStringArray.cpp
4 // Created:     14 Nov 2016
5 // Author:      Mikhail Ponikarov
6
7 #include "Model_AttributeStringArray.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
11
12 #include <TColStd_HArray1OfExtendedString.hxx>
13
14 //==================================================================================================
15 int Model_AttributeStringArray::size()
16 {
17   if (myArray.IsNull() || !myArray->IsValid()) {
18     // this could be on undo and then redo creation of the attribute
19     // in result creation it may be uninitialized
20     myIsInitialized =
21       myLab.FindAttribute(TDataStd_ExtStringArray::GetID(), myArray) == Standard_True;
22   }
23   // checking the validity because attribute (as a field) may be presented,
24   // but without label: it is undoed
25   return (myArray.IsNull() || !myArray->IsValid()) ? 0 : myArray->Length();
26 }
27
28 //==================================================================================================
29 void Model_AttributeStringArray::setSize(const int theSize)
30 {
31   if (myArray.IsNull() || !myArray->IsValid()) { // create array if it is not done yet
32     if (theSize != 0) { // if size is zero, nothing to do (null array means there is no array)
33       myArray = TDataStd_ExtStringArray::Set(myLab, 0, theSize - 1);
34       owner()->data()->sendAttributeUpdated(this);
35     }
36   } else { // reset the old array
37     if (theSize) {
38       if (theSize != myArray->Length()) { // old data is not keept, a new array is created
39         Handle(TColStd_HArray1OfExtendedString) aNewArray =
40           new TColStd_HArray1OfExtendedString(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_ExtStringArray::GetID());
48         owner()->data()->sendAttributeUpdated(this);
49       }
50     }
51   }
52 }
53
54 //==================================================================================================
55 void Model_AttributeStringArray::setValue(const int theIndex,
56                                           const std::string theValue)
57 {
58   if (!myArray->Value(theIndex).IsEqual(theValue.c_str())) {
59     myArray->SetValue(theIndex, theValue.c_str());
60     owner()->data()->sendAttributeUpdated(this);
61   }
62 }
63
64 //==================================================================================================
65 std::string Model_AttributeStringArray::value(const int theIndex)
66 {
67   return TCollection_AsciiString(myArray->Value(theIndex)).ToCString();
68 }
69
70 //==================================================================================================
71 Model_AttributeStringArray::Model_AttributeStringArray(TDF_Label& theLabel)
72 {
73   myLab = theLabel;
74   myIsInitialized =
75     myLab.FindAttribute(TDataStd_ExtStringArray::GetID(), myArray) == Standard_True;
76 }