1 // Copyright (C) 2019-2021 CEA/DEN, EDF R&D
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 "GeomData_Point2DArray.h"
22 #include <GeomAPI_Pnt2d.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Expression.h>
27 #include <ModelAPI_Feature.h>
31 GeomData_Point2DArray::GeomData_Point2DArray(TDF_Label& theLabel)
37 void GeomData_Point2DArray::reinit()
39 // check the attribute could be already presented in this doc (after load document)
40 myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
43 bool GeomData_Point2DArray::assign(std::shared_ptr<GeomDataAPI_Point2DArray> theOther)
45 std::shared_ptr<GeomData_Point2DArray> anOther =
46 std::dynamic_pointer_cast<GeomData_Point2DArray>(theOther);
50 setSize(anOther->size());
51 myArray->ChangeArray(anOther->myArray->Array(), false);
52 owner()->data()->sendAttributeUpdated(this);
57 int GeomData_Point2DArray::size()
59 if (myArray.IsNull() || !myArray->IsValid()) {
60 // this could be on undo and then redo creation of the attribute
61 // in result creation it may be uninitialized
62 myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
64 // checking the validity because attribute (as a field) may be presented,
65 // but without label: it is undoed
66 return (myArray.IsNull() || !myArray->IsValid()) ? 0 : myArray->Length() / 2;
69 void GeomData_Point2DArray::setSize(const int theSize)
71 int aValuesSize = 2 * theSize;
72 if (myArray.IsNull() || !myArray->IsValid()) { // create array if it is not done yet
73 if (aValuesSize != 0) { // if size is zero, nothing to do (null array means there is no array)
74 myArray = TDataStd_RealArray::Set(myLab, 0, aValuesSize - 1);
75 owner()->data()->sendAttributeUpdated(this);
78 else { // reset the old array
80 if (aValuesSize != myArray->Length()) { // old data is kept in the new array
81 Handle(TColStd_HArray1OfReal) aNewArray = new TColStd_HArray1OfReal(0, aValuesSize - 1);
82 for (int anIndex = 0; anIndex < aValuesSize && anIndex <= myArray->Upper(); ++anIndex)
83 aNewArray->SetValue(anIndex, myArray->Value(anIndex));
84 myArray->ChangeArray(aNewArray);
85 owner()->data()->sendAttributeUpdated(this);
88 else { // size is zero => array must be erased
89 if (!myArray.IsNull()) {
91 myLab.ForgetAttribute(TDataStd_RealArray::GetID());
92 owner()->data()->sendAttributeUpdated(this);
98 void GeomData_Point2DArray::setPnt(const int theIndex,
102 if (myArray->Value(2 * theIndex) != theX || myArray->Value(2 * theIndex + 1) != theY) {
103 myArray->SetValue(2 * theIndex, theX);
104 myArray->SetValue(2 * theIndex + 1, theY);
105 owner()->data()->sendAttributeUpdated(this);
109 void GeomData_Point2DArray::setPnt(const int theIndex, const GeomPnt2dPtr& thePoint)
111 setPnt(theIndex, thePoint->x(), thePoint->y());
114 GeomPnt2dPtr GeomData_Point2DArray::pnt(const int theIndex)
117 if (theIndex >= 0 && theIndex * 2 < myArray->Length())
118 aPoint.reset(new GeomAPI_Pnt2d(myArray->Value(2 * theIndex), myArray->Value(2 * theIndex + 1)));