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 GeomShapePtr aTangentCurve;
127 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
128 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
130 anEllipsePoints[aNbInitialized++] = aPassedPoint;
133 if (aNbInitialized <= 1)
134 return; // too few points for the ellipse
136 if (string(ELLIPSE_TYPE())->value() == ELLIPSE_TYPE_BY_AXIS_AND_POINT()) {
137 // ellipse is given by major axis and passing point,
138 // recalculate the first point to be a center
139 anEllipsePoints[0]->setX(0.5 * (anEllipsePoints[0]->x() + anEllipsePoints[1]->x()));
140 anEllipsePoints[0]->setY(0.5 * (anEllipsePoints[0]->y() + anEllipsePoints[1]->y()));
143 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
144 if (aNbInitialized == 2) {
145 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
146 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
147 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
148 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
149 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
151 else if (aNbInitialized == 3) {
152 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
153 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
156 if (!anEllipse || anEllipse->implPtr<void>() == 0)
159 myCenter = anEllipse->center();
160 myFocus = anEllipse->firstFocus();
161 myMajorRadius = anEllipse->majorRadius();
162 myMinorRadius = anEllipse->minorRadius();
164 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
165 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
167 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
168 // center attribute is used in processEvent() to set reference to reentrant arc
169 //// std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()))
170 //// ->setValue(myCenter);
171 aMajorRadiusAttr->setValue(myMajorRadius);
172 aMinorRadiusAttr->setValue(myMinorRadius);
173 data()->blockSendAttributeUpdated(aWasBlocked, false);
178 std::string SketchPlugin_MacroEllipse::processEvent(
179 const std::shared_ptr<Events_Message>& theMessage)
181 std::string aFilledAttributeName;
183 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
184 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
185 if (aReentrantMessage) {
186 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
187 std::string anEllipseType = aReentrantMessage->typeOfCreation();
189 string(ELLIPSE_TYPE())->setValue(anEllipseType);
191 aFilledAttributeName = ELLIPSE_TYPE();
192 ObjectPtr anObject = aReentrantMessage->selectedObject();
193 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
194 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
196 if (aClickedPoint && (anObject || anAttribute)) {
197 aFilledAttributeName = FIRST_POINT_ID();
198 std::string aReferenceAttributeName = FIRST_POINT_REF_ID();
200 // fill 2d point attribute
201 AttributePoint2DPtr aPointAttr =
202 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
203 aPointAttr->setValue(aClickedPoint);
205 // fill reference attribute
206 AttributeRefAttrPtr aRefAttr =
207 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
209 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
210 if (aCreatedFeature && anAttribute->id() == FIRST_POINT_ID())
211 anAttribute = aCreatedFeature->attribute(
212 anEllipseType == ELLIPSE_TYPE_BY_CENTER_AXIS_POINT() ?
213 SketchPlugin_Ellipse::CENTER_ID() :
214 SketchPlugin_Ellipse::MAJOR_AXIS_START_ID());
216 aRefAttr->setAttr(anAttribute);
218 else if (anObject.get()) {
219 // if attribute is NULL, only object is defined, it should be processed outside
220 // the feature because it might be an external feature, that will be
221 // removed/created again after restart operation
222 // #2468 - Crash when sketching circles successively on a repetition
223 aFilledAttributeName = ELLIPSE_TYPE();
226 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
228 return aFilledAttributeName;
232 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
233 FeaturePtr theEllipseFeature)
235 // tangency on-the-fly is not applicable for ellipses
236 static const bool isTangencyApplicable = false;
237 // Create constraints.
238 SketchPlugin_Tools::createCoincidenceOrTangency(
239 this, FIRST_POINT_REF_ID(),
240 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
241 ObjectPtr(), isTangencyApplicable);
242 SketchPlugin_Tools::createCoincidenceOrTangency(
243 this, SECOND_POINT_REF_ID(),
244 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
245 ObjectPtr(), isTangencyApplicable);
246 SketchPlugin_Tools::createCoincidenceOrTangency(
247 this, PASSED_POINT_REF_ID(), AttributePtr(),
248 theEllipseFeature->lastResult(), isTangencyApplicable);
251 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
252 FeaturePtr theEllipseFeature)
254 // tangency on-the-fly is not applicable for ellipses
255 static const bool isTangencyApplicable = false;
256 // Create constraints.
257 SketchPlugin_Tools::createCoincidenceOrTangency(
258 this, FIRST_POINT_REF_ID(),
259 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_START_ID()),
260 ObjectPtr(), isTangencyApplicable);
261 SketchPlugin_Tools::createCoincidenceOrTangency(
262 this, SECOND_POINT_REF_ID(),
263 theEllipseFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()),
264 ObjectPtr(), isTangencyApplicable);
265 SketchPlugin_Tools::createCoincidenceOrTangency(
266 this, PASSED_POINT_REF_ID(), AttributePtr(),
267 theEllipseFeature->lastResult(), isTangencyApplicable);
270 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
272 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
274 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
275 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
276 aCenterAttr->setValue(myCenter->x(), myCenter->y());
278 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
279 aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
280 aFocusAttr->setValue(myFocus->x(), myFocus->y());
282 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
283 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
285 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
286 boolean(AUXILIARY_ID())->value());
288 aEllipseFeature->execute();
289 return aEllipseFeature;
292 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
294 SketchPlugin_Sketch* aSketch = sketch();
295 if (!aSketch || !myCenter || myMajorRadius == 0)
296 return AISObjectPtr();
298 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
299 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
301 // Compute a ellipse in 3D view.
302 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
303 GeomPointPtr aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
304 GeomDirPtr aNormal = aNDir->dir();
305 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
306 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
308 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
309 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
310 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
311 if (!anEllipseShape.get() || !aCenterPointShape.get())
312 return AISObjectPtr();
314 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
315 aShapes.push_back(anEllipseShape);
316 aShapes.push_back(aCenterPointShape);
318 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
319 AISObjectPtr anAIS = thePrevious;
321 anAIS.reset(new GeomAPI_AISObject());
322 anAIS->createShape(aCompound);