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