1 // Copyright (C) 2019-2021 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>
22 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_MacroBSpline.h>
25 #include <SketchPlugin_Point.h>
26 #include <SketchPlugin_Sketch.h>
28 #include <Events_InfoMessage.h>
30 #include <GeomAlgoAPI_EdgeBuilder.h>
32 #include <GeomAPI_BSpline2d.h>
33 #include <GeomAPI_Pnt2d.h>
35 #include <GeomDataAPI_Point2D.h>
36 #include <GeomDataAPI_Point2DArray.h>
38 #include <ModelAPI_AttributeDouble.h>
39 #include <ModelAPI_AttributeDoubleArray.h>
40 #include <ModelAPI_AttributeIntArray.h>
41 #include <ModelAPI_AttributeInteger.h>
42 #include <ModelAPI_ResultConstruction.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_Validator.h>
47 SketchPlugin_BSplineBase::SketchPlugin_BSplineBase()
48 : SketchPlugin_SketchEntity()
52 void SketchPlugin_BSplineBase::initDerivedClassAttributes()
54 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
55 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
56 data()->addAttribute(KNOTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
57 data()->addAttribute(MULTS_ID(), ModelAPI_AttributeIntArray::typeId());
58 data()->addAttribute(DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
60 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
64 void SketchPlugin_BSplineBase::execute()
66 SketchPlugin_Sketch* aSketch = sketch();
71 AttributePoint2DArrayPtr aPolesArray =
72 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
73 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
74 AttributeDoubleArrayPtr aKnotsArray = data()->realArray(KNOTS_ID());
75 AttributeIntArrayPtr aMultsArray = data()->intArray(MULTS_ID());
76 AttributeIntegerPtr aDegreeAttr = data()->integer(DEGREE_ID());
79 std::list<GeomPnt2dPtr> aPoles2D;
80 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
81 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
82 aPoles2D.push_back(aPole);
85 std::list<double> aWeights;
86 for (int anIndex = 0; anIndex < aWeightsArray->size(); ++anIndex)
87 aWeights.push_back(aWeightsArray->value(anIndex));
89 std::list<double> aKnots;
90 for (int anIndex = 0; anIndex < aKnotsArray->size(); ++anIndex)
91 aKnots.push_back(aKnotsArray->value(anIndex));
92 // collect multiplicities
93 std::list<int> aMults;
94 for (int anIndex = 0; anIndex < aMultsArray->size(); ++anIndex)
95 aMults.push_back(aMultsArray->value(anIndex));
97 // create result B-spline curve
98 GeomShapePtr anEdge = GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(),
99 aPoles2D, aWeights, aKnots, aMults, aDegreeAttr->value(), isPeriodic());
101 ResultConstructionPtr aResult = document()->createConstruction(data(), 0);
102 aResult->setShape(anEdge);
103 aResult->setIsInHistory(false);
104 setResult(aResult, 0);
107 bool SketchPlugin_BSplineBase::isFixed() {
108 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
111 void SketchPlugin_BSplineBase::attributeChanged(const std::string& /*theID*/)
115 bool SketchPlugin_BSplineBase::customAction(const std::string& theActionId)
117 // parse for the action and an index divided by '#'
118 std::string anAction;
120 size_t pos = theActionId.find('#');
121 if (pos != std::string::npos) {
122 anAction = theActionId.substr(0, pos);
123 anIndex = std::stoi(theActionId.substr(pos + 1));
126 if (anAction == ADD_POLE_ACTION_ID()) {
127 return addPole(anIndex);
130 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
131 Events_InfoMessage("SketchPlugin_BSplineBase", aMsg).arg(getKind()).arg(theActionId).send();
135 bool SketchPlugin_BSplineBase::addPole(const int theAfter)
137 AttributePoint2DArrayPtr aPolesArray =
138 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
139 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
141 int anAfter = (!isPeriodic() && theAfter == aPolesArray->size() - 1) ? theAfter - 1 : theAfter;
143 // find internal coincidences applied to the poles with greater indices
144 std::list<AttributeIntegerPtr> aCoincidentPoleIndex;
145 std::map<int, FeaturePtr> aControlPoles, aControlSegments;
146 bool hasAuxSegment = false;
147 const std::set<AttributePtr>& aRefs = data()->refsToMe();
148 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
149 FeaturePtr aFeature = ModelAPI_Feature::feature((*anIt)->owner());
150 if (aFeature->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
151 AttributeIntegerPtr anIndex;
152 AttributeRefAttrPtr aNonSplinePoint;
153 if ((*anIt)->id() == SketchPlugin_ConstraintCoincidenceInternal::ENTITY_A()) {
154 anIndex = aFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
155 aNonSplinePoint = aFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
157 else if ((*anIt)->id() == SketchPlugin_ConstraintCoincidenceInternal::ENTITY_B()) {
158 anIndex = aFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
159 aNonSplinePoint = aFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
162 if (anIndex && anIndex->isInitialized()) {
163 if (anIndex->value() > anAfter) {
164 aCoincidentPoleIndex.push_back(anIndex);
165 FeaturePtr aParent = ModelAPI_Feature::feature(aNonSplinePoint->attr()->owner());
166 if (aParent->getKind() == SketchPlugin_Point::ID())
167 aControlPoles[anIndex->value()] = aParent;
168 else if (aParent->getKind() == SketchPlugin_Line::ID() &&
169 aNonSplinePoint->attr()->id() == SketchPlugin_Line::START_ID())
170 aControlSegments[anIndex->value()] = aParent;
172 else if (anIndex->value() == anAfter && !hasAuxSegment) {
173 // check the constrained object is a segment of the control polygon
174 if (aNonSplinePoint && !aNonSplinePoint->isObject() &&
175 aNonSplinePoint->attr()->id() == SketchPlugin_Line::START_ID()) {
176 hasAuxSegment = true;
177 aCoincidentPoleIndex.push_back(anIndex);
178 aControlSegments[anIndex->value()] =
179 ModelAPI_Feature::feature(aNonSplinePoint->attr()->owner());
186 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
188 // add new pole and default weight
189 std::list<GeomPnt2dPtr> aPoles;
190 aPolesArray->setSize(aPolesArray->size() + 1);
191 aPolesArray->setPnt(aPolesArray->size() - 1, aPolesArray->pnt(0)); // for periodic spline
192 for (int i = aPolesArray->size() - 2; i > anAfter; --i) {
193 aPoles.push_front(aPolesArray->pnt(i));
194 aPolesArray->setPnt(i + 1, aPoles.front());
197 GeomPnt2dPtr aCurPole = aPolesArray->pnt(anAfter);
198 GeomPnt2dPtr aNextPole = aPolesArray->pnt(anAfter + 1);
199 aPolesArray->setPnt(anAfter + 1, (aCurPole->x() + aNextPole->x()) * 0.5,
200 (aCurPole->y() + aNextPole->y()) * 0.5);
201 for (int i = anAfter + 1; i >= 0; --i)
202 aPoles.push_front(aPolesArray->pnt(i));
204 std::list<double> aWeights;
205 for (int i = 0; i < aWeightsArray->size(); ++i) {
206 aWeights.push_back(aWeightsArray->value(i));
208 aWeights.push_back(1.0); // default weight
210 aWeightsArray->setSize(aWeightsArray->size() + 1);
211 std::list<double>::iterator aWIt = aWeights.begin();
212 for (int i = 0; i < aWeightsArray->size(); ++i, ++aWIt)
213 aWeightsArray->setValue(i, *aWIt);
215 // recalculate knots and multiplicities
216 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve(
217 new GeomAPI_BSpline2d(aPoles, aWeights, isPeriodic()));
219 integer(DEGREE_ID())->setValue(aBSplineCurve->degree());
221 AttributeDoubleArrayPtr aKnotsAttr = data()->realArray(SketchPlugin_BSplineBase::KNOTS_ID());
222 std::list<double> aKnots = aBSplineCurve->knots();
223 int aSize = (int)aKnots.size();
224 aKnotsAttr->setSize(aSize);
225 std::list<double>::iterator aKIt = aKnots.begin();
226 for (int index = 0; index < aSize; ++index, ++aKIt)
227 aKnotsAttr->setValue(index, *aKIt);
229 AttributeIntArrayPtr aMultsAttr = data()->intArray(SketchPlugin_BSplineBase::MULTS_ID());
230 std::list<int> aMults = aBSplineCurve->mults();
231 aSize = (int)aMults.size();
232 aMultsAttr->setSize(aSize);
233 std::list<int>::iterator aMIt = aMults.begin();
234 for (int index = 0; index < aSize; ++index, ++aMIt)
235 aMultsAttr->setValue(index, *aMIt);
237 data()->blockSendAttributeUpdated(aWasBlocked, true);
239 // update indices of internal coincidences
240 for (std::list<AttributeIntegerPtr>::iterator aCIt = aCoincidentPoleIndex.begin();
241 aCIt != aCoincidentPoleIndex.end(); ++aCIt)
242 (*aCIt)->setValue((*aCIt)->value() + 1);
244 // create auxiliary segment and pole updating the control polygon
245 SketchPlugin_MacroBSpline::createAuxiliaryPole(aPolesArray, anAfter + 1);
247 SketchPlugin_MacroBSpline::createAuxiliarySegment(aPolesArray, anAfter, anAfter + 1);
249 // update names of features representing control polygon
250 for (std::map<int, FeaturePtr>::iterator anIt = aControlPoles.begin();
251 anIt != aControlPoles.end(); ++anIt) {
252 SketchPlugin_MacroBSpline::assignDefaultNameForAux(anIt->second, aPolesArray, anIt->first + 1);
254 for (std::map<int, FeaturePtr>::iterator anIt = aControlSegments.begin();
255 anIt != aControlSegments.end(); ++anIt) {
256 SketchPlugin_MacroBSpline::assignDefaultNameForAux(anIt->second, aPolesArray,
257 anIt->first + 1, (anIt->first + 2) % aPolesArray->size());