1 // Copyright (C) 2019-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <SketchPlugin_BSpline.h>
21 #include <SketchPlugin_Sketch.h>
23 #include <GeomAlgoAPI_EdgeBuilder.h>
25 #include <GeomAPI_Pnt2d.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <GeomDataAPI_Point2DArray.h>
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>
38 SketchPlugin_BSpline::SketchPlugin_BSpline()
39 : SketchPlugin_SketchEntity()
43 void SketchPlugin_BSpline::initDerivedClassAttributes()
45 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
46 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
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());
54 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
58 void SketchPlugin_BSpline::execute()
60 SketchPlugin_Sketch* aSketch = sketch();
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());
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);
79 std::list<double> aWeights;
80 for (int anIndex = 0; anIndex < aWeightsArray->size(); ++anIndex)
81 aWeights.push_back(aWeightsArray->value(anIndex));
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));
91 // create result non-periodic B-spline curve
92 GeomShapePtr anEdge = GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(),
93 aPoles2D, aWeights, aKnots, aMults, aDegreeAttr->value(), false);
95 ResultConstructionPtr aResult = document()->createConstruction(data(), 0);
96 aResult->setShape(anEdge);
97 aResult->setIsInHistory(false);
98 setResult(aResult, 0);
101 bool SketchPlugin_BSpline::isFixed() {
102 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
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();
110 // empty shape in selection shows that the shape is equal to context
111 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
113 aSelection = anExtRes->shape();
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();
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()));
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()));
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()));
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()));
137 //// real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
138 //// real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
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);
150 //// data()->blockSendAttributeUpdated(aWasBlocked, false);
152 //// fillCharacteristicPoints();
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));
163 //// else if (theID == REVERSED_ID() && myParamDelta == 0.0)
164 //// myParamDelta = 2.0 * PI;