1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_MacroArc.h"
23 #include "SketchPlugin_Arc.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(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
159 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
160 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
162 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
164 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
165 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
166 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
167 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
169 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
171 boolean(REVERSED_ID())->setValue(false);
172 string(EDIT_ARC_TYPE_ID())->setValue("");
174 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
175 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
176 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
177 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
178 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
181 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
183 std::string anArcType = string(ARC_TYPE())->value();
185 // If arc type switched reset according attributes.
186 if(theID == ARC_TYPE()) {
187 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
188 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
189 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
190 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
191 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
192 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
193 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
194 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
195 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
196 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
197 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
198 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
199 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
200 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
201 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
206 boolean(REVERSED_ID())->setValue(false);
208 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
209 fillByCenterAndTwoPassed();
210 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
211 fillByThreePassedPoints();
212 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
217 if(myCenter.get() && myStart.get()) {
218 aRadius = myCenter->distance(myStart);
220 if(myStart->isEqual(myEnd)) {
223 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
224 double aStartParam, anEndParam;
225 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
226 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
227 anAngle = (anEndParam - aStartParam) / PI * 180.0;
228 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
233 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
235 // center attribute is used in processEvent() to set reference to reentrant arc
236 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
237 ->setValue(myCenter);
239 real(RADIUS_ID())->setValue(aRadius);
240 real(ANGLE_ID())->setValue(anAngle);
241 data()->blockSendAttributeUpdated(aWasBlocked, false);
244 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
246 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
247 return GeomShapePtr();
250 SketchPlugin_Sketch* aSketch = sketch();
252 return GeomShapePtr();
255 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
256 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
257 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
258 std::shared_ptr<GeomDataAPI_Dir> aNDir =
259 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
260 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
262 GeomShapePtr anArcShape;
264 anArcShape = boolean(REVERSED_ID())->value() ?
265 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
266 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
268 double aRadius = aCenter->distance(aStart);
269 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
275 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
277 GeomShapePtr anArcShape = getArcShape();
278 if(!anArcShape.get())
279 return AISObjectPtr();
281 SketchPlugin_Sketch* aSketch = sketch();
282 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
283 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
285 if(!aCenterPointShape.get())
286 return AISObjectPtr();
288 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
289 aShapes.push_back(anArcShape);
290 aShapes.push_back(aCenterPointShape);
292 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
293 AISObjectPtr anAIS = thePrevious;
295 anAIS.reset(new GeomAPI_AISObject());
297 anAIS->createShape(aCompound);
301 void SketchPlugin_MacroArc::execute()
303 FeaturePtr anArcFeature = createArcFeature();
309 // Create constraints.
310 std::string anArcType = string(ARC_TYPE())->value();
311 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
312 SketchPlugin_Tools::createCoincidenceOrTangency(this,
313 CENTER_POINT_REF_ID(),
314 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
317 SketchPlugin_Tools::createCoincidenceOrTangency(this,
318 START_POINT_REF_ID(),
319 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
322 SketchPlugin_Tools::createCoincidenceOrTangency(this,
324 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
327 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
328 SketchPlugin_Tools::createCoincidenceOrTangency(this,
329 START_POINT_REF_ID(),
330 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
333 SketchPlugin_Tools::createCoincidenceOrTangency(this,
335 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
338 SketchPlugin_Tools::createCoincidenceOrTangency(this,
339 PASSED_POINT_REF_ID(),
341 anArcFeature->lastResult(),
343 } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
344 // constraints for tangent arc
345 SketchPlugin_Tools::createCoincidenceOrTangency(this,
347 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
350 FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
351 AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
352 AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
353 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
354 aRefAttrA->setObject(aTgFeature->lastResult());
355 AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
356 aRefAttrB->setObject(anArcFeature->lastResult());
357 // constraint for end point
358 SketchPlugin_Tools::createCoincidenceOrTangency(this,
360 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
365 // message to init reentrant operation
366 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
367 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
368 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
370 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
371 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
372 aMessage->setCreatedFeature(anArcFeature);
373 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;
454 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
456 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
457 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
458 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
459 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
460 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
461 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
462 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
463 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
464 ->setValue(boolean(REVERSED_ID())->value());
465 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
466 ->setValue(boolean(AUXILIARY_ID())->value());
467 anArcFeature->execute();
472 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
474 AttributePoint2DPtr aCenterPointAttr =
475 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
476 if (!aCenterPointAttr->isInitialized())
479 AttributePoint2DPtr aStartPointAttr =
480 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
481 if (!aStartPointAttr->isInitialized())
484 myCenter = aCenterPointAttr->pnt();
485 myStart = aStartPointAttr->pnt();
488 AttributePoint2DPtr anEndPointAttr =
489 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
490 if (!anEndPointAttr->isInitialized())
493 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
495 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
496 // check the end point is referred to another feature
497 GeomShapePtr aRefShape;
498 AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
499 if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
500 if (aEndPointRefAttr->isObject()) {
501 FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
503 aRefShape = aFeature->lastResult()->shape();
507 // Calculate end point as an intersection between circle and another shape
508 intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
510 // End point should be a projection on circle.
511 projectPointOnCircle(anEndPointAttr, aCircleForArc);
513 data()->blockSendAttributeUpdated(aWasBlocked, false);
514 myEnd = anEndPointAttr->pnt();
516 // update the REVERSED flag
517 recalculateReversedFlagByEnd(aCircleForArc);
520 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
522 double aParameterNew = 0.0;
523 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
524 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
525 boolean(REVERSED_ID())->setValue(true);
526 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
527 boolean(REVERSED_ID())->setValue(false);
530 myParamBefore = aParameterNew;
533 void SketchPlugin_MacroArc::fillByThreePassedPoints()
535 AttributePoint2DPtr aStartPointAttr =
536 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
537 if (!aStartPointAttr->isInitialized())
540 AttributePoint2DPtr anEndPointAttr =
541 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
542 if (!anEndPointAttr->isInitialized())
545 myStart = aStartPointAttr->pnt();
546 myEnd = anEndPointAttr->pnt();
548 AttributePoint2DPtr aPassedPointAttr =
549 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
550 if (aPassedPointAttr->isInitialized()) {
551 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
552 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
553 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
554 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
556 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
557 aCircBuilder.addPassingPoint(myStart);
558 aCircBuilder.addPassingPoint(myEnd);
560 aCircBuilder.addTangentCurve(aTangentCurve);
561 aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
563 aCircBuilder.addPassingPoint(aPassedPnt);
565 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
568 myCenter = aCircle->center();
570 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
571 recalculateReversedFlagByPassed(*aCircle);
573 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
576 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
577 const GeomAPI_Circ2d& theCurrentCircular)
579 AttributePoint2DPtr aPassedAttr =
580 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
581 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
583 double aEndParam, aPassedParam;
584 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
585 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
587 if(aPassedParam > aEndParam)
588 boolean(REVERSED_ID())->setValue(true);
590 boolean(REVERSED_ID())->setValue(false);
592 myParamBefore = aEndParam;
595 void SketchPlugin_MacroArc::fillByTangentEdge()
597 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
598 if (!aTangentAttr->isInitialized())
601 AttributePoint2DPtr aTangentPointAttr =
602 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
603 if (!aTangentPointAttr->isInitialized())
606 AttributePoint2DPtr anEndPointAttr =
607 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
608 if (!anEndPointAttr->isInitialized())
611 myStart = aTangentPointAttr->pnt();
612 myEnd = anEndPointAttr->pnt();
613 if (myStart->isEqual(myEnd))
616 // obtain a shape the tangent point belongs to
617 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
618 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
620 GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
621 aCircBuilder.addPassingPoint(myStart);
622 aCircBuilder.addPassingPoint(myEnd);
623 aCircBuilder.addTangentCurve(aTangentShape);
625 std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
628 myCenter = aCircle->center();
630 // rebuild circle to set start point equal to zero parameter
631 aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
632 recalculateReversedFlagByEnd(*aCircle);