Salome HOME
7a75652b2e538a60f53641fdb2bf667434d9d788
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Arc.cpp
4 // Created:     26 Apr 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_Arc.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_Session.h>
14
15 #include <GeomAPI_Ax2.h>
16 #include <GeomAPI_Circ2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAlgoAPI_PointBuilder.h>
22 #include <GeomAlgoAPI_EdgeBuilder.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 // for sqrt on Linux
25 #include <math.h>
26
27 const double tolerance = 1e-7;
28 const double paramTolerance = 1.e-4;
29 const double PI =3.141592653589793238463;
30
31
32 static const std::string& INVERSED_ID()
33 {
34   static const std::string MY_INVERSED_ID("InversedArc");
35   return MY_INVERSED_ID;
36 }
37
38 SketchPlugin_Arc::SketchPlugin_Arc()
39     : SketchPlugin_SketchEntity()
40 {
41   myStartUpdate = false;
42   myEndUpdate = false;
43   // default values
44   myXEndBefore = 0;
45   myYEndBefore = 0;
46
47   myParamBefore = 0;
48 }
49
50 void SketchPlugin_Arc::initAttributes()
51 {
52   SketchPlugin_SketchEntity::initAttributes();
53
54   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
56   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
57     GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
58   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
60
61   data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
62
63   // get the initial values
64   if (anEndAttr->isInitialized()) {
65     myXEndBefore = anEndAttr->x();
66     myYEndBefore = anEndAttr->y();
67   }
68 }
69
70 void SketchPlugin_Arc::execute()
71 {
72   SketchPlugin_Sketch* aSketch = sketch();
73   // result for the arc is set only when all obligatory attributes are initialized,
74   // otherwise AIS object is used to visualize the arc's preview
75   if (aSketch && isFeatureValid()) {
76     // compute a circle point in 3D view
77     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
78         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
79     // compute the arc start point
80     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
81         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
82
83     std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
84     // make a visible point
85     std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
86     std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
87         data(), 0);
88     aConstr1->setShape(aCenterPointShape);
89     aConstr1->setIsInHistory(false);
90     setResult(aConstr1, 0);
91
92     // make a visible circle
93     std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
94         aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
95     std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
96     std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
97
98     // compute and change the arc end point
99     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
100         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
101     /* must be automatically done in attributeChanged
102     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
103         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
104     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
105     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
106       anEndAttr->setValue(aProjection);
107     */
108     std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
109     AttributeBooleanPtr isInversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
110
111     std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
112     std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
113     std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
114     double aParameterNew = 0.0;
115     if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
116       if(0 < myParamBefore && myParamBefore <= PI / 2.0
117         && PI * 1.5 < aParameterNew && aParameterNew <= PI * 2.0) {
118           isInversed->setValue(true);
119       } else if(PI * 1.5 < myParamBefore && myParamBefore <= PI * 2.0
120         && 0 < aParameterNew && aParameterNew <= PI / 2.0) {
121           isInversed->setValue(false);
122       }
123     }
124     myParamBefore = aParameterNew;
125
126     std::shared_ptr<GeomAPI_Shape> aCircleShape;
127     if(!isInversed->value()) {
128       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
129     } else {
130       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
131     }
132
133     if (aCircleShape) {
134       std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
135           data(), 1);
136       aConstr2->setShape(aCircleShape);
137       aConstr2->setIsInHistory(false);
138       setResult(aConstr2, 1);
139     }
140   }
141 }
142
143 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
144 {
145   SketchPlugin_Sketch* aSketch = sketch();
146   if (aSketch) {
147     // if the feature is valid, the execute() method should be performed, AIS object is empty
148     if (!isFeatureValid()) {
149       // compute a circle point in 3D view
150       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
151           GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
152       if (aCenterAttr->isInitialized()) {
153         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
154
155         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
156             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
157         if (aStartAttr->isInitialized()) {
158           // make a visible circle
159           std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
160               aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
161           bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
162           if (aHasPlane) {
163             std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
164             std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
165             std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
166                                                             aCenter, aStartPoint, aStartPoint, aNormal);
167             if (aCircleShape) {
168               std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
169               // make a visible point
170               std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
171               aShapes.push_back(aCenterPointShape);
172
173               aShapes.push_back(aCircleShape);
174               if (!aShapes.empty())
175               {
176                 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
177                 AISObjectPtr anAIS = thePrevious;
178                 if (!anAIS)
179                   anAIS = AISObjectPtr(new GeomAPI_AISObject);
180                 anAIS->createShape(aCompound);
181                 anAIS->setWidth(3);
182                 return anAIS;
183               }
184             }
185           }
186         }
187       }
188     }
189   }
190   return AISObjectPtr();
191 }
192
193 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
194 {
195   std::shared_ptr<ModelAPI_Data> aData = data();
196   if (!aData->isValid())
197     return;
198
199   myStartUpdate = true;
200   myEndUpdate = true;
201   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202       aData->attribute(SketchPlugin_Arc::START_ID()));
203   aPoint2->move(theDeltaX, theDeltaY);
204
205   std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206       aData->attribute(SketchPlugin_Arc::END_ID()));
207   aPoint3->move(theDeltaX, theDeltaY);
208   myStartUpdate = false;
209   myEndUpdate = false;
210
211   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
213   aPoint1->move(theDeltaX, theDeltaY);
214 }
215
216 bool SketchPlugin_Arc::isFixed() {
217   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
218 }
219
220 bool SketchPlugin_Arc::isFeatureValid()
221 {
222   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
223       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
224   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
225       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
226   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
227       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
228
229   return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
230 }
231
232 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
233 {
234   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
235       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
236   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
237       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
238   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
239       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
240   // the second condition for unability to move external segments anywhere
241   if (theID == EXTERNAL_ID() || isFixed()) {
242     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
243     // update arguments due to the selection value
244     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
245       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
246       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
247       if (aCirc.get()) {
248         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
249         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
250         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
251       }
252     }
253     return;
254   }
255   if (!aCenterAttr->isInitialized())
256     return;
257   if (!aStartAttr->isInitialized())
258     return;
259   if (!anEndAttr->isInitialized())
260     return;
261
262   // update the points in accordance to the changed point changes
263   if (theID == END_ID() && !myEndUpdate) {
264     myEndUpdate = true;
265     // compute and change the arc end point
266     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
267         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
268     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
269     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance) {
270       // issue #855: trying to update only not-updated coordinate if it is possible
271       /*
272       if (abs(myXEndBefore - anEndAttr->x()) < 1.e-10) { // keep Y unchanged
273         double aVy = aCenterAttr->y() - anEndAttr->y();
274         double aVy2 = aVy * aVy;
275         double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
276         if (aVy2 <= aR2) {
277           double aDX = sqrt(aR2 - aVy * aVy);
278           if (anEndAttr->x() > aCenterAttr->x())
279             aProjection->setX(aCenterAttr->x() + aDX);
280           else 
281             aProjection->setX(aCenterAttr->x() - aDX);
282           aProjection->setY(anEndAttr->y());
283         }
284       } else if (abs(myYEndBefore - anEndAttr->y()) < 1.e-10) { // keep X unchanged
285         double aVx = aCenterAttr->x() - anEndAttr->x();
286         double aVx2 = aVx * aVx;
287         double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
288         if (aVx2 <= aR2) {
289           double aDY = sqrt(aR2 - aVx * aVx);
290           if (anEndAttr->y() > aCenterAttr->y())
291             aProjection->setY(aCenterAttr->y() + aDY);
292           else 
293             aProjection->setY(aCenterAttr->y() - aDY);
294           aProjection->setX(anEndAttr->x());
295         }
296       }*/
297
298       anEndAttr->setValue(aProjection);
299     }
300     myXEndBefore = anEndAttr->x();
301     myYEndBefore = anEndAttr->y();
302     myEndUpdate = false;
303   } else if (theID == START_ID() && !myStartUpdate) {
304     myStartUpdate = true;
305     // compute and change the arc start point
306     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
307         new GeomAPI_Circ2d(aCenterAttr->pnt(), anEndAttr->pnt()));
308     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aStartAttr->pnt());
309     if (aProjection && aStartAttr->pnt()->distance(aProjection) > tolerance)
310       aStartAttr->setValue(aProjection);
311     myStartUpdate = false;
312   } else if (theID == CENTER_ID() && !myEndUpdate) {
313     myEndUpdate = true;
314     // compute and change the arc end point
315     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
316         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
317     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
318     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
319       anEndAttr->setValue(aProjection);
320     myEndUpdate = false;
321   }
322 }