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