Salome HOME
Added option to create Construction Point by projection point on plane. Fixed CPP...
[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     // update arguments due to the selection value
193     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
194       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
195       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
196       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
197         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
198       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
199       real(RADIUS_ID())->setValue(aCirc->radius());
200     }
201   }
202   else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
203     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
204       data()->attribute(CIRCLE_TYPE()))->value();
205     if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult())  // adjust data from the solver
206       adjustThreePoints();
207   } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
208     // support the center and radius attributes enev in other mode: solver uses them
209     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
210       data()->attribute(CIRCLE_TYPE()))->value();
211     if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
212       return;
213     data()->blockSendAttributeUpdated(true); // to modify two attributes at once
214     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
215     int aNbInitialized = 0;
216     for (int i = 1; i <= 3; ++i) {
217       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
218           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
219       if (aCurPnt->isInitialized())
220         aPoints[aNbInitialized++] = aCurPnt->pnt();
221     }
222
223     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
224         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
225     AttributeDoublePtr aRadiusAttr = 
226       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
227
228     if (aNbInitialized == 1)
229       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
230     else if (aNbInitialized == 2) {
231       std::shared_ptr<GeomAPI_XY> aCoord =
232           aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
233       double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
234       aCenterAttr->setValue(aCoord->x(), aCoord->y());
235       aRadiusAttr->setValue(aRadius);
236     } else {
237       std::shared_ptr<GeomAPI_Circ2d> aCircle(
238           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
239
240       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
241       if (aCenter) {
242         double aRadius = aCircle->radius();
243         aCenterAttr->setValue(aCenter->x(), aCenter->y());
244         aRadiusAttr->setValue(aRadius);
245       }
246     }
247     data()->blockSendAttributeUpdated(false, false);
248
249   } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
250     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
251       data()->attribute(CIRCLE_TYPE()))->value();
252     if (aType == CIRCLE_TYPE_THREE_POINTS()) {
253       adjustThreePoints();
254     }
255   }
256 }
257
258 void SketchPlugin_Circle::adjustThreePoints()
259 {
260   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
261       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
262   if (!aCenterAttr->isInitialized())
263     return;
264   AttributeDoublePtr aRadiusAttr = 
265     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
266   if (!aRadiusAttr->isInitialized())
267     return;
268
269   data()->blockSendAttributeUpdated(true);
270   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
271       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
272   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
273       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
274   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
275       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
276   double aRadius = aRadiusAttr->value();
277
278   if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
279       fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
280       fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
281     aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
282     aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
283     aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
284   }
285   data()->blockSendAttributeUpdated(false, false);
286 }