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