1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_MacroArc.h"
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"
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>
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>
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Dir.h>
51 #include <GeomAlgoAPI_Circ2dBuilder.h>
52 #include <GeomAlgoAPI_EdgeBuilder.h>
53 #include <GeomAlgoAPI_CompoundBuilder.h>
54 #include <GeomAlgoAPI_PointBuilder.h>
59 const double tolerance = 1e-7;
60 const double paramTolerance = 1.e-4;
61 const double PI = 3.141592653589793238463;
63 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
65 std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
67 thePoint->setValue(aProjection);
70 static void intersectShapeAndCircle(const GeomShapePtr& theShape,
71 const GeomAPI_Circ2d& theCircle,
72 const SketchPlugin_Sketch* theSketch,
73 AttributePoint2DPtr& theIntersection)
75 if (!theShape->isEdge())
76 return projectPointOnCircle(theIntersection, theCircle);
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();
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());
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()));
103 GeomShapePtr aCircleShape =
104 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, theCircle.radius());
106 GeomShapePtr anInter = anEdge->intersect(aCircleShape);
107 std::shared_ptr<GeomAPI_Pnt2d> anInterPnt;
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;
116 GeomAPI_ShapeIterator anIt(anInter);
117 for (; anIt.more(); anIt.next()) {
118 GeomShapePtr aCurrent = anIt.current();
119 if (!aCurrent->isVertex())
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) {
130 if(anInterPnt.get()) {
131 theIntersection->setValue(anInterPnt);
136 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
137 : SketchPlugin_SketchEntity(),
142 void SketchPlugin_MacroArc::initAttributes()
144 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
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());
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());
154 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
155 data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
157 data()->addAttribute(TRANSVERSAL_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
158 data()->addAttribute(END_POINT_4_ID(), GeomDataAPI_Point2D::typeId());
160 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
162 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
163 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
165 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
167 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
168 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
169 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
170 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
172 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
174 boolean(REVERSED_ID())->setValue(false);
175 string(EDIT_ARC_TYPE_ID())->setValue("");
177 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
178 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
179 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
180 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
181 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
184 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
186 std::string anArcType = string(ARC_TYPE())->value();
188 // If arc type switched reset according attributes.
189 if(theID == ARC_TYPE()) {
190 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
191 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
192 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
193 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
194 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
195 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
196 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
197 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
198 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
199 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
200 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
201 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
202 SketchPlugin_Tools::resetAttribute(this, TRANSVERSAL_POINT_ID());
203 SketchPlugin_Tools::resetAttribute(this, END_POINT_4_ID());
204 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
205 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
206 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
211 boolean(REVERSED_ID())->setValue(false);
213 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
214 fillByCenterAndTwoPassed();
215 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
216 fillByThreePassedPoints();
217 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
219 else if (anArcType == ARC_TYPE_BY_TRANSVERSAL_LINE())
224 if(myCenter.get() && myStart.get()) {
225 aRadius = myCenter->distance(myStart);
227 if(myStart->isEqual(myEnd)) {
230 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
231 double aStartParam, anEndParam;
232 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
233 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
234 anAngle = (anEndParam - aStartParam) / PI * 180.0;
235 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
240 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
242 // center attribute is used in processEvent() to set reference to reentrant arc
243 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
244 ->setValue(myCenter);
246 real(RADIUS_ID())->setValue(aRadius);
247 real(ANGLE_ID())->setValue(anAngle);
248 data()->blockSendAttributeUpdated(aWasBlocked, false);
251 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
253 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
254 return GeomShapePtr();
257 SketchPlugin_Sketch* aSketch = sketch();
259 return GeomShapePtr();
262 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
263 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
264 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
265 std::shared_ptr<GeomDataAPI_Dir> aNDir =
266 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
267 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
269 GeomShapePtr anArcShape;
271 anArcShape = boolean(REVERSED_ID())->value() ?
272 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
273 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
275 double aRadius = aCenter->distance(aStart);
276 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
282 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
284 GeomShapePtr anArcShape = getArcShape();
285 if(!anArcShape.get())
286 return AISObjectPtr();
288 SketchPlugin_Sketch* aSketch = sketch();
289 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
290 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
292 if(!aCenterPointShape.get())
293 return AISObjectPtr();
295 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
296 aShapes.push_back(anArcShape);
297 aShapes.push_back(aCenterPointShape);
299 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
300 AISObjectPtr anAIS = thePrevious;
302 anAIS.reset(new GeomAPI_AISObject());
304 anAIS->createShape(aCompound);
305 SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
309 void SketchPlugin_MacroArc::execute()
311 FeaturePtr anArcFeature = createArcFeature();
317 // Create constraints.
318 std::string anArcType = string(ARC_TYPE())->value();
319 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
320 SketchPlugin_Tools::createCoincidenceOrTangency(this,
321 CENTER_POINT_REF_ID(),
322 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
325 SketchPlugin_Tools::createCoincidenceOrTangency(this,
326 START_POINT_REF_ID(),
327 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
330 SketchPlugin_Tools::createCoincidenceOrTangency(this,
332 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
335 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
336 SketchPlugin_Tools::createCoincidenceOrTangency(this,
337 START_POINT_REF_ID(),
338 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
341 SketchPlugin_Tools::createCoincidenceOrTangency(this,
343 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
346 SketchPlugin_Tools::createCoincidenceOrTangency(this,
347 PASSED_POINT_REF_ID(),
349 anArcFeature->lastResult(),
352 // coincident with connection point
353 const std::string& aPointAttr = anArcType == ARC_TYPE_BY_TANGENT_EDGE() ?
354 TANGENT_POINT_ID() : TRANSVERSAL_POINT_ID();
355 SketchPlugin_Tools::createCoincidenceOrTangency(this,
357 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
360 // tangent or perpendicular constraint
361 FeaturePtr aStartPointConstraint;
362 if (anArcType == ARC_TYPE_BY_TANGENT_EDGE())
363 aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
365 aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
366 // setting attributes of the start point constraint
367 AttributeRefAttrPtr aRefAttrA =
368 aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
369 AttributeRefAttrPtr aTgPntRefAttr = refattr(aPointAttr);
370 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
371 aRefAttrA->setObject(aTgFeature->lastResult());
372 AttributeRefAttrPtr aRefAttrB =
373 aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
374 aRefAttrB->setObject(anArcFeature->lastResult());
375 // constraint for end point
376 SketchPlugin_Tools::createCoincidenceOrTangency(this,
378 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
383 // message to init reentrant operation
384 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
385 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
386 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
388 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
389 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
390 aMessage->setCreatedFeature(anArcFeature);
391 Events_Loop::loop()->send(aMessage);
395 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
397 std::string aFilledAttributeName;
398 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
399 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
400 if (aReentrantMessage.get()) {
401 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
402 std::string anArcType = aReentrantMessage->typeOfCreation();
404 string(ARC_TYPE())->setValue(anArcType);
406 aFilledAttributeName = ARC_TYPE();
407 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
408 aFilledAttributeName = TANGENT_POINT_ID();
409 AttributeRefAttrPtr aRefAttr = refattr(aFilledAttributeName);
410 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
412 else if (anArcType == ARC_TYPE_BY_TRANSVERSAL_LINE()) {
413 AttributeRefAttrPtr aRefAttr = refattr(TRANSVERSAL_POINT_ID());
414 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
416 aRefAttr->setAttr(anAttribute);
417 aFilledAttributeName = TRANSVERSAL_POINT_ID();
421 ObjectPtr anObject = aReentrantMessage->selectedObject();
422 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
423 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
425 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
426 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
427 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
428 std::string aReferenceAttributeName;
429 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
430 aFilledAttributeName = CENTER_POINT_ID();
431 aReferenceAttributeName = CENTER_POINT_REF_ID();
434 aFilledAttributeName = START_POINT_2_ID();
435 aReferenceAttributeName = START_POINT_REF_ID();
437 // fill 2d point attribute
438 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
439 attribute(aFilledAttributeName));
440 aPointAttr->setValue(aClickedPoint);
441 // fill reference attribute
442 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
443 attribute(aReferenceAttributeName));
444 if (aRefAttr.get()) {
445 if (anAttribute.get()) {
446 if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
447 if (aCreatedFeature.get()) {
448 std::string anID = anAttribute->id();
450 if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
451 anID == END_POINT_3_ID())
452 anArcID = SketchPlugin_Arc::END_ID();
453 else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
454 anArcID = SketchPlugin_Arc::START_ID();
455 else if (anID == CENTER_POINT_ID())
456 anArcID = SketchPlugin_Arc::CENTER_ID();
457 anAttribute = aCreatedFeature->attribute(anArcID);
460 aRefAttr->setAttr(anAttribute);
462 else if (anObject.get()) {
463 // if attribute is NULL, only object is defined, it should be processed outside
464 // the feature because it might be an external feature, that will be
465 // removed/created again after restart operation
466 // #2468 - Crash when sketching circles successively on a repetition
467 aFilledAttributeName = ARC_TYPE();
473 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
475 return aFilledAttributeName;
479 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
481 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
482 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
483 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
484 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
485 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
486 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
488 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
489 ->setValue(boolean(REVERSED_ID())->value());
490 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
491 ->setValue(boolean(AUXILIARY_ID())->value());
492 anArcFeature->execute();
497 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
499 AttributePoint2DPtr aCenterPointAttr =
500 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
501 if (!aCenterPointAttr->isInitialized())
504 AttributePoint2DPtr aStartPointAttr =
505 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
506 if (!aStartPointAttr->isInitialized())
509 myCenter = aCenterPointAttr->pnt();
510 myStart = aStartPointAttr->pnt();
513 AttributePoint2DPtr anEndPointAttr =
514 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
515 if (!anEndPointAttr->isInitialized())
518 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
519 // avoid degerated arc, when the center and the start points are equal
520 if (!aCircleForArc.implPtr<void*>())
523 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
524 // check the end point is referred to another feature
525 GeomShapePtr aRefShape;
526 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
527 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
528 if (aEndPointRefAttr->isObject()) {
529 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
531 aRefShape = aFeature->lastResult()->shape();
535 // Calculate end point as an intersection between circle and another shape
536 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
538 // End point should be a projection on circle.
539 projectPointOnCircle(anEndPointAttr, aCircleForArc);
541 data()->blockSendAttributeUpdated(aWasBlocked, false);
542 myEnd = anEndPointAttr->pnt();
544 // update the REVERSED flag
545 recalculateReversedFlagByEnd(aCircleForArc);
548 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
550 double aParameterNew = 0.0;
551 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
552 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
553 boolean(REVERSED_ID())->setValue(true);
554 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
555 boolean(REVERSED_ID())->setValue(false);
558 myParamBefore = aParameterNew;
561 void SketchPlugin_MacroArc::fillByThreePassedPoints()
563 AttributePoint2DPtr aStartPointAttr =
564 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
565 if (!aStartPointAttr->isInitialized())
568 AttributePoint2DPtr anEndPointAttr =
569 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
570 if (!anEndPointAttr->isInitialized())
573 myStart = aStartPointAttr->pnt();
574 myEnd = anEndPointAttr->pnt();
576 AttributePoint2DPtr aPassedPointAttr =
577 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
578 if (aPassedPointAttr->isInitialized()) {
579 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
580 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
581 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
582 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
584 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
585 aCircBuilder.addPassingPoint(myStart);
586 aCircBuilder.addPassingPoint(myEnd);
588 aCircBuilder.addTangentCurve(aTangentCurve);
589 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
591 aCircBuilder.addPassingPoint(aPassedPnt);
593 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
596 myCenter = aCircle->center();
598 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
599 recalculateReversedFlagByPassed(*aCircle);
601 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
604 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
605 const GeomAPI_Circ2d& theCurrentCircular)
607 AttributePoint2DPtr aPassedAttr =
608 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
609 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
611 double aEndParam, aPassedParam;
612 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
613 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
615 if(aPassedParam > aEndParam)
616 boolean(REVERSED_ID())->setValue(true);
618 boolean(REVERSED_ID())->setValue(false);
620 myParamBefore = aEndParam;
623 void SketchPlugin_MacroArc::fillByEdge(bool theTransversal)
625 const std::string& aStartPoint = theTransversal ? TRANSVERSAL_POINT_ID() : TANGENT_POINT_ID();
626 const std::string& aEndPoint = theTransversal ? END_POINT_4_ID() : END_POINT_3_ID();
628 AttributeRefAttrPtr aTangentAttr = refattr(aStartPoint);
629 if (!aTangentAttr->isInitialized())
632 AttributePoint2DPtr aConnectionPointAttr =
633 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
634 if (!aConnectionPointAttr->isInitialized())
637 AttributePoint2DPtr anEndPointAttr =
638 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aEndPoint));
639 if (!anEndPointAttr->isInitialized())
642 myStart = aConnectionPointAttr->pnt();
643 myEnd = anEndPointAttr->pnt();
644 if (myStart->isEqual(myEnd))
647 // obtain a shape the tangent point belongs to
648 FeaturePtr aConnectedFeature = ModelAPI_Feature::feature(aConnectionPointAttr->owner());
649 std::shared_ptr<GeomAPI_Shape> aTangentShape = aConnectedFeature->lastResult()->shape();
651 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
652 aCircBuilder.addPassingPoint(myStart);
653 aCircBuilder.addPassingPoint(myEnd);
655 aCircBuilder.setTransversalLine(aTangentShape);
657 aCircBuilder.addTangentCurve(aTangentShape);
659 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
662 myCenter = aCircle->center();
664 // rebuild circle to set start point equal to zero parameter
665 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
666 recalculateReversedFlagByEnd(*aCircle);