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_Ellipse.h>
23 #include <SketchPlugin_MacroArcReentrantMessage.h>
24 #include <SketchPlugin_Tools.h>
25 #include <SketchPlugin_Sketch.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Events.h>
34 #include <GeomDataAPI_Point2D.h>
36 #include <GeomAPI_Dir2d.h>
37 #include <GeomAPI_Ellipse2d.h>
38 #include <GeomAPI_Vertex.h>
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41 #include <GeomAlgoAPI_EdgeBuilder.h>
42 #include <GeomAlgoAPI_PointBuilder.h>
45 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
46 : SketchPlugin_SketchEntity(),
52 void SketchPlugin_MacroEllipse::initAttributes()
54 data()->addAttribute(ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
55 data()->addAttribute(EDIT_ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
58 data()->addAttribute(FIRST_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
59 data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
60 data()->addAttribute(SECOND_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
61 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
64 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
65 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
66 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
68 string(EDIT_ELLIPSE_TYPE())->setValue("");
70 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FIRST_POINT_REF_ID());
71 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_POINT_REF_ID());
72 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ELLIPSE_TYPE());
76 void SketchPlugin_MacroEllipse::execute()
78 FeaturePtr anEllipse = createEllipseFeature();
80 std::string aType = string(ELLIPSE_TYPE())->value();
81 if (aType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT())
82 constraintsForEllipseByCenterAxisAndPassed(anEllipse);
83 else if (aType == ELLIPSE_TYPE_BY_AXIS_AND_POINT())
84 constraintsForEllipseByMajoxAxisAndPassed(anEllipse);
86 // message to init reentrant operation
87 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
88 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
89 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
91 std::string anEditType = string(EDIT_ELLIPSE_TYPE())->value();
92 aMessage->setTypeOfCreation(!anEditType.empty() ? anEditType : aType);
93 aMessage->setCreatedFeature(anEllipse);
94 Events_Loop::loop()->send(aMessage);
97 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
99 static const int NB_POINTS = 3;
100 std::string aPointAttrName[NB_POINTS] = { FIRST_POINT_ID(),
103 std::string aPointRefName[NB_POINTS] = { FIRST_POINT_REF_ID(),
104 SECOND_POINT_REF_ID(),
105 PASSED_POINT_REF_ID() };
107 // type of ellipse switched, thus reset all attributes
108 if (theID == ELLIPSE_TYPE()) {
109 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
110 SketchPlugin_Tools::resetAttribute(this, aPointAttrName[aPntIndex]);
111 SketchPlugin_Tools::resetAttribute(this, aPointRefName[aPntIndex]);
115 int aNbInitialized = 0;
116 GeomPnt2dPtr anEllipsePoints[NB_POINTS];
118 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
119 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
120 if (!aPointAttr->isInitialized())
123 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
124 // calculate ellipse parameters
125 GeomPnt2dPtr aPassedPoint =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
127 GeomShapePtr aTangentCurve;
128 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
129 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
131 anEllipsePoints[aNbInitialized++] = aPassedPoint;
134 if (aNbInitialized <= 1)
135 return; // too few points for the ellipse
137 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
138 // ellipse is given by major axis and passing point,
139 // recalculate the first point to be a center
140 anEllipsePoints[0]->setX(0.5 * (anEllipsePoints[0]->x() + anEllipsePoints[1]->x()));
141 anEllipsePoints[0]->setY(0.5 * (anEllipsePoints[0]->y() + anEllipsePoints[1]->y()));
144 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
145 if (aNbInitialized == 2) {
146 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
147 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
148 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
149 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
150 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
152 else if (aNbInitialized == 3) {
153 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
154 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
157 if (!anEllipse || anEllipse->implPtr<void>() == 0)
160 myCenter = anEllipse->center();
161 myFocus = anEllipse->firstFocus();
162 myMajorRadius = anEllipse->majorRadius();
163 myMinorRadius = anEllipse->minorRadius();
165 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
166 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
168 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
169 aMajorRadiusAttr->setValue(myMajorRadius);
170 aMinorRadiusAttr->setValue(myMinorRadius);
171 data()->blockSendAttributeUpdated(aWasBlocked, false);
176 std::string SketchPlugin_MacroEllipse::processEvent(
177 const std::shared_ptr<Events_Message>& theMessage)
179 std::string aFilledAttributeName;
181 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
182 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
183 if (aReentrantMessage) {
184 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
185 std::string anEllipseType = aReentrantMessage->typeOfCreation();
187 string(ELLIPSE_TYPE())->setValue(anEllipseType);
189 aFilledAttributeName = ELLIPSE_TYPE();
190 ObjectPtr anObject = aReentrantMessage->selectedObject();
191 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
192 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
194 if (aClickedPoint && (anObject || anAttribute)) {
195 aFilledAttributeName = FIRST_POINT_ID();
196 std::string aReferenceAttributeName = FIRST_POINT_REF_ID();
198 // fill 2d point attribute
199 AttributePoint2DPtr aPointAttr =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
201 aPointAttr->setValue(aClickedPoint);
203 // fill reference attribute
204 AttributeRefAttrPtr aRefAttr =
205 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
207 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
208 if (aCreatedFeature && anAttribute->id() == FIRST_POINT_ID())
209 anAttribute = aCreatedFeature->attribute(
210 anEllipseType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT() ?
211 SketchPlugin_Ellipse::CENTER_ID() :
212 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
214 aRefAttr->setAttr(anAttribute);
216 else if (anObject.get()) {
217 // if attribute is NULL, only object is defined, it should be processed outside
218 // the feature because it might be an external feature, that will be
219 // removed/created again after restart operation
220 // #2468 - Crash when sketching circles successively on a repetition
221 aFilledAttributeName = ELLIPSE_TYPE();
224 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
226 return aFilledAttributeName;
230 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
231 FeaturePtr theEllipseFeature)
233 // tangency on-the-fly is not applicable for ellipses
234 static const bool isTangencyApplicable = false;
235 // Create constraints.
236 SketchPlugin_Tools::createCoincidenceOrTangency(
237 this, FIRST_POINT_REF_ID(),
238 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
239 ObjectPtr(), isTangencyApplicable);
240 SketchPlugin_Tools::createCoincidenceOrTangency(
241 this, SECOND_POINT_REF_ID(),
242 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
243 ObjectPtr(), isTangencyApplicable);
244 SketchPlugin_Tools::createCoincidenceOrTangency(
245 this, PASSED_POINT_REF_ID(), AttributePtr(),
246 theEllipseFeature->lastResult(), isTangencyApplicable);
249 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
250 FeaturePtr theEllipseFeature)
252 // tangency on-the-fly is not applicable for ellipses
253 static const bool isTangencyApplicable = false;
254 // Create constraints.
255 SketchPlugin_Tools::createCoincidenceOrTangency(
256 this, FIRST_POINT_REF_ID(),
257 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
258 ObjectPtr(), isTangencyApplicable);
259 SketchPlugin_Tools::createCoincidenceOrTangency(
260 this, SECOND_POINT_REF_ID(),
261 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
262 ObjectPtr(), isTangencyApplicable);
263 SketchPlugin_Tools::createCoincidenceOrTangency(
264 this, PASSED_POINT_REF_ID(), AttributePtr(),
265 theEllipseFeature->lastResult(), isTangencyApplicable);
268 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
270 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
272 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
274 aCenterAttr->setValue(myCenter->x(), myCenter->y());
276 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
277 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
278 aFocusAttr->setValue(myFocus->x(), myFocus->y());
280 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
281 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
283 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
284 boolean(AUXILIARY_ID())->value());
286 aEllipseFeature->execute();
287 return aEllipseFeature;
290 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
292 SketchPlugin_Sketch* aSketch = sketch();
293 if (!aSketch || !myCenter || myMajorRadius == 0)
294 return AISObjectPtr();
296 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
297 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
299 // Compute a ellipse in 3D view.
300 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
301 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
302 GeomDirPtr aNormal = aNDir->dir();
303 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
304 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
306 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
307 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
308 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
309 if (!anEllipseShape.get() || !aCenterPointShape.get())
310 return AISObjectPtr();
312 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
313 aShapes.push_back(anEllipseShape);
314 aShapes.push_back(aCenterPointShape);
316 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
317 AISObjectPtr anAIS = thePrevious;
319 anAIS.reset(new GeomAPI_AISObject());
320 anAIS->createShape(aCompound);