1 // Copyright (C) 2017-2023 CEA, EDF
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>
47 static const double TOLERANCE = 1.e-7;
49 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
50 : SketchPlugin_SketchEntity(),
56 void SketchPlugin_MacroEllipse::initAttributes()
58 data()->addAttribute(ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
59 data()->addAttribute(EDIT_ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
61 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
63 data()->addAttribute(MAJOR_AXIS_POINT_ID(), GeomDataAPI_Point2D::typeId());
64 data()->addAttribute(MAJOR_AXIS_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
65 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
66 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
68 data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
69 data()->addAttribute(MAJOR_AXIS_START_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
70 data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
71 data()->addAttribute(MAJOR_AXIS_END_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
72 data()->addAttribute(PASSED_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
73 data()->addAttribute(PASSED_POINT_1_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
75 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
76 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
77 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
79 string(EDIT_ELLIPSE_TYPE())->setValue("");
81 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
82 ModelAPI_Session::get()->validators()->registerNotObligatory(
83 getKind(), MAJOR_AXIS_POINT_REF_ID());
84 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
85 ModelAPI_Session::get()->validators()->registerNotObligatory(
86 getKind(), MAJOR_AXIS_START_REF_ID());
87 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_REF_ID());
88 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_1_REF_ID());
89 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ELLIPSE_TYPE());
92 void SketchPlugin_MacroEllipse::execute()
94 FeaturePtr anEllipse = createEllipseFeature();
96 std::string aType = string(ELLIPSE_TYPE())->value();
97 if (aType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT())
98 constraintsForEllipseByCenterAxisAndPassed(anEllipse);
99 else if (aType == ELLIPSE_TYPE_BY_AXIS_AND_POINT())
100 constraintsForEllipseByMajoxAxisAndPassed(anEllipse);
102 // message to init reentrant operation
103 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
104 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
105 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
107 std::string anEditType = string(EDIT_ELLIPSE_TYPE())->value();
108 aMessage->setTypeOfCreation(!anEditType.empty() ? anEditType : aType);
109 aMessage->setCreatedFeature(anEllipse);
110 Events_Loop::loop()->send(aMessage);
113 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
115 static const int NB_POINTS = 3;
116 std::string aPointAttrName[NB_POINTS];
117 std::string aPointRefName[NB_POINTS];
118 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT()) {
119 aPointAttrName[0] = CENTER_POINT_ID();
120 aPointAttrName[1] = MAJOR_AXIS_POINT_ID();
121 aPointAttrName[2] = PASSED_POINT_ID();
122 aPointRefName[0] = CENTER_POINT_REF_ID();
123 aPointRefName[1] = MAJOR_AXIS_POINT_REF_ID();
124 aPointRefName[2] = PASSED_POINT_REF_ID();
125 } else if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
126 aPointAttrName[0] = MAJOR_AXIS_START_ID();
127 aPointAttrName[1] = MAJOR_AXIS_END_ID();
128 aPointAttrName[2] = PASSED_POINT_1_ID();
129 aPointRefName[0] = MAJOR_AXIS_START_REF_ID();
130 aPointRefName[1] = MAJOR_AXIS_END_REF_ID();
131 aPointRefName[2] = PASSED_POINT_1_REF_ID();
136 // type of ellipse switched, thus reset all attributes
137 if (theID == ELLIPSE_TYPE()) {
138 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
139 SketchPlugin_Tools::resetAttribute(this, aPointAttrName[aPntIndex]);
140 SketchPlugin_Tools::resetAttribute(this, aPointRefName[aPntIndex]);
144 int aNbInitialized = 0;
145 GeomPnt2dPtr anEllipsePoints[NB_POINTS];
147 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
148 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
149 if (!aPointAttr->isInitialized())
152 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
153 // calculate ellipse parameters
154 GeomPnt2dPtr aPassedPoint =
155 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
156 GeomShapePtr aTangentCurve;
157 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
158 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
160 anEllipsePoints[aNbInitialized++] = aPassedPoint;
163 if (aNbInitialized <= 1)
164 return; // too few points for the ellipse
166 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
167 // ellipse is given by major axis and passing point,
168 // recalculate the first point to be a center
169 anEllipsePoints[0]->setX(0.5 * (anEllipsePoints[0]->x() + anEllipsePoints[1]->x()));
170 anEllipsePoints[0]->setY(0.5 * (anEllipsePoints[0]->y() + anEllipsePoints[1]->y()));
173 if (anEllipsePoints[0]->distance(anEllipsePoints[1]) < TOLERANCE)
174 return; // ellipse is not valid
176 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
177 if (aNbInitialized == 2) {
178 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
179 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
180 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
181 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
182 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
184 else if (aNbInitialized == 3) {
185 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
186 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
189 if (!anEllipse || anEllipse->implPtr<void>() == 0)
192 myCenter = anEllipse->center();
193 myFocus = anEllipse->firstFocus();
194 myMajorRadius = anEllipse->majorRadius();
195 myMinorRadius = anEllipse->minorRadius();
197 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
198 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
200 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
201 aMajorRadiusAttr->setValue(myMajorRadius);
202 aMinorRadiusAttr->setValue(myMinorRadius);
203 data()->blockSendAttributeUpdated(aWasBlocked, false);
208 std::string SketchPlugin_MacroEllipse::processEvent(
209 const std::shared_ptr<Events_Message>& theMessage)
211 std::string aFilledAttributeName;
213 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
214 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
215 if (aReentrantMessage) {
216 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
217 std::string anEllipseType = aReentrantMessage->typeOfCreation();
219 string(ELLIPSE_TYPE())->setValue(anEllipseType);
221 aFilledAttributeName = ELLIPSE_TYPE();
222 ObjectPtr anObject = aReentrantMessage->selectedObject();
223 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
224 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
226 if (aClickedPoint && (anObject || anAttribute)) {
227 aFilledAttributeName = CENTER_POINT_ID();
228 std::string aReferenceAttributeName = CENTER_POINT_REF_ID();
229 if (anEllipseType == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
230 aFilledAttributeName = MAJOR_AXIS_START_ID();
231 aReferenceAttributeName = MAJOR_AXIS_START_REF_ID();
234 // fill 2d point attribute
235 AttributePoint2DPtr aPointAttr =
236 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
237 aPointAttr->setValue(aClickedPoint);
239 // fill reference attribute
240 AttributeRefAttrPtr aRefAttr =
241 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
243 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
244 if (aCreatedFeature && anAttribute->id() == CENTER_POINT_ID())
245 anAttribute = aCreatedFeature->attribute(
246 anEllipseType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT() ?
247 SketchPlugin_Ellipse::CENTER_ID() :
248 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
250 aRefAttr->setAttr(anAttribute);
252 else if (anObject.get()) {
253 // if attribute is NULL, only object is defined, it should be processed outside
254 // the feature because it might be an external feature, that will be
255 // removed/created again after restart operation
256 // #2468 - Crash when sketching circles successively on a repetition
257 aFilledAttributeName = ELLIPSE_TYPE();
260 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
262 return aFilledAttributeName;
266 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
267 FeaturePtr theEllipseFeature)
269 // tangency on-the-fly is not applicable for ellipses
270 static const bool isTangencyApplicable = false;
271 // Create constraints.
272 SketchPlugin_Tools::createCoincidenceOrTangency(
273 this, CENTER_POINT_REF_ID(),
274 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
275 ObjectPtr(), isTangencyApplicable);
276 SketchPlugin_Tools::createCoincidenceOrTangency(
277 this, MAJOR_AXIS_POINT_REF_ID(),
278 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
279 ObjectPtr(), isTangencyApplicable);
280 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
281 if (!refattr(PASSED_POINT_REF_ID())->isObject()) {
282 SketchPlugin_Tools::createCoincidenceOrTangency(
283 this, PASSED_POINT_REF_ID(), AttributePtr(),
284 theEllipseFeature->lastResult(), isTangencyApplicable);
288 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
289 FeaturePtr theEllipseFeature)
291 // tangency on-the-fly is not applicable for ellipses
292 static const bool isTangencyApplicable = false;
293 // Create constraints.
294 SketchPlugin_Tools::createCoincidenceOrTangency(
295 this, MAJOR_AXIS_START_REF_ID(),
296 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
297 ObjectPtr(), isTangencyApplicable);
298 SketchPlugin_Tools::createCoincidenceOrTangency(
299 this, MAJOR_AXIS_END_REF_ID(),
300 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
301 ObjectPtr(), isTangencyApplicable);
302 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
303 if (!refattr(PASSED_POINT_1_REF_ID())->isObject()) {
304 SketchPlugin_Tools::createCoincidenceOrTangency(
305 this, PASSED_POINT_1_REF_ID(), AttributePtr(),
306 theEllipseFeature->lastResult(), isTangencyApplicable);
310 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
312 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
314 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
315 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
316 aCenterAttr->setValue(myCenter->x(), myCenter->y());
318 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
319 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
320 aFocusAttr->setValue(myFocus->x(), myFocus->y());
322 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
323 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
325 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
326 boolean(AUXILIARY_ID())->value());
328 aEllipseFeature->execute();
330 // create auxiliary points
331 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
332 aEllipseFeature, SketchPlugin_Ellipse::CENTER_ID());
333 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
334 aEllipseFeature, SketchPlugin_Ellipse::FIRST_FOCUS_ID());
335 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
336 aEllipseFeature, SketchPlugin_Ellipse::SECOND_FOCUS_ID());
337 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
338 aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
339 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
340 aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
341 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
342 aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_START_ID());
343 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
344 aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
345 // create auxiliary axes
346 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
347 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID(),
348 SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
349 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
350 SketchPlugin_Ellipse::MINOR_AXIS_START_ID(),
351 SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
353 return aEllipseFeature;
356 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
358 SketchPlugin_Sketch* aSketch = sketch();
359 if (!aSketch || !myCenter || myMajorRadius == 0)
360 return AISObjectPtr();
362 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
363 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
365 // Compute a ellipse in 3D view.
366 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
367 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
368 GeomDirPtr aNormal = aNDir->dir();
369 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
370 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
372 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
373 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
374 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
375 if (!anEllipseShape.get() || !aCenterPointShape.get())
376 return AISObjectPtr();
378 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
379 aShapes.push_back(anEllipseShape);
380 aShapes.push_back(aCenterPointShape);
382 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
383 AISObjectPtr anAIS = thePrevious;
385 anAIS.reset(new GeomAPI_AISObject());
386 anAIS->createShape(aCompound);
389 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());