1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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_ConstraintTangent.h"
24 #include "SketchPlugin_Sketch.h"
25 #include "SketchPlugin_Tools.h"
26 #include "SketchPlugin_MacroArcReentrantMessage.h"
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
35 #include <GeomAPI_Circ.h>
36 #include <GeomAPI_Circ2d.h>
37 #include <GeomAPI_Curve.h>
38 #include <GeomAPI_Dir2d.h>
39 #include <GeomAPI_Edge.h>
40 #include <GeomAPI_Lin.h>
41 #include <GeomAPI_Lin2d.h>
42 #include <GeomAPI_Pnt2d.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomAPI_XY.h>
45 #include <GeomAPI_ShapeIterator.h>
47 #include <GeomDataAPI_Point2D.h>
48 #include <GeomDataAPI_Dir.h>
50 #include <GeomAlgoAPI_Circ2dBuilder.h>
51 #include <GeomAlgoAPI_EdgeBuilder.h>
52 #include <GeomAlgoAPI_CompoundBuilder.h>
53 #include <GeomAlgoAPI_PointBuilder.h>
58 const double tolerance = 1e-7;
59 const double paramTolerance = 1.e-4;
60 const double PI = 3.141592653589793238463;
62 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
64 std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
66 thePoint->setValue(aProjection);
69 static void intersectShapeAndCircle(const GeomShapePtr& theShape,
70 const GeomAPI_Circ2d& theCircle,
71 const SketchPlugin_Sketch* theSketch,
72 AttributePoint2DPtr& theIntersection)
74 if (!theShape->isEdge())
75 return projectPointOnCircle(theIntersection, theCircle);
77 // convert shape to unbounded
78 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
79 if (anEdge->isLine()) {
80 static const double HALF_SIZE = 1.e6;
81 std::shared_ptr<GeomAPI_XYZ> aLoc = anEdge->line()->location()->xyz();
82 std::shared_ptr<GeomAPI_XYZ> aDir = anEdge->line()->direction()->xyz();
84 std::shared_ptr<GeomAPI_Pnt> aStart(
85 new GeomAPI_Pnt(aLoc->added(aDir->multiplied(-HALF_SIZE))));
86 std::shared_ptr<GeomAPI_Pnt> aEnd(
87 new GeomAPI_Pnt(aLoc->added(aDir->multiplied(HALF_SIZE))));
88 anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, aEnd);
89 } else if (anEdge->isArc()) {
90 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
91 anEdge = GeomAlgoAPI_EdgeBuilder::lineCircle(
92 aCircle->center(), aCircle->normal(), aCircle->radius());
95 // convert 2D circle to 3D object
96 std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = theCircle.center();
97 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aCenter2d->x(), aCenter2d->y()));
98 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
99 const_cast<SketchPlugin_Sketch*>(theSketch)->attribute(SketchPlugin_Sketch::NORM_ID()));
100 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
102 GeomShapePtr aCircleShape =
103 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, theCircle.radius());
105 GeomShapePtr anInter = anEdge->intersect(aCircleShape);
106 std::shared_ptr<GeomAPI_Pnt2d> anInterPnt;
108 return projectPointOnCircle(theIntersection, theCircle);
109 if (anInter->isVertex()) {
110 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
111 anInterPnt = theSketch->to2D(aVertex->point());
112 } else if (anInter->isCompound()) {
113 double aMinDist = 1.e300;
115 GeomAPI_ShapeIterator anIt(anInter);
116 for (; anIt.more(); anIt.next()) {
117 GeomShapePtr aCurrent = anIt.current();
118 if (!aCurrent->isVertex())
120 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
121 std::shared_ptr<GeomAPI_Pnt2d> aPnt = theSketch->to2D(aVertex->point());
122 double aDist = aPnt->distance(theIntersection->pnt());
123 if (aDist < aMinDist) {
129 if(anInterPnt.get()) {
130 theIntersection->setValue(anInterPnt);
135 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
136 : SketchPlugin_SketchEntity(),
141 void SketchPlugin_MacroArc::initAttributes()
143 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
145 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
146 data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
147 data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
149 data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
150 data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
151 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
153 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
154 data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
156 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
158 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
159 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
161 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
163 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
164 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
165 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
166 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
168 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
170 boolean(REVERSED_ID())->setValue(false);
171 string(EDIT_ARC_TYPE_ID())->setValue("");
173 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
174 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
175 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
176 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
177 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
180 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
182 std::string anArcType = string(ARC_TYPE())->value();
184 // If arc type switched reset according attributes.
185 if(theID == ARC_TYPE()) {
186 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
187 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
188 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
189 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
190 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
191 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
192 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
193 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
194 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
195 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
196 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
197 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
198 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
199 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
200 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
205 boolean(REVERSED_ID())->setValue(false);
207 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
208 fillByCenterAndTwoPassed();
209 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
210 fillByThreePassedPoints();
211 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
216 if(myCenter.get() && myStart.get()) {
217 aRadius = myCenter->distance(myStart);
219 if(myStart->isEqual(myEnd)) {
222 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
223 double aStartParam, anEndParam;
224 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
225 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
226 anAngle = (anEndParam - aStartParam) / PI * 180.0;
227 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
232 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
234 // center attribute is used in processEvent() to set reference to reentrant arc
235 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
236 ->setValue(myCenter);
238 real(RADIUS_ID())->setValue(aRadius);
239 real(ANGLE_ID())->setValue(anAngle);
240 data()->blockSendAttributeUpdated(aWasBlocked, false);
243 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
245 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
246 return GeomShapePtr();
249 SketchPlugin_Sketch* aSketch = sketch();
251 return GeomShapePtr();
254 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
255 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
256 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
257 std::shared_ptr<GeomDataAPI_Dir> aNDir =
258 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
259 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
261 GeomShapePtr anArcShape;
263 anArcShape = boolean(REVERSED_ID())->value() ?
264 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
265 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
267 double aRadius = aCenter->distance(aStart);
268 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
274 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
276 GeomShapePtr anArcShape = getArcShape();
277 if(!anArcShape.get())
278 return AISObjectPtr();
280 SketchPlugin_Sketch* aSketch = sketch();
281 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
282 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
284 if(!aCenterPointShape.get())
285 return AISObjectPtr();
287 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
288 aShapes.push_back(anArcShape);
289 aShapes.push_back(aCenterPointShape);
291 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
292 AISObjectPtr anAIS = thePrevious;
294 anAIS.reset(new GeomAPI_AISObject());
296 anAIS->createShape(aCompound);
300 void SketchPlugin_MacroArc::execute()
302 FeaturePtr anArcFeature = createArcFeature();
308 // Create constraints.
309 std::string anArcType = string(ARC_TYPE())->value();
310 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
311 SketchPlugin_Tools::createCoincidenceOrTangency(this,
312 CENTER_POINT_REF_ID(),
313 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
316 SketchPlugin_Tools::createCoincidenceOrTangency(this,
317 START_POINT_REF_ID(),
318 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
321 SketchPlugin_Tools::createCoincidenceOrTangency(this,
323 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
326 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
327 SketchPlugin_Tools::createCoincidenceOrTangency(this,
328 START_POINT_REF_ID(),
329 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
332 SketchPlugin_Tools::createCoincidenceOrTangency(this,
334 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
337 SketchPlugin_Tools::createCoincidenceOrTangency(this,
338 PASSED_POINT_REF_ID(),
340 anArcFeature->lastResult(),
342 } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
343 // constraints for tangent arc
344 SketchPlugin_Tools::createCoincidenceOrTangency(this,
346 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
349 FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
350 AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
351 AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
352 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
353 aRefAttrA->setObject(aTgFeature->lastResult());
354 AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
355 aRefAttrB->setObject(anArcFeature->lastResult());
356 // constraint for end point
357 SketchPlugin_Tools::createCoincidenceOrTangency(this,
359 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
364 // message to init reentrant operation
365 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
366 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
367 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
369 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
370 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
371 aMessage->setCreatedFeature(anArcFeature);
372 Events_Loop::loop()->send(aMessage);
376 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
378 std::string aFilledAttributeName;
379 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
380 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
381 if (aReentrantMessage.get()) {
382 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
383 std::string anArcType = aReentrantMessage->typeOfCreation();
385 string(ARC_TYPE())->setValue(anArcType);
387 aFilledAttributeName = ARC_TYPE();
388 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
389 aFilledAttributeName = TANGENT_POINT_ID();
390 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
391 attribute(aFilledAttributeName));
392 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
393 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
396 ObjectPtr anObject = aReentrantMessage->selectedObject();
397 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
398 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
400 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
401 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
402 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
403 std::string aReferenceAttributeName;
404 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
405 aFilledAttributeName = CENTER_POINT_ID();
406 aReferenceAttributeName = CENTER_POINT_REF_ID();
409 aFilledAttributeName = START_POINT_2_ID();
410 aReferenceAttributeName = START_POINT_REF_ID();
412 // fill 2d point attribute
413 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
414 attribute(aFilledAttributeName));
415 aPointAttr->setValue(aClickedPoint);
416 // fill reference attribute
417 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
418 attribute(aReferenceAttributeName));
419 if (aRefAttr.get()) {
420 if (anAttribute.get()) {
421 if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
422 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
423 if (aCreatedFeature.get()) {
424 std::string anID = anAttribute->id();
426 if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
427 anID == END_POINT_3_ID())
428 anArcID = SketchPlugin_Arc::END_ID();
429 else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
430 anArcID = SketchPlugin_Arc::START_ID();
431 else if (anID == CENTER_POINT_ID())
432 anArcID = SketchPlugin_Arc::CENTER_ID();
433 anAttribute = aCreatedFeature->attribute(anArcID);
436 aRefAttr->setAttr(anAttribute);
438 else if (anObject.get()) {
439 // if attribute is NULL, only object is defined, it should be processed outside
440 // the feature because it might be an external feature, that will be
441 // removed/created again after restart operation
442 // #2468 - Crash when sketching circles successively on a repetition
443 aFilledAttributeName = ARC_TYPE();
449 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
451 return aFilledAttributeName;
455 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
457 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
458 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
459 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
460 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
461 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
462 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
463 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
464 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
465 ->setValue(boolean(REVERSED_ID())->value());
466 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
467 ->setValue(boolean(AUXILIARY_ID())->value());
468 anArcFeature->execute();
473 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
475 AttributePoint2DPtr aCenterPointAttr =
476 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
477 if (!aCenterPointAttr->isInitialized())
480 AttributePoint2DPtr aStartPointAttr =
481 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
482 if (!aStartPointAttr->isInitialized())
485 myCenter = aCenterPointAttr->pnt();
486 myStart = aStartPointAttr->pnt();
489 AttributePoint2DPtr anEndPointAttr =
490 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
491 if (!anEndPointAttr->isInitialized())
494 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
495 // avoid degerated arc, when the center and the start points are equal
496 if (!aCircleForArc.implPtr<void*>())
499 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
500 // check the end point is referred to another feature
501 GeomShapePtr aRefShape;
502 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
503 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
504 if (aEndPointRefAttr->isObject()) {
505 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
507 aRefShape = aFeature->lastResult()->shape();
511 // Calculate end point as an intersection between circle and another shape
512 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
514 // End point should be a projection on circle.
515 projectPointOnCircle(anEndPointAttr, aCircleForArc);
517 data()->blockSendAttributeUpdated(aWasBlocked, false);
518 myEnd = anEndPointAttr->pnt();
520 // update the REVERSED flag
521 recalculateReversedFlagByEnd(aCircleForArc);
524 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
526 double aParameterNew = 0.0;
527 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
528 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
529 boolean(REVERSED_ID())->setValue(true);
530 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
531 boolean(REVERSED_ID())->setValue(false);
534 myParamBefore = aParameterNew;
537 void SketchPlugin_MacroArc::fillByThreePassedPoints()
539 AttributePoint2DPtr aStartPointAttr =
540 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
541 if (!aStartPointAttr->isInitialized())
544 AttributePoint2DPtr anEndPointAttr =
545 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
546 if (!anEndPointAttr->isInitialized())
549 myStart = aStartPointAttr->pnt();
550 myEnd = anEndPointAttr->pnt();
552 AttributePoint2DPtr aPassedPointAttr =
553 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
554 if (aPassedPointAttr->isInitialized()) {
555 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
556 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
557 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
558 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
560 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
561 aCircBuilder.addPassingPoint(myStart);
562 aCircBuilder.addPassingPoint(myEnd);
564 aCircBuilder.addTangentCurve(aTangentCurve);
565 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
567 aCircBuilder.addPassingPoint(aPassedPnt);
569 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
572 myCenter = aCircle->center();
574 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
575 recalculateReversedFlagByPassed(*aCircle);
577 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
580 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
581 const GeomAPI_Circ2d& theCurrentCircular)
583 AttributePoint2DPtr aPassedAttr =
584 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
585 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
587 double aEndParam, aPassedParam;
588 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
589 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
591 if(aPassedParam > aEndParam)
592 boolean(REVERSED_ID())->setValue(true);
594 boolean(REVERSED_ID())->setValue(false);
596 myParamBefore = aEndParam;
599 void SketchPlugin_MacroArc::fillByTangentEdge()
601 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
602 if (!aTangentAttr->isInitialized())
605 AttributePoint2DPtr aTangentPointAttr =
606 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
607 if (!aTangentPointAttr->isInitialized())
610 AttributePoint2DPtr anEndPointAttr =
611 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
612 if (!anEndPointAttr->isInitialized())
615 myStart = aTangentPointAttr->pnt();
616 myEnd = anEndPointAttr->pnt();
617 if (myStart->isEqual(myEnd))
620 // obtain a shape the tangent point belongs to
621 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
622 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
624 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
625 aCircBuilder.addPassingPoint(myStart);
626 aCircBuilder.addPassingPoint(myEnd);
627 aCircBuilder.addTangentCurve(aTangentShape);
629 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
632 myCenter = aCircle->center();
634 // rebuild circle to set start point equal to zero parameter
635 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
636 recalculateReversedFlagByEnd(*aCircle);