Salome HOME
Merge branch 'Dev_2.8.0'
[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 bool SketchPlugin_Arc::isFixed()
139 {
140   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
141 }
142
143 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
144 {
145   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
146       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
147   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
148       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
149   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
150       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
151
152   // The second condition for unability to move external segments anywhere.
153   if(theID == EXTERNAL_ID() || isFixed()) {
154     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
155     if(!aSelection) {
156       // empty shape in selection shows that the shape is equal to context
157       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
158       if(anExtRes) {
159         aSelection = anExtRes->shape();
160       }
161     }
162     // update arguments due to the selection value
163     if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
164       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
165       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
166       if(aCirc.get()) {
167         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
168         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
169         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
170         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
171         data()->blockSendAttributeUpdated(aWasBlocked, false);
172
173         std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
174           std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
175                                                              aStartAttr->pnt()));
176
177         double anEndParam = 0.0;
178         aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
179         myParamBefore = anEndParam;
180
181         double aMidParam  = anEndParam / 2.0;
182         std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
183         aCircle2d->D0(aMidParam, aMidPnt2d);
184         std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
185         double aStartParam = 0.0;
186         aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
187         aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
188         aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
189
190         // adjust period
191         anEndParam -= aStartParam;
192         aMidParam -= aStartParam;
193         if (anEndParam < 0.0)
194           anEndParam += 2.0 * PI;
195         if (aMidParam < 0.0)
196           aMidParam += 2.0 * PI;
197
198         aWasBlocked = data()->blockSendAttributeUpdated(true);
199         if(aMidParam < anEndParam) {
200           setReversed(false);
201         } else {
202           setReversed(true);
203         }
204         data()->blockSendAttributeUpdated(aWasBlocked, false);
205       }
206     }
207   } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
208     if(!aCenterAttr->isInitialized()
209       || !aStartAttr->isInitialized()
210       || !anEndAttr->isInitialized()) {
211       return;
212     }
213     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
214     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
215     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
216     double aRadius = aCenter->distance(aStart);
217     if (aRadius < tolerance)
218       return;
219     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
220
221     // Do not recalculate REVERSED flag if the arc is not consistent
222     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
223     if (aProjection && anEnd->distance(aProjection) <= tolerance) {
224       double aParameterNew = 0.0;
225       if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
226         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
227         if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
228           if(!boolean(REVERSED_ID())->value()) {
229             boolean(REVERSED_ID())->setValue(true);
230           }
231         } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
232           if(boolean(REVERSED_ID())->value()) {
233             boolean(REVERSED_ID())->setValue(false);
234           }
235         }
236         data()->blockSendAttributeUpdated(aWasBlocked, false);
237       }
238       if (fabs(aParameterNew) < paramTolerance ||
239           fabs(aParameterNew - 2.0 * PI) < paramTolerance)
240         aParameterNew = 2.0 * PI;
241       myParamBefore = aParameterNew;
242     }
243   }
244
245   double aRadius = 0;
246   double anAngle = 0;
247   if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
248     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
249     if(anEndAttr->isInitialized()) {
250       if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
251         anAngle = 360;
252       } else {
253         GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
254         double aStartParam, anEndParam;
255         aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
256         aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
257         anAngle = (anEndParam - aStartParam) / PI * 180.0;
258         if(isReversed()) anAngle = 360.0 - anAngle;
259       }
260     }
261   }
262
263   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
264   real(RADIUS_ID())->setValue(aRadius);
265   real(ANGLE_ID())->setValue(anAngle);
266   data()->blockSendAttributeUpdated(aWasBlocked, false);
267 }
268
269 void SketchPlugin_Arc::setReversed(bool isReversed)
270 {
271   boolean(REVERSED_ID())->setValue(isReversed);
272 }
273
274 bool SketchPlugin_Arc::isReversed()
275 {
276   return boolean(REVERSED_ID())->value();
277 }