Salome HOME
Merge branch 'V9_4_BR'
[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   constraintsForEllipticArc(anEllipse);
87
88   // message to init reentrant operation
89   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
90   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
91     <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
92   aMessage->setCreatedFeature(anEllipse);
93   Events_Loop::loop()->send(aMessage);
94 }
95
96 void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& theID)
97 {
98   static const int NB_POINTS = 4;
99   std::string aPointAttrName[NB_POINTS] = { CENTER_ID(),
100                                             MAJOR_AXIS_POINT_ID(),
101                                             START_POINT_ID(),
102                                             END_POINT_ID() };
103   std::string aPointRefName[NB_POINTS] = { CENTER_REF_ID(),
104                                            MAJOR_AXIS_POINT_REF_ID(),
105                                            START_POINT_REF_ID(),
106                                            END_POINT_REF_ID() };
107
108   int aNbInitialized = 0;
109   GeomPnt2dPtr anEllipsePoints[NB_POINTS];
110
111   for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
112     AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
113     if (!aPointAttr->isInitialized())
114       continue;
115
116     AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
117     // calculate ellipse parameters
118     GeomPnt2dPtr aPassedPoint =
119         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr)->pnt();
120     GeomShapePtr aTangentCurve;
121     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
122       aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
123
124     anEllipsePoints[aNbInitialized++] = aPassedPoint;
125   }
126
127   if (aNbInitialized <= 1)
128     return; // too few points for the ellipse
129
130   myCenter    = anEllipsePoints[0];
131   myMajorAxis = anEllipsePoints[1];
132   myStartPnt  = anEllipsePoints[2];
133   myEndPnt    = anEllipsePoints[3];
134
135   std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
136   if (aNbInitialized == 2) {
137     GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
138                                          anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
139     double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
140     anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
141         new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
142   }
143   else if (aNbInitialized >= 3) {
144     anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
145       new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
146   }
147
148   if (!anEllipse || anEllipse->implPtr<void>() == 0)
149     return;
150
151   myMajorRadius = anEllipse->majorRadius();
152   myMinorRadius = anEllipse->minorRadius();
153
154   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
155   real(MAJOR_RADIUS_ID())->setValue(myMajorRadius);
156   real(MINOR_RADIUS_ID())->setValue(myMinorRadius);
157   data()->blockSendAttributeUpdated(aWasBlocked, false);
158
159   // update the REVERSED flag
160   if (myEndPnt) {
161     double aParameterEnd = 0.0;
162     GeomPnt2dPtr aEnd = anEllipse->project(myEndPnt);
163     if (anEllipse->parameter(aEnd, paramTolerance, aParameterEnd)) {
164       double aParamStart = 0.0;
165       anEllipse->parameter(myStartPnt, paramTolerance, aParamStart);
166       aParameterEnd -= aParamStart;
167
168       if (myParamDelta > 0.0 && myParamDelta <= PI * 0.5 &&
169           aParameterEnd < 0 && aParameterEnd >= -PI * 0.5) {
170         boolean(REVERSED_ID())->setValue(true);
171       }
172       else if (myParamDelta < 0.0 && myParamDelta >= -PI * 0.5 &&
173                aParameterEnd > 0.0 && aParameterEnd <= PI * 0.5) {
174         boolean(REVERSED_ID())->setValue(false);
175       }
176     }
177     myParamDelta = aParameterEnd;
178   }
179 }
180
181 // LCOV_EXCL_START
182 std::string SketchPlugin_MacroEllipticArc::processEvent(
183                                               const std::shared_ptr<Events_Message>& theMessage)
184 {
185   std::string aFilledAttributeName;
186
187   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
188       std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
189   if (aReentrantMessage) {
190     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
191
192     ObjectPtr anObject = aReentrantMessage->selectedObject();
193     AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
194     std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
195
196     if (aClickedPoint && (anObject || anAttribute)) {
197       aFilledAttributeName = CENTER_ID();
198       std::string aReferenceAttributeName = CENTER_REF_ID();
199
200       // fill 2d point attribute
201       AttributePoint2DPtr aPointAttr =
202           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
203       aPointAttr->setValue(aClickedPoint);
204
205       // fill reference attribute
206       AttributeRefAttrPtr aRefAttr =
207           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
208       if (anAttribute) {
209         if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
210           if (aCreatedFeature && anAttribute->id() == CENTER_ID())
211             anAttribute = aCreatedFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID());
212         }
213         aRefAttr->setAttr(anAttribute);
214       }
215       else if (anObject.get()) {
216         // if attribute is NULL, only object is defined, it should be processed outside
217         // the feature because it might be an external feature, that will be
218         // removed/created again after restart operation
219         // #2468 - Crash when sketching circles successively on a repetition
220         aFilledAttributeName = "";
221       }
222     }
223     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
224   }
225   return aFilledAttributeName;
226 }
227 // LCOV_EXCL_STOP
228
229 FeaturePtr SketchPlugin_MacroEllipticArc::createEllipticArcFeature()
230 {
231   GeomShapePtr anArc = getArcShape();
232   GeomEllipsePtr anEllipse;
233   GeomPointPtr aStartPoint, aEndPoint;
234   if (anArc->isEdge()) {
235     GeomEdgePtr anArcEdge = anArc->edge();
236     aStartPoint = anArcEdge->firstPoint();
237     aEndPoint = anArcEdge->lastPoint();
238     if (boolean(REVERSED_ID())->value())
239       std::swap(aStartPoint, aEndPoint);
240
241     if (anArcEdge->isEllipse())
242       anEllipse = anArcEdge->ellipse();
243   }
244
245   if (!anEllipse)
246     return FeaturePtr();
247
248   // Create and fill new EllipticArc feature
249   SketchPlugin_Sketch* aSketch = sketch();
250   FeaturePtr aEllipseFeature = aSketch->addFeature(SketchPlugin_EllipticArc::ID());
251
252   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
253       aEllipseFeature->attribute(SketchPlugin_EllipticArc::CENTER_ID()));
254   aCenterAttr->setValue(aSketch->to2D(anEllipse->center()));
255
256   AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
257       aEllipseFeature->attribute(SketchPlugin_EllipticArc::FIRST_FOCUS_ID()));
258   aFocusAttr->setValue(aSketch->to2D(anEllipse->firstFocus()));
259
260   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
261       aEllipseFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
262   aStartAttr->setValue(aSketch->to2D(aStartPoint));
263
264   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
265       aEllipseFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
266   aEndAttr->setValue(aSketch->to2D(aEndPoint));
267
268   aEllipseFeature->real(SketchPlugin_EllipticArc::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
269   aEllipseFeature->real(SketchPlugin_EllipticArc::MINOR_RADIUS_ID())->setValue(myMinorRadius);
270
271   aEllipseFeature->boolean(SketchPlugin_EllipticArc::REVERSED_ID())->setValue(
272       boolean(REVERSED_ID())->value());
273
274   aEllipseFeature->boolean(SketchPlugin_EllipticArc::AUXILIARY_ID())->setValue(
275       boolean(AUXILIARY_ID())->value());
276
277   aEllipseFeature->execute();
278
279   // create auxiliary points
280   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
281       aEllipseFeature, SketchPlugin_EllipticArc::CENTER_ID());
282   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
283       aEllipseFeature, SketchPlugin_EllipticArc::FIRST_FOCUS_ID());
284   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
285       aEllipseFeature, SketchPlugin_EllipticArc::SECOND_FOCUS_ID());
286   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
287       aEllipseFeature, SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID());
288   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
289       aEllipseFeature, SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
290   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
291       aEllipseFeature, SketchPlugin_EllipticArc::MINOR_AXIS_START_ID());
292   SketchPlugin_Tools::createAuxiliaryPointOnEllipse(
293       aEllipseFeature, SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
294   // create auxiliary axes
295   SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
296                       SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
297                       SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
298   SketchPlugin_Tools::createAuxiliaryAxisOfEllipse(aEllipseFeature,
299                       SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
300                       SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
301
302   return aEllipseFeature;
303 }
304
305 void SketchPlugin_MacroEllipticArc::constraintsForEllipticArc(FeaturePtr theEllipticArc)
306 {
307   // tangency on-the-fly is not applicable for elliptic arcs
308   static const bool isTangencyApplicable = false;
309   // Create constraints.
310   SketchPlugin_Tools::createCoincidenceOrTangency(
311       this, CENTER_REF_ID(),
312       theEllipticArc->attribute(SketchPlugin_EllipticArc::CENTER_ID()),
313       ObjectPtr(), isTangencyApplicable);
314   // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
315   if (!refattr(MAJOR_AXIS_POINT_REF_ID())->isObject()) {
316     SketchPlugin_Tools::createCoincidenceOrTangency(
317         this, MAJOR_AXIS_POINT_REF_ID(),
318         AttributePtr(),
319         theEllipticArc->lastResult(), isTangencyApplicable);
320   }
321   SketchPlugin_Tools::createCoincidenceOrTangency(
322       this, START_POINT_REF_ID(),
323       theEllipticArc->attribute(SketchPlugin_EllipticArc::START_POINT_ID()),
324       ObjectPtr(), isTangencyApplicable);
325   SketchPlugin_Tools::createCoincidenceOrTangency(
326       this, END_POINT_REF_ID(),
327       theEllipticArc->attribute(SketchPlugin_EllipticArc::END_POINT_ID()),
328       ObjectPtr(), isTangencyApplicable);
329 }
330
331 AISObjectPtr SketchPlugin_MacroEllipticArc::getAISObject(AISObjectPtr thePrevious)
332 {
333   SketchPlugin_Sketch* aSketch = sketch();
334   if (!aSketch || !myCenter || !myMajorAxis)
335     return AISObjectPtr();
336
337   // Compute a elliptic arc in 3D view.
338   GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
339   GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
340   GeomShapePtr anArcShape = getArcShape();
341   if (!anArcShape.get() || !aCenterPointShape.get())
342     return AISObjectPtr();
343
344   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
345   aShapes.push_back(anArcShape);
346   aShapes.push_back(aCenterPointShape);
347
348   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
349   AISObjectPtr anAIS = thePrevious;
350   if (!anAIS)
351     anAIS.reset(new GeomAPI_AISObject());
352   anAIS->createShape(aCompound);
353   SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
354   return anAIS;
355 }
356
357 GeomShapePtr SketchPlugin_MacroEllipticArc::getArcShape()
358 {
359   if (!myCenter.get() || !myMajorAxis.get())
360     return GeomShapePtr();
361
362   SketchPlugin_Sketch* aSketch = sketch();
363   if (!aSketch)
364     return GeomShapePtr();
365
366   GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
367   GeomPointPtr aMajorAxisPnt(aSketch->to3D(myMajorAxis->x(), myMajorAxis->y()));
368   GeomDirPtr aMajorAxisDir(new GeomAPI_Dir(aMajorAxisPnt->x() - aCenter->x(),
369                                            aMajorAxisPnt->y() - aCenter->y(),
370                                            aMajorAxisPnt->z() - aCenter->z()));
371
372   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
373       aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
374   GeomDirPtr aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
375
376   GeomPointPtr aStart, anEnd;
377   if (myStartPnt)
378     aStart = aSketch->to3D(myStartPnt->x(), myStartPnt->y());
379   if (myEndPnt)
380     anEnd = aSketch->to3D(myEndPnt->x(), myEndPnt->y());
381
382   GeomShapePtr anArcShape;
383   if (anEnd) {
384     if (boolean(REVERSED_ID())->value())
385       std::swap(aStart, anEnd);
386
387     anArcShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxisDir,
388         myMajorRadius, myMinorRadius, aStart, anEnd);
389   }
390   else {
391     anArcShape = GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxisDir,
392         myMajorRadius, myMinorRadius);
393   }
394
395   return anArcShape;
396 }