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