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