1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_AttributeImage.h>
21 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_Attribute.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Object.h>
26 #include <Standard_TypeDef.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_ExtendedString.hxx>
30 #include <TDataStd_IntegerArray.hxx>
31 #include <TDataStd_ByteArray.hxx>
32 #include <TDataStd_Comment.hxx>
33 #include <TDF_CopyLabel.hxx>
35 void Model_AttributeImage::setTexture(const int theWidth,
37 const std::list<unsigned char>& theByteArray,
38 const std::string& theFormat,
39 const bool sendUpdated)
41 if (theWidth > 0 && theHeight > 0 && theByteArray.size() > 0) { // set new data
42 // Find or create attributes
43 Handle_TDataStd_ByteArray aByteArray =
44 TDataStd_ByteArray::Set(myLab, 0, int(theByteArray.size()) - 1);
45 Handle_TDataStd_IntegerArray aDimensions =
46 TDataStd_IntegerArray::Set(myLab, 0, 1);
47 Handle_TDataStd_Comment aFormat = TDataStd_Comment::Set(myLab, theFormat.c_str());
50 aDimensions->SetValue(0, theWidth);
51 aDimensions->SetValue(1, theHeight);
54 Handle(TColStd_HArray1OfByte) aNewArray =
55 new TColStd_HArray1OfByte(0, int(theByteArray.size()) - 1);
56 std::list<unsigned char>::const_iterator itBA = theByteArray.begin();
57 for (int j = 0; itBA != theByteArray.end(); ++itBA, ++j) {
58 aNewArray->SetValue(j, (Standard_Byte)(*itBA));
60 aByteArray->ChangeArray(aNewArray);
63 owner()->data()->sendAttributeUpdated(this);
65 else { // size is zero => arrays must be erased
66 bool isForgotten1 = myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
67 bool isForgotten2 = myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
68 bool isForgotten3 = myLab.ForgetAttribute(TDataStd_Comment::GetID());
70 if (sendUpdated && (isForgotten1 || isForgotten2 || isForgotten3))
71 owner()->data()->sendAttributeUpdated(this);
75 bool Model_AttributeImage::hasTexture()
77 Handle_TDataStd_ByteArray aByteArray;
78 Handle_TDataStd_IntegerArray aDimensions;
79 if (myLab.FindAttribute(TDataStd_ByteArray::GetID(), aByteArray) == Standard_True &&
80 myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aDimensions) == Standard_True) {
83 if (aDimensions->Value(0) > 0 && aDimensions->Value(1) > 0)
85 return aByteArray->Length() > 0;
90 bool Model_AttributeImage::texture(int& theWidth,
92 std::list<unsigned char>& theByteArray,
93 std::string& theFormat)
101 Handle_TDataStd_ByteArray aByteArray;
102 Handle_TDataStd_IntegerArray aDimensions;
103 Handle_TDataStd_Comment aFormat;
104 if (myLab.FindAttribute(TDataStd_ByteArray::GetID(), aByteArray) == Standard_True &&
105 myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aDimensions) == Standard_True &&
106 myLab.FindAttribute(TDataStd_Comment::GetID(), aFormat) == Standard_True) {
109 theWidth = aDimensions->Value(0);
110 theHeight = aDimensions->Value(1);
113 const Handle(TColStd_HArray1OfByte) byteArray = aByteArray->InternalArray();
114 for (int j = byteArray->Lower(); j <= byteArray->Upper(); j++) {
115 theByteArray.push_back((unsigned char)byteArray->Value( j ));
119 theFormat = TCollection_AsciiString(aFormat->Get()).ToCString();
126 void Model_AttributeImage::copyTo(AttributeImagePtr theTarget) const
128 std::shared_ptr<Model_AttributeImage> aTarget =
129 std::dynamic_pointer_cast<Model_AttributeImage>(theTarget);
131 //Model_AttributeSelectionList::copyAttrs(myLab, aTarget->myLab);
132 TDF_CopyLabel aCopyAlgo (myLab, aTarget->myLab);
138 Model_AttributeImage::Model_AttributeImage(TDF_Label& theLabel)
144 void Model_AttributeImage::reinit()
146 myIsInitialized = myLab.IsAttribute(TDataStd_ByteArray::GetID());