Salome HOME
Update all attributes of circle when changing any (issue #1316)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Circle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Circle.cpp
4 // Created:     26 May 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_Circle.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_Validator.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Session.h>
16
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Circ.h>
19 #include <GeomAPI_Circ2d.h>
20 #include <GeomAPI_XY.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24 #include <GeomAlgoAPI_EdgeBuilder.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
26
27 const double tolerance = 1e-7;
28
29 namespace {
30   static const std::string& CIRCLE_TYPE_CENTER_AND_RADIUS()
31   {
32     static const std::string TYPE("CenterRadius");
33     return TYPE;
34   }
35   static const std::string& CIRCLE_TYPE_THREE_POINTS()
36   {
37     static const std::string TYPE("ThreePoints");
38     return TYPE;
39   }
40
41   static const std::string& FIRST_POINT_ID()
42   {
43     static const std::string FIRST_PNT("FirstPoint");
44     return FIRST_PNT;
45   }
46   static const std::string& SECOND_POINT_ID()
47   {
48     static const std::string SECOND_PNT("SecondPoint");
49     return SECOND_PNT;
50   }
51   static const std::string& THIRD_POINT_ID()
52   {
53     static const std::string THIRD_PNT("ThirdPoint");
54     return THIRD_PNT;
55   }
56   static const std::string& POINT_ID(int theIndex)
57   {
58     switch (theIndex) {
59     case 1: return FIRST_POINT_ID();
60     case 2: return SECOND_POINT_ID();
61     case 3: return THIRD_POINT_ID();
62     }
63
64     static const std::string DUMMY;
65     return DUMMY;
66   }
67 }
68
69
70 SketchPlugin_Circle::SketchPlugin_Circle()
71     : SketchPlugin_SketchEntity()
72 {
73 }
74
75 void SketchPlugin_Circle::initDerivedClassAttributes()
76 {
77   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
78   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
79   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
80   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
81
82   data()->addAttribute(CIRCLE_TYPE(), ModelAPI_AttributeString::typeId());
83   data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
84   data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
85   data()->addAttribute(THIRD_POINT_ID(), GeomDataAPI_Point2D::typeId());
86   std::dynamic_pointer_cast<ModelAPI_AttributeString>(
87       data()->attribute(CIRCLE_TYPE()))->setValue(CIRCLE_TYPE_CENTER_AND_RADIUS());
88 }
89
90 void SketchPlugin_Circle::execute()
91 {
92   SketchPlugin_Sketch* aSketch = sketch();
93   if (aSketch) {
94     std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
95
96     // compute a circle point in 3D view
97     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
98         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
99     AttributeDoublePtr aRadiusAttr = 
100       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
101     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
102       std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
103       //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
104       // make a visible point
105       SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
106
107       // make a visible circle
108       std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
109         aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
110       std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
111       // compute the circle radius
112       double aRadius = aRadiusAttr->value();
113
114       std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
115         aCenter, aNormal, aRadius);
116       aShapes.push_back(aCircleShape);
117       std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
118         data(), 1);
119       aConstr2->setShape(aCircleShape);
120       aConstr2->setIsInHistory(false);
121       setResult(aConstr2, 1);
122
123       adjustThreePoints();
124     }
125   }
126 }
127
128 AISObjectPtr SketchPlugin_Circle::getAISObject(AISObjectPtr thePrevious)
129 {
130   SketchPlugin_Sketch* aSketch = sketch();
131   if (aSketch && !isFeatureValid()) {
132     // compute a circle point in 3D view
133     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
134         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
135     AttributeDoublePtr aRadiusAttr = 
136         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
137     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
138         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
139
140         // make a visible circle
141         std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142             aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
143         std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
144
145         double aRadius = aRadiusAttr->value();
146         std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
147             aCenter, aNormal, aRadius);
148         if (aCircleShape && aRadius != 0) {
149           std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
150           // make a visible point
151           std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
152           aShapes.push_back(aCenterPointShape);
153           aShapes.push_back(aCircleShape);
154
155           std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
156           AISObjectPtr anAIS = thePrevious;
157           if (!anAIS)
158             anAIS = AISObjectPtr(new GeomAPI_AISObject);
159           anAIS->createShape(aCompound);
160           anAIS->setWidth(3);
161           return anAIS;
162         }
163     }
164   }
165   return AISObjectPtr();
166 }
167
168 bool SketchPlugin_Circle::isFeatureValid()
169 {
170   std::shared_ptr<GeomDataAPI_Point2D> aCenter = 
171       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
172   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
173       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
174   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
175       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
176   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
177       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
178
179   return aCenter->isInitialized() && aFirstPnt->isInitialized() &&
180          aSecondPnt->isInitialized() && aThirdPnt->isInitialized();
181 }
182
183 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
184 {
185   std::shared_ptr<ModelAPI_Data> aData = data();
186   if (!aData->isValid())
187     return;
188
189   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190       aData->attribute(CENTER_ID()));
191   aPoint->move(theDeltaX, theDeltaY);
192
193   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
194   aPoint->move(theDeltaX, theDeltaY);
195   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
196   aPoint->move(theDeltaX, theDeltaY);
197   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
198   aPoint->move(theDeltaX, theDeltaY);
199 }
200
201 bool SketchPlugin_Circle::isFixed() {
202   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
203 }
204
205 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
206   // the second condition for unability to move external segments anywhere
207   if (theID == EXTERNAL_ID() || isFixed()) {
208     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
209     // update arguments due to the selection value
210     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
211       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
212       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
213       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
214         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
215       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
216       real(RADIUS_ID())->setValue(aCirc->radius());
217     }
218   }
219   else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
220     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
221       data()->attribute(CIRCLE_TYPE()))->value();
222     if (aType == CIRCLE_TYPE_THREE_POINTS())
223       return;
224
225     // check the execute() was called and the shape was built
226     if (!lastResult())
227       return;
228
229     adjustThreePoints();
230   }
231   else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
232     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
233       data()->attribute(CIRCLE_TYPE()))->value();
234     if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
235       return;
236
237     data()->blockSendAttributeUpdated(true);
238
239     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
240     int aNbInitialized = 0;
241     for (int i = 1; i <= 3; ++i) {
242       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
243           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
244       if (aCurPnt->isInitialized())
245         aPoints[aNbInitialized++] = aCurPnt->pnt();
246     }
247
248     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
249         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
250     AttributeDoublePtr aRadiusAttr = 
251       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
252
253     if (aNbInitialized == 1)
254       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
255     else if (aNbInitialized == 2) {
256       std::shared_ptr<GeomAPI_XY> aCoord =
257           aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
258       double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
259       aCenterAttr->setValue(aCoord->x(), aCoord->y());
260       aRadiusAttr->setValue(aRadius);
261     } else {
262       std::shared_ptr<GeomAPI_Circ2d> aCircle(
263           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
264
265       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
266       if (aCenter) {
267         double aRadius = aCircle->radius();
268         aCenterAttr->setValue(aCenter->x(), aCenter->y());
269         aRadiusAttr->setValue(aRadius);
270       }
271     }
272
273     data()->blockSendAttributeUpdated(false);
274   }
275 }
276
277 void SketchPlugin_Circle::adjustThreePoints()
278 {
279   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
280       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
281   if (!aCenterAttr->isInitialized())
282     return;
283   AttributeDoublePtr aRadiusAttr = 
284     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
285   if (!aRadiusAttr->isInitialized())
286     return;
287
288   data()->blockSendAttributeUpdated(true);
289   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
290       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
291   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
292       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
293   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
294       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
295   double aRadius = aRadiusAttr->value();
296
297   if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
298       fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
299       fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
300     aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
301     aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
302     aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
303   }
304   data()->blockSendAttributeUpdated(false);
305 }