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);
287 Events_Loop::loop()->flush(anId);
290 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
292 std::string aFilledAttributeName;
293 std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
294 std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
295 if (aReentrantMessage.get()) {
296 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
297 std::string anArcType = aReentrantMessage->typeOfCreation();
299 string(ARC_TYPE())->setValue(anArcType);
301 aFilledAttributeName = ARC_TYPE();
302 if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
303 aFilledAttributeName = TANGENT_POINT_ID();
304 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
305 attribute(aFilledAttributeName));
306 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
307 aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
310 ObjectPtr anObject = aReentrantMessage->selectedObject();
311 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
312 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
314 if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
315 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
316 anArcType == ARC_TYPE_BY_THREE_POINTS()) {
317 std::string aReferenceAttributeName;
318 if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
319 aFilledAttributeName = CENTER_POINT_ID();
320 aReferenceAttributeName = CENTER_POINT_REF_ID();
323 aFilledAttributeName = START_POINT_2_ID();
324 aReferenceAttributeName = START_POINT_REF_ID();
326 // fill 2d point attribute
327 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
328 attribute(aFilledAttributeName));
329 aPointAttr->setValue(aClickedPoint);
330 // fill reference attribute
331 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
332 attribute(aReferenceAttributeName));
333 if (aRefAttr.get()) {
334 if (anAttribute.get())
335 aRefAttr->setAttr(anAttribute);
336 else if (anObject.get()) {
337 // if presentation of previous reentrant macro arc is used, the object is invalid,
338 // we should use result of previous feature of the message(Arc)
339 if (!anObject->data()->isValid()) {
340 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
341 anObject = aCreatedFeature->lastResult();
343 aRefAttr->setObject(anObject);
349 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
351 return aFilledAttributeName;
354 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
356 FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
357 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
358 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
359 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
360 anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
361 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
362 anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
363 anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
364 ->setValue(boolean(REVERSED_ID())->value());
365 anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
366 ->setValue(boolean(AUXILIARY_ID())->value());
367 anArcFeature->execute();
372 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
374 AttributePoint2DPtr aCenterPointAttr =
375 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
376 if (!aCenterPointAttr->isInitialized())
379 AttributePoint2DPtr aStartPointAttr =
380 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
381 if (!aStartPointAttr->isInitialized())
384 myCenter = aCenterPointAttr->pnt();
385 myStart = aStartPointAttr->pnt();
388 AttributePoint2DPtr anEndPointAttr =
389 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
390 if (!anEndPointAttr->isInitialized())
393 GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
395 // End point should be a projection on circle.
396 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
397 projectPointOnCircle(anEndPointAttr, aCircleForArc);
398 data()->blockSendAttributeUpdated(aWasBlocked, false);
399 myEnd = anEndPointAttr->pnt();
401 // update the REVERSED flag
402 recalculateReversedFlagByEnd(aCircleForArc);
405 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
407 double aParameterNew = 0.0;
408 if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
409 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
410 boolean(REVERSED_ID())->setValue(true);
411 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
412 boolean(REVERSED_ID())->setValue(false);
415 myParamBefore = aParameterNew;
418 void SketchPlugin_MacroArc::fillByThreePassedPoints()
420 AttributePoint2DPtr aStartPointAttr =
421 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
422 if (!aStartPointAttr->isInitialized())
425 AttributePoint2DPtr anEndPointAttr =
426 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
427 if (!anEndPointAttr->isInitialized())
430 myStart = aStartPointAttr->pnt();
431 myEnd = anEndPointAttr->pnt();
433 AttributePoint2DPtr aPassedPointAttr =
434 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
435 if (aPassedPointAttr->isInitialized()) {
436 std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
437 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
438 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
439 refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
441 std::shared_ptr<GeomAPI_Interface> aPassed;
443 aPassed = aTangentCurve;
445 aPassed = aPassedPnt;
447 std::shared_ptr<GeomAPI_Ax3> anAxis = SketchPlugin_Sketch::plane(sketch());
448 GeomAPI_Circ2d aCircle(myStart, myEnd, aPassed, anAxis);
449 myCenter = aCircle.center();
450 aCircle = GeomAPI_Circ2d(myCenter, myStart);
452 recalculateReversedFlagByPassed(aCircle);
454 myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
457 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
458 const GeomAPI_Circ2d& theCurrentCircular)
460 AttributePoint2DPtr aPassedAttr =
461 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
462 std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
464 double aEndParam, aPassedParam;
465 theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
466 theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
468 if(aPassedParam > aEndParam)
469 boolean(REVERSED_ID())->setValue(true);
471 boolean(REVERSED_ID())->setValue(false);
473 myParamBefore = aEndParam;
476 void SketchPlugin_MacroArc::fillByTangentEdge()
478 AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
479 if (!aTangentAttr->isInitialized())
482 AttributePoint2DPtr aTangentPointAttr =
483 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
484 if (!aTangentPointAttr->isInitialized())
487 AttributePoint2DPtr anEndPointAttr =
488 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
489 if (!anEndPointAttr->isInitialized())
492 myStart = aTangentPointAttr->pnt();
493 myEnd = anEndPointAttr->pnt();
494 if (myStart->isEqual(myEnd))
497 // obtain a shape the tangent point belongs to
498 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
499 std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
501 std::shared_ptr<GeomAPI_Ax3> anAxis = SketchPlugin_Sketch::plane(sketch());
502 GeomAPI_Circ2d aCircle(myStart, myEnd, aTangentShape, anAxis);
503 myCenter = aCircle.center();
505 // rebuild circle to set start point equal to zero parameter
506 aCircle = GeomAPI_Circ2d(myCenter, myStart);
507 recalculateReversedFlagByEnd(aCircle);