Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_Arc.h"
22 #include "SketchPlugin_Sketch.h"
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_ConstraintTangent.h>
25
26 #include <Events_Loop.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Tools.h>
37
38 #include <GeomAPI_Ax2.h>
39 #include <GeomAPI_Circ2d.h>
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_Dir2d.h>
42 #include <GeomAPI_Dir.h>
43 #include <GeomAPI_Lin2d.h>
44 #include <GeomAPI_Lin.h>
45 #include <GeomAPI_Pnt2d.h>
46 #include <GeomAPI_Vertex.h>
47 #include <GeomAPI_XY.h>
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Dir.h>
50 #include <GeomAlgoAPI_PointBuilder.h>
51 #include <GeomAlgoAPI_EdgeBuilder.h>
52 #include <GeomAlgoAPI_CompoundBuilder.h>
53 // for sqrt on Linux
54 #include <cmath>
55
56 const double tolerance = 1e-7;
57 const double paramTolerance = 1.e-4;
58 const double PI = 3.141592653589793238463;
59
60
61 SketchPlugin_Arc::SketchPlugin_Arc()
62 : SketchPlugin_SketchEntity()
63 {
64   myParamBefore = 0.0;
65 }
66
67 void SketchPlugin_Arc::initDerivedClassAttributes()
68 {
69   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
70   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
71   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
72
73   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
75
76   AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
77     data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
78
79   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
80   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
81
82   // set after all to avoid in attributeChanged reference to not existing attributes
83   if (!isReversed->isInitialized()) {
84     isReversed->setValue(false);
85   }
86 }
87
88 void SketchPlugin_Arc::execute()
89 {
90   SketchPlugin_Sketch* aSketch = sketch();
91   if(!aSketch) {
92     return;
93   }
94
95   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
96       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
97   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
98       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
99   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
100       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
101   if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
102     return;
103   }
104
105   // Make a visible point.
106   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
107
108   // Make a visible arc.
109   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
110   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
111   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
112   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
113       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
114   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
115
116   if (myParamBefore == 0) { // parameter has not been calculate yet
117     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
118         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
119     aCircleForArc->parameter(anEndAttr->pnt(), paramTolerance, myParamBefore);
120   }
121
122   GeomShapePtr anArcShape;
123   if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
124     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
125     myParamBefore = 0;
126   } else {
127     anArcShape = boolean(REVERSED_ID())->value() ?
128       GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
129     : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
130   }
131
132   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
133   aResult->setShape(anArcShape);
134   aResult->setIsInHistory(false);
135   setResult(aResult, 1);
136 }
137
138 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
139 {
140   std::shared_ptr<ModelAPI_Data> aData = data();
141   if(!aData->isValid()) {
142     return;
143   }
144
145   bool aWasBlocked = aData->blockSendAttributeUpdated(true);
146
147   std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148       attribute(CENTER_ID()));
149   if(aCenter->isInitialized()) {
150     aCenter->move(theDeltaX, theDeltaY);
151   }
152
153   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154       attribute(START_ID()));
155   if(aStart->isInitialized()) {
156     aStart->move(theDeltaX, theDeltaY);
157   }
158
159   std::shared_ptr<GeomDataAPI_Point2D> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160       attribute(END_ID()));
161   if(anEnd->isInitialized()) {
162     anEnd->move(theDeltaX, theDeltaY);
163   }
164
165   aData->blockSendAttributeUpdated(aWasBlocked);
166 }
167
168 bool SketchPlugin_Arc::isFixed()
169 {
170   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
171 }
172
173 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
174 {
175   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
176       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
177   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
178       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
179   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
180       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
181
182   // The second condition for unability to move external segments anywhere.
183   if(theID == EXTERNAL_ID() || isFixed()) {
184     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
185     if(!aSelection) {
186       // empty shape in selection shows that the shape is equal to context
187       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
188       if(anExtRes) {
189         aSelection = anExtRes->shape();
190       }
191     }
192     // update arguments due to the selection value
193     if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
194       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
195       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
196       if(aCirc.get()) {
197         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
198         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
199         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
200         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
201         data()->blockSendAttributeUpdated(aWasBlocked, false);
202
203         std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
204           std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
205                                                              aStartAttr->pnt()));
206
207         double anEndParam = 0.0;
208         aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
209         myParamBefore = anEndParam;
210
211         double aMidParam  = anEndParam / 2.0;
212         std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
213         aCircle2d->D0(aMidParam, aMidPnt2d);
214         std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
215         double aStartParam = 0.0;
216         aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
217         aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
218         aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
219
220         // adjust period
221         anEndParam -= aStartParam;
222         aMidParam -= aStartParam;
223         if (anEndParam < 0.0)
224           anEndParam += 2.0 * PI;
225         if (aMidParam < 0.0)
226           aMidParam += 2.0 * PI;
227
228         aWasBlocked = data()->blockSendAttributeUpdated(true);
229         if(aMidParam < anEndParam) {
230           setReversed(false);
231         } else {
232           setReversed(true);
233         }
234         data()->blockSendAttributeUpdated(aWasBlocked, false);
235       }
236     }
237   } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
238     if(!aCenterAttr->isInitialized()
239       || !aStartAttr->isInitialized()
240       || !anEndAttr->isInitialized()) {
241       return;
242     }
243     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
244     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
245     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
246     double aRadius = aCenter->distance(aStart);
247     if (aRadius < tolerance)
248       return;
249     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
250
251     // Do not recalculate REVERSED flag if the arc is not consistent
252     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
253     if (aProjection && anEnd->distance(aProjection) <= tolerance) {
254       double aParameterNew = 0.0;
255       if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
256         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
257         if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
258           if(!boolean(REVERSED_ID())->value()) {
259             boolean(REVERSED_ID())->setValue(true);
260           }
261         } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
262           if(boolean(REVERSED_ID())->value()) {
263             boolean(REVERSED_ID())->setValue(false);
264           }
265         }
266         data()->blockSendAttributeUpdated(aWasBlocked, false);
267       }
268       if (fabs(aParameterNew) < paramTolerance ||
269           fabs(aParameterNew - 2.0 * PI) < paramTolerance)
270         aParameterNew = 2.0 * PI;
271       myParamBefore = aParameterNew;
272     }
273   }
274
275   double aRadius = 0;
276   double anAngle = 0;
277   if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
278     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
279     if(anEndAttr->isInitialized()) {
280       if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
281         anAngle = 360;
282       } else {
283         GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
284         double aStartParam, anEndParam;
285         aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
286         aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
287         anAngle = (anEndParam - aStartParam) / PI * 180.0;
288         if(isReversed()) anAngle = 360.0 - anAngle;
289       }
290     }
291   }
292
293   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
294   real(RADIUS_ID())->setValue(aRadius);
295   real(ANGLE_ID())->setValue(anAngle);
296   data()->blockSendAttributeUpdated(aWasBlocked, false);
297 }
298
299 void SketchPlugin_Arc::setReversed(bool isReversed)
300 {
301   boolean(REVERSED_ID())->setValue(isReversed);
302 }
303
304 bool SketchPlugin_Arc::isReversed()
305 {
306   return boolean(REVERSED_ID())->value();
307 }