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