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