1 // Copyright (C) 2019-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <SketchPlugin_MacroBSpline.h>
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>
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>
37 #include <GeomDataAPI_Point2DArray.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_EdgeBuilder.h>
41 #include <GeomAlgoAPI_PointBuilder.h>
43 #include <GeomAPI_BSpline2d.h>
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);
54 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
55 : SketchPlugin_SketchEntity(),
61 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline(bool isPeriodic)
62 : SketchPlugin_SketchEntity(),
64 myIsPeriodic(isPeriodic)
68 void SketchPlugin_MacroBSpline::initAttributes()
70 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
71 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
73 data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
76 data()->addAttribute(CONTROL_POLYGON_ID(), ModelAPI_AttributeBoolean::typeId());
78 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
81 void SketchPlugin_MacroBSpline::execute()
83 FeaturePtr aBSpline = createBSplineFeature();
85 if (boolean(CONTROL_POLYGON_ID())->value()) {
86 std::list<FeaturePtr> aControlPoles;
87 createControlPolygon(aBSpline, myIsPeriodic, aControlPoles);
88 constraintsForPoles(aControlPoles);
92 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
94 if (myKnots.empty() || myMultiplicities.empty())
95 getAISObject(AISObjectPtr()); // fill B-spline parameters
97 FeaturePtr aBSpline = sketch()->addFeature(
98 myIsPeriodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
100 aBSpline->integer(SketchPlugin_BSplineBase::DEGREE_ID())->setValue(myDegree);
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);
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));
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);
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);
132 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aBSpline->attribute(SketchPlugin_BSpline::START_ID()));
134 aStartPoint->setValue(aPoles->pnt(0));
136 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
137 aBSpline->attribute(SketchPlugin_BSpline::END_ID()));
138 aEndPoint->setValue(aPoles->pnt(aPoles->size() - 1));
141 aBSpline->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
142 boolean(AUXILIARY_ID())->value());
149 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
151 std::list<FeaturePtr>& thePoles)
153 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
154 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
155 int aSize = aPoles->size();
157 for (int index = 0; index < aSize; ++index)
158 thePoles.push_back(createAuxiliaryPole(aPoles, index));
160 for (int index = 1; index < aSize; ++index)
161 createAuxiliarySegment(aPoles, index - 1, index);
163 // additional segment to close the control polygon
164 createAuxiliarySegment(aPoles, aSize - 1, 0);
168 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
170 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
171 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
173 aList = aRefAttrList->list();
175 SketchPlugin_Sketch* aSketch = sketch();
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)
182 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
183 SketchPlugin_ConstraintCoincidence::ID(),
184 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
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);
195 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
197 SketchPlugin_Sketch* aSketch = sketch();
199 return AISObjectPtr();
201 AttributePoint2DArrayPtr aPolesArray =
202 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
203 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
205 if (aPolesArray->size() < 2)
206 return AISObjectPtr();
208 std::list<GeomShapePtr> aShapes;
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
218 aWeights.push_back(aWeight);
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));
226 // create result non-periodic B-spline curve
227 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
229 aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, myIsPeriodic));
231 // cannot build a B-spline curve
232 return AISObjectPtr();
234 GeomShapePtr anEdge =
235 GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
237 return AISObjectPtr();
239 // store transient parameters of B-spline curve
240 myDegree = aBSplineCurve->degree();
241 myKnots = aBSplineCurve->knots();
242 myMultiplicities = aBSplineCurve->mults();
244 aShapes.push_back(anEdge);
245 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
247 AISObjectPtr anAIS = thePrevious;
249 anAIS.reset(new GeomAPI_AISObject());
250 anAIS->createShape(aCompound);
253 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
260 // ========================== Auxiliary functions ===========================================
262 void SketchPlugin_MacroBSpline::assignDefaultNameForAux(FeaturePtr theAuxFeature,
263 AttributePoint2DArrayPtr theBSplinePoles,
264 const int thePoleIndex1,
265 const int thePoleIndex2)
267 FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
269 std::ostringstream aName;
270 aName << aBSpline->name();
271 if (theAuxFeature->getKind() == SketchPlugin_Point::ID())
272 aName << "_" << theBSplinePoles->id() << "_" << thePoleIndex1;
274 aName << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
276 theAuxFeature->data()->setName(aName.str());
277 theAuxFeature->lastResult()->data()->setName(aName.str());
280 FeaturePtr SketchPlugin_MacroBSpline::createAuxiliaryPole(AttributePoint2DArrayPtr theBSplinePoles,
281 const int thePoleIndex)
283 FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
285 SketchPlugin_Sketch* aSketch =
286 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBSpline)->sketch();
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);
293 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
295 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
297 aCoord->setValue(aPole);
299 aPointFeature->execute();
300 assignDefaultNameForAux(aPointFeature, theBSplinePoles, thePoleIndex);
302 // internal constraint to keep position of the point
303 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
305 return aPointFeature;
308 void SketchPlugin_MacroBSpline::createAuxiliarySegment(AttributePoint2DArrayPtr theBSplinePoles,
309 const int thePoleIndex1,
310 const int thePoleIndex2)
312 FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
314 SketchPlugin_Sketch* aSketch =
315 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBSpline)->sketch();
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);
322 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
323 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
324 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
326 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
327 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
328 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
330 aLineFeature->execute();
331 assignDefaultNameForAux(aLineFeature, theBSplinePoles, thePoleIndex1, thePoleIndex2);
333 // internal constraints to keep the segment position
334 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
335 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
338 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
339 AttributePtr thePoint,
340 AttributePtr theBSplinePoles,
341 const int thePoleIndex)
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);