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 aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
161 boolean(AUXILIARY_ID())->value());
168 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
169 std::list<FeaturePtr>& thePoles)
171 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
172 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
173 int aSize = aPoles->size();
175 for (int index = 0; index < aSize; ++index)
176 thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
178 for (int index = 1; index < aSize; ++index)
179 createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
182 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
184 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
185 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
187 aList = aRefAttrList->list();
189 SketchPlugin_Sketch* aSketch = sketch();
191 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
192 std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
193 for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
194 // firstly, check the attribute (in this case the object will be not empty too)
196 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
197 SketchPlugin_ConstraintCoincidence::ID(),
198 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
200 // now add coincidence with the result
201 else if (aLIt->first) {
202 SketchPlugin_Tools::createConstraintAttrObject(aSketch,
203 SketchPlugin_ConstraintCoincidence::ID(),
204 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
209 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
211 SketchPlugin_Sketch* aSketch = sketch();
213 return AISObjectPtr();
215 static const bool PERIODIC = false;
217 AttributePoint2DArrayPtr aPolesArray =
218 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
219 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
221 if (aPolesArray->size() < 2)
222 return AISObjectPtr();
224 std::list<GeomShapePtr> aShapes;
226 // convert poles to vertices and collect weights
227 std::list<GeomPnt2dPtr> aPoles2D;
228 std::list<double> aWeights;
229 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
230 double aWeight = aWeightsArray->value(anIndex);
231 if (aWeight < 1.e-10)
232 continue; // skip poles with zero weights
234 aWeights.push_back(aWeight);
236 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
237 aPoles2D.push_back(aPole);
238 GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
239 aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
242 // create result non-periodic B-spline curve
243 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
245 aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, PERIODIC));
247 // cannot build a B-spline curve
248 return AISObjectPtr();
250 GeomShapePtr anEdge =
251 GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->plane(), aBSplineCurve);
253 return AISObjectPtr();
255 myDegree = aBSplineCurve->degree();
257 aShapes.push_back(anEdge);
258 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
260 AISObjectPtr anAIS = thePrevious;
262 anAIS.reset(new GeomAPI_AISObject());
263 anAIS->createShape(aCompound);
266 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
273 // ========================== Auxiliary functions ===========================================
275 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
276 AttributePoint2DArrayPtr theBSplinePoles,
277 const int thePoleIndex)
279 SketchPlugin_Sketch* aSketch =
280 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
282 // create child point equal to the B-spline's pole
283 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
284 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
285 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
287 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
289 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
290 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
291 aCoord->setValue(aPole);
293 aPointFeature->execute();
295 std::ostringstream aName;
296 aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
297 aPointFeature->data()->setName(aName.str());
298 aPointFeature->lastResult()->data()->setName(aName.str());
300 // internal constraint to keep position of the point
301 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
303 return aPointFeature;
306 void createAuxiliarySegment(FeaturePtr theBSpline,
307 AttributePoint2DArrayPtr theBSplinePoles,
308 const int thePoleIndex1,
309 const int thePoleIndex2)
311 SketchPlugin_Sketch* aSketch =
312 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
314 // create child segment between B-spline poles
315 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
316 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
317 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
319 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
320 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
321 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
323 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
324 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
325 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
327 aLineFeature->execute();
329 std::ostringstream aName;
330 aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
331 aLineFeature->data()->setName(aName.str());
332 aLineFeature->lastResult()->data()->setName(aName.str());
334 // internal constraints to keep the segment position
335 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
336 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
339 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
340 AttributePtr thePoint,
341 AttributePtr theBSplinePoles,
342 const int thePoleIndex)
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);