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(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
61 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
62 data()->addAttribute(MAJOR_AXIS_POINT_ID(), GeomDataAPI_Point2D::typeId());
63 data()->addAttribute(MAJOR_AXIS_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_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
68 data()->addAttribute(MAJOR_AXIS_START_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
69 data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
70 data()->addAttribute(MAJOR_AXIS_END_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
71 data()->addAttribute(PASSED_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
72 data()->addAttribute(PASSED_POINT_1_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
74 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
75 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
76 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
78 string(EDIT_ELLIPSE_TYPE())->setValue("");
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
81 ModelAPI_Session::get()->validators()->registerNotObligatory(
82 getKind(), MAJOR_AXIS_POINT_REF_ID());
83 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
84 ModelAPI_Session::get()->validators()->registerNotObligatory(
85 getKind(), MAJOR_AXIS_START_REF_ID());
86 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_REF_ID());
87 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_1_REF_ID());
88 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ELLIPSE_TYPE());
91 void SketchPlugin_MacroEllipse::execute()
93 FeaturePtr anEllipse = createEllipseFeature();
95 std::string aType = string(ELLIPSE_TYPE())->value();
96 if (aType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT())
97 constraintsForEllipseByCenterAxisAndPassed(anEllipse);
98 else if (aType == ELLIPSE_TYPE_BY_AXIS_AND_POINT())
99 constraintsForEllipseByMajoxAxisAndPassed(anEllipse);
101 // message to init reentrant operation
102 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
103 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
104 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
106 std::string anEditType = string(EDIT_ELLIPSE_TYPE())->value();
107 aMessage->setTypeOfCreation(!anEditType.empty() ? anEditType : aType);
108 aMessage->setCreatedFeature(anEllipse);
109 Events_Loop::loop()->send(aMessage);
112 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
114 static const int NB_POINTS = 3;
115 std::string aPointAttrName[NB_POINTS];
116 std::string aPointRefName[NB_POINTS];
117 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT()) {
118 aPointAttrName[0] = CENTER_POINT_ID();
119 aPointAttrName[1] = MAJOR_AXIS_POINT_ID();
120 aPointAttrName[2] = PASSED_POINT_ID();
121 aPointRefName[0] = CENTER_POINT_REF_ID();
122 aPointRefName[1] = MAJOR_AXIS_POINT_REF_ID();
123 aPointRefName[2] = PASSED_POINT_REF_ID();
124 } else if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
125 aPointAttrName[0] = MAJOR_AXIS_START_ID();
126 aPointAttrName[1] = MAJOR_AXIS_END_ID();
127 aPointAttrName[2] = PASSED_POINT_1_ID();
128 aPointRefName[0] = MAJOR_AXIS_START_REF_ID();
129 aPointRefName[1] = MAJOR_AXIS_END_REF_ID();
130 aPointRefName[2] = PASSED_POINT_1_REF_ID();
135 // type of ellipse switched, thus reset all attributes
136 if (theID == ELLIPSE_TYPE()) {
137 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
138 SketchPlugin_Tools::resetAttribute(this, aPointAttrName[aPntIndex]);
139 SketchPlugin_Tools::resetAttribute(this, aPointRefName[aPntIndex]);
143 int aNbInitialized = 0;
144 GeomPnt2dPtr anEllipsePoints[NB_POINTS];
146 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
147 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
148 if (!aPointAttr->isInitialized())
151 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
152 // calculate ellipse parameters
153 GeomPnt2dPtr aPassedPoint =
154 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
155 GeomShapePtr aTangentCurve;
156 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
157 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
159 anEllipsePoints[aNbInitialized++] = aPassedPoint;
162 if (aNbInitialized <= 1)
163 return; // too few points for the ellipse
165 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
166 // ellipse is given by major axis and passing point,
167 // recalculate the first point to be a center
168 anEllipsePoints[0]->setX(0.5 * (anEllipsePoints[0]->x() + anEllipsePoints[1]->x()));
169 anEllipsePoints[0]->setY(0.5 * (anEllipsePoints[0]->y() + anEllipsePoints[1]->y()));
172 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
173 if (aNbInitialized == 2) {
174 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
175 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
176 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
177 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
178 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
180 else if (aNbInitialized == 3) {
181 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
182 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
185 if (!anEllipse || anEllipse->implPtr<void>() == 0)
188 myCenter = anEllipse->center();
189 myFocus = anEllipse->firstFocus();
190 myMajorRadius = anEllipse->majorRadius();
191 myMinorRadius = anEllipse->minorRadius();
193 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
194 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
196 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
197 aMajorRadiusAttr->setValue(myMajorRadius);
198 aMinorRadiusAttr->setValue(myMinorRadius);
199 data()->blockSendAttributeUpdated(aWasBlocked, false);
204 std::string SketchPlugin_MacroEllipse::processEvent(
205 const std::shared_ptr<Events_Message>& theMessage)
207 std::string aFilledAttributeName;
209 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
210 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
211 if (aReentrantMessage) {
212 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
213 std::string anEllipseType = aReentrantMessage->typeOfCreation();
215 string(ELLIPSE_TYPE())->setValue(anEllipseType);
217 aFilledAttributeName = ELLIPSE_TYPE();
218 ObjectPtr anObject = aReentrantMessage->selectedObject();
219 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
220 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
222 if (aClickedPoint && (anObject || anAttribute)) {
223 aFilledAttributeName = CENTER_POINT_ID();
224 std::string aReferenceAttributeName = CENTER_POINT_REF_ID();
225 if (anEllipseType == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
226 aFilledAttributeName = MAJOR_AXIS_START_ID();
227 aReferenceAttributeName = MAJOR_AXIS_START_REF_ID();
230 // fill 2d point attribute
231 AttributePoint2DPtr aPointAttr =
232 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
233 aPointAttr->setValue(aClickedPoint);
235 // fill reference attribute
236 AttributeRefAttrPtr aRefAttr =
237 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
239 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
240 if (aCreatedFeature && anAttribute->id() == CENTER_POINT_ID())
241 anAttribute = aCreatedFeature->attribute(
242 anEllipseType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT() ?
243 SketchPlugin_Ellipse::CENTER_ID() :
244 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
246 aRefAttr->setAttr(anAttribute);
248 else if (anObject.get()) {
249 // if attribute is NULL, only object is defined, it should be processed outside
250 // the feature because it might be an external feature, that will be
251 // removed/created again after restart operation
252 // #2468 - Crash when sketching circles successively on a repetition
253 aFilledAttributeName = ELLIPSE_TYPE();
256 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
258 return aFilledAttributeName;
262 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
263 FeaturePtr theEllipseFeature)
265 // tangency on-the-fly is not applicable for ellipses
266 static const bool isTangencyApplicable = false;
267 // Create constraints.
268 SketchPlugin_Tools::createCoincidenceOrTangency(
269 this, CENTER_POINT_REF_ID(),
270 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
271 ObjectPtr(), isTangencyApplicable);
272 SketchPlugin_Tools::createCoincidenceOrTangency(
273 this, MAJOR_AXIS_POINT_REF_ID(),
274 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
275 ObjectPtr(), isTangencyApplicable);
276 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
277 if (!refattr(PASSED_POINT_REF_ID())->isObject()) {
278 SketchPlugin_Tools::createCoincidenceOrTangency(
279 this, PASSED_POINT_REF_ID(), AttributePtr(),
280 theEllipseFeature->lastResult(), isTangencyApplicable);
284 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
285 FeaturePtr theEllipseFeature)
287 // tangency on-the-fly is not applicable for ellipses
288 static const bool isTangencyApplicable = false;
289 // Create constraints.
290 SketchPlugin_Tools::createCoincidenceOrTangency(
291 this, MAJOR_AXIS_START_REF_ID(),
292 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
293 ObjectPtr(), isTangencyApplicable);
294 SketchPlugin_Tools::createCoincidenceOrTangency(
295 this, MAJOR_AXIS_END_REF_ID(),
296 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
297 ObjectPtr(), isTangencyApplicable);
298 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
299 if (!refattr(PASSED_POINT_1_REF_ID())->isObject()) {
300 SketchPlugin_Tools::createCoincidenceOrTangency(
301 this, PASSED_POINT_1_REF_ID(), AttributePtr(),
302 theEllipseFeature->lastResult(), isTangencyApplicable);
306 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
308 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
310 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
311 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
312 aCenterAttr->setValue(myCenter->x(), myCenter->y());
314 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
315 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
316 aFocusAttr->setValue(myFocus->x(), myFocus->y());
318 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
319 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
321 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
322 boolean(AUXILIARY_ID())->value());
324 aEllipseFeature->execute();
326 // create auxiliary points
327 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
328 aEllipseFeature, SketchPlugin_Ellipse::CENTER_ID());
329 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
330 aEllipseFeature, SketchPlugin_Ellipse::FIRST_FOCUS_ID());
331 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
332 aEllipseFeature, SketchPlugin_Ellipse::SECOND_FOCUS_ID());
333 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
334 aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
335 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
336 aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
337 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
338 aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_START_ID());
339 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
340 aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
341 // create auxiliary axes
342 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
343 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID(),
344 SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
345 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
346 SketchPlugin_Ellipse::MINOR_AXIS_START_ID(),
347 SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
349 return aEllipseFeature;
352 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
354 SketchPlugin_Sketch* aSketch = sketch();
355 if (!aSketch || !myCenter || myMajorRadius == 0)
356 return AISObjectPtr();
358 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
359 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
361 // Compute a ellipse in 3D view.
362 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
363 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
364 GeomDirPtr aNormal = aNDir->dir();
365 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
366 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
368 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
369 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
370 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
371 if (!anEllipseShape.get() || !aCenterPointShape.get())
372 return AISObjectPtr();
374 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
375 aShapes.push_back(anEllipseShape);
376 aShapes.push_back(aCenterPointShape);
378 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
379 AISObjectPtr anAIS = thePrevious;
381 anAIS.reset(new GeomAPI_AISObject());
382 anAIS->createShape(aCompound);
385 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());