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