]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MacroEllipse.cpp
Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroEllipse.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_MacroEllipse.h>
21
22 #include <SketchPlugin_Ellipse.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_AttributeString.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Events.h>
33
34 #include <GeomDataAPI_Point2D.h>
35
36 #include <GeomAPI_Dir2d.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 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
46 : SketchPlugin_SketchEntity(),
47   myMajorRadius(0.0),
48   myMinorRadius(0.0)
49 {
50 }
51
52 void SketchPlugin_MacroEllipse::initAttributes()
53 {
54   data()->addAttribute(ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
55   data()->addAttribute(EDIT_ELLIPSE_TYPE(), ModelAPI_AttributeString::typeId());
56
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());
63
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());
67
68   string(EDIT_ELLIPSE_TYPE())->setValue("");
69
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());
74 }
75
76 void SketchPlugin_MacroEllipse::execute()
77 {
78   FeaturePtr anEllipse = createEllipseFeature();
79
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);
85
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));
90
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);
95 }
96
97 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
98 {
99   static const int NB_POINTS = 3;
100   std::string aPointAttrName[NB_POINTS] = { FIRST_POINT_ID(),
101                                             SECOND_POINT_ID(),
102                                             PASSED_POINT_ID() };
103   std::string aPointRefName[NB_POINTS] = { FIRST_POINT_REF_ID(),
104                                            SECOND_POINT_REF_ID(),
105                                            PASSED_POINT_REF_ID() };
106
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]);
112     }
113   }
114   else {
115     int aNbInitialized = 0;
116     GeomPnt2dPtr anEllipsePoints[NB_POINTS];
117
118     for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
119       AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
120       if (!aPointAttr->isInitialized())
121         continue;
122
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);
129
130       anEllipsePoints[aNbInitialized++] = aPassedPoint;
131     }
132
133     if (aNbInitialized <= 1)
134       return; // too few points for the ellipse
135
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()));
141     }
142
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));
150     }
151     else if (aNbInitialized == 3) {
152       anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
153         new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
154     }
155
156     if (!anEllipse || anEllipse->implPtr<void>() == 0)
157       return;
158
159     myCenter = anEllipse->center();
160     myFocus = anEllipse->firstFocus();
161     myMajorRadius = anEllipse->majorRadius();
162     myMinorRadius = anEllipse->minorRadius();
163
164     AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
165     AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
166
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);
174   }
175 }
176
177 // LCOV_EXCL_START
178 std::string SketchPlugin_MacroEllipse::processEvent(
179                                               const std::shared_ptr<Events_Message>& theMessage)
180 {
181   std::string aFilledAttributeName;
182
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();
188
189     string(ELLIPSE_TYPE())->setValue(anEllipseType);
190
191     aFilledAttributeName = ELLIPSE_TYPE();
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 = FIRST_POINT_ID();
198       std::string aReferenceAttributeName = FIRST_POINT_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() == 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());
215         }
216         aRefAttr->setAttr(anAttribute);
217       }
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();
224       }
225     }
226     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
227   }
228   return aFilledAttributeName;
229 }
230 // LCOV_EXCL_STOP
231
232 void SketchPlugin_MacroEllipse::constraintsForEllipseByCenterAxisAndPassed(
233     FeaturePtr theEllipseFeature)
234 {
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);
249 }
250
251 void SketchPlugin_MacroEllipse::constraintsForEllipseByMajoxAxisAndPassed(
252     FeaturePtr theEllipseFeature)
253 {
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);
268 }
269
270 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
271 {
272   FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
273
274   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
275       aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
276   aCenterAttr->setValue(myCenter->x(), myCenter->y());
277
278   AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
279       aEllipseFeature->attribute(SketchPlugin_Ellipse::FIRST_FOCUS_ID()));
280   aFocusAttr->setValue(myFocus->x(), myFocus->y());
281
282   aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
283   aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
284
285   aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
286       boolean(AUXILIARY_ID())->value());
287
288   aEllipseFeature->execute();
289   return aEllipseFeature;
290 }
291
292 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
293 {
294   SketchPlugin_Sketch* aSketch = sketch();
295   if (!aSketch || !myCenter || myMajorRadius == 0)
296     return AISObjectPtr();
297
298   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
299       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
300
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()));
307
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();
313
314   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
315   aShapes.push_back(anEllipseShape);
316   aShapes.push_back(aCenterPointShape);
317
318   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
319   AISObjectPtr anAIS = thePrevious;
320   if (!anAIS)
321     anAIS.reset(new GeomAPI_AISObject());
322   anAIS->createShape(aCompound);
323   return anAIS;
324 }