Salome HOME
Issue #2149 Split does not highlight the selected edge
[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 <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintTangent.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Tools.h>
23
24 #include <GeomAPI_Ax2.h>
25 #include <GeomAPI_Circ2d.h>
26 #include <GeomAPI_Circ.h>
27 #include <GeomAPI_Dir2d.h>
28 #include <GeomAPI_Dir.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_Vertex.h>
33 #include <GeomAPI_XY.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomDataAPI_Dir.h>
36 #include <GeomAlgoAPI_PointBuilder.h>
37 #include <GeomAlgoAPI_EdgeBuilder.h>
38 #include <GeomAlgoAPI_CompoundBuilder.h>
39 // for sqrt on Linux
40 #include <cmath>
41
42 const double tolerance = 1e-7;
43 const double paramTolerance = 1.e-4;
44 const double PI = 3.141592653589793238463;
45
46
47 SketchPlugin_Arc::SketchPlugin_Arc()
48 : SketchPlugin_SketchEntity()
49 {
50   myParamBefore = 0.0;
51 }
52
53 void SketchPlugin_Arc::initDerivedClassAttributes()
54 {
55   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
56   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
57   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
58
59   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
61
62   AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
63     data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
64
65   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
66   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
67
68   // set after all to avoid in attributeChanged reference to not existing attributes
69   if (!isReversed->isInitialized()) {
70     isReversed->setValue(false);
71   }
72 }
73
74 void SketchPlugin_Arc::execute()
75 {
76   SketchPlugin_Sketch* aSketch = sketch();
77   if(!aSketch) {
78     return;
79   }
80
81   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
82       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
83   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
84       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
85   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
86       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
87   if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
88     return;
89   }
90
91   // Make a visible point.
92   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
93
94   // Make a visible arc.
95   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
96   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
97   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
98   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
99       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
100   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
101
102   if (myParamBefore == 0) { // parameter has not been calculate yet
103     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
104         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
105     aCircleForArc->parameter(anEndAttr->pnt(), paramTolerance, myParamBefore);
106   }
107
108   GeomShapePtr anArcShape;
109   if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
110     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
111     myParamBefore = 0;
112   } else {
113     anArcShape = boolean(REVERSED_ID())->value() ?
114       GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
115     : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
116   }
117
118   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
119   aResult->setShape(anArcShape);
120   aResult->setIsInHistory(false);
121   setResult(aResult, 1);
122 }
123
124 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
125 {
126   std::shared_ptr<ModelAPI_Data> aData = data();
127   if(!aData->isValid()) {
128     return;
129   }
130
131   bool aWasBlocked = aData->blockSendAttributeUpdated(true);
132
133   std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
134       attribute(CENTER_ID()));
135   if(aCenter->isInitialized()) {
136     aCenter->move(theDeltaX, theDeltaY);
137   }
138
139   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
140       attribute(START_ID()));
141   if(aStart->isInitialized()) {
142     aStart->move(theDeltaX, theDeltaY);
143   }
144
145   std::shared_ptr<GeomDataAPI_Point2D> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146       attribute(END_ID()));
147   if(anEnd->isInitialized()) {
148     anEnd->move(theDeltaX, theDeltaY);
149   }
150
151   aData->blockSendAttributeUpdated(aWasBlocked);
152 }
153
154 bool SketchPlugin_Arc::isFixed()
155 {
156   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
157 }
158
159 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
160 {
161   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
162       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
163   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
164       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
165   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
166       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
167
168   // The second condition for unability to move external segments anywhere.
169   if(theID == EXTERNAL_ID() || isFixed()) {
170     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
171     if(!aSelection) {
172       // empty shape in selection shows that the shape is equal to context
173       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
174       if(anExtRes) {
175         aSelection = anExtRes->shape();
176       }
177     }
178     // update arguments due to the selection value
179     if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
180       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
181       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
182       if(aCirc.get()) {
183         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
184         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
185         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
186         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
187         data()->blockSendAttributeUpdated(aWasBlocked, false);
188
189         std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
190           std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
191                                                              aStartAttr->pnt()));
192
193         double anEndParam = 0.0;
194         aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
195         myParamBefore = anEndParam;
196
197         double aMidParam  = anEndParam / 2.0;
198         std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
199         aCircle2d->D0(aMidParam, aMidPnt2d);
200         std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
201         double aStartParam = 0.0;
202         aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
203         aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
204         aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
205
206         // adjust period
207         anEndParam -= aStartParam;
208         aMidParam -= aStartParam;
209         if (anEndParam < 0.0)
210           anEndParam += 2.0 * PI;
211         if (aMidParam < 0.0)
212           aMidParam += 2.0 * PI;
213
214         aWasBlocked = data()->blockSendAttributeUpdated(true);
215         if(aMidParam < anEndParam) {
216           setReversed(false);
217         } else {
218           setReversed(true);
219         }
220         data()->blockSendAttributeUpdated(aWasBlocked, false);
221       }
222     }
223   } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
224     if(!aCenterAttr->isInitialized()
225       || !aStartAttr->isInitialized()
226       || !anEndAttr->isInitialized()) {
227       return;
228     }
229     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
230     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
231     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
232     double aRadius = aCenter->distance(aStart);
233     if (aRadius < tolerance)
234       return;
235     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
236
237     // Do not recalculate REVERSED flag if the arc is not consistent
238     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
239     if (aProjection && anEnd->distance(aProjection) <= tolerance) {
240       double aParameterNew = 0.0;
241       if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
242         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
243         if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
244           if(!boolean(REVERSED_ID())->value()) {
245             boolean(REVERSED_ID())->setValue(true);
246           }
247         } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
248           if(boolean(REVERSED_ID())->value()) {
249             boolean(REVERSED_ID())->setValue(false);
250           }
251         }
252         data()->blockSendAttributeUpdated(aWasBlocked, false);
253       }
254       if (fabs(aParameterNew) < paramTolerance ||
255           fabs(aParameterNew - 2.0 * PI) < paramTolerance)
256         aParameterNew = 2.0 * PI;
257       myParamBefore = aParameterNew;
258     }
259   }
260
261   double aRadius = 0;
262   double anAngle = 0;
263   if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
264     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
265     if(anEndAttr->isInitialized()) {
266       if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
267         anAngle = 360;
268       } else {
269         GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
270         double aStartParam, anEndParam;
271         aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
272         aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
273         anAngle = (anEndParam - aStartParam) / PI * 180.0;
274         if(isReversed()) anAngle = 360.0 - anAngle;
275       }
276     }
277   }
278
279   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
280   real(RADIUS_ID())->setValue(aRadius);
281   real(ANGLE_ID())->setValue(anAngle);
282   data()->blockSendAttributeUpdated(aWasBlocked, false);
283 }
284
285 void SketchPlugin_Arc::setReversed(bool isReversed)
286 {
287   boolean(REVERSED_ID())->setValue(isReversed);
288 }
289
290 bool SketchPlugin_Arc::isReversed()
291 {
292   return boolean(REVERSED_ID())->value();
293 }