]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MacroBSpline.cpp
Salome HOME
0b81547a3dbcafe0344ac313374d94acddfe80b2
[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   aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
161       boolean(AUXILIARY_ID())->value());
162
163   aBSpline->execute();
164
165   return aBSpline;
166 }
167
168 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
169                                                      std::list<FeaturePtr>& thePoles)
170 {
171   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
172       theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
173   int aSize = aPoles->size();
174   // poles
175   for (int index = 0; index < aSize; ++index)
176     thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
177   // segments
178   for (int index = 1; index < aSize; ++index)
179     createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
180 }
181
182 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
183 {
184   AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
185   std::list<std::pair<ObjectPtr, AttributePtr> > aList;
186   if (aRefAttrList)
187     aList = aRefAttrList->list();
188
189   SketchPlugin_Sketch* aSketch = sketch();
190
191   std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
192   std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
193   for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
194     // firstly, check the attribute (in this case the object will be not empty too)
195     if (aLIt->second) {
196       SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
197           SketchPlugin_ConstraintCoincidence::ID(),
198           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
199     }
200     // now add coincidence with the result
201     else if (aLIt->first) {
202       SketchPlugin_Tools::createConstraintAttrObject(aSketch,
203           SketchPlugin_ConstraintCoincidence::ID(),
204           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
205     }
206   }
207 }
208
209 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
210 {
211   SketchPlugin_Sketch* aSketch = sketch();
212   if (!aSketch)
213     return AISObjectPtr();
214
215   static const bool PERIODIC = false;
216
217   AttributePoint2DArrayPtr aPolesArray =
218       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
219   AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
220
221   if (aPolesArray->size() < 2)
222     return AISObjectPtr();
223
224   std::list<GeomShapePtr> aShapes;
225
226   // convert poles to vertices and collect weights
227   std::list<GeomPnt2dPtr> aPoles2D;
228   std::list<double> aWeights;
229   for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
230     double aWeight = aWeightsArray->value(anIndex);
231     if (aWeight < 1.e-10)
232       continue; // skip poles with zero weights
233
234     aWeights.push_back(aWeight);
235
236     GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
237     aPoles2D.push_back(aPole);
238     GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
239     aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
240   }
241
242   // create result non-periodic B-spline curve
243   std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
244   try {
245     aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, PERIODIC));
246   } catch (...) {
247     // cannot build a B-spline curve
248     return AISObjectPtr();
249   }
250   GeomShapePtr anEdge =
251       GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->plane(), aBSplineCurve);
252   if (!anEdge)
253     return AISObjectPtr();
254
255   myDegree = aBSplineCurve->degree();
256
257   aShapes.push_back(anEdge);
258   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
259
260   AISObjectPtr anAIS = thePrevious;
261   if (!anAIS)
262     anAIS.reset(new GeomAPI_AISObject());
263   anAIS->createShape(aCompound);
264
265   // Modify attributes
266   SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
267
268   return anAIS;
269 }
270
271
272
273 // ==========================     Auxiliary functions    ===========================================
274
275 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
276                                AttributePoint2DArrayPtr theBSplinePoles,
277                                const int thePoleIndex)
278 {
279   SketchPlugin_Sketch* aSketch =
280       std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
281
282   // create child point equal to the B-spline's pole
283   FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
284   aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
285   aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
286
287   GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
288
289   AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
290       aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
291   aCoord->setValue(aPole);
292
293   aPointFeature->execute();
294
295   std::ostringstream aName;
296   aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
297   aPointFeature->data()->setName(aName.str());
298   aPointFeature->lastResult()->data()->setName(aName.str());
299
300   // internal constraint to keep position of the point
301   createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
302
303   return aPointFeature;
304 }
305
306 void createAuxiliarySegment(FeaturePtr theBSpline,
307                             AttributePoint2DArrayPtr theBSplinePoles,
308                             const int thePoleIndex1,
309                             const int thePoleIndex2)
310 {
311   SketchPlugin_Sketch* aSketch =
312     std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
313
314   // create child segment between B-spline poles
315   FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
316   aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
317   aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
318
319   AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
320     aLineFeature->attribute(SketchPlugin_Line::START_ID()));
321   aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
322
323   AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
324     aLineFeature->attribute(SketchPlugin_Line::END_ID()));
325   aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
326
327   aLineFeature->execute();
328
329   std::ostringstream aName;
330   aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
331   aLineFeature->data()->setName(aName.str());
332   aLineFeature->lastResult()->data()->setName(aName.str());
333
334   // internal constraints to keep the segment position
335   createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
336   createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
337 }
338
339 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
340                               AttributePtr thePoint,
341                               AttributePtr theBSplinePoles,
342                               const int thePoleIndex)
343 {
344   std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
345       std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
346       theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
347   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
348   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
349   aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
350       ->setValue(thePoleIndex);
351 }