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>
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomDataAPI_Dir.h>
35 #include <GeomAlgoAPI_PointBuilder.h>
36 #include <GeomAlgoAPI_EdgeBuilder.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
42 const double tolerance = 1e-7;
43 const double paramTolerance = 1.e-4;
44 const double PI = 3.141592653589793238463;
46 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
48 std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
50 thePoint->setValue(aProjection);
54 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
55 : SketchPlugin_SketchEntity(),
60 void SketchPlugin_MacroArc::initAttributes()
62 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
64 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
65 data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
66 data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
68 data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
69 data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
70 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
72 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
73 data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
75 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
77 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
78 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
80 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
82 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
83 data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
84 data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
85 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
87 data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
89 boolean(REVERSED_ID())->setValue(false);
90 string(EDIT_ARC_TYPE_ID())->setValue("");
92 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
93 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
94 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
95 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
96 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
99 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
101 std::string anArcType = string(ARC_TYPE())->value();
103 // If arc type switched reset according attributes.
104 if(theID == ARC_TYPE()) {
105 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
106 SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
107 SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
108 SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
109 SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
110 SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
111 SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
112 SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
113 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
114 SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
115 SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
116 SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
117 SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
118 SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
119 SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
124 boolean(REVERSED_ID())->setValue(false);
126 } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
127 fillByCenterAndTwoPassed();
128 else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
129 fillByThreePassedPoints();
130 else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
135 if(myCenter.get() && myStart.get()) {
136 aRadius = myCenter->distance(myStart);
138 if(myStart->isEqual(myEnd)) {
141 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
142 double aStartParam, anEndParam;
143 aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
144 aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
145 anAngle = (anEndParam - aStartParam) / PI * 180.0;
146 if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
151 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
152 real(RADIUS_ID())->setValue(aRadius);
153 real(ANGLE_ID())->setValue(anAngle);
154 data()->blockSendAttributeUpdated(aWasBlocked, false);
157 GeomShapePtr SketchPlugin_MacroArc::getArcShape()
159 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
160 return GeomShapePtr();
163 SketchPlugin_Sketch* aSketch = sketch();
165 return GeomShapePtr();
168 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
169 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
170 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
171 std::shared_ptr<GeomDataAPI_Dir> aNDir =
172 std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
173 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
175 GeomShapePtr anArcShape = boolean(REVERSED_ID())->value() ?
176 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
177 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
182 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
184 if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
185 return AISObjectPtr();
188 SketchPlugin_Sketch* aSketch = sketch();
190 return AISObjectPtr();
193 GeomShapePtr anArcShape = getArcShape();
194 std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
195 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
197 if(!anArcShape.get() || !aCenterPointShape.get()) {
198 return AISObjectPtr();
201 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
202 aShapes.push_back(anArcShape);
203 aShapes.push_back(aCenterPointShape);
205 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
206 AISObjectPtr anAIS = thePrevious;
208 anAIS.reset(new GeomAPI_AISObject());
210 anAIS->createShape(aCompound);
214 void SketchPlugin_MacroArc::execute()
216 FeaturePtr anArcFeature = createArcFeature();
222 // Create constraints.
223 std::string anArcType = string(ARC_TYPE())->value();
224 if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
225 SketchPlugin_Tools::createConstraint(this,
226 CENTER_POINT_REF_ID(),
227 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
230 SketchPlugin_Tools::createConstraint(this,
231 START_POINT_REF_ID(),
232 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
235 SketchPlugin_Tools::createConstraint(this,
237 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
240 } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
241 SketchPlugin_Tools::createConstraint(this,
242 START_POINT_REF_ID(),
243 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
246 SketchPlugin_Tools::createConstraint(this,
248 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
251 SketchPlugin_Tools::createConstraint(this,
252 PASSED_POINT_REF_ID(),
254 anArcFeature->lastResult(),
256 } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
257 // constraints for tangent arc
258 SketchPlugin_Tools::createConstraint(this,
260 anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
263 FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
264 AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
265 AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
266 FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
267 aRefAttrA->setObject(aTgFeature->lastResult());
268 AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
269 aRefAttrB->setObject(anArcFeature->lastResult());
270 // constraint for end point
271 SketchPlugin_Tools::createConstraint(this,
273 anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
278 // message to init reentrant operation
279 static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
280 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
281 <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, 0));
283 std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
284 aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
285 aMessage->setCreatedFeature(anArcFeature);
286 Events_Loop::loop()->send(aMessage);
289 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
291 std::string aFilledAttributeName;
292 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
293 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
294 if (aReentrantMessage.get()) {
295 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
296 std::string anArcType = aReentrantMessage->typeOfCreation();
298 string(ARC_TYPE())->setValue(anArcType);
300 aFilledAttributeName = ARC_TYPE();
301 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
302 aFilledAttributeName = TANGENT_POINT_ID();
303 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
304 attribute(aFilledAttributeName));
305 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
306 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
309 ObjectPtr anObject = aReentrantMessage->selectedObject();
310 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
311 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
313 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
314 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
315 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
316 std::string aReferenceAttributeName;
317 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
318 aFilledAttributeName = CENTER_POINT_ID();
319 aReferenceAttributeName = CENTER_POINT_REF_ID();
322 aFilledAttributeName = START_POINT_2_ID();
323 aReferenceAttributeName = START_POINT_REF_ID();
325 // fill 2d point attribute
326 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
327 attribute(aFilledAttributeName));
328 aPointAttr->setValue(aClickedPoint);
329 // fill reference attribute
330 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
331 attribute(aReferenceAttributeName));
332 if (aRefAttr.get()) {
333 if (anAttribute.get())
334 aRefAttr->setAttr(anAttribute);
335 else if (anObject.get()) {
336 // if presentation of previous reentrant macro arc is used, the object is invalid,
337 // we should use result of previous feature of the message(Arc)
338 if (!anObject->data()->isValid()) {
339 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
340 anObject = aCreatedFeature->lastResult();
342 aRefAttr->setObject(anObject);
348 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
350 return aFilledAttributeName;
353 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
355 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
356 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
357 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
358 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
359 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
360 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
361 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
362 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
363 ->setValue(boolean(REVERSED_ID())->value());
364 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
365 ->setValue(boolean(AUXILIARY_ID())->value());
366 anArcFeature->execute();
371 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
373 AttributePoint2DPtr aCenterPointAttr =
374 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
375 if (!aCenterPointAttr->isInitialized())
378 AttributePoint2DPtr aStartPointAttr =
379 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
380 if (!aStartPointAttr->isInitialized())
383 myCenter = aCenterPointAttr->pnt();
384 myStart = aStartPointAttr->pnt();
387 AttributePoint2DPtr anEndPointAttr =
388 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
389 if (!anEndPointAttr->isInitialized())
392 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
394 // End point should be a projection on circle.
395 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
396 projectPointOnCircle(anEndPointAttr, aCircleForArc);
397 data()->blockSendAttributeUpdated(aWasBlocked, false);
398 myEnd = anEndPointAttr->pnt();
400 // update the REVERSED flag
401 recalculateReversedFlagByEnd(aCircleForArc);
404 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
406 double aParameterNew = 0.0;
407 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
408 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
409 boolean(REVERSED_ID())->setValue(true);
410 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
411 boolean(REVERSED_ID())->setValue(false);
414 myParamBefore = aParameterNew;
417 void SketchPlugin_MacroArc::fillByThreePassedPoints()
419 AttributePoint2DPtr aStartPointAttr =
420 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
421 if (!aStartPointAttr->isInitialized())
424 AttributePoint2DPtr anEndPointAttr =
425 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
426 if (!anEndPointAttr->isInitialized())
429 myStart = aStartPointAttr->pnt();
430 myEnd = anEndPointAttr->pnt();
432 AttributePoint2DPtr aPassedPointAttr =
433 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
434 if (aPassedPointAttr->isInitialized()) {
435 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
436 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
437 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
438 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
440 std::shared_ptr<GeomAPI_Interface> aPassed;
442 aPassed = aTangentCurve;
444 aPassed = aPassedPnt;
446 std::shared_ptr<GeomAPI_Ax3> anAxis = SketchPlugin_Sketch::plane(sketch());
447 GeomAPI_Circ2d aCircle(myStart, myEnd, aPassed, anAxis);
448 myCenter = aCircle.center();
449 aCircle = GeomAPI_Circ2d(myCenter, myStart);
451 recalculateReversedFlagByPassed(aCircle);
453 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
456 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
457 const GeomAPI_Circ2d& theCurrentCircular)
459 AttributePoint2DPtr aPassedAttr =
460 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
461 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
463 double aEndParam, aPassedParam;
464 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
465 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
467 if(aPassedParam > aEndParam)
468 boolean(REVERSED_ID())->setValue(true);
470 boolean(REVERSED_ID())->setValue(false);
472 myParamBefore = aEndParam;
475 void SketchPlugin_MacroArc::fillByTangentEdge()
477 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
478 if (!aTangentAttr->isInitialized())
481 AttributePoint2DPtr aTangentPointAttr =
482 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
483 if (!aTangentPointAttr->isInitialized())
486 AttributePoint2DPtr anEndPointAttr =
487 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
488 if (!anEndPointAttr->isInitialized())
491 myStart = aTangentPointAttr->pnt();
492 myEnd = anEndPointAttr->pnt();
493 if (myStart->isEqual(myEnd))
496 // obtain a shape the tangent point belongs to
497 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
498 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
500 std::shared_ptr<GeomAPI_Ax3> anAxis = SketchPlugin_Sketch::plane(sketch());
501 GeomAPI_Circ2d aCircle(myStart, myEnd, aTangentShape, anAxis);
502 myCenter = aCircle.center();
504 // rebuild circle to set start point equal to zero parameter
505 aCircle = GeomAPI_Circ2d(myCenter, myStart);
506 recalculateReversedFlagByEnd(aCircle);