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_EventReentrantMessage.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
38 #include <GeomDataAPI_Point2DArray.h>
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41 #include <GeomAlgoAPI_EdgeBuilder.h>
42 #include <GeomAlgoAPI_PointBuilder.h>
44 #include <GeomAPI_BSpline2d.h>
48 // Create Point feature coincident with the B-spline pole
49 static FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
50 AttributePoint2DArrayPtr theBSplinePoles,
51 const int thePoleIndex);
52 // Create segment between consequtive B-spline poles
53 static void createAuxiliarySegment(FeaturePtr theBSpline,
54 AttributePoint2DArrayPtr theBSplinePoles,
55 const int thePoleIndex1,
56 const int thePoleIndex2);
57 // Create internal coincidence constraint with B-spline pole
58 static void createInternalConstraint(SketchPlugin_Sketch* theSketch,
59 AttributePtr thePoint,
60 AttributePtr theBSplinePoles,
61 const int thePoleIndex);
64 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
65 : SketchPlugin_SketchEntity(),
71 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline(bool isPeriodic)
72 : SketchPlugin_SketchEntity(),
74 myIsPeriodic(isPeriodic)
78 void SketchPlugin_MacroBSpline::initAttributes()
80 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
81 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
83 data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
84 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
86 data()->addAttribute(CONTROL_POLYGON_ID(), ModelAPI_AttributeBoolean::typeId());
88 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
91 void SketchPlugin_MacroBSpline::execute()
93 FeaturePtr aBSpline = createBSplineFeature();
95 if (boolean(CONTROL_POLYGON_ID())->value()) {
96 std::list<FeaturePtr> aControlPoles;
97 createControlPolygon(aBSpline, aControlPoles);
98 constraintsForPoles(aControlPoles);
100 // message to init reentrant operation
101 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
102 ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
103 // set here the last pole to make coincidence with the start point of the next B-spline curve
104 aMessage->setCreatedFeature(aControlPoles.back());
105 Events_Loop::loop()->send(aMessage);
110 std::string SketchPlugin_MacroBSpline::processEvent(
111 const std::shared_ptr<Events_Message>& theMessage)
113 ReentrantMessagePtr aReentrantMessage =
114 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
115 if (aReentrantMessage) {
116 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
117 ObjectPtr anObject = aReentrantMessage->selectedObject();
118 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
119 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
122 // fill points list (it consists of 2 points to make editable the second one)
123 AttributePoint2DArrayPtr aPointArrayAttr =
124 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
125 aPointArrayAttr->setSize(2);
126 aPointArrayAttr->setPnt(0, aClickedPoint);
127 aPointArrayAttr->setPnt(1, aClickedPoint);
130 AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
131 aWeightsArrayAttr->setSize(2);
132 aWeightsArrayAttr->setValue(0, 1.0);
133 aWeightsArrayAttr->setValue(1, 1.0);
135 // fill reference attribute
136 AttributeRefAttrListPtr aRefAttrList =
137 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
139 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
140 if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
141 anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
143 aRefAttrList->append(anAttribute);
146 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
148 return std::string();
152 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
154 FeaturePtr aBSpline = sketch()->addFeature(
155 myIsPeriodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
157 aBSpline->integer(SketchPlugin_BSplineBase::DEGREE_ID())->setValue(myDegree);
159 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
160 aBSpline->attribute(SketchPlugin_BSplineBase::POLES_ID()));
161 AttributePoint2DArrayPtr aPolesMacro =
162 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
163 aPoles->assign(aPolesMacro);
165 AttributeDoubleArrayPtr aWeights =
166 aBSpline->data()->realArray(SketchPlugin_BSplineBase::WEIGHTS_ID());
167 AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
168 int aSize = aWeightsMacro->size();
169 aWeights->setSize(aSize);
170 for (int index = 0; index < aSize; ++index)
171 aWeights->setValue(index, aWeightsMacro->value(index));
173 AttributeDoubleArrayPtr aKnots =
174 aBSpline->data()->realArray(SketchPlugin_BSplineBase::KNOTS_ID());
175 aSize = (int)myKnots.size();
176 aKnots->setSize(aSize);
177 std::list<double>::iterator aKIt = myKnots.begin();
178 for (int index = 0; index < aSize; ++index, ++aKIt)
179 aKnots->setValue(index, *aKIt);
181 AttributeIntArrayPtr aMults = aBSpline->data()->intArray(SketchPlugin_BSplineBase::MULTS_ID());
182 aSize = (int)myMultiplicities.size();
183 aMults->setSize(aSize);
184 std::list<int>::iterator aMIt = myMultiplicities.begin();
185 for (int index = 0; index < aSize; ++index, ++aMIt)
186 aMults->setValue(index, *aMIt);
189 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aBSpline->attribute(SketchPlugin_BSpline::START_ID()));
191 aStartPoint->setValue(aPoles->pnt(0));
193 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194 aBSpline->attribute(SketchPlugin_BSpline::END_ID()));
195 aEndPoint->setValue(aPoles->pnt(aPoles->size() - 1));
198 aBSpline->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
199 boolean(AUXILIARY_ID())->value());
206 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
207 std::list<FeaturePtr>& thePoles)
209 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
210 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
211 int aSize = aPoles->size();
213 for (int index = 0; index < aSize; ++index)
214 thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
216 for (int index = 1; index < aSize; ++index)
217 createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
219 // additional segment to close the control polygon
220 createAuxiliarySegment(theBSpline, aPoles, aSize - 1, 0);
224 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
226 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
227 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
229 aList = aRefAttrList->list();
231 SketchPlugin_Sketch* aSketch = sketch();
233 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
234 std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
235 for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
236 // firstly, check the attribute (in this case the object will be not empty too)
238 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
239 SketchPlugin_ConstraintCoincidence::ID(),
240 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
242 // now add coincidence with the result
243 else if (aLIt->first) {
244 SketchPlugin_Tools::createConstraintAttrObject(aSketch,
245 SketchPlugin_ConstraintCoincidence::ID(),
246 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
251 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
253 SketchPlugin_Sketch* aSketch = sketch();
255 return AISObjectPtr();
257 AttributePoint2DArrayPtr aPolesArray =
258 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
259 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
261 if (aPolesArray->size() < 2)
262 return AISObjectPtr();
264 std::list<GeomShapePtr> aShapes;
266 // convert poles to vertices and collect weights
267 std::list<GeomPnt2dPtr> aPoles2D;
268 std::list<double> aWeights;
269 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
270 double aWeight = aWeightsArray->value(anIndex);
271 if (aWeight < 1.e-10)
272 continue; // skip poles with zero weights
274 aWeights.push_back(aWeight);
276 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
277 aPoles2D.push_back(aPole);
278 GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
279 aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
282 // create result non-periodic B-spline curve
283 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
285 aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, myIsPeriodic));
287 // cannot build a B-spline curve
288 return AISObjectPtr();
290 GeomShapePtr anEdge =
291 GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
293 return AISObjectPtr();
295 // store transient parameters of B-spline curve
296 myDegree = aBSplineCurve->degree();
297 myKnots = aBSplineCurve->knots();
298 myMultiplicities = aBSplineCurve->mults();
300 aShapes.push_back(anEdge);
301 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
303 AISObjectPtr anAIS = thePrevious;
305 anAIS.reset(new GeomAPI_AISObject());
306 anAIS->createShape(aCompound);
309 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
316 // ========================== Auxiliary functions ===========================================
318 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
319 AttributePoint2DArrayPtr theBSplinePoles,
320 const int thePoleIndex)
322 SketchPlugin_Sketch* aSketch =
323 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
325 // create child point equal to the B-spline's pole
326 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
327 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
328 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
330 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
332 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
333 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
334 aCoord->setValue(aPole);
336 aPointFeature->execute();
338 std::ostringstream aName;
339 aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
340 aPointFeature->data()->setName(aName.str());
341 aPointFeature->lastResult()->data()->setName(aName.str());
343 // internal constraint to keep position of the point
344 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
346 return aPointFeature;
349 void createAuxiliarySegment(FeaturePtr theBSpline,
350 AttributePoint2DArrayPtr theBSplinePoles,
351 const int thePoleIndex1,
352 const int thePoleIndex2)
354 SketchPlugin_Sketch* aSketch =
355 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
357 // create child segment between B-spline poles
358 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
359 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
360 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
362 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
363 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
364 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
366 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
367 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
368 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
370 aLineFeature->execute();
372 std::ostringstream aName;
373 aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
374 aLineFeature->data()->setName(aName.str());
375 aLineFeature->lastResult()->data()->setName(aName.str());
377 // internal constraints to keep the segment position
378 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
379 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
382 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
383 AttributePtr thePoint,
384 AttributePtr theBSplinePoles,
385 const int thePoleIndex)
387 std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
388 std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
389 theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
390 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
391 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
392 aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
393 ->setValue(thePoleIndex);