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 theIntersection->setValue(anInterPnt);
120 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
121 : SketchPlugin_SketchEntity(),
126 void SketchPlugin_MacroArc::initAttributes()
128 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
130 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
131 data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
132 data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
134 data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
135 data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
136 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
138 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
139 data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
141 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
143 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
144 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
146 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
148 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
149 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
150 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
151 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
153 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
155 boolean(REVERSED_ID())->setValue(false);
156 string(EDIT_ARC_TYPE_ID())->setValue("");
158 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
159 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
160 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
161 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
162 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
165 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
167 std::string anArcType = string(ARC_TYPE())->value();
169 // If arc type switched reset according attributes.
170 if(theID == ARC_TYPE()) {
171 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
172 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
173 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
174 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
175 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
176 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
177 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
178 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
179 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
180 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
181 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
182 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
183 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
184 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
185 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
190 boolean(REVERSED_ID())->setValue(false);
192 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
193 fillByCenterAndTwoPassed();
194 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
195 fillByThreePassedPoints();
196 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
201 if(myCenter.get() && myStart.get()) {
202 aRadius = myCenter->distance(myStart);
204 if(myStart->isEqual(myEnd)) {
207 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
208 double aStartParam, anEndParam;
209 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
210 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
211 anAngle = (anEndParam - aStartParam) / PI * 180.0;
212 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
217 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
219 // center attribute is used in processEvent() to set reference to reentrant arc
220 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
221 ->setValue(myCenter);
223 real(RADIUS_ID())->setValue(aRadius);
224 real(ANGLE_ID())->setValue(anAngle);
225 data()->blockSendAttributeUpdated(aWasBlocked, false);
228 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
230 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
231 return GeomShapePtr();
234 SketchPlugin_Sketch* aSketch = sketch();
236 return GeomShapePtr();
239 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
240 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
241 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
242 std::shared_ptr<GeomDataAPI_Dir> aNDir =
243 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
244 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
246 GeomShapePtr anArcShape;
248 anArcShape = boolean(REVERSED_ID())->value() ?
249 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
250 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
252 double aRadius = aCenter->distance(aStart);
253 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
259 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
261 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
262 return AISObjectPtr();
265 SketchPlugin_Sketch* aSketch = sketch();
267 return AISObjectPtr();
270 GeomShapePtr anArcShape = getArcShape();
271 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
272 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
274 if(!anArcShape.get() || !aCenterPointShape.get()) {
275 return AISObjectPtr();
278 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
279 aShapes.push_back(anArcShape);
280 aShapes.push_back(aCenterPointShape);
282 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
283 AISObjectPtr anAIS = thePrevious;
285 anAIS.reset(new GeomAPI_AISObject());
287 anAIS->createShape(aCompound);
291 void SketchPlugin_MacroArc::execute()
293 FeaturePtr anArcFeature = createArcFeature();
299 // Create constraints.
300 std::string anArcType = string(ARC_TYPE())->value();
301 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
302 SketchPlugin_Tools::createConstraint(this,
303 CENTER_POINT_REF_ID(),
304 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
307 SketchPlugin_Tools::createConstraint(this,
308 START_POINT_REF_ID(),
309 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
312 SketchPlugin_Tools::createConstraint(this,
314 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
317 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
318 SketchPlugin_Tools::createConstraint(this,
319 START_POINT_REF_ID(),
320 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
323 SketchPlugin_Tools::createConstraint(this,
325 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
328 SketchPlugin_Tools::createConstraint(this,
329 PASSED_POINT_REF_ID(),
331 anArcFeature->lastResult(),
333 } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
334 // constraints for tangent arc
335 SketchPlugin_Tools::createConstraint(this,
337 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
340 FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
341 AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
342 AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
343 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
344 aRefAttrA->setObject(aTgFeature->lastResult());
345 AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
346 aRefAttrB->setObject(anArcFeature->lastResult());
347 // constraint for end point
348 SketchPlugin_Tools::createConstraint(this,
350 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
355 // message to init reentrant operation
356 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
357 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
358 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
360 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
361 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
362 aMessage->setCreatedFeature(anArcFeature);
363 Events_Loop::loop()->send(aMessage);
366 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
368 std::string aFilledAttributeName;
369 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
370 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
371 if (aReentrantMessage.get()) {
372 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
373 std::string anArcType = aReentrantMessage->typeOfCreation();
375 string(ARC_TYPE())->setValue(anArcType);
377 aFilledAttributeName = ARC_TYPE();
378 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
379 aFilledAttributeName = TANGENT_POINT_ID();
380 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
381 attribute(aFilledAttributeName));
382 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
383 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
386 ObjectPtr anObject = aReentrantMessage->selectedObject();
387 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
388 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
390 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
391 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
392 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
393 std::string aReferenceAttributeName;
394 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
395 aFilledAttributeName = CENTER_POINT_ID();
396 aReferenceAttributeName = CENTER_POINT_REF_ID();
399 aFilledAttributeName = START_POINT_2_ID();
400 aReferenceAttributeName = START_POINT_REF_ID();
402 // fill 2d point attribute
403 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
404 attribute(aFilledAttributeName));
405 aPointAttr->setValue(aClickedPoint);
406 // fill reference attribute
407 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
408 attribute(aReferenceAttributeName));
409 if (aRefAttr.get()) {
410 if (anAttribute.get()) {
411 if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
412 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
413 if (aCreatedFeature.get()) {
414 std::string anID = anAttribute->id();
416 if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
417 anID == END_POINT_3_ID())
418 anArcID = SketchPlugin_Arc::END_ID();
419 else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
420 anArcID = SketchPlugin_Arc::START_ID();
421 else if (anID == CENTER_POINT_ID())
422 anArcID = SketchPlugin_Arc::CENTER_ID();
423 anAttribute = aCreatedFeature->attribute(anArcID);
426 aRefAttr->setAttr(anAttribute);
428 else if (anObject.get()) {
429 // if presentation of previous reentrant macro arc is used, the object is invalid,
430 // we should use result of previous feature of the message(Arc)
431 if (!anObject->data()->isValid()) {
432 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
433 if (aCreatedFeature.get())
434 anObject = aCreatedFeature->lastResult();
436 aRefAttr->setObject(anObject);
442 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
444 return aFilledAttributeName;
447 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
449 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
450 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
451 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
452 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
453 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
454 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
455 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
456 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
457 ->setValue(boolean(REVERSED_ID())->value());
458 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
459 ->setValue(boolean(AUXILIARY_ID())->value());
460 anArcFeature->execute();
465 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
467 AttributePoint2DPtr aCenterPointAttr =
468 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
469 if (!aCenterPointAttr->isInitialized())
472 AttributePoint2DPtr aStartPointAttr =
473 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
474 if (!aStartPointAttr->isInitialized())
477 myCenter = aCenterPointAttr->pnt();
478 myStart = aStartPointAttr->pnt();
481 AttributePoint2DPtr anEndPointAttr =
482 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
483 if (!anEndPointAttr->isInitialized())
486 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
488 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
489 // check the end point is referred to another feature
490 GeomShapePtr aRefShape;
491 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
492 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
493 if (aEndPointRefAttr->isObject()) {
494 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
496 aRefShape = aFeature->lastResult()->shape();
500 // Calculate end point as an intersection between circle and another shape
501 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
503 // End point should be a projection on circle.
504 projectPointOnCircle(anEndPointAttr, aCircleForArc);
506 data()->blockSendAttributeUpdated(aWasBlocked, false);
507 myEnd = anEndPointAttr->pnt();
509 // update the REVERSED flag
510 recalculateReversedFlagByEnd(aCircleForArc);
513 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
515 double aParameterNew = 0.0;
516 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
517 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
518 boolean(REVERSED_ID())->setValue(true);
519 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
520 boolean(REVERSED_ID())->setValue(false);
523 myParamBefore = aParameterNew;
526 void SketchPlugin_MacroArc::fillByThreePassedPoints()
528 AttributePoint2DPtr aStartPointAttr =
529 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
530 if (!aStartPointAttr->isInitialized())
533 AttributePoint2DPtr anEndPointAttr =
534 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
535 if (!anEndPointAttr->isInitialized())
538 myStart = aStartPointAttr->pnt();
539 myEnd = anEndPointAttr->pnt();
541 AttributePoint2DPtr aPassedPointAttr =
542 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
543 if (aPassedPointAttr->isInitialized()) {
544 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
545 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
546 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
547 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
549 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
550 aCircBuilder.addPassingPoint(myStart);
551 aCircBuilder.addPassingPoint(myEnd);
553 aCircBuilder.addTangentCurve(aTangentCurve);
554 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
556 aCircBuilder.addPassingPoint(aPassedPnt);
558 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
561 myCenter = aCircle->center();
563 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
564 recalculateReversedFlagByPassed(*aCircle);
566 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
569 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
570 const GeomAPI_Circ2d& theCurrentCircular)
572 AttributePoint2DPtr aPassedAttr =
573 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
574 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
576 double aEndParam, aPassedParam;
577 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
578 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
580 if(aPassedParam > aEndParam)
581 boolean(REVERSED_ID())->setValue(true);
583 boolean(REVERSED_ID())->setValue(false);
585 myParamBefore = aEndParam;
588 void SketchPlugin_MacroArc::fillByTangentEdge()
590 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
591 if (!aTangentAttr->isInitialized())
594 AttributePoint2DPtr aTangentPointAttr =
595 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
596 if (!aTangentPointAttr->isInitialized())
599 AttributePoint2DPtr anEndPointAttr =
600 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
601 if (!anEndPointAttr->isInitialized())
604 myStart = aTangentPointAttr->pnt();
605 myEnd = anEndPointAttr->pnt();
606 if (myStart->isEqual(myEnd))
609 // obtain a shape the tangent point belongs to
610 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
611 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
613 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
614 aCircBuilder.addPassingPoint(myStart);
615 aCircBuilder.addPassingPoint(myEnd);
616 aCircBuilder.addTangentCurve(aTangentShape);
618 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
619 myCenter = aCircle->center();
621 // rebuild circle to set start point equal to zero parameter
622 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
623 recalculateReversedFlagByEnd(*aCircle);