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_ConstraintCoincidenceInternal.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Point.h>
26 #include <SketchPlugin_Tools.h>
27 #include <SketchPlugin_Sketch.h>
29 #include <ModelAPI_AttributeDoubleArray.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeRefAttrList.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_EventReentrantMessage.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 Point feature coincident with the B-spline pole
48 static FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
49 AttributePoint2DArrayPtr theBSplinePoles,
50 const int thePoleIndex);
51 // Create segment between consequtive B-spline poles
52 static void createAuxiliarySegment(FeaturePtr theBSpline,
53 AttributePoint2DArrayPtr theBSplinePoles,
54 const int thePoleIndex1,
55 const int thePoleIndex2);
56 // Create internal coincidence constraint with B-spline pole
57 static void createInternalConstraint(SketchPlugin_Sketch* theSketch,
58 AttributePtr thePoint,
59 AttributePtr theBSplinePoles,
60 const int thePoleIndex);
63 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
64 : SketchPlugin_SketchEntity(),
70 void SketchPlugin_MacroBSpline::initAttributes()
72 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
73 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
75 data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
78 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
81 void SketchPlugin_MacroBSpline::execute()
83 FeaturePtr aBSpline = createBSplineFeature();
85 std::list<FeaturePtr> aControlPoles;
86 createControlPolygon(aBSpline, aControlPoles);
87 constraintsForPoles(aControlPoles);
89 // message to init reentrant operation
90 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
91 ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
92 // set here the last pole to make coincidence with the start point of the next B-spline curve
93 aMessage->setCreatedFeature(aControlPoles.back());
94 Events_Loop::loop()->send(aMessage);
98 std::string SketchPlugin_MacroBSpline::processEvent(
99 const std::shared_ptr<Events_Message>& theMessage)
101 ReentrantMessagePtr aReentrantMessage =
102 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
103 if (aReentrantMessage) {
104 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
105 ObjectPtr anObject = aReentrantMessage->selectedObject();
106 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
107 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
110 // fill points list (it consists of 2 points to make editable the second one)
111 AttributePoint2DArrayPtr aPointArrayAttr =
112 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
113 aPointArrayAttr->setSize(2);
114 aPointArrayAttr->setPnt(0, aClickedPoint);
115 aPointArrayAttr->setPnt(1, aClickedPoint);
118 AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
119 aWeightsArrayAttr->setSize(2);
120 aWeightsArrayAttr->setValue(0, 1.0);
121 aWeightsArrayAttr->setValue(1, 1.0);
123 // fill reference attribute
124 AttributeRefAttrListPtr aRefAttrList =
125 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
127 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
128 if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
129 anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
131 aRefAttrList->append(anAttribute);
134 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
136 return std::string();
140 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
142 FeaturePtr aBSpline = sketch()->addFeature(SketchPlugin_BSpline::ID());
144 aBSpline->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(myDegree);
146 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
147 aBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
148 AttributePoint2DArrayPtr aPolesMacro =
149 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
150 aPoles->assign(aPolesMacro);
152 AttributeDoubleArrayPtr aWeights =
153 aBSpline->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
154 AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
155 int aSize = aWeightsMacro->size();
156 aWeights->setSize(aSize);
157 for (int index = 0; index < aSize; ++index)
158 aWeights->setValue(index, aWeightsMacro->value(index));
160 AttributeDoubleArrayPtr aKnots =
161 aBSpline->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
162 aSize = (int)myKnots.size();
163 aKnots->setSize(aSize);
164 std::list<double>::iterator aKIt = myKnots.begin();
165 for (int index = 0; index < aSize; ++index, ++aKIt)
166 aKnots->setValue(index, *aKIt);
168 AttributeIntArrayPtr aMults =
169 aBSpline->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
170 aSize = (int)myMultiplicities.size();
171 aMults->setSize(aSize);
172 std::list<int>::iterator aMIt = myMultiplicities.begin();
173 for (int index = 0; index < aSize; ++index, ++aMIt)
174 aMults->setValue(index, *aMIt);
176 aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
177 boolean(AUXILIARY_ID())->value());
184 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
185 std::list<FeaturePtr>& thePoles)
187 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
188 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
189 int aSize = aPoles->size();
191 for (int index = 0; index < aSize; ++index)
192 thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
194 for (int index = 1; index < aSize; ++index)
195 createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
198 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
200 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
201 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
203 aList = aRefAttrList->list();
205 SketchPlugin_Sketch* aSketch = sketch();
207 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
208 std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
209 for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
210 // firstly, check the attribute (in this case the object will be not empty too)
212 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
213 SketchPlugin_ConstraintCoincidence::ID(),
214 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
216 // now add coincidence with the result
217 else if (aLIt->first) {
218 SketchPlugin_Tools::createConstraintAttrObject(aSketch,
219 SketchPlugin_ConstraintCoincidence::ID(),
220 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
225 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
227 SketchPlugin_Sketch* aSketch = sketch();
229 return AISObjectPtr();
231 static const bool PERIODIC = false;
233 AttributePoint2DArrayPtr aPolesArray =
234 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
235 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
237 if (aPolesArray->size() < 2)
238 return AISObjectPtr();
240 std::list<GeomShapePtr> aShapes;
242 // convert poles to vertices and collect weights
243 std::list<GeomPnt2dPtr> aPoles2D;
244 std::list<double> aWeights;
245 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
246 double aWeight = aWeightsArray->value(anIndex);
247 if (aWeight < 1.e-10)
248 continue; // skip poles with zero weights
250 aWeights.push_back(aWeight);
252 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
253 aPoles2D.push_back(aPole);
254 GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
255 aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
258 // create result non-periodic B-spline curve
259 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
261 aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, PERIODIC));
263 // cannot build a B-spline curve
264 return AISObjectPtr();
266 GeomShapePtr anEdge =
267 GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
269 return AISObjectPtr();
271 // store transient parameters of B-spline curve
272 myDegree = aBSplineCurve->degree();
273 myKnots = aBSplineCurve->knots();
274 myMultiplicities = aBSplineCurve->mults();
276 aShapes.push_back(anEdge);
277 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
279 AISObjectPtr anAIS = thePrevious;
281 anAIS.reset(new GeomAPI_AISObject());
282 anAIS->createShape(aCompound);
285 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
292 // ========================== Auxiliary functions ===========================================
294 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
295 AttributePoint2DArrayPtr theBSplinePoles,
296 const int thePoleIndex)
298 SketchPlugin_Sketch* aSketch =
299 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
301 // create child point equal to the B-spline's pole
302 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
303 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
304 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
306 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
308 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
309 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
310 aCoord->setValue(aPole);
312 aPointFeature->execute();
314 std::ostringstream aName;
315 aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
316 aPointFeature->data()->setName(aName.str());
317 aPointFeature->lastResult()->data()->setName(aName.str());
319 // internal constraint to keep position of the point
320 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
322 return aPointFeature;
325 void createAuxiliarySegment(FeaturePtr theBSpline,
326 AttributePoint2DArrayPtr theBSplinePoles,
327 const int thePoleIndex1,
328 const int thePoleIndex2)
330 SketchPlugin_Sketch* aSketch =
331 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
333 // create child segment between B-spline poles
334 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
335 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
336 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
338 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
339 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
340 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
342 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
343 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
344 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
346 aLineFeature->execute();
348 std::ostringstream aName;
349 aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
350 aLineFeature->data()->setName(aName.str());
351 aLineFeature->lastResult()->data()->setName(aName.str());
353 // internal constraints to keep the segment position
354 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
355 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
358 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
359 AttributePtr thePoint,
360 AttributePtr theBSplinePoles,
361 const int thePoleIndex)
363 std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
364 std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
365 theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
366 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
367 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
368 aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
369 ->setValue(thePoleIndex);