1 // Copyright (C) 2017-2019 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_MacroEllipse.h>
22 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
23 #include <SketchPlugin_Ellipse.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_MacroArcReentrantMessage.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_Tools.h>
28 #include <SketchPlugin_Sketch.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Events.h>
37 #include <GeomDataAPI_Point2D.h>
39 #include <GeomAPI_Dir2d.h>
40 #include <GeomAPI_Ellipse2d.h>
41 #include <GeomAPI_Vertex.h>
43 #include <GeomAlgoAPI_CompoundBuilder.h>
44 #include <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomAlgoAPI_PointBuilder.h>
48 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
49 : SketchPlugin_SketchEntity(),
55 void SketchPlugin_MacroEllipse::initAttributes()
57 data()->addAttribute(ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
58 data()->addAttribute(EDIT_ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
60 data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
61 data()->addAttribute(FIRST_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
62 data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
63 data()->addAttribute(SECOND_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
64 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
65 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
67 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
69 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
71 string(EDIT_ELLIPSE_TYPE())->setValue("");
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FIRST_POINT_REF_ID());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_POINT_REF_ID());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ELLIPSE_TYPE());
79 void SketchPlugin_MacroEllipse::execute()
81 FeaturePtr anEllipse = createEllipseFeature();
83 std::string aType = string(ELLIPSE_TYPE())->value();
84 if (aType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT())
85 constraintsForEllipseByCenterAxisAndPassed(anEllipse);
86 else if (aType == ELLIPSE_TYPE_BY_AXIS_AND_POINT())
87 constraintsForEllipseByMajoxAxisAndPassed(anEllipse);
89 // message to init reentrant operation
90 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
91 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
92 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
94 std::string anEditType = string(EDIT_ELLIPSE_TYPE())->value();
95 aMessage->setTypeOfCreation(!anEditType.empty() ? anEditType : aType);
96 aMessage->setCreatedFeature(anEllipse);
97 Events_Loop::loop()->send(aMessage);
100 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
102 static const int NB_POINTS = 3;
103 std::string aPointAttrName[NB_POINTS] = { FIRST_POINT_ID(),
106 std::string aPointRefName[NB_POINTS] = { FIRST_POINT_REF_ID(),
107 SECOND_POINT_REF_ID(),
108 PASSED_POINT_REF_ID() };
110 // type of ellipse switched, thus reset all attributes
111 if (theID == ELLIPSE_TYPE()) {
112 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
113 SketchPlugin_Tools::resetAttribute(this, aPointAttrName[aPntIndex]);
114 SketchPlugin_Tools::resetAttribute(this, aPointRefName[aPntIndex]);
118 int aNbInitialized = 0;
119 GeomPnt2dPtr anEllipsePoints[NB_POINTS];
121 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
122 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
123 if (!aPointAttr->isInitialized())
126 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
127 // calculate ellipse parameters
128 GeomPnt2dPtr aPassedPoint =
129 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
130 GeomShapePtr aTangentCurve;
131 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
132 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
134 anEllipsePoints[aNbInitialized++] = aPassedPoint;
137 if (aNbInitialized <= 1)
138 return; // too few points for the ellipse
140 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
141 // ellipse is given by major axis and passing point,
142 // recalculate the first point to be a center
143 anEllipsePoints[0]->setX(0.5 * (anEllipsePoints[0]->x() + anEllipsePoints[1]->x()));
144 anEllipsePoints[0]->setY(0.5 * (anEllipsePoints[0]->y() + anEllipsePoints[1]->y()));
147 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
148 if (aNbInitialized == 2) {
149 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
150 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
151 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
152 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
153 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
155 else if (aNbInitialized == 3) {
156 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
157 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
160 if (!anEllipse || anEllipse->implPtr<void>() == 0)
163 myCenter = anEllipse->center();
164 myFocus = anEllipse->firstFocus();
165 myMajorRadius = anEllipse->majorRadius();
166 myMinorRadius = anEllipse->minorRadius();
168 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
169 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
171 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
172 aMajorRadiusAttr->setValue(myMajorRadius);
173 aMinorRadiusAttr->setValue(myMinorRadius);
174 data()->blockSendAttributeUpdated(aWasBlocked, false);
179 std::string SketchPlugin_MacroEllipse::processEvent(
180 const std::shared_ptr<Events_Message>& theMessage)
182 std::string aFilledAttributeName;
184 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
185 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
186 if (aReentrantMessage) {
187 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
188 std::string anEllipseType = aReentrantMessage->typeOfCreation();
190 string(ELLIPSE_TYPE())->setValue(anEllipseType);
192 aFilledAttributeName = ELLIPSE_TYPE();
193 ObjectPtr anObject = aReentrantMessage->selectedObject();
194 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
195 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
197 if (aClickedPoint && (anObject || anAttribute)) {
198 aFilledAttributeName = FIRST_POINT_ID();
199 std::string aReferenceAttributeName = FIRST_POINT_REF_ID();
201 // fill 2d point attribute
202 AttributePoint2DPtr aPointAttr =
203 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
204 aPointAttr->setValue(aClickedPoint);
206 // fill reference attribute
207 AttributeRefAttrPtr aRefAttr =
208 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
210 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
211 if (aCreatedFeature && anAttribute->id() == FIRST_POINT_ID())
212 anAttribute = aCreatedFeature->attribute(
213 anEllipseType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT() ?
214 SketchPlugin_Ellipse::CENTER_ID() :
215 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
217 aRefAttr->setAttr(anAttribute);
219 else if (anObject.get()) {
220 // if attribute is NULL, only object is defined, it should be processed outside
221 // the feature because it might be an external feature, that will be
222 // removed/created again after restart operation
223 // #2468 - Crash when sketching circles successively on a repetition
224 aFilledAttributeName = ELLIPSE_TYPE();
227 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
229 return aFilledAttributeName;
233 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
234 FeaturePtr theEllipseFeature)
236 // tangency on-the-fly is not applicable for ellipses
237 static const bool isTangencyApplicable = false;
238 // Create constraints.
239 SketchPlugin_Tools::createCoincidenceOrTangency(
240 this, FIRST_POINT_REF_ID(),
241 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
242 ObjectPtr(), isTangencyApplicable);
243 SketchPlugin_Tools::createCoincidenceOrTangency(
244 this, SECOND_POINT_REF_ID(),
245 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
246 ObjectPtr(), isTangencyApplicable);
247 SketchPlugin_Tools::createCoincidenceOrTangency(
248 this, PASSED_POINT_REF_ID(), AttributePtr(),
249 theEllipseFeature->lastResult(), isTangencyApplicable);
252 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
253 FeaturePtr theEllipseFeature)
255 // tangency on-the-fly is not applicable for ellipses
256 static const bool isTangencyApplicable = false;
257 // Create constraints.
258 SketchPlugin_Tools::createCoincidenceOrTangency(
259 this, FIRST_POINT_REF_ID(),
260 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
261 ObjectPtr(), isTangencyApplicable);
262 SketchPlugin_Tools::createCoincidenceOrTangency(
263 this, SECOND_POINT_REF_ID(),
264 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
265 ObjectPtr(), isTangencyApplicable);
266 SketchPlugin_Tools::createCoincidenceOrTangency(
267 this, PASSED_POINT_REF_ID(), AttributePtr(),
268 theEllipseFeature->lastResult(), isTangencyApplicable);
271 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
273 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
275 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
276 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
277 aCenterAttr->setValue(myCenter->x(), myCenter->y());
279 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
280 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
281 aFocusAttr->setValue(myFocus->x(), myFocus->y());
283 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
284 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
286 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
287 boolean(AUXILIARY_ID())->value());
289 aEllipseFeature->execute();
291 // create auxiliary points
292 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()), "FirstFocus");
293 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::SECOND_FOCUS_ID()), "SecondFocus");
294 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()), "MajorAxisNegative");
295 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()), "MajorAxisPositive");
296 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_START_ID()), "MinorAxisNegative");
297 createAuxiliaryPoint(aEllipseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_END_ID()), "MinorAxisPositive");
298 // create auxiliary axes
299 createAuxiliaryAxis(aEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
300 aEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()));
301 createAuxiliaryAxis(aEllipseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_START_ID()),
302 aEllipseFeature->attribute(SketchPlugin_Ellipse::MINOR_AXIS_END_ID()));
304 return aEllipseFeature;
307 void SketchPlugin_MacroEllipse::createAuxiliaryPoint(const AttributePtr& theEllipsePoint,
308 const std::string& theName)
310 FeaturePtr aPointFeature = sketch()->addFeature(SketchPlugin_Point::ID());
311 aPointFeature->data()->setName(theName);
312 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
314 AttributePoint2DPtr anElPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEllipsePoint);
316 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
317 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
318 aCoord->setValue(anElPoint->x(), anElPoint->y());
320 createInternalConstraint(anElPoint, aCoord);
323 void SketchPlugin_MacroEllipse::createAuxiliaryAxis(const AttributePtr& theStartPoint,
324 const AttributePtr& theEndPoint)
326 FeaturePtr aLineFeature = sketch()->addFeature(SketchPlugin_Line::ID());
327 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
329 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theStartPoint);
330 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEndPoint);
332 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
333 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
334 aLineStart->setValue(aStartPoint->x(), aStartPoint->y());
336 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
337 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
338 aLineEnd->setValue(aEndPoint->x(), aEndPoint->y());
340 createInternalConstraint(aStartPoint, aLineStart);
341 createInternalConstraint(aEndPoint, aLineEnd);
344 void SketchPlugin_MacroEllipse::createInternalConstraint(const AttributePtr& thePoint1,
345 const AttributePtr& thePoint2)
347 SketchPlugin_Tools::createConstraintAttrAttr(
348 sketch(), SketchPlugin_ConstraintCoincidenceInternal::ID(), thePoint1, thePoint2);
351 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
353 SketchPlugin_Sketch* aSketch = sketch();
354 if (!aSketch || !myCenter || myMajorRadius == 0)
355 return AISObjectPtr();
357 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
358 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
360 // Compute a ellipse in 3D view.
361 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
362 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
363 GeomDirPtr aNormal = aNDir->dir();
364 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
365 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
367 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
368 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
369 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
370 if (!anEllipseShape.get() || !aCenterPointShape.get())
371 return AISObjectPtr();
373 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
374 aShapes.push_back(anEllipseShape);
375 aShapes.push_back(aCenterPointShape);
377 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
378 AISObjectPtr anAIS = thePrevious;
380 anAIS.reset(new GeomAPI_AISObject());
381 anAIS->createShape(aCompound);