1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_AttributeTables.cpp
4 // Created: 14 Nov 2016
5 // Author: Mikhail Ponikarov
7 #include "Model_AttributeTables.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Object.h>
12 #include <TColStd_ListIteratorOfListOfInteger.hxx>
13 #include <TDataStd_RealArray.hxx>
14 #include <TDataStd_BooleanArray.hxx>
15 #include <TDataStd_IntegerArray.hxx>
16 #include <TDataStd_ExtStringArray.hxx>
18 #include <TColStd_HArray1OfReal.hxx>
19 #include <TColStd_HArray1OfByte.hxx>
20 #include <TColStd_HArray1OfInteger.hxx>
21 #include <TColStd_HArray1OfExtendedString.hxx>
25 // returns the array attribute GUID for the given type
26 #define MY_ARRAY_ID(type) \
27 ((type) == ModelAPI_AttributeTables::DOUBLE ? TDataStd_RealArray::GetID(): \
28 ((type) == ModelAPI_AttributeTables::BOOLEAN ? TDataStd_BooleanArray::GetID(): \
29 ((type) == ModelAPI_AttributeTables::INTEGER? TDataStd_IntegerArray::GetID(): \
30 TDataStd_ExtStringArray::GetID())))
32 int Model_AttributeTables::rows()
37 int Model_AttributeTables::columns()
42 int Model_AttributeTables::tables()
47 void Model_AttributeTables::setSize(const int theRows, const int theColumns, const int theTables)
49 if (theRows != myRows || theColumns != myCols || theTables != myTables) {
50 owner()->data()->sendAttributeUpdated(this);
51 int aSize = myRows * myCols * myTables;
52 int aNewSize = theRows * theColumns * theTables;
53 if (aSize == 0 && aNewSize == 0) {
55 } else if (aNewSize == 0) {
57 myLab.ForgetAttribute(MY_ARRAY_ID(myType));
60 Handle(TColStd_HArray1OfReal) anOldDouble, aNewDouble =
61 (myType == ModelAPI_AttributeTables::DOUBLE) ?
62 new TColStd_HArray1OfReal(0, aNewSize - 1) : Handle(TColStd_HArray1OfReal)();
63 Handle(TColStd_HArray1OfByte) anOldBool, aNewBool =
64 (myType == ModelAPI_AttributeTables::BOOLEAN) ?
65 new TColStd_HArray1OfByte(0, aNewSize - 1) : Handle(TColStd_HArray1OfByte)();
66 Handle(TColStd_HArray1OfInteger) anOldInt, aNewInt =
67 (myType == ModelAPI_AttributeTables::INTEGER) ?
68 new TColStd_HArray1OfInteger(0, aNewSize - 1) : Handle(TColStd_HArray1OfInteger)();
69 Handle(TColStd_HArray1OfExtendedString) anOldStr, aNewStr =
70 (myType == ModelAPI_AttributeTables::STRING) ?
71 new TColStd_HArray1OfExtendedString(0, aNewSize - 1) :
72 Handle(TColStd_HArray1OfExtendedString)();
73 if (aSize != 0) { // copy the previous values into new positions, otherwise default values
74 Handle(TDF_Attribute) anArray;
75 myLab.FindAttribute(MY_ARRAY_ID(myType), anArray);
77 case ModelAPI_AttributeTables::DOUBLE:
78 anOldDouble = Handle(TDataStd_RealArray)::DownCast(anArray)->Array();
80 case ModelAPI_AttributeTables::BOOLEAN:
81 anOldBool = Handle(TDataStd_BooleanArray)::DownCast(anArray)->InternalArray();
83 case ModelAPI_AttributeTables::INTEGER:
84 anOldInt = Handle(TDataStd_IntegerArray)::DownCast(anArray)->Array();
86 case ModelAPI_AttributeTables::STRING:
87 anOldStr = Handle(TDataStd_ExtStringArray)::DownCast(anArray)->Array();
91 for(int aTable = 0; aTable < theTables; aTable++) {
92 for(int aColumn = 0; aColumn < theColumns; aColumn++) {
93 for(int aRow = 0; aRow < theRows; aRow++) {
94 int anOldIndex, anIndex = aTable * theRows * theColumns + aRow * theColumns + aColumn;
95 bool aRestore = aTable < myTables && aColumn < myCols && aRow < myRows;
97 anOldIndex = aTable * myRows * myCols + aRow * myCols + aColumn;
99 case ModelAPI_AttributeTables::DOUBLE: {
100 aNewDouble->SetValue(anIndex, aRestore ? anOldDouble->Value(anOldIndex) : 0.);
103 case ModelAPI_AttributeTables::BOOLEAN: {
104 aNewBool->SetValue(anIndex, aRestore ? anOldBool->Value(anOldIndex) : Standard_False);
107 case ModelAPI_AttributeTables::INTEGER: {
108 aNewInt->SetValue(anIndex, aRestore ? anOldInt->Value(anOldIndex) : 0);
111 case ModelAPI_AttributeTables::STRING: {
112 aNewStr->SetValue(anIndex, aRestore ? anOldStr->Value(anOldIndex) : "");
119 // store the new array
121 case ModelAPI_AttributeTables::DOUBLE:
122 TDataStd_RealArray::Set(myLab, 0, aNewSize - 1)->ChangeArray(aNewDouble);
124 case ModelAPI_AttributeTables::BOOLEAN:
125 TDataStd_BooleanArray::Set(myLab, 0, aNewSize - 1)->SetInternalArray(aNewBool);
127 case ModelAPI_AttributeTables::INTEGER:
128 TDataStd_IntegerArray::Set(myLab, 0, aNewSize - 1)->ChangeArray(aNewInt);
130 case ModelAPI_AttributeTables::STRING:
131 TDataStd_ExtStringArray::Set(myLab, 0, aNewSize - 1)->ChangeArray(aNewStr);
135 // store the new sizes
138 myTables = theTables;
140 myProp->Append(int(myType)); // default
141 myProp->Append(myTables);
142 myProp->Append(myRows);
143 myProp->Append(myCols);
144 owner()->data()->sendAttributeUpdated(this);
148 void Model_AttributeTables::setType(ModelAPI_AttributeTables::ValueType theType)
150 if (myType != theType) {
151 // remove the old attr
152 int aSize = myRows * myCols * myTables;
154 myLab.ForgetAttribute(MY_ARRAY_ID(theType));
156 int aTables = myTables;
157 myTables = 0; // to let setSize know that there is no old array
158 setSize(myRows, myCols, myTables);
161 owner()->data()->sendAttributeUpdated(this);
166 const ModelAPI_AttributeTables::ValueType& Model_AttributeTables::type(ValueType) const
171 void Model_AttributeTables::setValue(const ModelAPI_AttributeTables::Value theValue,
172 const int theRow, const int theColumn, const int theTable)
174 int anIndex = theTable * myRows * myCols + theRow * myCols + theColumn;
175 Handle(TDF_Attribute) anArray;
176 myLab.FindAttribute(MY_ARRAY_ID(myType), anArray);
178 case ModelAPI_AttributeTables::DOUBLE: {
179 Handle(TDataStd_RealArray)::DownCast(anArray)->SetValue(anIndex, theValue.myDouble);
182 case ModelAPI_AttributeTables::BOOLEAN: {
183 Handle(TDataStd_BooleanArray)::DownCast(anArray)->SetValue(anIndex, theValue.myBool);
186 case ModelAPI_AttributeTables::INTEGER: {
187 Handle(TDataStd_IntegerArray)::DownCast(anArray)->SetValue(anIndex, theValue.myInt);
190 case ModelAPI_AttributeTables::STRING: {
191 Handle(TDataStd_ExtStringArray)::DownCast(anArray)->SetValue(anIndex, theValue.myStr.c_str());
195 owner()->data()->sendAttributeUpdated(this);
198 ModelAPI_AttributeTables::Value Model_AttributeTables::value(
199 const int theRow, const int theColumn, const int theTable)
201 ModelAPI_AttributeTables::Value aResult;
202 int anIndex = theTable * myRows * myCols + theRow * myCols + theColumn;
203 Handle(TDF_Attribute) anArray;
204 myLab.FindAttribute(MY_ARRAY_ID(myType), anArray);
206 case ModelAPI_AttributeTables::DOUBLE: {
207 aResult.myDouble = Handle(TDataStd_RealArray)::DownCast(anArray)->Value(anIndex);
210 case ModelAPI_AttributeTables::BOOLEAN: {
211 aResult.myBool = Handle(TDataStd_BooleanArray)::DownCast(anArray)->Value(anIndex) ==
215 case ModelAPI_AttributeTables::INTEGER: {
216 aResult.myInt = Handle(TDataStd_IntegerArray)::DownCast(anArray)->Value(anIndex);
219 case ModelAPI_AttributeTables::STRING: {
220 aResult.myStr = TCollection_AsciiString(Handle(TDataStd_ExtStringArray)::DownCast(anArray)->
221 Value(anIndex)).ToCString();
229 //==================================================================================================
230 Model_AttributeTables::Model_AttributeTables(TDF_Label& theLabel)
236 void Model_AttributeTables::reinit()
238 // check the attribute could be already presented in this doc (after load document)
240 myLab.FindAttribute(TDataStd_IntegerList::GetID(), myProp) == Standard_True;
241 if (!myIsInitialized) {
242 myProp = TDataStd_IntegerList::Set(myLab);
243 myProp->Append(int(Model_AttributeTables::DOUBLE)); // default
248 TColStd_ListIteratorOfListOfInteger aListIter(myProp->List());
249 myType = ModelAPI_AttributeTables::ValueType(aListIter.Value());
251 myTables = aListIter.Value();
253 myRows = aListIter.Value();
255 myCols = aListIter.Value();