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(CONTROL_POLYGON_ID(), ModelAPI_AttributeBoolean::typeId());
80 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
83 void SketchPlugin_MacroBSpline::execute()
85 FeaturePtr aBSpline = createBSplineFeature();
87 if (boolean(CONTROL_POLYGON_ID())->value()) {
88 std::list<FeaturePtr> aControlPoles;
89 createControlPolygon(aBSpline, aControlPoles);
90 constraintsForPoles(aControlPoles);
92 // message to init reentrant operation
93 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
94 ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
95 // set here the last pole to make coincidence with the start point of the next B-spline curve
96 aMessage->setCreatedFeature(aControlPoles.back());
97 Events_Loop::loop()->send(aMessage);
102 std::string SketchPlugin_MacroBSpline::processEvent(
103 const std::shared_ptr<Events_Message>& theMessage)
105 ReentrantMessagePtr aReentrantMessage =
106 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
107 if (aReentrantMessage) {
108 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
109 ObjectPtr anObject = aReentrantMessage->selectedObject();
110 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
111 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
114 // fill points list (it consists of 2 points to make editable the second one)
115 AttributePoint2DArrayPtr aPointArrayAttr =
116 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
117 aPointArrayAttr->setSize(2);
118 aPointArrayAttr->setPnt(0, aClickedPoint);
119 aPointArrayAttr->setPnt(1, aClickedPoint);
122 AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
123 aWeightsArrayAttr->setSize(2);
124 aWeightsArrayAttr->setValue(0, 1.0);
125 aWeightsArrayAttr->setValue(1, 1.0);
127 // fill reference attribute
128 AttributeRefAttrListPtr aRefAttrList =
129 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
131 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
132 if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
133 anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
135 aRefAttrList->append(anAttribute);
138 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
140 return std::string();
144 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
146 FeaturePtr aBSpline = sketch()->addFeature(SketchPlugin_BSpline::ID());
148 aBSpline->integer(SketchPlugin_BSpline::DEGREE_ID())->setValue(myDegree);
150 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
151 aBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
152 AttributePoint2DArrayPtr aPolesMacro =
153 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
154 aPoles->assign(aPolesMacro);
156 AttributeDoubleArrayPtr aWeights =
157 aBSpline->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
158 AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
159 int aSize = aWeightsMacro->size();
160 aWeights->setSize(aSize);
161 for (int index = 0; index < aSize; ++index)
162 aWeights->setValue(index, aWeightsMacro->value(index));
164 AttributeDoubleArrayPtr aKnots =
165 aBSpline->data()->realArray(SketchPlugin_BSpline::KNOTS_ID());
166 aSize = (int)myKnots.size();
167 aKnots->setSize(aSize);
168 std::list<double>::iterator aKIt = myKnots.begin();
169 for (int index = 0; index < aSize; ++index, ++aKIt)
170 aKnots->setValue(index, *aKIt);
172 AttributeIntArrayPtr aMults =
173 aBSpline->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
174 aSize = (int)myMultiplicities.size();
175 aMults->setSize(aSize);
176 std::list<int>::iterator aMIt = myMultiplicities.begin();
177 for (int index = 0; index < aSize; ++index, ++aMIt)
178 aMults->setValue(index, *aMIt);
180 SketchPlugin_Sketch* aSketch =
181 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBSpline)->sketch();
183 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184 aBSpline->attribute(SketchPlugin_BSpline::START_ID()));
185 aStartPoint->setValue(aPoles->pnt(0));
186 // internal constraint to keep position of the point
187 createInternalConstraint(aSketch, aStartPoint, aPoles, 0);
189 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aBSpline->attribute(SketchPlugin_BSpline::END_ID()));
191 aEndPoint->setValue(aPoles->pnt(aPoles->size() - 1));
192 // internal constraint to keep position of the point
193 createInternalConstraint(aSketch, aEndPoint, aPoles, aPoles->size() - 1);
195 aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
196 boolean(AUXILIARY_ID())->value());
203 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
204 std::list<FeaturePtr>& thePoles)
206 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
207 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
208 int aSize = aPoles->size();
210 for (int index = 0; index < aSize; ++index)
211 thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
213 for (int index = 1; index < aSize; ++index)
214 createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
217 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
219 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
220 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
222 aList = aRefAttrList->list();
224 SketchPlugin_Sketch* aSketch = sketch();
226 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
227 std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
228 for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
229 // firstly, check the attribute (in this case the object will be not empty too)
231 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
232 SketchPlugin_ConstraintCoincidence::ID(),
233 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
235 // now add coincidence with the result
236 else if (aLIt->first) {
237 SketchPlugin_Tools::createConstraintAttrObject(aSketch,
238 SketchPlugin_ConstraintCoincidence::ID(),
239 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
244 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
246 SketchPlugin_Sketch* aSketch = sketch();
248 return AISObjectPtr();
250 static const bool PERIODIC = false;
252 AttributePoint2DArrayPtr aPolesArray =
253 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
254 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
256 if (aPolesArray->size() < 2)
257 return AISObjectPtr();
259 std::list<GeomShapePtr> aShapes;
261 // convert poles to vertices and collect weights
262 std::list<GeomPnt2dPtr> aPoles2D;
263 std::list<double> aWeights;
264 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
265 double aWeight = aWeightsArray->value(anIndex);
266 if (aWeight < 1.e-10)
267 continue; // skip poles with zero weights
269 aWeights.push_back(aWeight);
271 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
272 aPoles2D.push_back(aPole);
273 GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
274 aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
277 // create result non-periodic B-spline curve
278 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
280 aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, PERIODIC));
282 // cannot build a B-spline curve
283 return AISObjectPtr();
285 GeomShapePtr anEdge =
286 GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
288 return AISObjectPtr();
290 // store transient parameters of B-spline curve
291 myDegree = aBSplineCurve->degree();
292 myKnots = aBSplineCurve->knots();
293 myMultiplicities = aBSplineCurve->mults();
295 aShapes.push_back(anEdge);
296 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
298 AISObjectPtr anAIS = thePrevious;
300 anAIS.reset(new GeomAPI_AISObject());
301 anAIS->createShape(aCompound);
304 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
311 // ========================== Auxiliary functions ===========================================
313 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
314 AttributePoint2DArrayPtr theBSplinePoles,
315 const int thePoleIndex)
317 SketchPlugin_Sketch* aSketch =
318 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
320 // create child point equal to the B-spline's pole
321 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
322 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
323 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
325 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
327 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
328 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
329 aCoord->setValue(aPole);
331 aPointFeature->execute();
333 std::ostringstream aName;
334 aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
335 aPointFeature->data()->setName(aName.str());
336 aPointFeature->lastResult()->data()->setName(aName.str());
338 // internal constraint to keep position of the point
339 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
341 return aPointFeature;
344 void createAuxiliarySegment(FeaturePtr theBSpline,
345 AttributePoint2DArrayPtr theBSplinePoles,
346 const int thePoleIndex1,
347 const int thePoleIndex2)
349 SketchPlugin_Sketch* aSketch =
350 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
352 // create child segment between B-spline poles
353 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
354 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
355 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
357 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
358 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
359 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
361 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
362 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
363 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
365 aLineFeature->execute();
367 std::ostringstream aName;
368 aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
369 aLineFeature->data()->setName(aName.str());
370 aLineFeature->lastResult()->data()->setName(aName.str());
372 // internal constraints to keep the segment position
373 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
374 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
377 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
378 AttributePtr thePoint,
379 AttributePtr theBSplinePoles,
380 const int thePoleIndex)
382 std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
383 std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
384 theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
385 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
386 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
387 aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
388 ->setValue(thePoleIndex);