Salome HOME
Update copyrights
[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 // File:        SketchPlugin_MacroEllipse.cpp
21 // Created:     26 April 2017
22 // Author:      Artem ZHIDKOV
23
24 #include <SketchPlugin_MacroEllipse.h>
25
26 #include <SketchPlugin_Ellipse.h>
27 #include <SketchPlugin_Tools.h>
28 #include <SketchPlugin_Sketch.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_EventReentrantMessage.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Events.h>
36
37 #include <GeomDataAPI_Point2D.h>
38
39 #include <GeomAPI_Dir2d.h>
40 #include <GeomAPI_Pnt2d.h>
41 #include <GeomAPI_Ellipse2d.h>
42 #include <GeomAPI_Vertex.h>
43
44 #include <GeomAlgoAPI_CompoundBuilder.h>
45 #include <GeomAlgoAPI_EdgeBuilder.h>
46 #include <GeomAlgoAPI_PointBuilder.h>
47
48
49 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
50 : SketchPlugin_SketchEntity(),
51   myMajorRadius(0.0),
52   myMinorRadius(0.0)
53 {
54 }
55
56 void SketchPlugin_MacroEllipse::initAttributes()
57 {
58   data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
59   data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
60   data()->addAttribute(MAJOR_AXIS_POINT_ID(), GeomDataAPI_Point2D::typeId());
61   data()->addAttribute(MAJOR_AXIS_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
62   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
63   data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
64
65   data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
66   data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
67   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
68
69   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
70   ModelAPI_Session::get()->validators()->
71       registerNotObligatory(getKind(), MAJOR_AXIS_POINT_REF_ID());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
73 }
74
75 void SketchPlugin_MacroEllipse::execute()
76 {
77   FeaturePtr anEllipse = createEllipseFeature();
78   constraintsForEllipse(anEllipse);
79
80   // message to init reentrant operation
81   static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
82   ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
83   aMessage->setCreatedFeature(anEllipse);
84   Events_Loop::loop()->send(aMessage);
85 }
86
87 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
88 {
89   static const int NB_POINTS = 3;
90   std::string aPointAttrName[NB_POINTS] = { CENTER_POINT_ID(),
91                                             MAJOR_AXIS_POINT_ID(),
92                                             PASSED_POINT_ID() };
93   std::string aPointRefName[NB_POINTS] = { CENTER_POINT_REF_ID(),
94                                            MAJOR_AXIS_POINT_REF_ID(),
95                                            PASSED_POINT_REF_ID() };
96
97   int aNbInitialized = 0;
98   std::shared_ptr<GeomAPI_Pnt2d> anEllipsePoints[NB_POINTS];
99
100   for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
101     AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
102     if (!aPointAttr->isInitialized())
103       continue;
104
105     AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
106     // calculate ellipse parameters
107     std::shared_ptr<GeomAPI_Pnt2d> aPassedPoint;
108     std::shared_ptr<GeomAPI_Shape> aTangentCurve;
109     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
110         aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
111
112     anEllipsePoints[aNbInitialized++] = aPassedPoint;
113   }
114
115   std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
116   if (aNbInitialized == 2) {
117     std::shared_ptr<GeomAPI_Dir2d> aXDir(new GeomAPI_Dir2d(
118         anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
119         anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
120     double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
121     anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
122         new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
123   } else if (aNbInitialized == 3) {
124     anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
125         new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
126   }
127
128   if (!anEllipse || anEllipse->implPtr<void>() == 0)
129     return;
130
131   myCenter = anEllipse->center();
132   myFocus = anEllipse->firstFocus();
133   myMajorRadius = anEllipse->majorRadius();
134   myMinorRadius = anEllipse->minorRadius();
135
136   AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
137   AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
138
139   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
140   // center attribute is used in processEvent() to set reference to reentrant arc
141   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))->setValue(myCenter);
142   aMajorRadiusAttr->setValue(myMajorRadius);
143   aMinorRadiusAttr->setValue(myMinorRadius);
144   data()->blockSendAttributeUpdated(aWasBlocked, false);
145 }
146
147 std::string SketchPlugin_MacroEllipse::processEvent(
148                                               const std::shared_ptr<Events_Message>& theMessage)
149 {
150   std::string aFilledAttributeName;
151
152   ReentrantMessagePtr aReentrantMessage =
153         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
154   if (aReentrantMessage) {
155     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
156     ObjectPtr anObject = aReentrantMessage->selectedObject();
157     AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
158     std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
159
160     if (aClickedPoint && (anObject || anAttribute)) {
161       aFilledAttributeName = CENTER_POINT_ID();
162       std::string aReferenceAttributeName = CENTER_POINT_REF_ID();
163
164       // fill 2d point attribute
165       AttributePoint2DPtr aPointAttr =
166           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
167       aPointAttr->setValue(aClickedPoint);
168
169       // fill reference attribute
170       AttributeRefAttrPtr aRefAttr =
171           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
172       if (anAttribute) {
173         if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
174           if (aCreatedFeature && anAttribute->id() == CENTER_POINT_ID())
175             anAttribute = aCreatedFeature->attribute(SketchPlugin_Ellipse::CENTER_ID());
176         }
177         aRefAttr->setAttr(anAttribute);
178       }
179       else if (anObject.get()) {
180         // if presentation of previous reentrant macro arc is used, the object is invalid,
181         // we should use result of previous feature of the message(Arc)
182         if (!anObject->data()->isValid())
183           anObject = aCreatedFeature->lastResult();
184         aRefAttr->setObject(anObject);
185       }
186     }
187     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
188   }
189   return aFilledAttributeName;
190 }
191
192 void SketchPlugin_MacroEllipse::constraintsForEllipse(FeaturePtr theEllipseFeature)
193 {
194   // Create constraints.
195   SketchPlugin_Tools::createCoincidenceOrTangency(
196       this, CENTER_POINT_REF_ID(),
197       theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
198       ObjectPtr(), false);
199   SketchPlugin_Tools::createCoincidenceOrTangency(
200       this, MAJOR_AXIS_POINT_REF_ID(), AttributePtr(),
201       theEllipseFeature->lastResult(), true);
202   SketchPlugin_Tools::createCoincidenceOrTangency(
203       this, PASSED_POINT_REF_ID(), AttributePtr(),
204       theEllipseFeature->lastResult(), true);
205 }
206
207 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
208 {
209   FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
210
211   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212       aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
213   aCenterAttr->setValue(myCenter->x(), myCenter->y());
214
215   AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
216       aEllipseFeature->attribute(SketchPlugin_Ellipse::FOCUS_ID()));
217   aFocusAttr->setValue(myFocus->x(), myFocus->y());
218
219   aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
220   aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
221
222   aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
223       boolean(AUXILIARY_ID())->value());
224
225   aEllipseFeature->execute();
226   return aEllipseFeature;
227 }
228
229 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
230 {
231   SketchPlugin_Sketch* aSketch = sketch();
232   if (!aSketch || !myCenter || myMajorRadius == 0)
233     return AISObjectPtr();
234
235   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
236       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
237
238   // Compute a ellipse in 3D view.
239   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
240   std::shared_ptr<GeomAPI_Pnt> aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
241   std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
242   std::shared_ptr<GeomAPI_Dir> aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
243       aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
244
245   std::shared_ptr<GeomAPI_Shape> anEllipseShape =
246       GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
247   GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
248   if (!anEllipseShape.get() || !aCenterPointShape.get())
249     return AISObjectPtr();
250
251   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
252   aShapes.push_back(anEllipseShape);
253   aShapes.push_back(aCenterPointShape);
254
255   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
256   AISObjectPtr anAIS = thePrevious;
257   if (!anAIS)
258     anAIS.reset(new GeomAPI_AISObject());
259   anAIS->createShape(aCompound);
260   return anAIS;
261 }