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