Salome HOME
Refactoring of B-splines in SketchPlugin
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_BSplineBase.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 <SketchPlugin_BSplineBase.h>
21 #include <SketchPlugin_Sketch.h>
22
23 #include <GeomAlgoAPI_EdgeBuilder.h>
24
25 #include <GeomAPI_Pnt2d.h>
26
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomDataAPI_Point2DArray.h>
29
30 #include <ModelAPI_AttributeDoubleArray.h>
31 #include <ModelAPI_AttributeIntArray.h>
32 #include <ModelAPI_AttributeInteger.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36
37
38 SketchPlugin_BSplineBase::SketchPlugin_BSplineBase()
39   : SketchPlugin_SketchEntity()
40 {
41 }
42
43 void SketchPlugin_BSplineBase::initDerivedClassAttributes()
44 {
45   data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
46   data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
47   data()->addAttribute(KNOTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
48   data()->addAttribute(MULTS_ID(), ModelAPI_AttributeIntArray::typeId());
49   data()->addAttribute(DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
50
51   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
53 }
54
55 void SketchPlugin_BSplineBase::execute()
56 {
57   SketchPlugin_Sketch* aSketch = sketch();
58   if(!aSketch) {
59     return;
60   }
61
62   AttributePoint2DArrayPtr aPolesArray =
63       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
64   AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
65   AttributeDoubleArrayPtr aKnotsArray = data()->realArray(KNOTS_ID());
66   AttributeIntArrayPtr aMultsArray = data()->intArray(MULTS_ID());
67   AttributeIntegerPtr aDegreeAttr = data()->integer(DEGREE_ID());
68
69   // collect poles
70   std::list<GeomPnt2dPtr> aPoles2D;
71   for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
72     GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
73     aPoles2D.push_back(aPole);
74   }
75   // collect weights
76   std::list<double> aWeights;
77   for (int anIndex = 0; anIndex < aWeightsArray->size(); ++anIndex)
78     aWeights.push_back(aWeightsArray->value(anIndex));
79   // collect knots
80   std::list<double> aKnots;
81   for (int anIndex = 0; anIndex < aKnotsArray->size(); ++anIndex)
82     aKnots.push_back(aKnotsArray->value(anIndex));
83   // collect multiplicities
84   std::list<int> aMults;
85   for (int anIndex = 0; anIndex < aMultsArray->size(); ++anIndex)
86     aMults.push_back(aMultsArray->value(anIndex));
87
88   // create result B-spline curve
89   GeomShapePtr anEdge = GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(),
90       aPoles2D, aWeights, aKnots, aMults, aDegreeAttr->value(), isPeriodic());
91
92   ResultConstructionPtr aResult = document()->createConstruction(data(), 0);
93   aResult->setShape(anEdge);
94   aResult->setIsInHistory(false);
95   setResult(aResult, 0);
96 }
97
98 bool SketchPlugin_BSplineBase::isFixed() {
99   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
100 }
101
102 void SketchPlugin_BSplineBase::attributeChanged(const std::string& theID) {
103   // the second condition for unability to move external segments anywhere
104   if (theID == EXTERNAL_ID() || isFixed()) {
105     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
106     if (!aSelection) {
107       // empty shape in selection shows that the shape is equal to context
108       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
109       if (anExtRes)
110         aSelection = anExtRes->shape();
111     }
112 ////    // update arguments due to the selection value
113 ////    if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
114 ////      std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelection));
115 ////      std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
116 ////
117 ////      bool aWasBlocked = data()->blockSendAttributeUpdated(true);
118 ////      std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
119 ////        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
120 ////      aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
121 ////
122 ////      std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
123 ////        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
124 ////      aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
125 ////
126 ////      std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
127 ////        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
128 ////      aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
129 ////
130 ////      std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
131 ////        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
132 ////      aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
133 ////
134 ////      real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
135 ////      real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
136 ////
137 ////      double aStartParam, aMidParam, aEndParam;
138 ////      anEllipse->parameter(anEdge->firstPoint(), tolerance, aStartParam);
139 ////      anEllipse->parameter(anEdge->middlePoint(), tolerance, aMidParam);
140 ////      anEllipse->parameter(anEdge->lastPoint(), tolerance, aEndParam);
141 ////      if (aEndParam < aStartParam)
142 ////        aEndParam += 2.0 * PI;
143 ////      if (aMidParam < aStartParam)
144 ////        aMidParam += 2.0 * PI;
145 ////      boolean(REVERSED_ID())->setValue(aMidParam > aEndParam);
146 ////
147 ////      data()->blockSendAttributeUpdated(aWasBlocked, false);
148 ////
149 ////      fillCharacteristicPoints();
150 ////    }
151   }
152 }