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