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