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