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_MacroEllipticArc.h>
22 #include <SketchPlugin_EllipticArc.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_Session.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Events.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Dir2d.h>
36 #include <GeomAPI_Ellipse.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 const double paramTolerance = 1.e-4;
46 const double PI = 3.141592653589793238463;
49 SketchPlugin_MacroEllipticArc::SketchPlugin_MacroEllipticArc()
50 : SketchPlugin_SketchEntity(),
57 void SketchPlugin_MacroEllipticArc::initAttributes()
59 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
60 data()->addAttribute(CENTER_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
61 data()->addAttribute(MAJOR_AXIS_POINT_ID(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(MAJOR_AXIS_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
63 data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
64 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
65 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
66 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
68 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
69 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
71 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
72 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
74 boolean(REVERSED_ID())->setValue(false);
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_REF_ID());
77 ModelAPI_Session::get()->validators()
78 ->registerNotObligatory(getKind(), MAJOR_AXIS_POINT_REF_ID());
79 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
83 void SketchPlugin_MacroEllipticArc::execute()
85 FeaturePtr anEllipse = createEllipticArcFeature();
87 // message to init reentrant operation
88 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
89 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
90 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
91 aMessage->setCreatedFeature(anEllipse);
92 Events_Loop::loop()->send(aMessage);
95 void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& theID)
97 static const int NB_POINTS = 4;
98 std::string aPointAttrName[NB_POINTS] = { CENTER_ID(),
99 MAJOR_AXIS_POINT_ID(),
102 std::string aPointRefName[NB_POINTS] = { CENTER_REF_ID(),
103 MAJOR_AXIS_POINT_REF_ID(),
104 START_POINT_REF_ID(),
105 END_POINT_REF_ID() };
107 int aNbInitialized = 0;
108 GeomPnt2dPtr anEllipsePoints[NB_POINTS];
110 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
111 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
112 if (!aPointAttr->isInitialized())
115 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
116 // calculate ellipse parameters
117 GeomPnt2dPtr aPassedPoint =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
119 GeomShapePtr aTangentCurve;
120 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
121 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
123 anEllipsePoints[aNbInitialized++] = aPassedPoint;
126 if (aNbInitialized <= 1)
127 return; // too few points for the ellipse
129 myCenter = anEllipsePoints[0];
130 myMajorAxis = anEllipsePoints[1];
131 myStartPnt = anEllipsePoints[2];
132 myEndPnt = anEllipsePoints[3];
134 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
135 if (aNbInitialized == 2) {
136 GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
137 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
138 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
139 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
140 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
142 else if (aNbInitialized >= 3) {
143 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
144 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
147 if (!anEllipse || anEllipse->implPtr<void>() == 0)
150 myMajorRadius = anEllipse->majorRadius();
151 myMinorRadius = anEllipse->minorRadius();
153 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
154 real(MAJOR_RADIUS_ID())->setValue(myMajorRadius);
155 real(MINOR_RADIUS_ID())->setValue(myMinorRadius);
156 data()->blockSendAttributeUpdated(aWasBlocked, false);
158 // update the REVERSED flag
160 double aParameterEnd = 0.0;
161 GeomPnt2dPtr aEnd = anEllipse->project(myEndPnt);
162 if (anEllipse->parameter(aEnd, paramTolerance, aParameterEnd)) {
163 double aParamStart = 0.0;
164 anEllipse->parameter(myStartPnt, paramTolerance, aParamStart);
165 aParameterEnd -= aParamStart;
167 if (myParamDelta > 0.0 && myParamDelta <= PI * 0.5 &&
168 aParameterEnd < 0 && aParameterEnd >= -PI * 0.5) {
169 boolean(REVERSED_ID())->setValue(true);
171 else if (myParamDelta < 0.0 && myParamDelta >= -PI * 0.5 &&
172 aParameterEnd > 0.0 && aParameterEnd <= PI * 0.5) {
173 boolean(REVERSED_ID())->setValue(false);
176 myParamDelta = aParameterEnd;
181 std::string SketchPlugin_MacroEllipticArc::processEvent(
182 const std::shared_ptr<Events_Message>& theMessage)
184 std::string aFilledAttributeName;
186 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
187 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
188 if (aReentrantMessage) {
189 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
191 ObjectPtr anObject = aReentrantMessage->selectedObject();
192 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
193 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
195 if (aClickedPoint && (anObject || anAttribute)) {
196 aFilledAttributeName = CENTER_ID();
197 std::string aReferenceAttributeName = CENTER_REF_ID();
199 // fill 2d point attribute
200 AttributePoint2DPtr aPointAttr =
201 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
202 aPointAttr->setValue(aClickedPoint);
204 // fill reference attribute
205 AttributeRefAttrPtr aRefAttr =
206 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
208 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
209 if (aCreatedFeature && anAttribute->id() == CENTER_ID())
210 anAttribute = aCreatedFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID());
212 aRefAttr->setAttr(anAttribute);
214 else if (anObject.get()) {
215 // if attribute is NULL, only object is defined, it should be processed outside
216 // the feature because it might be an external feature, that will be
217 // removed/created again after restart operation
218 // #2468 - Crash when sketching circles successively on a repetition
219 aFilledAttributeName = "";
222 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
224 return aFilledAttributeName;
228 FeaturePtr SketchPlugin_MacroEllipticArc::createEllipticArcFeature()
230 GeomShapePtr anArc = getArcShape();
231 GeomEllipsePtr anEllipse;
232 GeomPointPtr aStartPoint, aEndPoint;
233 if (anArc->isEdge()) {
234 GeomEdgePtr anArcEdge = anArc->edge();
235 aStartPoint = anArcEdge->firstPoint();
236 aEndPoint = anArcEdge->lastPoint();
237 if (boolean(REVERSED_ID())->value())
238 std::swap(aStartPoint, aEndPoint);
240 if (anArcEdge->isEllipse())
241 anEllipse = anArcEdge->ellipse();
247 // Create and fill new EllipticArc feature
248 SketchPlugin_Sketch* aSketch = sketch();
249 FeaturePtr aEllipseFeature = aSketch->addFeature(SketchPlugin_EllipticArc::ID());
251 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252 aEllipseFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
253 aCenterAttr->setValue(aSketch->to2D(anEllipse->center()));
255 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
256 aEllipseFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
257 aFocusAttr->setValue(aSketch->to2D(anEllipse->firstFocus()));
259 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260 aEllipseFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
261 aStartAttr->setValue(aSketch->to2D(aStartPoint));
263 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
264 aEllipseFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
265 aEndAttr->setValue(aSketch->to2D(aEndPoint));
267 aEllipseFeature->real(SketchPlugin_EllipticArc::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
268 aEllipseFeature->real(SketchPlugin_EllipticArc::MINOR_RADIUS_ID())->setValue(myMinorRadius);
270 aEllipseFeature->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(
271 boolean(REVERSED_ID())->value());
273 aEllipseFeature->boolean(SketchPlugin_EllipticArc::AUXILIARY_ID())->setValue(
274 boolean(AUXILIARY_ID())->value());
276 aEllipseFeature->execute();
278 // create auxiliary points
279 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
280 aEllipseFeature, SketchPlugin_EllipticArc::CENTER_ID());
281 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
282 aEllipseFeature, SketchPlugin_EllipticArc::FIRST_FOCUS_ID());
283 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
284 aEllipseFeature, SketchPlugin_EllipticArc::SECOND_FOCUS_ID());
285 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
286 aEllipseFeature, SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID());
287 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
288 aEllipseFeature, SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
289 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
290 aEllipseFeature, SketchPlugin_EllipticArc::MINOR_AXIS_START_ID());
291 SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
292 aEllipseFeature, SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
293 // create auxiliary axes
294 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
295 SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
296 SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
297 SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
298 SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
299 SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
301 return aEllipseFeature;
304 AISObjectPtr SketchPlugin_MacroEllipticArc::getAISObject(AISObjectPtr thePrevious)
306 SketchPlugin_Sketch* aSketch = sketch();
307 if (!aSketch || !myCenter || !myMajorAxis)
308 return AISObjectPtr();
310 // Compute a elliptic arc in 3D view.
311 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
312 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
313 GeomShapePtr anArcShape = getArcShape();
314 if (!anArcShape.get() || !aCenterPointShape.get())
315 return AISObjectPtr();
317 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
318 aShapes.push_back(anArcShape);
319 aShapes.push_back(aCenterPointShape);
321 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
322 AISObjectPtr anAIS = thePrevious;
324 anAIS.reset(new GeomAPI_AISObject());
325 anAIS->createShape(aCompound);
329 GeomShapePtr SketchPlugin_MacroEllipticArc::getArcShape()
331 if (!myCenter.get() || !myMajorAxis.get())
332 return GeomShapePtr();
334 SketchPlugin_Sketch* aSketch = sketch();
336 return GeomShapePtr();
338 GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
339 GeomPointPtr aMajorAxisPnt(aSketch->to3D(myMajorAxis->x(), myMajorAxis->y()));
340 GeomDirPtr aMajorAxisDir(new GeomAPI_Dir(aMajorAxisPnt->x() - aCenter->x(),
341 aMajorAxisPnt->y() - aCenter->y(),
342 aMajorAxisPnt->z() - aCenter->z()));
344 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
345 aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
346 GeomDirPtr aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
348 GeomPointPtr aStart, anEnd;
350 aStart = aSketch->to3D(myStartPnt->x(), myStartPnt->y());
352 anEnd = aSketch->to3D(myEndPnt->x(), myEndPnt->y());
354 GeomShapePtr anArcShape;
356 if (boolean(REVERSED_ID())->value())
357 std::swap(aStart, anEnd);
359 anArcShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxisDir,
360 myMajorRadius, myMinorRadius, aStart, anEnd);
363 anArcShape = GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxisDir,
364 myMajorRadius, myMinorRadius);