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_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);
308 void SketchPlugin_MacroArc::execute()
310 FeaturePtr anArcFeature = createArcFeature();
316 // Create constraints.
317 std::string anArcType = string(ARC_TYPE())->value();
318 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
319 SketchPlugin_Tools::createCoincidenceOrTangency(this,
320 CENTER_POINT_REF_ID(),
321 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
324 SketchPlugin_Tools::createCoincidenceOrTangency(this,
325 START_POINT_REF_ID(),
326 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
329 SketchPlugin_Tools::createCoincidenceOrTangency(this,
331 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
334 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
335 SketchPlugin_Tools::createCoincidenceOrTangency(this,
336 START_POINT_REF_ID(),
337 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
340 SketchPlugin_Tools::createCoincidenceOrTangency(this,
342 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
345 SketchPlugin_Tools::createCoincidenceOrTangency(this,
346 PASSED_POINT_REF_ID(),
348 anArcFeature->lastResult(),
351 // coincident with connection point
352 const std::string& aPointAttr = anArcType == ARC_TYPE_BY_TANGENT_EDGE() ?
353 TANGENT_POINT_ID() : TRANSVERSAL_POINT_ID();
354 SketchPlugin_Tools::createCoincidenceOrTangency(this,
356 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
359 // tangent or perpendicular constraint
360 FeaturePtr aStartPointConstraint;
361 if (anArcType == ARC_TYPE_BY_TANGENT_EDGE())
362 aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
364 aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
365 // setting attributes of the start point constraint
366 AttributeRefAttrPtr aRefAttrA =
367 aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
368 AttributeRefAttrPtr aTgPntRefAttr = refattr(aPointAttr);
369 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
370 aRefAttrA->setObject(aTgFeature->lastResult());
371 AttributeRefAttrPtr aRefAttrB =
372 aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
373 aRefAttrB->setObject(anArcFeature->lastResult());
374 // constraint for end point
375 SketchPlugin_Tools::createCoincidenceOrTangency(this,
377 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
382 // message to init reentrant operation
383 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
384 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
385 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
387 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
388 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
389 aMessage->setCreatedFeature(anArcFeature);
390 Events_Loop::loop()->send(aMessage);
394 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
396 std::string aFilledAttributeName;
397 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
398 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
399 if (aReentrantMessage.get()) {
400 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
401 std::string anArcType = aReentrantMessage->typeOfCreation();
403 string(ARC_TYPE())->setValue(anArcType);
405 aFilledAttributeName = ARC_TYPE();
406 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
407 aFilledAttributeName = TANGENT_POINT_ID();
408 AttributeRefAttrPtr aRefAttr = refattr(aFilledAttributeName);
409 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
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 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
448 if (aCreatedFeature.get()) {
449 std::string anID = anAttribute->id();
451 if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
452 anID == END_POINT_3_ID())
453 anArcID = SketchPlugin_Arc::END_ID();
454 else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
455 anArcID = SketchPlugin_Arc::START_ID();
456 else if (anID == CENTER_POINT_ID())
457 anArcID = SketchPlugin_Arc::CENTER_ID();
458 anAttribute = aCreatedFeature->attribute(anArcID);
461 aRefAttr->setAttr(anAttribute);
463 else if (anObject.get()) {
464 // if attribute is NULL, only object is defined, it should be processed outside
465 // the feature because it might be an external feature, that will be
466 // removed/created again after restart operation
467 // #2468 - Crash when sketching circles successively on a repetition
468 aFilledAttributeName = ARC_TYPE();
474 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
476 return aFilledAttributeName;
480 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
482 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
483 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
484 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
485 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
486 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
487 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
488 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
489 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
490 ->setValue(boolean(REVERSED_ID())->value());
491 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
492 ->setValue(boolean(AUXILIARY_ID())->value());
493 anArcFeature->execute();
498 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
500 AttributePoint2DPtr aCenterPointAttr =
501 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
502 if (!aCenterPointAttr->isInitialized())
505 AttributePoint2DPtr aStartPointAttr =
506 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
507 if (!aStartPointAttr->isInitialized())
510 myCenter = aCenterPointAttr->pnt();
511 myStart = aStartPointAttr->pnt();
514 AttributePoint2DPtr anEndPointAttr =
515 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
516 if (!anEndPointAttr->isInitialized())
519 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
520 // avoid degerated arc, when the center and the start points are equal
521 if (!aCircleForArc.implPtr<void*>())
524 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
525 // check the end point is referred to another feature
526 GeomShapePtr aRefShape;
527 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
528 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
529 if (aEndPointRefAttr->isObject()) {
530 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
532 aRefShape = aFeature->lastResult()->shape();
536 // Calculate end point as an intersection between circle and another shape
537 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
539 // End point should be a projection on circle.
540 projectPointOnCircle(anEndPointAttr, aCircleForArc);
542 data()->blockSendAttributeUpdated(aWasBlocked, false);
543 myEnd = anEndPointAttr->pnt();
545 // update the REVERSED flag
546 recalculateReversedFlagByEnd(aCircleForArc);
549 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
551 double aParameterNew = 0.0;
552 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
553 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
554 boolean(REVERSED_ID())->setValue(true);
555 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
556 boolean(REVERSED_ID())->setValue(false);
559 myParamBefore = aParameterNew;
562 void SketchPlugin_MacroArc::fillByThreePassedPoints()
564 AttributePoint2DPtr aStartPointAttr =
565 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
566 if (!aStartPointAttr->isInitialized())
569 AttributePoint2DPtr anEndPointAttr =
570 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
571 if (!anEndPointAttr->isInitialized())
574 myStart = aStartPointAttr->pnt();
575 myEnd = anEndPointAttr->pnt();
577 AttributePoint2DPtr aPassedPointAttr =
578 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
579 if (aPassedPointAttr->isInitialized()) {
580 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
581 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
582 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
583 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
585 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
586 aCircBuilder.addPassingPoint(myStart);
587 aCircBuilder.addPassingPoint(myEnd);
589 aCircBuilder.addTangentCurve(aTangentCurve);
590 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
592 aCircBuilder.addPassingPoint(aPassedPnt);
594 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
597 myCenter = aCircle->center();
599 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
600 recalculateReversedFlagByPassed(*aCircle);
602 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
605 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
606 const GeomAPI_Circ2d& theCurrentCircular)
608 AttributePoint2DPtr aPassedAttr =
609 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
610 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
612 double aEndParam, aPassedParam;
613 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
614 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
616 if(aPassedParam > aEndParam)
617 boolean(REVERSED_ID())->setValue(true);
619 boolean(REVERSED_ID())->setValue(false);
621 myParamBefore = aEndParam;
624 void SketchPlugin_MacroArc::fillByEdge(bool theTransversal)
626 const std::string& aStartPoint = theTransversal ? TRANSVERSAL_POINT_ID() : TANGENT_POINT_ID();
627 const std::string& aEndPoint = theTransversal ? END_POINT_4_ID() : END_POINT_3_ID();
629 AttributeRefAttrPtr aTangentAttr = refattr(aStartPoint);
630 if (!aTangentAttr->isInitialized())
633 AttributePoint2DPtr aConnectionPointAttr =
634 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
635 if (!aConnectionPointAttr->isInitialized())
638 AttributePoint2DPtr anEndPointAttr =
639 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aEndPoint));
640 if (!anEndPointAttr->isInitialized())
643 myStart = aConnectionPointAttr->pnt();
644 myEnd = anEndPointAttr->pnt();
645 if (myStart->isEqual(myEnd))
648 // obtain a shape the tangent point belongs to
649 FeaturePtr aConnectedFeature = ModelAPI_Feature::feature(aConnectionPointAttr->owner());
650 std::shared_ptr<GeomAPI_Shape> aTangentShape = aConnectedFeature->lastResult()->shape();
652 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
653 aCircBuilder.addPassingPoint(myStart);
654 aCircBuilder.addPassingPoint(myEnd);
656 aCircBuilder.setTransversalLine(aTangentShape);
658 aCircBuilder.addTangentCurve(aTangentShape);
660 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
663 myCenter = aCircle->center();
665 // rebuild circle to set start point equal to zero parameter
666 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
667 recalculateReversedFlagByEnd(*aCircle);