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 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
248 if (!refattr(PASSED_POINT_REF_ID())->isObject()) {
249 SketchPlugin_Tools::createCoincidenceOrTangency(
250 this, PASSED_POINT_REF_ID(), AttributePtr(),
251 theEllipseFeature->lastResult(), isTangencyApplicable);
255 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
256 FeaturePtr theEllipseFeature)
258 // tangency on-the-fly is not applicable for ellipses
259 static const bool isTangencyApplicable = false;
260 // Create constraints.
261 SketchPlugin_Tools::createCoincidenceOrTangency(
262 this, FIRST_POINT_REF_ID(),
263 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
264 ObjectPtr(), isTangencyApplicable);
265 SketchPlugin_Tools::createCoincidenceOrTangency(
266 this, SECOND_POINT_REF_ID(),
267 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
268 ObjectPtr(), isTangencyApplicable);
269 // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
270 if (!refattr(PASSED_POINT_REF_ID())->isObject()) {
271 SketchPlugin_Tools::createCoincidenceOrTangency(
272 this, PASSED_POINT_REF_ID(), AttributePtr(),
273 theEllipseFeature->lastResult(), isTangencyApplicable);
277 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
279 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
281 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
282 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
283 aCenterAttr->setValue(myCenter->x(), myCenter->y());
285 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
286 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
287 aFocusAttr->setValue(myFocus->x(), myFocus->y());
289 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
290 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
292 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
293 boolean(AUXILIARY_ID())->value());
295 aEllipseFeature->execute();
297 // create auxiliary points
298 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::CENTER_ID());
299 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::FIRST_FOCUS_ID());
300 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::SECOND_FOCUS_ID());
301 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
302 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
303 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_START_ID());
304 createAuxiliaryPoint(aEllipseFeature, SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
305 // create auxiliary axes
306 createAuxiliaryAxis(aEllipseFeature,
307 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID(),
308 SketchPlugin_Ellipse::MAJOR_AXIS_END_ID());
309 createAuxiliaryAxis(aEllipseFeature,
310 SketchPlugin_Ellipse::MINOR_AXIS_START_ID(),
311 SketchPlugin_Ellipse::MINOR_AXIS_END_ID());
313 return aEllipseFeature;
316 void SketchPlugin_MacroEllipse::createAuxiliaryPoint(const FeaturePtr& theEllipseFeature,
317 const std::string& theEllipsePoint)
319 FeaturePtr aPointFeature = sketch()->addFeature(SketchPlugin_Point::ID());
320 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
321 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theEllipseFeature);
323 AttributePoint2DPtr anElPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
324 theEllipseFeature->attribute(theEllipsePoint));
326 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
327 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
328 aCoord->setValue(anElPoint->x(), anElPoint->y());
330 aPointFeature->execute();
331 std::string aName = theEllipseFeature->name() + "_" + theEllipsePoint;
332 aPointFeature->data()->setName(aName);
333 aPointFeature->lastResult()->data()->setName(aName);
335 createInternalConstraint(anElPoint, aCoord);
338 void SketchPlugin_MacroEllipse::createAuxiliaryAxis(const FeaturePtr& theEllipseFeature,
339 const std::string& theStartPoint,
340 const std::string& theEndPoint)
342 FeaturePtr aLineFeature = sketch()->addFeature(SketchPlugin_Line::ID());
343 aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
344 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theEllipseFeature);
346 AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
347 theEllipseFeature->attribute(theStartPoint));
348 AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
349 theEllipseFeature->attribute(theEndPoint));
351 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
352 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
353 aLineStart->setValue(aStartPoint->x(), aStartPoint->y());
355 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
356 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
357 aLineEnd->setValue(aEndPoint->x(), aEndPoint->y());
359 aLineFeature->execute();
360 std::string aName = theEllipseFeature->name() + "_" +
361 (theStartPoint == SketchPlugin_Ellipse::MAJOR_AXIS_START_ID() ? "major_axis" : "minor_axis");
362 aLineFeature->data()->setName(aName);
363 aLineFeature->lastResult()->data()->setName(aName);
365 createInternalConstraint(aStartPoint, aLineStart);
366 createInternalConstraint(aEndPoint, aLineEnd);
369 void SketchPlugin_MacroEllipse::createInternalConstraint(const AttributePtr& thePoint1,
370 const AttributePtr& thePoint2)
372 SketchPlugin_Tools::createConstraintAttrAttr(
373 sketch(), SketchPlugin_ConstraintCoincidenceInternal::ID(), thePoint1, thePoint2);
376 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
378 SketchPlugin_Sketch* aSketch = sketch();
379 if (!aSketch || !myCenter || myMajorRadius == 0)
380 return AISObjectPtr();
382 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
383 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
385 // Compute a ellipse in 3D view.
386 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
387 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
388 GeomDirPtr aNormal = aNDir->dir();
389 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
390 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
392 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
393 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
394 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
395 if (!anEllipseShape.get() || !aCenterPointShape.get())
396 return AISObjectPtr();
398 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
399 aShapes.push_back(anEllipseShape);
400 aShapes.push_back(aCenterPointShape);
402 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
403 AISObjectPtr anAIS = thePrevious;
405 anAIS.reset(new GeomAPI_AISObject());
406 anAIS->createShape(aCompound);