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