1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_MacroArc.cpp
4 // Created: 26 Apr 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_MacroArc.h"
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_ConstraintTangent.h"
11 #include "SketchPlugin_Sketch.h"
12 #include "SketchPlugin_Tools.h"
13 #include "SketchPlugin_MacroArcReentrantMessage.h"
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
22 #include <GeomAPI_Circ.h>
23 #include <GeomAPI_Circ2d.h>
24 #include <GeomAPI_Curve.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_Lin2d.h>
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_Vertex.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomDataAPI_Dir.h>
37 #include <GeomAlgoAPI_Circ2dBuilder.h>
38 #include <GeomAlgoAPI_EdgeBuilder.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_PointBuilder.h>
45 const double tolerance = 1e-7;
46 const double paramTolerance = 1.e-4;
47 const double PI = 3.141592653589793238463;
49 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
51 std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
53 thePoint->setValue(aProjection);
56 static void intersectShapeAndCircle(const GeomShapePtr& theShape,
57 const GeomAPI_Circ2d& theCircle,
58 const SketchPlugin_Sketch* theSketch,
59 AttributePoint2DPtr& theIntersection)
61 if (!theShape->isEdge())
62 return projectPointOnCircle(theIntersection, theCircle);
64 // convert shape to unbounded
65 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
66 if (anEdge->isLine()) {
67 static const double HALF_SIZE = 1.e6;
68 std::shared_ptr<GeomAPI_XYZ> aLoc = anEdge->line()->location()->xyz();
69 std::shared_ptr<GeomAPI_XYZ> aDir = anEdge->line()->direction()->xyz();
71 std::shared_ptr<GeomAPI_Pnt> aStart(
72 new GeomAPI_Pnt(aLoc->added(aDir->multiplied(-HALF_SIZE))));
73 std::shared_ptr<GeomAPI_Pnt> aEnd(
74 new GeomAPI_Pnt(aLoc->added(aDir->multiplied(HALF_SIZE))));
75 anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, aEnd);
76 } else if (anEdge->isArc()) {
77 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
78 anEdge = GeomAlgoAPI_EdgeBuilder::lineCircle(
79 aCircle->center(), aCircle->normal(), aCircle->radius());
82 // convert 2D circle to 3D object
83 std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = theCircle.center();
84 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aCenter2d->x(), aCenter2d->y()));
85 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86 const_cast<SketchPlugin_Sketch*>(theSketch)->attribute(SketchPlugin_Sketch::NORM_ID()));
87 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
89 GeomShapePtr aCircleShape =
90 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, theCircle.radius());
92 GeomShapePtr anInter = anEdge->intersect(aCircleShape);
93 std::shared_ptr<GeomAPI_Pnt2d> anInterPnt;
95 return projectPointOnCircle(theIntersection, theCircle);
96 if (anInter->isVertex()) {
97 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
98 anInterPnt = theSketch->to2D(aVertex->point());
99 } else if (anInter->isCompound()) {
100 double aMinDist = 1.e300;
102 GeomAPI_ShapeIterator anIt(anInter);
103 for (; anIt.more(); anIt.next()) {
104 GeomShapePtr aCurrent = anIt.current();
105 if (!aCurrent->isVertex())
107 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
108 std::shared_ptr<GeomAPI_Pnt2d> aPnt = theSketch->to2D(aVertex->point());
109 double aDist = aPnt->distance(theIntersection->pnt());
110 if (aDist < aMinDist) {
116 if(anInterPnt.get()) {
117 theIntersection->setValue(anInterPnt);
122 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
123 : SketchPlugin_SketchEntity(),
128 void SketchPlugin_MacroArc::initAttributes()
130 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
132 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
133 data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
134 data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
136 data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
137 data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
138 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
140 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
141 data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
143 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
145 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
146 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
148 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
150 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
151 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
152 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
153 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
155 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
157 boolean(REVERSED_ID())->setValue(false);
158 string(EDIT_ARC_TYPE_ID())->setValue("");
160 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
161 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
162 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
163 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
164 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
167 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
169 std::string anArcType = string(ARC_TYPE())->value();
171 // If arc type switched reset according attributes.
172 if(theID == ARC_TYPE()) {
173 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
174 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
175 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
176 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
177 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
178 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
179 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
180 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
181 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
182 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
183 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
184 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
185 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
186 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
187 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
192 boolean(REVERSED_ID())->setValue(false);
194 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
195 fillByCenterAndTwoPassed();
196 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
197 fillByThreePassedPoints();
198 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
203 if(myCenter.get() && myStart.get()) {
204 aRadius = myCenter->distance(myStart);
206 if(myStart->isEqual(myEnd)) {
209 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
210 double aStartParam, anEndParam;
211 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
212 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
213 anAngle = (anEndParam - aStartParam) / PI * 180.0;
214 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
219 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
221 // center attribute is used in processEvent() to set reference to reentrant arc
222 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
223 ->setValue(myCenter);
225 real(RADIUS_ID())->setValue(aRadius);
226 real(ANGLE_ID())->setValue(anAngle);
227 data()->blockSendAttributeUpdated(aWasBlocked, false);
230 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
232 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
233 return GeomShapePtr();
236 SketchPlugin_Sketch* aSketch = sketch();
238 return GeomShapePtr();
241 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
242 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
243 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
244 std::shared_ptr<GeomDataAPI_Dir> aNDir =
245 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
246 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
248 GeomShapePtr anArcShape;
250 anArcShape = boolean(REVERSED_ID())->value() ?
251 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
252 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
254 double aRadius = aCenter->distance(aStart);
255 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
261 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
263 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
264 return AISObjectPtr();
267 SketchPlugin_Sketch* aSketch = sketch();
269 return AISObjectPtr();
272 GeomShapePtr anArcShape = getArcShape();
273 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
274 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
276 if(!anArcShape.get() || !aCenterPointShape.get()) {
277 return AISObjectPtr();
280 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
281 aShapes.push_back(anArcShape);
282 aShapes.push_back(aCenterPointShape);
284 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
285 AISObjectPtr anAIS = thePrevious;
287 anAIS.reset(new GeomAPI_AISObject());
289 anAIS->createShape(aCompound);
293 void SketchPlugin_MacroArc::execute()
295 FeaturePtr anArcFeature = createArcFeature();
301 // Create constraints.
302 std::string anArcType = string(ARC_TYPE())->value();
303 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
304 SketchPlugin_Tools::createConstraint(this,
305 CENTER_POINT_REF_ID(),
306 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
309 SketchPlugin_Tools::createConstraint(this,
310 START_POINT_REF_ID(),
311 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
314 SketchPlugin_Tools::createConstraint(this,
316 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
319 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
320 SketchPlugin_Tools::createConstraint(this,
321 START_POINT_REF_ID(),
322 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
325 SketchPlugin_Tools::createConstraint(this,
327 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
330 SketchPlugin_Tools::createConstraint(this,
331 PASSED_POINT_REF_ID(),
333 anArcFeature->lastResult(),
335 } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
336 // constraints for tangent arc
337 SketchPlugin_Tools::createConstraint(this,
339 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
342 FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
343 AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
344 AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
345 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
346 aRefAttrA->setObject(aTgFeature->lastResult());
347 AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
348 aRefAttrB->setObject(anArcFeature->lastResult());
349 // constraint for end point
350 SketchPlugin_Tools::createConstraint(this,
352 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
357 // message to init reentrant operation
358 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
359 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
360 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
362 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
363 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
364 aMessage->setCreatedFeature(anArcFeature);
365 Events_Loop::loop()->send(aMessage);
368 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
370 std::string aFilledAttributeName;
371 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
372 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
373 if (aReentrantMessage.get()) {
374 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
375 std::string anArcType = aReentrantMessage->typeOfCreation();
377 string(ARC_TYPE())->setValue(anArcType);
379 aFilledAttributeName = ARC_TYPE();
380 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
381 aFilledAttributeName = TANGENT_POINT_ID();
382 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
383 attribute(aFilledAttributeName));
384 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
385 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
388 ObjectPtr anObject = aReentrantMessage->selectedObject();
389 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
390 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
392 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
393 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
394 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
395 std::string aReferenceAttributeName;
396 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
397 aFilledAttributeName = CENTER_POINT_ID();
398 aReferenceAttributeName = CENTER_POINT_REF_ID();
401 aFilledAttributeName = START_POINT_2_ID();
402 aReferenceAttributeName = START_POINT_REF_ID();
404 // fill 2d point attribute
405 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
406 attribute(aFilledAttributeName));
407 aPointAttr->setValue(aClickedPoint);
408 // fill reference attribute
409 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
410 attribute(aReferenceAttributeName));
411 if (aRefAttr.get()) {
412 if (anAttribute.get()) {
413 if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
414 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
415 if (aCreatedFeature.get()) {
416 std::string anID = anAttribute->id();
418 if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
419 anID == END_POINT_3_ID())
420 anArcID = SketchPlugin_Arc::END_ID();
421 else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
422 anArcID = SketchPlugin_Arc::START_ID();
423 else if (anID == CENTER_POINT_ID())
424 anArcID = SketchPlugin_Arc::CENTER_ID();
425 anAttribute = aCreatedFeature->attribute(anArcID);
428 aRefAttr->setAttr(anAttribute);
430 else if (anObject.get()) {
431 // if presentation of previous reentrant macro arc is used, the object is invalid,
432 // we should use result of previous feature of the message(Arc)
433 if (!anObject->data()->isValid()) {
434 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
435 if (aCreatedFeature.get())
436 anObject = aCreatedFeature->lastResult();
438 aRefAttr->setObject(anObject);
444 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
446 return aFilledAttributeName;
449 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
451 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
452 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
453 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
454 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
455 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
456 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
457 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
458 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
459 ->setValue(boolean(REVERSED_ID())->value());
460 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
461 ->setValue(boolean(AUXILIARY_ID())->value());
462 anArcFeature->execute();
467 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
469 AttributePoint2DPtr aCenterPointAttr =
470 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
471 if (!aCenterPointAttr->isInitialized())
474 AttributePoint2DPtr aStartPointAttr =
475 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
476 if (!aStartPointAttr->isInitialized())
479 myCenter = aCenterPointAttr->pnt();
480 myStart = aStartPointAttr->pnt();
483 AttributePoint2DPtr anEndPointAttr =
484 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
485 if (!anEndPointAttr->isInitialized())
488 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
490 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
491 // check the end point is referred to another feature
492 GeomShapePtr aRefShape;
493 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
494 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
495 if (aEndPointRefAttr->isObject()) {
496 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
498 aRefShape = aFeature->lastResult()->shape();
502 // Calculate end point as an intersection between circle and another shape
503 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
505 // End point should be a projection on circle.
506 projectPointOnCircle(anEndPointAttr, aCircleForArc);
508 data()->blockSendAttributeUpdated(aWasBlocked, false);
509 myEnd = anEndPointAttr->pnt();
511 // update the REVERSED flag
512 recalculateReversedFlagByEnd(aCircleForArc);
515 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
517 double aParameterNew = 0.0;
518 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
519 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
520 boolean(REVERSED_ID())->setValue(true);
521 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
522 boolean(REVERSED_ID())->setValue(false);
525 myParamBefore = aParameterNew;
528 void SketchPlugin_MacroArc::fillByThreePassedPoints()
530 AttributePoint2DPtr aStartPointAttr =
531 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
532 if (!aStartPointAttr->isInitialized())
535 AttributePoint2DPtr anEndPointAttr =
536 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
537 if (!anEndPointAttr->isInitialized())
540 myStart = aStartPointAttr->pnt();
541 myEnd = anEndPointAttr->pnt();
543 AttributePoint2DPtr aPassedPointAttr =
544 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
545 if (aPassedPointAttr->isInitialized()) {
546 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
547 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
548 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
549 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
551 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
552 aCircBuilder.addPassingPoint(myStart);
553 aCircBuilder.addPassingPoint(myEnd);
555 aCircBuilder.addTangentCurve(aTangentCurve);
556 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
558 aCircBuilder.addPassingPoint(aPassedPnt);
560 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
563 myCenter = aCircle->center();
565 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
566 recalculateReversedFlagByPassed(*aCircle);
568 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
571 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
572 const GeomAPI_Circ2d& theCurrentCircular)
574 AttributePoint2DPtr aPassedAttr =
575 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
576 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
578 double aEndParam, aPassedParam;
579 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
580 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
582 if(aPassedParam > aEndParam)
583 boolean(REVERSED_ID())->setValue(true);
585 boolean(REVERSED_ID())->setValue(false);
587 myParamBefore = aEndParam;
590 void SketchPlugin_MacroArc::fillByTangentEdge()
592 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
593 if (!aTangentAttr->isInitialized())
596 AttributePoint2DPtr aTangentPointAttr =
597 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
598 if (!aTangentPointAttr->isInitialized())
601 AttributePoint2DPtr anEndPointAttr =
602 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
603 if (!anEndPointAttr->isInitialized())
606 myStart = aTangentPointAttr->pnt();
607 myEnd = anEndPointAttr->pnt();
608 if (myStart->isEqual(myEnd))
611 // obtain a shape the tangent point belongs to
612 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
613 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
615 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
616 aCircBuilder.addPassingPoint(myStart);
617 aCircBuilder.addPassingPoint(myEnd);
618 aCircBuilder.addTangentCurve(aTangentShape);
620 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
621 myCenter = aCircle->center();
623 // rebuild circle to set start point equal to zero parameter
624 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
625 recalculateReversedFlagByEnd(*aCircle);