Salome HOME
f0379c4618eac92385f15b151b3f1eaedd048e20
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroArc.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include "SketchPlugin_MacroArc.h"
21
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_ConstraintPerpendicular.h"
24 #include "SketchPlugin_ConstraintTangent.h"
25 #include "SketchPlugin_Sketch.h"
26 #include "SketchPlugin_Tools.h"
27 #include "SketchPlugin_MacroArcReentrantMessage.h"
28
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35
36 #include <GeomAPI_Circ.h>
37 #include <GeomAPI_Circ2d.h>
38 #include <GeomAPI_Curve.h>
39 #include <GeomAPI_Dir2d.h>
40 #include <GeomAPI_Edge.h>
41 #include <GeomAPI_Lin.h>
42 #include <GeomAPI_Lin2d.h>
43 #include <GeomAPI_Pnt2d.h>
44 #include <GeomAPI_Vertex.h>
45 #include <GeomAPI_XY.h>
46 #include <GeomAPI_ShapeIterator.h>
47
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Dir.h>
50
51 #include <GeomAlgoAPI_Circ2dBuilder.h>
52 #include <GeomAlgoAPI_EdgeBuilder.h>
53 #include <GeomAlgoAPI_CompoundBuilder.h>
54 #include <GeomAlgoAPI_PointBuilder.h>
55
56 // for sqrt on Linux
57 #include <math.h>
58
59 const double tolerance = 1e-7;
60 const double paramTolerance = 1.e-4;
61 const double PI = 3.141592653589793238463;
62
63 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
64 {
65   std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
66   if(aProjection.get())
67     thePoint->setValue(aProjection);
68 }
69
70 static void intersectShapeAndCircle(const GeomShapePtr& theShape,
71                                     const GeomAPI_Circ2d& theCircle,
72                                     const SketchPlugin_Sketch* theSketch,
73                                     AttributePoint2DPtr& theIntersection)
74 {
75   if (!theShape->isEdge())
76     return projectPointOnCircle(theIntersection, theCircle);
77
78   // convert shape to unbounded
79   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
80   if (anEdge->isLine()) {
81     static const double HALF_SIZE = 1.e6;
82     std::shared_ptr<GeomAPI_XYZ> aLoc = anEdge->line()->location()->xyz();
83     std::shared_ptr<GeomAPI_XYZ> aDir = anEdge->line()->direction()->xyz();
84
85     std::shared_ptr<GeomAPI_Pnt> aStart(
86         new GeomAPI_Pnt(aLoc->added(aDir->multiplied(-HALF_SIZE))));
87     std::shared_ptr<GeomAPI_Pnt> aEnd(
88         new GeomAPI_Pnt(aLoc->added(aDir->multiplied(HALF_SIZE))));
89     anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, aEnd);
90   } else if (anEdge->isArc()) {
91     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
92     anEdge = GeomAlgoAPI_EdgeBuilder::lineCircle(
93         aCircle->center(), aCircle->normal(), aCircle->radius());
94   }
95
96   // convert 2D circle to 3D object
97   std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = theCircle.center();
98   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aCenter2d->x(), aCenter2d->y()));
99   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
100       const_cast<SketchPlugin_Sketch*>(theSketch)->attribute(SketchPlugin_Sketch::NORM_ID()));
101   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
102
103   GeomShapePtr aCircleShape =
104       GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, theCircle.radius());
105
106   GeomShapePtr anInter = anEdge->intersect(aCircleShape);
107   std::shared_ptr<GeomAPI_Pnt2d> anInterPnt;
108   if (!anInter)
109     return projectPointOnCircle(theIntersection, theCircle);
110   if (anInter->isVertex()) {
111     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
112     anInterPnt = theSketch->to2D(aVertex->point());
113   } else if (anInter->isCompound()) {
114     double aMinDist = 1.e300;
115
116     GeomAPI_ShapeIterator anIt(anInter);
117     for (; anIt.more(); anIt.next()) {
118       GeomShapePtr aCurrent = anIt.current();
119       if (!aCurrent->isVertex())
120         continue;
121       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
122       std::shared_ptr<GeomAPI_Pnt2d> aPnt = theSketch->to2D(aVertex->point());
123       double aDist = aPnt->distance(theIntersection->pnt());
124       if (aDist < aMinDist) {
125         aMinDist = aDist;
126         anInterPnt = aPnt;
127       }
128     }
129   }
130   if(anInterPnt.get()) {
131     theIntersection->setValue(anInterPnt);
132   }
133 }
134
135
136 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
137 : SketchPlugin_SketchEntity(),
138   myParamBefore(0.0)
139 {
140 }
141
142 void SketchPlugin_MacroArc::initAttributes()
143 {
144   data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
145
146   data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
147   data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
148   data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
149
150   data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
151   data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
152   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
153
154   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
155   data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
156
157   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
158
159   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
160   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
161
162   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
163
164   data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
165   data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
166   data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
167   data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
168
169   data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
170
171   boolean(REVERSED_ID())->setValue(false);
172   string(EDIT_ARC_TYPE_ID())->setValue("");
173
174   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
175   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
176   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
177   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
178   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
179 }
180
181 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
182 {
183   std::string anArcType = string(ARC_TYPE())->value();
184
185   // If arc type switched reset according attributes.
186   if(theID == ARC_TYPE()) {
187     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
188     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
189     SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
190     SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
191     SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
192     SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
193     SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
194     SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
195     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
196     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
197     SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
198     SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
199     SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
200     SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
201     SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
202
203     myCenter.reset();
204     myStart.reset();
205     myEnd.reset();
206     boolean(REVERSED_ID())->setValue(false);
207     myParamBefore = 0.0;
208   } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
209     fillByCenterAndTwoPassed();
210   else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
211     fillByThreePassedPoints();
212   else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
213     fillByEdge(false);
214   else if (anArcType == ARC_TYPE_BY_TRANSVERSAL_LINE())
215     fillByEdge(true);
216
217   double aRadius = 0;
218   double anAngle = 0;
219   if(myCenter.get() && myStart.get()) {
220     aRadius = myCenter->distance(myStart);
221     if(myEnd.get()) {
222       if(myStart->isEqual(myEnd)) {
223         anAngle = 360;
224       } else {
225         GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
226         double aStartParam, anEndParam;
227         aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
228         aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
229         anAngle = (anEndParam - aStartParam) / PI * 180.0;
230         if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
231       }
232     }
233   }
234
235   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
236   if(myCenter.get()) {
237     // center attribute is used in processEvent() to set reference to reentrant arc
238     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
239         ->setValue(myCenter);
240   }
241   real(RADIUS_ID())->setValue(aRadius);
242   real(ANGLE_ID())->setValue(anAngle);
243   data()->blockSendAttributeUpdated(aWasBlocked, false);
244 }
245
246 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
247 {
248   if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
249     return GeomShapePtr();
250   }
251
252   SketchPlugin_Sketch* aSketch = sketch();
253   if(!aSketch) {
254     return GeomShapePtr();
255   }
256
257   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
258   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
259   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
260   std::shared_ptr<GeomDataAPI_Dir> aNDir =
261     std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
262   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
263
264   GeomShapePtr anArcShape;
265   if (isBound) {
266     anArcShape = boolean(REVERSED_ID())->value() ?
267         GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
268       : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
269   } else {
270     double aRadius = aCenter->distance(aStart);
271     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
272   }
273
274   return anArcShape;
275 }
276
277 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
278 {
279   GeomShapePtr anArcShape = getArcShape();
280   if(!anArcShape.get())
281     return AISObjectPtr();
282
283   SketchPlugin_Sketch* aSketch = sketch();
284   std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
285   GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
286
287   if(!aCenterPointShape.get())
288     return AISObjectPtr();
289
290   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
291   aShapes.push_back(anArcShape);
292   aShapes.push_back(aCenterPointShape);
293
294   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
295   AISObjectPtr anAIS = thePrevious;
296   if(!anAIS.get()) {
297     anAIS.reset(new GeomAPI_AISObject());
298   }
299   anAIS->createShape(aCompound);
300   return anAIS;
301 }
302
303 void SketchPlugin_MacroArc::execute()
304 {
305   FeaturePtr anArcFeature = createArcFeature();
306
307   myCenter.reset();
308   myStart.reset();
309   myEnd.reset();
310
311   // Create constraints.
312   std::string anArcType = string(ARC_TYPE())->value();
313   if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
314     SketchPlugin_Tools::createCoincidenceOrTangency(this,
315                                          CENTER_POINT_REF_ID(),
316                                          anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
317                                          ObjectPtr(),
318                                          false);
319     SketchPlugin_Tools::createCoincidenceOrTangency(this,
320                                          START_POINT_REF_ID(),
321                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
322                                          ObjectPtr(),
323                                          false);
324     SketchPlugin_Tools::createCoincidenceOrTangency(this,
325                                          END_POINT_REF_ID(),
326                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
327                                          ObjectPtr(),
328                                          false);
329   } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
330     SketchPlugin_Tools::createCoincidenceOrTangency(this,
331                                          START_POINT_REF_ID(),
332                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
333                                          ObjectPtr(),
334                                          false);
335     SketchPlugin_Tools::createCoincidenceOrTangency(this,
336                                          END_POINT_REF_ID(),
337                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
338                                          ObjectPtr(),
339                                          false);
340     SketchPlugin_Tools::createCoincidenceOrTangency(this,
341                                          PASSED_POINT_REF_ID(),
342                                          AttributePtr(),
343                                          anArcFeature->lastResult(),
344                                          true);
345   } else {
346     // coincident with connection point
347     SketchPlugin_Tools::createCoincidenceOrTangency(this,
348                                          TANGENT_POINT_ID(),
349                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
350                                          ObjectPtr(),
351                                          false);
352     // tangent or perpendicular constraint
353     FeaturePtr aStartPointConstraint;
354     if (anArcType == ARC_TYPE_BY_TANGENT_EDGE())
355       aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
356     else
357       aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
358     // setting attributes of the start point constraint
359     AttributeRefAttrPtr aRefAttrA =
360         aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
361     AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
362     FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
363     aRefAttrA->setObject(aTgFeature->lastResult());
364     AttributeRefAttrPtr aRefAttrB =
365         aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
366     aRefAttrB->setObject(anArcFeature->lastResult());
367     // constraint for end point
368     SketchPlugin_Tools::createCoincidenceOrTangency(this,
369                                          END_POINT_REF_ID(),
370                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
371                                          ObjectPtr(),
372                                          false);
373   }
374
375   // message to init reentrant operation
376   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
377   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
378     <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
379
380   std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
381   aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
382   aMessage->setCreatedFeature(anArcFeature);
383   Events_Loop::loop()->send(aMessage);
384 }
385
386 // LCOV_EXCL_START
387 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
388 {
389   std::string aFilledAttributeName;
390   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
391         std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
392   if (aReentrantMessage.get()) {
393     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
394     std::string anArcType = aReentrantMessage->typeOfCreation();
395
396     string(ARC_TYPE())->setValue(anArcType);
397
398     aFilledAttributeName = ARC_TYPE();
399     if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
400       aFilledAttributeName = TANGENT_POINT_ID();
401       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
402                                                         attribute(aFilledAttributeName));
403       FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
404       aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
405     }
406     else {
407       ObjectPtr anObject = aReentrantMessage->selectedObject();
408       AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
409       std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
410
411       if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
412         if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
413             anArcType == ARC_TYPE_BY_THREE_POINTS()) {
414           std::string aReferenceAttributeName;
415           if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
416             aFilledAttributeName = CENTER_POINT_ID();
417             aReferenceAttributeName = CENTER_POINT_REF_ID();
418           }
419           else {
420             aFilledAttributeName = START_POINT_2_ID();
421             aReferenceAttributeName = START_POINT_REF_ID();
422           }
423           // fill 2d point attribute
424           AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
425                                                             attribute(aFilledAttributeName));
426           aPointAttr->setValue(aClickedPoint);
427           // fill reference attribute
428           AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
429                                                           attribute(aReferenceAttributeName));
430           if (aRefAttr.get()) {
431             if (anAttribute.get()) {
432               if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
433                 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
434                 if (aCreatedFeature.get()) {
435                   std::string anID = anAttribute->id();
436                   std::string anArcID;
437                   if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
438                       anID == END_POINT_3_ID())
439                     anArcID = SketchPlugin_Arc::END_ID();
440                   else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
441                     anArcID = SketchPlugin_Arc::START_ID();
442                   else if (anID == CENTER_POINT_ID())
443                     anArcID = SketchPlugin_Arc::CENTER_ID();
444                   anAttribute = aCreatedFeature->attribute(anArcID);
445                 }
446               }
447               aRefAttr->setAttr(anAttribute);
448             }
449             else if (anObject.get()) {
450               // if attribute is NULL, only object is defined, it should be processed outside
451               // the feature because it might be an external feature, that will be
452               // removed/created again after restart operation
453               // #2468 - Crash when sketching circles successively on a repetition
454               aFilledAttributeName = ARC_TYPE();
455             }
456           }
457         }
458       }
459     }
460     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
461   }
462   return aFilledAttributeName;
463 }
464 // LCOV_EXCL_STOP
465
466 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
467 {
468   FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
469   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
470       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
471   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
472       anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
473   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
474       anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
475   anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
476                 ->setValue(boolean(REVERSED_ID())->value());
477   anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
478                 ->setValue(boolean(AUXILIARY_ID())->value());
479   anArcFeature->execute();
480
481   return anArcFeature;
482 }
483
484 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
485 {
486   AttributePoint2DPtr aCenterPointAttr =
487       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
488   if (!aCenterPointAttr->isInitialized())
489       return;
490
491   AttributePoint2DPtr aStartPointAttr =
492       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
493   if (!aStartPointAttr->isInitialized())
494     return;
495
496   myCenter = aCenterPointAttr->pnt();
497   myStart = aStartPointAttr->pnt();
498   myEnd = myStart;
499
500   AttributePoint2DPtr anEndPointAttr =
501       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
502   if (!anEndPointAttr->isInitialized())
503     return;
504
505   GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
506   // avoid degerated arc, when the center and the start points are equal
507   if (!aCircleForArc.implPtr<void*>())
508     return;
509
510   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
511   // check the end point is referred to another feature
512   GeomShapePtr aRefShape;
513   AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
514   if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
515     if (aEndPointRefAttr->isObject()) {
516       FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
517       if (aFeature)
518         aRefShape = aFeature->lastResult()->shape();
519     }
520   }
521   if (aRefShape) {
522     // Calculate end point as an intersection between circle and another shape
523     intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
524   } else {
525     // End point should be a projection on circle.
526     projectPointOnCircle(anEndPointAttr, aCircleForArc);
527   }
528   data()->blockSendAttributeUpdated(aWasBlocked, false);
529   myEnd = anEndPointAttr->pnt();
530
531   // update the REVERSED flag
532   recalculateReversedFlagByEnd(aCircleForArc);
533 }
534
535 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
536 {
537   double aParameterNew = 0.0;
538   if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
539     if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
540       boolean(REVERSED_ID())->setValue(true);
541     } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
542       boolean(REVERSED_ID())->setValue(false);
543     }
544   }
545   myParamBefore = aParameterNew;
546 }
547
548 void SketchPlugin_MacroArc::fillByThreePassedPoints()
549 {
550   AttributePoint2DPtr aStartPointAttr =
551       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
552   if (!aStartPointAttr->isInitialized())
553     return;
554
555   AttributePoint2DPtr anEndPointAttr =
556       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
557   if (!anEndPointAttr->isInitialized())
558     return;
559
560   myStart = aStartPointAttr->pnt();
561   myEnd = anEndPointAttr->pnt();
562
563   AttributePoint2DPtr aPassedPointAttr =
564       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
565   if (aPassedPointAttr->isInitialized()) {
566     std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
567     std::shared_ptr<GeomAPI_Shape> aTangentCurve;
568     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
569         refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
570
571     GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
572     aCircBuilder.addPassingPoint(myStart);
573     aCircBuilder.addPassingPoint(myEnd);
574     if (aTangentCurve) {
575       aCircBuilder.addTangentCurve(aTangentCurve);
576       aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
577     } else
578       aCircBuilder.addPassingPoint(aPassedPnt);
579
580     std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
581     if (!aCircle)
582       return;
583     myCenter = aCircle->center();
584
585     aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
586     recalculateReversedFlagByPassed(*aCircle);
587   } else
588     myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
589 }
590
591 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
592     const GeomAPI_Circ2d& theCurrentCircular)
593 {
594   AttributePoint2DPtr aPassedAttr =
595       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
596   std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
597
598   double aEndParam, aPassedParam;
599   theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
600   theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
601
602   if(aPassedParam > aEndParam)
603     boolean(REVERSED_ID())->setValue(true);
604   else
605     boolean(REVERSED_ID())->setValue(false);
606
607   myParamBefore = aEndParam;
608 }
609
610 void SketchPlugin_MacroArc::fillByEdge(bool theTransversal)
611 {
612   AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
613   if (!aTangentAttr->isInitialized())
614     return;
615
616   AttributePoint2DPtr aConnectionPointAttr =
617       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
618   if (!aConnectionPointAttr->isInitialized())
619     return;
620
621   AttributePoint2DPtr anEndPointAttr =
622       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
623   if (!anEndPointAttr->isInitialized())
624     return;
625
626   myStart = aConnectionPointAttr->pnt();
627   myEnd = anEndPointAttr->pnt();
628   if (myStart->isEqual(myEnd))
629     return;
630
631   // obtain a shape the tangent point belongs to
632   FeaturePtr aConnectedFeature = ModelAPI_Feature::feature(aConnectionPointAttr->owner());
633   std::shared_ptr<GeomAPI_Shape> aTangentShape = aConnectedFeature->lastResult()->shape();
634
635   GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
636   aCircBuilder.addPassingPoint(myStart);
637   aCircBuilder.addPassingPoint(myEnd);
638   if (theTransversal)
639     aCircBuilder.setTransversalLine(aTangentShape);
640   else
641     aCircBuilder.addTangentCurve(aTangentShape);
642
643   std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
644   if (!aCircle)
645     return;
646   myCenter = aCircle->center();
647
648   // rebuild circle to set start point equal to zero parameter
649   aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
650   recalculateReversedFlagByEnd(*aCircle);
651 }