Salome HOME
Merge remote-tracking branch 'remotes/origin/HighLevelDump'
[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   aPoint->move(theDeltaX, theDeltaY);
175
176   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
177   aPoint->move(theDeltaX, theDeltaY);
178   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
179   aPoint->move(theDeltaX, theDeltaY);
180   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
181   aPoint->move(theDeltaX, theDeltaY);
182 }
183
184 bool SketchPlugin_Circle::isFixed() {
185   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
186 }
187
188 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
189   // the second condition for unability to move external segments anywhere
190   if (theID == EXTERNAL_ID() || isFixed()) {
191     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
192     if (!aSelection) {
193       // empty shape in selection shows that the shape is equal to context
194       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
195       if (anExtRes)
196         aSelection = anExtRes->shape();
197     }
198     // update arguments due to the selection value
199     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
200       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
201       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
202       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
203         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
204       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
205       real(RADIUS_ID())->setValue(aCirc->radius());
206     }
207   }
208   else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
209     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
210       data()->attribute(CIRCLE_TYPE()))->value();
211     if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult())  // adjust data from the solver
212       adjustThreePoints();
213   } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
214     // support the center and radius attributes enev in other mode: solver uses them
215     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
216       data()->attribute(CIRCLE_TYPE()))->value();
217     if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
218       return;
219     data()->blockSendAttributeUpdated(true); // to modify two attributes at once
220     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
221     int aNbInitialized = 0;
222     for (int i = 1; i <= 3; ++i) {
223       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
224           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
225       if (aCurPnt->isInitialized())
226         aPoints[aNbInitialized++] = aCurPnt->pnt();
227     }
228
229     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
230         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
231     AttributeDoublePtr aRadiusAttr = 
232       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
233
234     if (aNbInitialized == 1)
235       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
236     else if (aNbInitialized == 2) {
237       std::shared_ptr<GeomAPI_XY> aCoord =
238           aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
239       double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
240       aCenterAttr->setValue(aCoord->x(), aCoord->y());
241       aRadiusAttr->setValue(aRadius);
242     } else {
243       std::shared_ptr<GeomAPI_Circ2d> aCircle(
244           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
245
246       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
247       if (aCenter) {
248         double aRadius = aCircle->radius();
249         aCenterAttr->setValue(aCenter->x(), aCenter->y());
250         aRadiusAttr->setValue(aRadius);
251       }
252     }
253     data()->blockSendAttributeUpdated(false, false);
254
255   } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
256     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
257       data()->attribute(CIRCLE_TYPE()))->value();
258     if (aType == CIRCLE_TYPE_THREE_POINTS()) {
259       adjustThreePoints();
260     }
261   }
262 }
263
264 void SketchPlugin_Circle::adjustThreePoints()
265 {
266   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
267       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
268   if (!aCenterAttr->isInitialized())
269     return;
270   AttributeDoublePtr aRadiusAttr = 
271     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
272   if (!aRadiusAttr->isInitialized())
273     return;
274
275   data()->blockSendAttributeUpdated(true);
276   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
277       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
278   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
279       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
280   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
281       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
282   double aRadius = aRadiusAttr->value();
283
284   if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
285       fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
286       fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
287     aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
288     aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
289     aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
290   }
291   data()->blockSendAttributeUpdated(false, false);
292 }