Salome HOME
Issue #17347: B-Splines in Sketcher
[modules/shaper.git] / src / GeomData / GeomData_Point2DArray.cpp
1 // Copyright (C) 2019-2020  CEA/DEN, EDF R&D
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 }
53
54 int GeomData_Point2DArray::size()
55 {
56   if (myArray.IsNull() || !myArray->IsValid()) {
57     // this could be on undo and then redo creation of the attribute
58     // in result creation it may be uninitialized
59     myIsInitialized = myLab.FindAttribute(TDataStd_RealArray::GetID(), myArray) == Standard_True;
60   }
61   // checking the validity because attribute (as a field) may be presented,
62   // but without label: it is undoed
63   return (myArray.IsNull() || !myArray->IsValid()) ? 0 : myArray->Length() / 2;
64 }
65
66 void GeomData_Point2DArray::setSize(const int theSize)
67 {
68   int aValuesSize = 2 * theSize;
69   if (myArray.IsNull() || !myArray->IsValid()) { // create array if it is not done yet
70     if (aValuesSize != 0) { // if size is zero, nothing to do (null array means there is no array)
71       myArray = TDataStd_RealArray::Set(myLab, 0, aValuesSize - 1);
72       owner()->data()->sendAttributeUpdated(this);
73     }
74   }
75   else { // reset the old array
76     if (aValuesSize) {
77       if (aValuesSize != myArray->Length()) { // old data is not keept, a new array is created
78         Handle(TColStd_HArray1OfReal) aNewArray = new TColStd_HArray1OfReal(0, aValuesSize - 1);
79         myArray->ChangeArray(aNewArray);
80         owner()->data()->sendAttributeUpdated(this);
81       }
82     }
83     else { // size is zero => array must be erased
84       if (!myArray.IsNull()) {
85         myArray.Nullify();
86         myLab.ForgetAttribute(TDataStd_RealArray::GetID());
87         owner()->data()->sendAttributeUpdated(this);
88       }
89     }
90   }
91 }
92
93 void GeomData_Point2DArray::setPnt(const int theIndex,
94                                    const double theX,
95                                    const double theY)
96 {
97   if (myArray->Value(2 * theIndex) != theX || myArray->Value(2 * theIndex + 1) != theY) {
98     myArray->SetValue(2 * theIndex, theX);
99     myArray->SetValue(2 * theIndex + 1, theY);
100     owner()->data()->sendAttributeUpdated(this);
101   }
102 }
103
104 void GeomData_Point2DArray::setPnt(const int theIndex, const GeomPnt2dPtr& thePoint)
105 {
106   setPnt(theIndex, thePoint->x(), thePoint->y());
107 }
108
109 GeomPnt2dPtr GeomData_Point2DArray::pnt(const int theIndex)
110 {
111   GeomPnt2dPtr aPoint;
112   if (theIndex >= 0 && theIndex * 2 < myArray->Length())
113     aPoint.reset(new GeomAPI_Pnt2d(myArray->Value(2 * theIndex), myArray->Value(2 * theIndex + 1)));
114   return aPoint;
115 }