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