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>
45 // Create Point feature coincident with the B-spline pole
46 static FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
47 AttributePoint2DArrayPtr theBSplinePoles,
48 const int thePoleIndex);
49 // Create segment between consequtive B-spline poles
50 static void createAuxiliarySegment(FeaturePtr theBSpline,
51 AttributePoint2DArrayPtr theBSplinePoles,
52 const int thePoleIndex1,
53 const int thePoleIndex2);
54 // Create internal coincidence constraint with B-spline pole
55 static void createInternalConstraint(SketchPlugin_Sketch* theSketch,
56 AttributePtr thePoint,
57 AttributePtr theBSplinePoles,
58 const int thePoleIndex);
61 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
62 : SketchPlugin_SketchEntity(),
67 void SketchPlugin_MacroBSpline::initAttributes()
69 data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
70 data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
72 data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
75 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
78 void SketchPlugin_MacroBSpline::execute()
80 FeaturePtr aBSpline = createBSplineFeature();
82 std::list<FeaturePtr> aControlPoles;
83 createControlPolygon(aBSpline, aControlPoles);
84 constraintsForPoles(aControlPoles);
86 // message to init reentrant operation
87 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
88 ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
89 // set here the last pole to make coincidence with the start point of the next B-spline curve
90 aMessage->setCreatedFeature(aControlPoles.back());
91 Events_Loop::loop()->send(aMessage);
95 std::string SketchPlugin_MacroBSpline::processEvent(
96 const std::shared_ptr<Events_Message>& theMessage)
98 ReentrantMessagePtr aReentrantMessage =
99 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
100 if (aReentrantMessage) {
101 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
102 ObjectPtr anObject = aReentrantMessage->selectedObject();
103 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
104 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
107 // fill points list (it consists of 2 points to make editable the second one)
108 AttributePoint2DArrayPtr aPointArrayAttr =
109 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
110 aPointArrayAttr->setSize(2);
111 aPointArrayAttr->setPnt(0, aClickedPoint);
112 aPointArrayAttr->setPnt(1, aClickedPoint);
115 AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
116 aWeightsArrayAttr->setSize(2);
117 aWeightsArrayAttr->setValue(0, 1.0);
118 aWeightsArrayAttr->setValue(1, 1.0);
120 // fill reference attribute
121 AttributeRefAttrListPtr aRefAttrList =
122 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
124 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
125 if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
126 anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
128 aRefAttrList->append(anAttribute);
131 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
133 return std::string();
137 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
139 FeaturePtr aBSpline = sketch()->addFeature(SketchPlugin_BSpline::ID());
141 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
142 aBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
143 AttributePoint2DArrayPtr aPolesMacro =
144 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
145 aPoles->assign(aPolesMacro);
147 AttributeDoubleArrayPtr aWeights =
148 aBSpline->data()->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
149 AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
150 int aSize = aWeightsMacro->size();
151 aWeights->setSize(aSize);
152 for (int index = 0; index < aSize; ++index)
153 aWeights->setValue(index, aWeightsMacro->value(index));
155 aBSpline->boolean(SketchPlugin_BSpline::AUXILIARY_ID())->setValue(
156 boolean(AUXILIARY_ID())->value());
163 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
164 std::list<FeaturePtr>& thePoles)
166 AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
167 theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
168 int aSize = aPoles->size();
170 for (int index = 0; index < aSize; ++index)
171 thePoles.push_back(createAuxiliaryPole(theBSpline, aPoles, index));
173 for (int index = 1; index < aSize; ++index)
174 createAuxiliarySegment(theBSpline, aPoles, index - 1, index);
177 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
179 AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
180 std::list<std::pair<ObjectPtr, AttributePtr> > aList;
182 aList = aRefAttrList->list();
184 SketchPlugin_Sketch* aSketch = sketch();
186 std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
187 std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
188 for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
189 // firstly, check the attribute (in this case the object will be not empty too)
191 SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
192 SketchPlugin_ConstraintCoincidence::ID(),
193 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
195 // now add coincidence with the result
196 else if (aLIt->first) {
197 SketchPlugin_Tools::createConstraintAttrObject(aSketch,
198 SketchPlugin_ConstraintCoincidence::ID(),
199 (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
204 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
206 SketchPlugin_Sketch* aSketch = sketch();
208 return AISObjectPtr();
210 AttributePoint2DArrayPtr aPolesArray =
211 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
212 AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
214 std::list<GeomShapePtr> aShapes;
216 // convert poles to 3D and collect weights
217 std::vector<GeomPointPtr> aPoles3D;
218 aPoles3D.reserve(aPolesArray->size());
219 std::vector<double> aWeights;
220 aWeights.reserve(aWeightsArray->size());
221 for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
222 double aWeight = aWeightsArray->value(anIndex);
223 if (aWeight < 1.e-10)
224 continue; // skip poles with zero weights
226 aWeights.push_back(aWeight);
228 GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
229 GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
230 aPoles3D.push_back(aPole3D);
231 aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
234 // create result non-periodic B-spline curve
235 GeomShapePtr anEdge = GeomAlgoAPI_EdgeBuilder::bspline(aPoles3D, aWeights, false);
237 return AISObjectPtr();
239 aShapes.push_back(anEdge);
240 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
242 AISObjectPtr anAIS = thePrevious;
244 anAIS.reset(new GeomAPI_AISObject());
245 anAIS->createShape(aCompound);
248 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
255 // ========================== Auxiliary functions ===========================================
257 FeaturePtr createAuxiliaryPole(FeaturePtr theBSpline,
258 AttributePoint2DArrayPtr theBSplinePoles,
259 const int thePoleIndex)
261 SketchPlugin_Sketch* aSketch =
262 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
264 // create child point equal to the B-spline's pole
265 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
266 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
267 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
269 GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
271 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
273 aCoord->setValue(aPole);
275 aPointFeature->execute();
277 std::ostringstream aName;
278 aName << theBSpline->name() << "_" << theBSplinePoles->id() << "_" << thePoleIndex;
279 aPointFeature->data()->setName(aName.str());
280 aPointFeature->lastResult()->data()->setName(aName.str());
282 // internal constraint to keep position of the point
283 createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
285 return aPointFeature;
288 void createAuxiliarySegment(FeaturePtr theBSpline,
289 AttributePoint2DArrayPtr theBSplinePoles,
290 const int thePoleIndex1,
291 const int thePoleIndex2)
293 SketchPlugin_Sketch* aSketch =
294 std::dynamic_pointer_cast<SketchPlugin_Feature>(theBSpline)->sketch();
296 // create child segment between B-spline poles
297 FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
298 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
299 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
301 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
302 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
303 aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
305 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
306 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
307 aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
309 aLineFeature->execute();
311 std::ostringstream aName;
312 aName << theBSpline->name() << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
313 aLineFeature->data()->setName(aName.str());
314 aLineFeature->lastResult()->data()->setName(aName.str());
316 // internal constraints to keep the segment position
317 createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
318 createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
321 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
322 AttributePtr thePoint,
323 AttributePtr theBSplinePoles,
324 const int thePoleIndex)
326 std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
327 std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
328 theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
329 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
330 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
331 aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
332 ->setValue(thePoleIndex);