1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Arc.cpp
4 // Created: 26 Apr 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Arc.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintTangent.h>
12 #include <Events_Loop.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Session.h>
23 #include <GeomAPI_Ax2.h>
24 #include <GeomAPI_Circ2d.h>
25 #include <GeomAPI_Circ.h>
26 #include <GeomAPI_Dir2d.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Lin2d.h>
29 #include <GeomAPI_Lin.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <GeomDataAPI_Dir.h>
34 #include <GeomAlgoAPI_PointBuilder.h>
35 #include <GeomAlgoAPI_EdgeBuilder.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
40 const double tolerance = 1e-7;
41 const double paramTolerance = 1.e-4;
42 const double PI =3.141592653589793238463;
45 static const std::string& ARC_TYPE_CENTER_START_END()
47 static const std::string TYPE("CenterStartEnd");
50 static const std::string& ARC_TYPE_THREE_POINTS()
52 static const std::string TYPE("ThreePoints");
56 static const std::string& PASSED_POINT_ID()
58 static const std::string PASSED_PNT("ArcPassedPoint");
61 static const std::string& RADIUS_ID()
63 static const std::string RADIUS("ArcRadius");
66 static const std::string& ANGLE_ID()
68 static const std::string ANGLE("ArcAngle");
72 static const std::string& POINT_ID(int theIndex)
75 case 1: return SketchPlugin_Arc::START_ID();
76 case 2: return SketchPlugin_Arc::END_ID();
77 case 3: return PASSED_POINT_ID();
80 static const std::string DUMMY;
87 SketchPlugin_Arc::SketchPlugin_Arc()
88 : SketchPlugin_SketchEntity()
90 myStartUpdate = false;
99 void SketchPlugin_Arc::initDerivedClassAttributes()
101 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
102 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
103 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
104 GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
105 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
106 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
108 data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
109 AttributeBooleanPtr isInversed =
110 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
111 if (!isInversed->isInitialized())
112 isInversed->setValue(false);
114 // get the initial values
115 if (anEndAttr->isInitialized()) {
116 myXEndBefore = anEndAttr->x();
117 myYEndBefore = anEndAttr->y();
120 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
121 std::dynamic_pointer_cast<ModelAPI_AttributeString>(
122 data()->attribute(ARC_TYPE()))->setValue(ARC_TYPE_CENTER_START_END());
124 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
125 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
126 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
127 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
130 void SketchPlugin_Arc::execute()
132 SketchPlugin_Sketch* aSketch = sketch();
133 // result for the arc is set only when all obligatory attributes are initialized,
134 // otherwise AIS object is used to visualize the arc's preview
135 if (aSketch && isFeatureValid()) {
136 ResultPtr aLastResult = lastResult();
137 bool hasResult = aLastResult && aLastResult.get();
139 // compute a circle point in 3D view
140 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
141 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
142 // compute the arc start point
143 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
144 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
146 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
147 // make a visible point
148 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
149 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
151 aConstr1->setShape(aCenterPointShape);
152 aConstr1->setIsInHistory(false);
153 setResult(aConstr1, 0);
155 // make a visible circle
156 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
157 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
158 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
159 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
161 // compute and change the arc end point
162 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
163 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
164 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
165 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
166 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
167 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
168 anEndAttr->setValue(aProjection);
169 std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
170 AttributeBooleanPtr isInversed =
171 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
173 std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
174 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
175 std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
176 double aParameterNew = 0.0;
177 if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
178 if(0 <= myParamBefore && myParamBefore <= PI / 2.0
179 && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
180 isInversed->setValue(true);
181 } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
182 && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
183 isInversed->setValue(false);
186 myParamBefore = aParameterNew;
188 std::shared_ptr<GeomAPI_Shape> aCircleShape;
189 if(!isInversed->value()) {
190 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
192 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
196 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
198 aConstr2->setShape(aCircleShape);
199 aConstr2->setIsInHistory(false);
200 setResult(aConstr2, 1);
203 // update radius and angle
204 updateDependentAttributes();
208 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
210 SketchPlugin_Sketch* aSketch = sketch();
212 // if the feature is valid, the execute() method should be performed, AIS object is empty
213 if (!isFeatureValid()) {
214 // compute a circle point in 3D view
215 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
216 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
218 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
219 if (aCenterAttr->isInitialized()) {
220 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
221 // make a visible point
222 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
223 aShapes.push_back(aCenterPointShape);
225 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
226 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
227 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<
228 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
229 AttributeStringPtr aTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
230 data()->attribute(ARC_TYPE()));
232 if (aStartAttr->isInitialized()) {
233 // make a visible circle
234 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
235 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
236 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
238 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
239 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
240 std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
241 if (aTypeAttr && aTypeAttr->isInitialized() &&
242 aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized())
243 aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
245 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
246 aCenter, aStartPoint, aEndPoint, aNormal);
248 aShapes.push_back(aCircleShape);
252 if (!aShapes.empty()) {
253 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
254 AISObjectPtr anAIS = thePrevious;
256 anAIS = AISObjectPtr(new GeomAPI_AISObject);
257 anAIS->createShape(aCompound);
263 return AISObjectPtr();
266 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
268 std::shared_ptr<ModelAPI_Data> aData = data();
269 if (!aData->isValid())
272 aData->blockSendAttributeUpdated(true);
274 myStartUpdate = true;
276 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
277 aData->attribute(SketchPlugin_Arc::START_ID()));
278 aPoint2->move(theDeltaX, theDeltaY);
280 std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
281 aData->attribute(SketchPlugin_Arc::END_ID()));
282 aPoint3->move(theDeltaX, theDeltaY);
283 myStartUpdate = false;
286 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
287 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
288 aPoint1->move(theDeltaX, theDeltaY);
290 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
291 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
292 aPassedPoint->move(theDeltaX, theDeltaY);
293 aData->blockSendAttributeUpdated(false);
296 bool SketchPlugin_Arc::isFixed() {
297 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
300 bool SketchPlugin_Arc::isFeatureValid()
302 AttributeStringPtr anArcTypeAttr =
303 std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
306 std::string anArcType = anArcTypeAttr->value();
308 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
309 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
310 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
311 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
312 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
313 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
314 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
315 GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
317 bool isValid = false;
318 if (anArcType == ARC_TYPE_THREE_POINTS())
319 isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
321 isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
326 static inline void adjustPeriod(double& theParam)
328 static const double PERIOD = 2.0 * PI;
329 while (theParam < 0.0) theParam += PERIOD;
330 while (theParam >= PERIOD) theParam -= PERIOD;
333 static inline void calculateArcAngleRadius(
334 const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
335 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
336 const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
337 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
338 AttributeDoublePtr theAngleAttr,
339 AttributeDoublePtr theRadiusAttr)
341 double aStartParam, aEndParam, aPassedParam;
342 theCircle->parameter(theStartPoint, paramTolerance, aStartParam);
343 theCircle->parameter(theEndPoint, paramTolerance, aEndParam);
344 theCircle->parameter(thePassedPoint, paramTolerance, aPassedParam);
345 adjustPeriod(aStartParam);
346 adjustPeriod(aEndParam);
347 adjustPeriod(aPassedParam);
349 if (aPassedParam >= aStartParam && aPassedParam <= aEndParam)
350 theAngleAttr->setValue((aEndParam - aStartParam) * 180.0 / PI);
352 theAngleAttr->setValue((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
353 theRadiusAttr->setValue(theCircle->radius());
356 static inline void calculatePassedPoint(
357 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
358 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
359 const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
361 std::shared_ptr<GeomDataAPI_Point2D> thePassedPoint)
363 if (theCenter->distance(theStartPoint) < tolerance ||
364 theCenter->distance(theEndPoint) < tolerance)
367 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
368 theStartPoint->xy()->decreased(theCenter->xy())));
369 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
370 theEndPoint->xy()->decreased(theCenter->xy())));
371 std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
372 if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
373 // start and end directions are opposite, so middle direction will be orthogonal
374 aMidDirXY->setX(-aStartDir->y());
375 aMidDirXY->setY(aStartDir->x());
377 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
378 if ((aStartDir->cross(aMidDir) > 0) ^ !theArcReversed)
381 double aRadius = theCenter->distance(theStartPoint);
382 std::shared_ptr<GeomAPI_XY> aPassedPnt = theCenter->xy()->added( aMidDir->xy()->multiplied(aRadius) );
383 thePassedPoint->setValue(aPassedPnt->x(), aPassedPnt->y());
386 void SketchPlugin_Arc::updateDependentAttributes()
388 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
389 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
390 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
391 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
392 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
393 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
394 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
395 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
396 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
397 data()->attribute(RADIUS_ID()));
398 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
399 data()->attribute(ANGLE_ID()));
404 data()->blockSendAttributeUpdated(true);
406 calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
407 isReversed(), aPassedPoint);
408 if (aRadiusAttr && anAngleAttr) {
409 std::shared_ptr<GeomAPI_Circ2d> aCircle(
410 new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
411 if (aCircle->implPtr<void*>())
412 calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
413 anAngleAttr, aRadiusAttr);
415 data()->blockSendAttributeUpdated(false);
419 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
421 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
422 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
423 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
424 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
425 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
426 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
427 // the second condition for unability to move external segments anywhere
428 if (theID == EXTERNAL_ID() || isFixed()) {
429 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
430 // update arguments due to the selection value
431 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
432 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
433 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
435 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
436 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
437 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
443 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
444 data()->attribute(RADIUS_ID()));
445 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
446 data()->attribute(ANGLE_ID()));
448 if (theID == RADIUS_ID()) {
449 if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
451 // move center and passed point
452 std::shared_ptr<GeomAPI_XY> aStartPnt = aStartAttr->pnt()->xy();
453 std::shared_ptr<GeomAPI_XY> aEndPnt = anEndAttr->pnt()->xy();
454 double aDist = aStartPnt->distance(aEndPnt);
455 if (fabs(aDist) < tolerance)
457 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
458 std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
459 std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->added(aEndPnt)->multiplied(0.5);
461 double anAngle = anAngleAttr->value() * PI / 180.0;
462 adjustPeriod(anAngle);
464 aMidPerpDir->reverse();
466 double aRadius = aRadiusAttr->value();
467 // The center is placed on a perpendicular bisector of a start-end points segment.
468 // If the radius is smaller that necessary, start and end points are moved too.
469 double aDist2 = aRadius * aRadius - aDist * aDist / 4.0;
470 aDist = aDist2 > 0.0 ? sqrt(aDist2) : 0.0;
471 // distance between middle point and start point (does not changed if the arc diameter is greater than start-end distance)
472 aDist2 = sqrt(aRadius * aRadius - aDist * aDist);
474 std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
475 aStartPnt = aMidPnt->added(aDir->xy()->multiplied(-aDist2));
476 aEndPnt = aMidPnt->added(aDir->xy()->multiplied(aDist2));
478 data()->blockSendAttributeUpdated(true);
479 aCenterAttr->setValue(aCenter->x(), aCenter->y());
480 aStartAttr->setValue(aStartPnt->x(), aStartPnt->y());
481 anEndAttr->setValue(aEndPnt->x(), aEndPnt->y());
482 updateDependentAttributes();
483 data()->blockSendAttributeUpdated(false);
486 if (theID == ANGLE_ID()) {
487 if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
489 data()->blockSendAttributeUpdated(true);
490 // move end point and passed point
491 std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
492 double anAngle = anAngleAttr->value() * PI / 180.0;
493 double sinA = sin(anAngle);
494 double cosA = cos(anAngle);
495 std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
496 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
497 aStartDir->x() * cosA - aStartDir->y() * sinA,
498 aStartDir->x() * sinA + aStartDir->y() * cosA));
499 anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
504 aDir = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(
505 aStartDir->x() * cosA - aStartDir->y() * sinA,
506 aStartDir->x() * sinA + aStartDir->y() * cosA));
507 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
508 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
509 aPassedPoint->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
511 std::shared_ptr<GeomAPI_Circ2d> aCircle(
512 new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
513 calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
514 anAngleAttr, aRadiusAttr);
515 data()->blockSendAttributeUpdated(false);
519 if (theID == CENTER_ID()) {
520 if (isFeatureValid())
525 AttributeStringPtr aTypeAttr =
526 std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
529 std::string anArcType = aTypeAttr->value();
531 // update the points in accordance to the changed point changes
532 if (anArcType == ARC_TYPE_CENTER_START_END()) {
533 if (!isFeatureValid())
535 if (theID == END_ID() && isStable()) {
536 // The arc is under construction, so its end point projected
537 // on the circle formed by center and start points
540 updateDependentAttributes();
542 else if (anArcType == ARC_TYPE_THREE_POINTS() &&
543 (theID == START_ID() || theID == END_ID() || theID == PASSED_POINT_ID())) {
544 data()->blockSendAttributeUpdated(true);
546 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
547 int aNbInitialized = 0;
548 for (int i = 1; i <= 3; ++i) {
549 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
550 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
551 if (aCurPnt->isInitialized())
552 aPoints[aNbInitialized++] = aCurPnt->pnt();
555 if (aNbInitialized == 1)
556 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
557 else if (aNbInitialized == 2) {
558 // calculate center point, which gives a quarter of circle for the given start and end points
559 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aPoints[0];
560 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aPoints[1];
561 std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy())->multiplied(0.5);
562 double x = aDir->x();
563 double y = aDir->y();
566 std::shared_ptr<GeomAPI_XY> aCenter = aStartPnt->xy()->added(aDir);
567 double aRadius = sqrt(aDir->dot(aDir));
569 aCenterAttr->setValue(aCenter->x(), aCenter->y());
570 aRadiusAttr->setValue(aRadius);
571 anAngleAttr->setValue(90.0);
574 std::shared_ptr<GeomAPI_Circ2d> aCircle(
575 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
577 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
579 aCenterAttr->setValue(aCenter);
580 if (theID == START_ID() || theID == END_ID())
581 updateDependentAttributes();
583 calculateArcAngleRadius(aCircle, aPoints[0], aPoints[1], aPoints[2],
584 anAngleAttr, aRadiusAttr);
588 data()->blockSendAttributeUpdated(false);
590 else if (anArcType == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
591 SketchPlugin_Sketch* aSketch = sketch();
592 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
593 data()->attribute(TANGENT_POINT_ID()));
595 if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
596 data()->blockSendAttributeUpdated(true);
597 // compute orthogonal direction
598 std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
599 std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
600 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
601 std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
602 FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
603 std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
604 aTangFeature->lastResult()->shape());
605 if (aTangEdge->isLine()) {
606 std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
607 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
608 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
609 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
611 else if (aTangEdge->isArc()) {
612 std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
613 std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
614 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
615 new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
618 // compute parameters of the middle perpendicular
619 std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
620 std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
621 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
622 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
623 new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
625 // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
626 std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
627 std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
628 std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
630 aCenterAttr->setValue(aCenter);
631 aStartAttr->setValue(aTangPnt2d);
632 updateDependentAttributes();
635 data()->blockSendAttributeUpdated(false);
637 if (theID == TANGENT_POINT_ID())
638 tangencyArcConstraints();
643 void SketchPlugin_Arc::setReversed(bool isReversed)
645 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
649 bool SketchPlugin_Arc::isReversed()
651 return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
654 void SketchPlugin_Arc::tangencyArcConstraints()
659 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
660 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
661 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
662 attribute(TANGENT_POINT_ID()));
663 if (!aTangPtAttr->attr())
666 FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
667 ObjectPtr aThisArc = aFeature->lastResult();
668 aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
669 ObjectPtr aTangFeature = aFeature->lastResult();
671 // trying to find constraints to fix the tangency of the arc
672 std::set<FeaturePtr> aCoincidence;
673 std::set<FeaturePtr> aTangency;
675 AttributeRefAttrPtr aRefAttrA, aRefAttrB;
676 std::set<AttributePtr> aRefs = data()->refsToMe();
677 const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
678 aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
679 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
680 for (; aRefIt != aRefs.end(); ++aRefIt) {
681 FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
682 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
683 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
684 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
685 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
686 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
687 if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
688 (aRefAttrB && aRefAttrB->attr() == aStartAttr))
689 aCoincidence.insert(aConstrFeature);
691 else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
692 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
693 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
694 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
695 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
696 if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
697 (aRefAttrB && aRefAttrB->object() == aThisArc))
698 aTangency.insert(aConstrFeature);
701 // search applicable pair of constraints
702 bool isFound = false;
703 FeaturePtr aPrevCoincidence, aPrevTangency;
704 std::set<FeaturePtr>::const_iterator aCIt, aTIt;
705 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
706 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
707 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
708 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
709 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
710 AttributePtr anOtherPoint =
711 aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
712 for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
713 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
714 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
715 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
716 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
717 ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
718 aRefAttrB->object() : aRefAttrA->object();
719 if (anOtherPoint->owner() == anOtherObject) {
721 aPrevCoincidence = *aCIt;
722 aPrevTangency = *aTIt;
728 // update previous constraints
729 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
730 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
731 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
732 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
733 if (aRefAttrA->attr() == aStartAttr)
734 aRefAttrB->setAttr(aTangPtAttr->attr());
736 aRefAttrA->setAttr(aTangPtAttr->attr());
738 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
739 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
740 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
741 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
742 if (aRefAttrA->object() == aThisArc)
743 aRefAttrB->setObject(aTangFeature);
745 aRefAttrA->setObject(aTangFeature);
747 // Wait all constraints being removed, then send update events
748 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
749 bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
751 Events_Loop::loop()->setFlushed(aDeleteEvent, false);
752 // Remove all obtained constraints which use current arc, because
753 // there is no information which of them were used to build tangency arc.
754 DocumentPtr aDoc = sketch()->document();
755 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
756 aDoc->removeFeature(*aCIt);
757 for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
758 aDoc->removeFeature(*aTIt);
759 // Send events to update the sub-features by the solver.
761 Events_Loop::loop()->setFlushed(aDeleteEvent, true);
763 Events_Loop::loop()->flush(aDeleteEvent);
765 // Wait all constraints being created, then send update events
766 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
767 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
769 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
771 // Create new constraints
772 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
773 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
774 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
775 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
776 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
777 aRefAttrA->setAttr(aStartAttr);
778 aRefAttrB->setAttr(aTangPtAttr->attr());
779 aConstraint->execute();
780 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
782 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
783 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
784 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
785 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
786 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
787 aRefAttrA->setObject(aThisArc);
788 aRefAttrB->setObject(aTangFeature);
789 aConstraint->execute();
790 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
792 // Send events to update the sub-features by the solver.
794 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
798 void SketchPlugin_Arc::projectEndPoint()
800 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
801 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
802 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
803 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
804 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
805 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
807 if (aCenterAttr->pnt()->distance(aStartAttr->pnt()) < tolerance)
809 data()->blockSendAttributeUpdated(true);
810 // compute and change the arc end point
811 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
812 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
813 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
814 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
815 anEndAttr->setValue(aProjection);
816 updateDependentAttributes();
817 data()->blockSendAttributeUpdated(false);