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_BSplineBase.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_BSplineBase::SketchPlugin_BSplineBase()
39 : SketchPlugin_SketchEntity()
43 void SketchPlugin_BSplineBase::initDerivedClassAttributes()
45 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
46 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
47 data()->addAttribute(KNOTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
48 data()->addAttribute(MULTS_ID(), ModelAPI_AttributeIntArray::typeId());
49 data()->addAttribute(DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
51 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
55 void SketchPlugin_BSplineBase::execute()
57 SketchPlugin_Sketch* aSketch = sketch();
62 AttributePoint2DArrayPtr aPolesArray =
63 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
64 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
65 AttributeDoubleArrayPtr aKnotsArray = data()->realArray(KNOTS_ID());
66 AttributeIntArrayPtr aMultsArray = data()->intArray(MULTS_ID());
67 AttributeIntegerPtr aDegreeAttr = data()->integer(DEGREE_ID());
70 std::list<GeomPnt2dPtr> aPoles2D;
71 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
72 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
73 aPoles2D.push_back(aPole);
76 std::list<double> aWeights;
77 for (int anIndex = 0; anIndex < aWeightsArray->size(); ++anIndex)
78 aWeights.push_back(aWeightsArray->value(anIndex));
80 std::list<double> aKnots;
81 for (int anIndex = 0; anIndex < aKnotsArray->size(); ++anIndex)
82 aKnots.push_back(aKnotsArray->value(anIndex));
83 // collect multiplicities
84 std::list<int> aMults;
85 for (int anIndex = 0; anIndex < aMultsArray->size(); ++anIndex)
86 aMults.push_back(aMultsArray->value(anIndex));
88 // create result B-spline curve
89 GeomShapePtr anEdge = GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(),
90 aPoles2D, aWeights, aKnots, aMults, aDegreeAttr->value(), isPeriodic());
92 ResultConstructionPtr aResult = document()->createConstruction(data(), 0);
93 aResult->setShape(anEdge);
94 aResult->setIsInHistory(false);
95 setResult(aResult, 0);
98 bool SketchPlugin_BSplineBase::isFixed() {
99 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
102 void SketchPlugin_BSplineBase::attributeChanged(const std::string& theID) {
103 // the second condition for unability to move external segments anywhere
104 if (theID == EXTERNAL_ID() || isFixed()) {
105 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
107 // empty shape in selection shows that the shape is equal to context
108 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
110 aSelection = anExtRes->shape();
112 //// // update arguments due to the selection value
113 //// if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
114 //// std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelection));
115 //// std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
117 //// bool aWasBlocked = data()->blockSendAttributeUpdated(true);
118 //// std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
119 //// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
120 //// aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
122 //// std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
123 //// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
124 //// aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
126 //// std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
127 //// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
128 //// aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
130 //// std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
131 //// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
132 //// aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
134 //// real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
135 //// real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
137 //// double aStartParam, aMidParam, aEndParam;
138 //// anEllipse->parameter(anEdge->firstPoint(), tolerance, aStartParam);
139 //// anEllipse->parameter(anEdge->middlePoint(), tolerance, aMidParam);
140 //// anEllipse->parameter(anEdge->lastPoint(), tolerance, aEndParam);
141 //// if (aEndParam < aStartParam)
142 //// aEndParam += 2.0 * PI;
143 //// if (aMidParam < aStartParam)
144 //// aMidParam += 2.0 * PI;
145 //// boolean(REVERSED_ID())->setValue(aMidParam > aEndParam);
147 //// data()->blockSendAttributeUpdated(aWasBlocked, false);
149 //// fillCharacteristicPoints();