]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MacroBSpline.cpp
Salome HOME
Issue #17347: B-Splines in Sketcher
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroBSpline.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_MacroBSpline.h>
21
22 #include <SketchPlugin_BSpline.h>
23 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Point.h>
26 #include <SketchPlugin_Tools.h>
27 #include <SketchPlugin_Sketch.h>
28
29 #include <ModelAPI_AttributeDoubleArray.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeRefAttrList.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_EventReentrantMessage.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36
37 #include <GeomDataAPI_Point2DArray.h>
38
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_EdgeBuilder.h>
41 #include <GeomAlgoAPI_PointBuilder.h>
42
43 #include <GeomAPI_BSpline2d.h>
44
45 #include <sstream>
46
47 // Create Point feature coincident with the B-spline pole
48 static FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
49                                       AttributePoint2DArrayPtr theBSplinePoles,
50                                       const int thePoleIndex);
51 // Create segment between consequtive B-spline poles
52 static void createAuxiliarySegment(FeaturePtr theBSpline,
53                                    AttributePoint2DArrayPtr theBSplinePoles,
54                                    const int thePoleIndex1,
55                                    const int thePoleIndex2);
56 // Create internal coincidence constraint with B-spline pole
57 static void createInternalConstraint(SketchPlugin_Sketch* theSketch,
58                                      AttributePtr thePoint,
59                                      AttributePtr theBSplinePoles,
60                                      const int thePoleIndex);
61
62
63 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
64   : SketchPlugin_SketchEntity(),
65     myDegree(3),
66     myIsPeriodic(false)
67 {
68 }
69
70 void SketchPlugin_MacroBSpline::initAttributes()
71 {
72   data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
73   data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
74
75   data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
76   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
77
78   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
79 }
80
81 void SketchPlugin_MacroBSpline::execute()
82 {
83   FeaturePtr aBSpline = createBSplineFeature();
84
85   std::list<FeaturePtr> aControlPoles;
86   createControlPolygon(aBSpline, aControlPoles);
87   constraintsForPoles(aControlPoles);
88
89   // message to init reentrant operation
90   static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
91   ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
92   // set here the last pole to make coincidence with the start point of the next B-spline curve
93   aMessage->setCreatedFeature(aControlPoles.back());
94   Events_Loop::loop()->send(aMessage);
95 }
96
97 // LCOV_EXCL_START
98 std::string SketchPlugin_MacroBSpline::processEvent(
99                                               const std::shared_ptr<Events_Message>& theMessage)
100 {
101   ReentrantMessagePtr aReentrantMessage =
102       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
103   if (aReentrantMessage) {
104     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
105     ObjectPtr anObject = aReentrantMessage->selectedObject();
106     AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
107     std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
108
109     if (aClickedPoint) {
110       // fill points list (it consists of 2 points to make editable the second one)
111       AttributePoint2DArrayPtr aPointArrayAttr =
112           std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
113       aPointArrayAttr->setSize(2);
114       aPointArrayAttr->setPnt(0, aClickedPoint);
115       aPointArrayAttr->setPnt(1, aClickedPoint);
116
117       // fill weights
118       AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
119       aWeightsArrayAttr->setSize(2);
120       aWeightsArrayAttr->setValue(0, 1.0);
121       aWeightsArrayAttr->setValue(1, 1.0);
122
123       // fill reference attribute
124       AttributeRefAttrListPtr aRefAttrList =
125           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
126       if (anAttribute) {
127         if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
128           if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
129             anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
130         }
131         aRefAttrList->append(anAttribute);
132       }
133     }
134     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
135   }
136   return std::string();
137 }
138 // LCOV_EXCL_STOP
139
140 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
141 {
142   FeaturePtr aBSpline = sketch()->addFeature(SketchPlugin_BSpline::ID());
143
144   aBSpline->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(myDegree);
145
146   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
147       aBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
148   AttributePoint2DArrayPtr aPolesMacro =
149       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
150   aPoles->assign(aPolesMacro);
151
152   AttributeDoubleArrayPtr aWeights =
153       aBSpline->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
154   AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
155   int aSize = aWeightsMacro->size();
156   aWeights->setSize(aSize);
157   for (int index = 0; index < aSize; ++index)
158     aWeights->setValue(index, aWeightsMacro->value(index));
159
160   AttributeDoubleArrayPtr aKnots =
161       aBSpline->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
162   aSize = (int)myKnots.size();
163   aKnots->setSize(aSize);
164   std::list<double>::iterator aKIt = myKnots.begin();
165   for (int index = 0; index < aSize; ++index, ++aKIt)
166     aKnots->setValue(index, *aKIt);
167
168   AttributeIntArrayPtr aMults =
169       aBSpline->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
170   aSize = (int)myMultiplicities.size();
171   aMults->setSize(aSize);
172   std::list<int>::iterator aMIt = myMultiplicities.begin();
173   for (int index = 0; index < aSize; ++index, ++aMIt)
174     aMults->setValue(index, *aMIt);
175
176   aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
177       boolean(AUXILIARY_ID())->value());
178
179   aBSpline->execute();
180
181   return aBSpline;
182 }
183
184 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
185                                                      std::list<FeaturePtr>& thePoles)
186 {
187   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
188       theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
189   int aSize = aPoles->size();
190   // poles
191   for (int index = 0; index < aSize; ++index)
192     thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
193   // segments
194   for (int index = 1; index < aSize; ++index)
195     createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
196 }
197
198 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
199 {
200   AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
201   std::list<std::pair<ObjectPtr, AttributePtr> > aList;
202   if (aRefAttrList)
203     aList = aRefAttrList->list();
204
205   SketchPlugin_Sketch* aSketch = sketch();
206
207   std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
208   std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
209   for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
210     // firstly, check the attribute (in this case the object will be not empty too)
211     if (aLIt->second) {
212       SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
213           SketchPlugin_ConstraintCoincidence::ID(),
214           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
215     }
216     // now add coincidence with the result
217     else if (aLIt->first) {
218       SketchPlugin_Tools::createConstraintAttrObject(aSketch,
219           SketchPlugin_ConstraintCoincidence::ID(),
220           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
221     }
222   }
223 }
224
225 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
226 {
227   SketchPlugin_Sketch* aSketch = sketch();
228   if (!aSketch)
229     return AISObjectPtr();
230
231   static const bool PERIODIC = false;
232
233   AttributePoint2DArrayPtr aPolesArray =
234       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
235   AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
236
237   if (aPolesArray->size() < 2)
238     return AISObjectPtr();
239
240   std::list<GeomShapePtr> aShapes;
241
242   // convert poles to vertices and collect weights
243   std::list<GeomPnt2dPtr> aPoles2D;
244   std::list<double> aWeights;
245   for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
246     double aWeight = aWeightsArray->value(anIndex);
247     if (aWeight < 1.e-10)
248       continue; // skip poles with zero weights
249
250     aWeights.push_back(aWeight);
251
252     GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
253     aPoles2D.push_back(aPole);
254     GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
255     aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
256   }
257
258   // create result non-periodic B-spline curve
259   std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
260   try {
261     aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, PERIODIC));
262   } catch (...) {
263     // cannot build a B-spline curve
264     return AISObjectPtr();
265   }
266   GeomShapePtr anEdge =
267       GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
268   if (!anEdge)
269     return AISObjectPtr();
270
271   // store transient parameters of B-spline curve
272   myDegree = aBSplineCurve->degree();
273   myKnots = aBSplineCurve->knots();
274   myMultiplicities = aBSplineCurve->mults();
275
276   aShapes.push_back(anEdge);
277   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
278
279   AISObjectPtr anAIS = thePrevious;
280   if (!anAIS)
281     anAIS.reset(new GeomAPI_AISObject());
282   anAIS->createShape(aCompound);
283
284   // Modify attributes
285   SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
286
287   return anAIS;
288 }
289
290
291
292 // ==========================     Auxiliary functions    ===========================================
293
294 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
295                                AttributePoint2DArrayPtr theBSplinePoles,
296                                const int thePoleIndex)
297 {
298   SketchPlugin_Sketch* aSketch =
299       std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
300
301   // create child point equal to the B-spline's pole
302   FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
303   aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
304   aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
305
306   GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
307
308   AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
309       aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
310   aCoord->setValue(aPole);
311
312   aPointFeature->execute();
313
314   std::ostringstream aName;
315   aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
316   aPointFeature->data()->setName(aName.str());
317   aPointFeature->lastResult()->data()->setName(aName.str());
318
319   // internal constraint to keep position of the point
320   createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
321
322   return aPointFeature;
323 }
324
325 void createAuxiliarySegment(FeaturePtr theBSpline,
326                             AttributePoint2DArrayPtr theBSplinePoles,
327                             const int thePoleIndex1,
328                             const int thePoleIndex2)
329 {
330   SketchPlugin_Sketch* aSketch =
331     std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
332
333   // create child segment between B-spline poles
334   FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
335   aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
336   aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
337
338   AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
339     aLineFeature->attribute(SketchPlugin_Line::START_ID()));
340   aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
341
342   AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
343     aLineFeature->attribute(SketchPlugin_Line::END_ID()));
344   aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
345
346   aLineFeature->execute();
347
348   std::ostringstream aName;
349   aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
350   aLineFeature->data()->setName(aName.str());
351   aLineFeature->lastResult()->data()->setName(aName.str());
352
353   // internal constraints to keep the segment position
354   createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
355   createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
356 }
357
358 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
359                               AttributePtr thePoint,
360                               AttributePtr theBSplinePoles,
361                               const int thePoleIndex)
362 {
363   std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
364       std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
365       theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
366   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
367   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
368   aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
369       ->setValue(thePoleIndex);
370 }