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