Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroEllipticArc.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchPlugin_MacroEllipticArc.h>
21
22 #include <SketchPlugin_EllipticArc.h>
23 #include <SketchPlugin_MacroArcReentrantMessage.h>
24 #include <SketchPlugin_Tools.h>
25 #include <SketchPlugin_Sketch.h>
26
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>
32
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <GeomAPI_Dir2d.h>
36 #include <GeomAPI_Ellipse.h>
37 #include <GeomAPI_Ellipse2d.h>
38 #include <GeomAPI_Vertex.h>
39
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41 #include <GeomAlgoAPI_EdgeBuilder.h>
42 #include <GeomAlgoAPI_PointBuilder.h>
43
44
45 const double paramTolerance = 1.e-4;
46 const double PI = 3.141592653589793238463;
47
48
49 SketchPlugin_MacroEllipticArc::SketchPlugin_MacroEllipticArc()
50   : SketchPlugin_SketchEntity(),
51     myMajorRadius(0.0),
52     myMinorRadius(0.0),
53     myParamDelta(0.0)
54 {
55 }
56
57 void SketchPlugin_MacroEllipticArc::initAttributes()
58 {
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());
67
68   data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
69   data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
70
71   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
72   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
73
74   boolean(REVERSED_ID())->setValue(false);
75
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());
81 }
82
83 void SketchPlugin_MacroEllipticArc::execute()
84 {
85   FeaturePtr anEllipse = createEllipticArcFeature();
86
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);
93 }
94
95 void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& theID)
96 {
97   static const int NB_POINTS = 4;
98   std::string aPointAttrName[NB_POINTS] = { CENTER_ID(),
99                                             MAJOR_AXIS_POINT_ID(),
100                                             START_POINT_ID(),
101                                             END_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() };
106
107   int aNbInitialized = 0;
108   GeomPnt2dPtr anEllipsePoints[NB_POINTS];
109
110   for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
111     AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
112     if (!aPointAttr->isInitialized())
113       continue;
114
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);
122
123     anEllipsePoints[aNbInitialized++] = aPassedPoint;
124   }
125
126   if (aNbInitialized <= 1)
127     return; // too few points for the ellipse
128
129   myCenter    = anEllipsePoints[0];
130   myMajorAxis = anEllipsePoints[1];
131   myStartPnt  = anEllipsePoints[2];
132   myEndPnt    = anEllipsePoints[3];
133
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));
141   }
142   else if (aNbInitialized >= 3) {
143     anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
144       new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
145   }
146
147   if (!anEllipse || anEllipse->implPtr<void>() == 0)
148     return;
149
150   myMajorRadius = anEllipse->majorRadius();
151   myMinorRadius = anEllipse->minorRadius();
152
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);
157
158   // update the REVERSED flag
159   if (myEndPnt) {
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;
166
167       if (myParamDelta > 0.0 && myParamDelta <= PI * 0.5 &&
168           aParameterEnd < 0 && aParameterEnd >= -PI * 0.5) {
169         boolean(REVERSED_ID())->setValue(true);
170       }
171       else if (myParamDelta < 0.0 && myParamDelta >= -PI * 0.5 &&
172                aParameterEnd > 0.0 && aParameterEnd <= PI * 0.5) {
173         boolean(REVERSED_ID())->setValue(false);
174       }
175     }
176     myParamDelta = aParameterEnd;
177   }
178 }
179
180 // LCOV_EXCL_START
181 std::string SketchPlugin_MacroEllipticArc::processEvent(
182                                               const std::shared_ptr<Events_Message>& theMessage)
183 {
184   std::string aFilledAttributeName;
185
186   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
187       std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
188   if (aReentrantMessage) {
189     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
190
191     ObjectPtr anObject = aReentrantMessage->selectedObject();
192     AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
193     std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
194
195     if (aClickedPoint && (anObject || anAttribute)) {
196       aFilledAttributeName = CENTER_ID();
197       std::string aReferenceAttributeName = CENTER_REF_ID();
198
199       // fill 2d point attribute
200       AttributePoint2DPtr aPointAttr =
201           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
202       aPointAttr->setValue(aClickedPoint);
203
204       // fill reference attribute
205       AttributeRefAttrPtr aRefAttr =
206           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
207       if (anAttribute) {
208         if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
209           if (aCreatedFeature && anAttribute->id() == CENTER_ID())
210             anAttribute = aCreatedFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID());
211         }
212         aRefAttr->setAttr(anAttribute);
213       }
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 = "";
220       }
221     }
222     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
223   }
224   return aFilledAttributeName;
225 }
226 // LCOV_EXCL_STOP
227
228 FeaturePtr SketchPlugin_MacroEllipticArc::createEllipticArcFeature()
229 {
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);
239
240     if (anArcEdge->isEllipse())
241       anEllipse = anArcEdge->ellipse();
242   }
243
244   if (!anEllipse)
245     return FeaturePtr();
246
247   // Create and fill new EllipticArc feature
248   SketchPlugin_Sketch* aSketch = sketch();
249   FeaturePtr aEllipseFeature = aSketch->addFeature(SketchPlugin_EllipticArc::ID());
250
251   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252       aEllipseFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
253   aCenterAttr->setValue(aSketch->to2D(anEllipse->center()));
254
255   AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
256       aEllipseFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
257   aFocusAttr->setValue(aSketch->to2D(anEllipse->firstFocus()));
258
259   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260       aEllipseFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
261   aStartAttr->setValue(aSketch->to2D(aStartPoint));
262
263   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
264       aEllipseFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
265   aEndAttr->setValue(aSketch->to2D(aEndPoint));
266
267   aEllipseFeature->real(SketchPlugin_EllipticArc::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
268   aEllipseFeature->real(SketchPlugin_EllipticArc::MINOR_RADIUS_ID())->setValue(myMinorRadius);
269
270   aEllipseFeature->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(
271       boolean(REVERSED_ID())->value());
272
273   aEllipseFeature->boolean(SketchPlugin_EllipticArc::AUXILIARY_ID())->setValue(
274       boolean(AUXILIARY_ID())->value());
275
276   aEllipseFeature->execute();
277
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());
300
301   return aEllipseFeature;
302 }
303
304 AISObjectPtr SketchPlugin_MacroEllipticArc::getAISObject(AISObjectPtr thePrevious)
305 {
306   SketchPlugin_Sketch* aSketch = sketch();
307   if (!aSketch || !myCenter || !myMajorAxis)
308     return AISObjectPtr();
309
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();
316
317   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
318   aShapes.push_back(anArcShape);
319   aShapes.push_back(aCenterPointShape);
320
321   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
322   AISObjectPtr anAIS = thePrevious;
323   if (!anAIS)
324     anAIS.reset(new GeomAPI_AISObject());
325   anAIS->createShape(aCompound);
326   return anAIS;
327 }
328
329 GeomShapePtr SketchPlugin_MacroEllipticArc::getArcShape()
330 {
331   if (!myCenter.get() || !myMajorAxis.get())
332     return GeomShapePtr();
333
334   SketchPlugin_Sketch* aSketch = sketch();
335   if (!aSketch)
336     return GeomShapePtr();
337
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()));
343
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()));
347
348   GeomPointPtr aStart, anEnd;
349   if (myStartPnt)
350     aStart = aSketch->to3D(myStartPnt->x(), myStartPnt->y());
351   if (myEndPnt)
352     anEnd = aSketch->to3D(myEndPnt->x(), myEndPnt->y());
353
354   GeomShapePtr anArcShape;
355   if (anEnd) {
356     if (boolean(REVERSED_ID())->value())
357       std::swap(aStart, anEnd);
358
359     anArcShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxisDir,
360         myMajorRadius, myMinorRadius, aStart, anEnd);
361   }
362   else {
363     anArcShape = GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxisDir,
364         myMajorRadius, myMinorRadius);
365   }
366
367   return anArcShape;
368 }