]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Circle.cpp
Salome HOME
Optimization of circle and arc creation
[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   }
126 }
127
128 AISObjectPtr SketchPlugin_Circle::getAISObject(AISObjectPtr thePrevious)
129 {
130   SketchPlugin_Sketch* aSketch = sketch();
131   if (aSketch && !isFeatureValid()) {
132     // compute a circle point in 3D view
133     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
134         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
135     AttributeDoublePtr aRadiusAttr = 
136         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
137     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
138         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
139
140         // make a visible circle
141         std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142             aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
143         std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
144
145         double aRadius = aRadiusAttr->value();
146         std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
147             aCenter, aNormal, aRadius);
148         if (aCircleShape && aRadius != 0) {
149           std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
150           // make a visible point
151           std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
152           aShapes.push_back(aCenterPointShape);
153           aShapes.push_back(aCircleShape);
154
155           std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
156           AISObjectPtr anAIS = thePrevious;
157           if (!anAIS)
158             anAIS = AISObjectPtr(new GeomAPI_AISObject);
159           anAIS->createShape(aCompound);
160           anAIS->setWidth(3);
161           return anAIS;
162         }
163     }
164   }
165   return AISObjectPtr();
166 }
167
168 bool SketchPlugin_Circle::isFeatureValid()
169 {
170   std::shared_ptr<GeomDataAPI_Point2D> aCenter = 
171       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
172   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
173       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
174   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
175       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
176   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
177       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
178
179   return aCenter->isInitialized() && aFirstPnt->isInitialized() &&
180          aSecondPnt->isInitialized() && aThirdPnt->isInitialized();
181 }
182
183 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
184 {
185   std::shared_ptr<ModelAPI_Data> aData = data();
186   if (!aData->isValid())
187     return;
188
189   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190       aData->attribute(CENTER_ID()));
191   aPoint->move(theDeltaX, theDeltaY);
192
193   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
194   aPoint->move(theDeltaX, theDeltaY);
195   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
196   aPoint->move(theDeltaX, theDeltaY);
197   aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
198   aPoint->move(theDeltaX, theDeltaY);
199 }
200
201 bool SketchPlugin_Circle::isFixed() {
202   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
203 }
204
205 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
206   // the second condition for unability to move external segments anywhere
207   if (theID == EXTERNAL_ID() || isFixed()) {
208     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
209     // update arguments due to the selection value
210     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
211       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
212       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
213       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
214         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
215       aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
216       real(RADIUS_ID())->setValue(aCirc->radius());
217     }
218   }
219   else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
220     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
221       data()->attribute(CIRCLE_TYPE()))->value();
222     if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult())  // adjust data from the solver
223       adjustThreePoints();
224   } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
225     // support the center and radius attributes enev in other mode: solver uses them
226     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
227       data()->attribute(CIRCLE_TYPE()))->value();
228     if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
229       return;
230     data()->blockSendAttributeUpdated(true); // to modify two attributes at once
231     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
232     int aNbInitialized = 0;
233     for (int i = 1; i <= 3; ++i) {
234       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
235           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
236       if (aCurPnt->isInitialized())
237         aPoints[aNbInitialized++] = aCurPnt->pnt();
238     }
239
240     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
241         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
242     AttributeDoublePtr aRadiusAttr = 
243       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
244
245     if (aNbInitialized == 1)
246       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
247     else if (aNbInitialized == 2) {
248       std::shared_ptr<GeomAPI_XY> aCoord =
249           aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
250       double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
251       aCenterAttr->setValue(aCoord->x(), aCoord->y());
252       aRadiusAttr->setValue(aRadius);
253     } else {
254       std::shared_ptr<GeomAPI_Circ2d> aCircle(
255           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
256
257       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
258       if (aCenter) {
259         double aRadius = aCircle->radius();
260         aCenterAttr->setValue(aCenter->x(), aCenter->y());
261         aRadiusAttr->setValue(aRadius);
262       }
263     }
264     data()->blockSendAttributeUpdated(false, false);
265
266   } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
267     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
268       data()->attribute(CIRCLE_TYPE()))->value();
269     if (aType == CIRCLE_TYPE_THREE_POINTS()) {
270       adjustThreePoints();
271     }
272   }
273 }
274
275 void SketchPlugin_Circle::adjustThreePoints()
276 {
277   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
278       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
279   if (!aCenterAttr->isInitialized())
280     return;
281   AttributeDoublePtr aRadiusAttr = 
282     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
283   if (!aRadiusAttr->isInitialized())
284     return;
285
286   data()->blockSendAttributeUpdated(true);
287   std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
288       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
289   std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
290       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
291   std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
292       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
293   double aRadius = aRadiusAttr->value();
294
295   if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
296       fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
297       fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
298     aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
299     aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
300     aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
301   }
302   data()->blockSendAttributeUpdated(false, false);
303 }