Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomData / GeomData_Point2DArray.cpp
1 // Copyright (C) 2019-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomData_Point2DArray.h"
21
22 #include <GeomAPI_Pnt2d.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Expression.h>
27 #include <ModelAPI_Feature.h>
28
29 #include <cassert>
30
31 GeomData_Point2DArray::GeomData_Point2DArray(TDF_Label& theLabel)
32   : myLab(theLabel)
33 {
34   reinit();
35 }
36
37 void GeomData_Point2DArray::reinit()
38 {
39   // check the attribute could be already presented in this doc (after load document)
40   myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
41 }
42
43 bool GeomData_Point2DArray::assign(std::shared_ptr<GeomDataAPI_Point2DArray> theOther)
44 {
45   std::shared_ptr<GeomData_Point2DArray> anOther =
46       std::dynamic_pointer_cast<GeomData_Point2DArray>(theOther);
47   if (!anOther)
48     return false;
49
50   setSize(anOther->size());
51   myArray->ChangeArray(anOther->myArray->Array(), false);
52   owner()->data()->sendAttributeUpdated(this);
53
54   return true;
55 }
56
57 int GeomData_Point2DArray::size()
58 {
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;
63   }
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;
67 }
68
69 void GeomData_Point2DArray::setSize(const int theSize)
70 {
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);
76     }
77   }
78   else { // reset the old array
79     if (aValuesSize) {
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);
86       }
87     }
88     else { // size is zero => array must be erased
89       if (!myArray.IsNull()) {
90         myArray.Nullify();
91         myLab.ForgetAttribute(TDataStd_RealArray::GetID());
92         owner()->data()->sendAttributeUpdated(this);
93       }
94     }
95   }
96 }
97
98 void GeomData_Point2DArray::setPnt(const int theIndex,
99                                    const double theX,
100                                    const double theY)
101 {
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);
106   }
107 }
108
109 void GeomData_Point2DArray::setPnt(const int theIndex, const GeomPnt2dPtr& thePoint)
110 {
111   setPnt(theIndex, thePoint->x(), thePoint->y());
112 }
113
114 GeomPnt2dPtr GeomData_Point2DArray::pnt(const int theIndex)
115 {
116   GeomPnt2dPtr aPoint;
117   if (theIndex >= 0 && theIndex * 2 < myArray->Length())
118     aPoint.reset(new GeomAPI_Pnt2d(myArray->Value(2 * theIndex), myArray->Value(2 * theIndex + 1)));
119   return aPoint;
120 }