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()
47 static const std::string TYPE("ArcType");
50 static const std::string& ARC_TYPE_CENTER_START_END()
52 static const std::string TYPE("CenterStartEnd");
55 static const std::string& ARC_TYPE_THREE_POINTS()
57 static const std::string TYPE("ThreePoints");
60 static const std::string& ARC_TYPE_TANGENT()
62 static const std::string TYPE("Tangent");
66 static const std::string& PASSED_POINT_ID()
68 static const std::string PASSED_PNT("ArcPassedPoint");
71 static const std::string& TANGENT_POINT_ID()
73 static const std::string TANGENT_PNT("ArcTangentPoint");
76 static const std::string& RADIUS_ID()
78 static const std::string RADIUS("ArcRadius");
81 static const std::string& ANGLE_ID()
83 static const std::string ANGLE("ArcAngle");
87 static const std::string& POINT_ID(int theIndex)
90 case 1: return SketchPlugin_Arc::START_ID();
91 case 2: return SketchPlugin_Arc::END_ID();
92 case 3: return PASSED_POINT_ID();
95 static const std::string DUMMY;
102 SketchPlugin_Arc::SketchPlugin_Arc()
103 : SketchPlugin_SketchEntity()
105 myStartUpdate = false;
114 void SketchPlugin_Arc::initDerivedClassAttributes()
116 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
117 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
118 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
119 GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
120 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
121 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
123 data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
124 AttributeBooleanPtr isInversed =
125 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
126 if (!isInversed->isInitialized())
127 isInversed->setValue(false);
129 // get the initial values
130 if (anEndAttr->isInitialized()) {
131 myXEndBefore = anEndAttr->x();
132 myYEndBefore = anEndAttr->y();
135 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
136 std::dynamic_pointer_cast<ModelAPI_AttributeString>(
137 data()->attribute(ARC_TYPE()))->setValue(ARC_TYPE_CENTER_START_END());
139 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
140 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
141 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
142 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
145 void SketchPlugin_Arc::execute()
147 SketchPlugin_Sketch* aSketch = sketch();
148 // result for the arc is set only when all obligatory attributes are initialized,
149 // otherwise AIS object is used to visualize the arc's preview
150 if (aSketch && isFeatureValid()) {
151 ResultPtr aLastResult = lastResult();
152 bool hasResult = aLastResult && aLastResult.get();
154 // compute a circle point in 3D view
155 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
156 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
157 // compute the arc start point
158 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
159 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
161 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
162 // make a visible point
163 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
164 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
166 aConstr1->setShape(aCenterPointShape);
167 aConstr1->setIsInHistory(false);
168 setResult(aConstr1, 0);
170 // make a visible circle
171 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
173 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
174 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
176 // compute and change the arc end point
177 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
178 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
179 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
180 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
181 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
182 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
183 anEndAttr->setValue(aProjection);
184 std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
185 AttributeBooleanPtr isInversed =
186 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
188 std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
189 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
190 std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
191 double aParameterNew = 0.0;
192 if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
193 if(0 <= myParamBefore && myParamBefore <= PI / 2.0
194 && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
195 isInversed->setValue(true);
196 } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
197 && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
198 isInversed->setValue(false);
201 myParamBefore = aParameterNew;
203 std::shared_ptr<GeomAPI_Shape> aCircleShape;
204 if(!isInversed->value()) {
205 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
207 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
211 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
213 aConstr2->setShape(aCircleShape);
214 aConstr2->setIsInHistory(false);
215 setResult(aConstr2, 1);
218 AttributeRefAttrPtr aTangentPoint = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
219 attribute(TANGENT_POINT_ID()));
220 if (!hasResult && aTangentPoint->attr())
221 tangencyArcConstraints();
225 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
227 SketchPlugin_Sketch* aSketch = sketch();
229 // if the feature is valid, the execute() method should be performed, AIS object is empty
230 if (!isFeatureValid()) {
231 // compute a circle point in 3D view
232 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
233 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
234 if (aCenterAttr->isInitialized()) {
235 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
237 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
238 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
239 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<
240 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
241 AttributeStringPtr aTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
242 data()->attribute(ARC_TYPE()));
244 if (aStartAttr->isInitialized()) {
245 // make a visible circle
246 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
247 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
248 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
250 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
251 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
252 std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
253 if (aTypeAttr && aTypeAttr->isInitialized() &&
254 aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized())
255 aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
256 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
257 aCenter, aStartPoint, aEndPoint, aNormal);
259 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
260 // make a visible point
261 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
262 aShapes.push_back(aCenterPointShape);
264 aShapes.push_back(aCircleShape);
265 if (!aShapes.empty())
267 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
268 AISObjectPtr anAIS = thePrevious;
270 anAIS = AISObjectPtr(new GeomAPI_AISObject);
271 anAIS->createShape(aCompound);
281 return AISObjectPtr();
284 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
286 std::shared_ptr<ModelAPI_Data> aData = data();
287 if (!aData->isValid())
290 aData->blockSendAttributeUpdated(true);
292 myStartUpdate = true;
294 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
295 aData->attribute(SketchPlugin_Arc::START_ID()));
296 aPoint2->move(theDeltaX, theDeltaY);
298 std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
299 aData->attribute(SketchPlugin_Arc::END_ID()));
300 aPoint3->move(theDeltaX, theDeltaY);
301 myStartUpdate = false;
304 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
305 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
306 aPoint1->move(theDeltaX, theDeltaY);
308 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
309 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
310 aPassedPoint->move(theDeltaX, theDeltaY);
311 aData->blockSendAttributeUpdated(false);
314 bool SketchPlugin_Arc::isFixed() {
315 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
318 bool SketchPlugin_Arc::isFeatureValid()
320 AttributeStringPtr anArcTypeAttr =
321 std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
324 std::string anArcType = anArcTypeAttr->value();
326 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
327 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
328 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
329 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
330 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
331 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
332 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
333 GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
335 bool isValid = false;
336 if (anArcType == ARC_TYPE_THREE_POINTS())
337 isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
339 isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
344 static inline void adjustPeriod(double& theParam)
346 static const double PERIOD = 2.0 * PI;
347 while (theParam < 0.0) theParam += PERIOD;
348 while (theParam >= PERIOD) theParam -= PERIOD;
351 static inline void calculateArcAngleRadius(
352 const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
353 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
354 const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
355 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
356 AttributeDoublePtr theAngleAttr,
357 AttributeDoublePtr theRadiusAttr)
359 double aStartParam, aEndParam, aPassedParam;
360 theCircle->parameter(theStartPoint, paramTolerance, aStartParam);
361 theCircle->parameter(theEndPoint, paramTolerance, aEndParam);
362 theCircle->parameter(thePassedPoint, paramTolerance, aPassedParam);
363 adjustPeriod(aStartParam);
364 adjustPeriod(aEndParam);
365 adjustPeriod(aPassedParam);
367 if (aPassedParam >= aStartParam && aPassedParam <= aEndParam)
368 theAngleAttr->setValue((aEndParam - aStartParam) * 180.0 / PI);
370 theAngleAttr->setValue((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
371 theRadiusAttr->setValue(theCircle->radius());
374 static inline void calculatePassedPoint(
375 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
376 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
377 const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
379 std::shared_ptr<GeomDataAPI_Point2D> thePassedPoint)
381 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
382 theStartPoint->xy()->decreased(theCenter->xy())));
383 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
384 theEndPoint->xy()->decreased(theCenter->xy())));
385 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(
386 aStartDir->xy()->added(aEndDir->xy())));
387 if ((aStartDir->cross(aMidDir) > 0) ^ !theArcReversed)
390 double aRadius = theCenter->distance(theStartPoint);
391 std::shared_ptr<GeomAPI_XY> aPassedPnt = theCenter->xy()->added( aMidDir->xy()->multiplied(aRadius) );
392 thePassedPoint->setValue(aPassedPnt->x(), aPassedPnt->y());
395 void SketchPlugin_Arc::updateDependentAttributes()
397 data()->blockSendAttributeUpdated(true);
399 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
400 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
401 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
402 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
403 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
404 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
405 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
406 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
407 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
408 data()->attribute(RADIUS_ID()));
409 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
410 data()->attribute(ANGLE_ID()));
412 calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
413 isReversed(), aPassedPoint);
414 std::shared_ptr<GeomAPI_Circ2d> aCircle(
415 new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
416 calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
417 anAngleAttr, aRadiusAttr);
418 data()->blockSendAttributeUpdated(false);
422 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
424 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
425 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
426 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
427 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
428 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
429 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
430 // the second condition for unability to move external segments anywhere
431 if (theID == EXTERNAL_ID() || isFixed()) {
432 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
433 // update arguments due to the selection value
434 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
435 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
436 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
438 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
439 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
440 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
446 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
447 data()->attribute(RADIUS_ID()));
448 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
449 data()->attribute(ANGLE_ID()));
451 if (theID == RADIUS_ID()) {
452 if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
454 // move center and passed point
455 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
456 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = anEndAttr->pnt();
457 double aDist = aStartPnt->distance(aEndPnt);
458 if (fabs(aDist) < tolerance)
460 std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy());
461 std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
462 std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->xy()->added(aEndPnt->xy())->multiplied(0.5);
464 double anAngle = anAngleAttr->value() * PI / 180.0;
465 adjustPeriod(anAngle);
467 aMidPerpDir->reverse();
469 double aRadius = aRadiusAttr->value();
470 aDist = sqrt(aRadius * aRadius - aDist * aDist / 4.0);
472 std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
474 data()->blockSendAttributeUpdated(true);
475 aCenterAttr->setValue(aCenter->x(), aCenter->y());
476 updateDependentAttributes();
477 data()->blockSendAttributeUpdated(false);
480 if (theID == ANGLE_ID()) {
481 if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
483 data()->blockSendAttributeUpdated(true);
484 // move end point and passed point
485 std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
486 double anAngle = anAngleAttr->value() * PI / 180.0;
487 double sinA = sin(anAngle);
488 double cosA = cos(anAngle);
489 std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
490 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
491 aStartDir->x() * cosA - aStartDir->y() * sinA,
492 aStartDir->x() * sinA + aStartDir->y() * cosA));
493 anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
498 aDir = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(
499 aStartDir->x() * cosA - aStartDir->y() * sinA,
500 aStartDir->x() * sinA + aStartDir->y() * cosA));
501 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
502 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
503 aPassedPoint->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
505 std::shared_ptr<GeomAPI_Circ2d> aCircle(
506 new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
507 calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
508 anAngleAttr, aRadiusAttr);
509 data()->blockSendAttributeUpdated(false);
513 if (theID == CENTER_ID()) {
514 if (!isFeatureValid())
516 data()->blockSendAttributeUpdated(true);
517 // compute and change the arc end point
518 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
519 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
520 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
521 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
522 anEndAttr->setValue(aProjection);
523 updateDependentAttributes();
524 data()->blockSendAttributeUpdated(false);
528 AttributeStringPtr aTypeAttr =
529 std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
532 std::string anArcType = aTypeAttr->value();
534 // update the points in accordance to the changed point changes
535 if (anArcType == ARC_TYPE_CENTER_START_END()) {
536 if (!isFeatureValid())
538 updateDependentAttributes();
540 else if (anArcType == ARC_TYPE_THREE_POINTS() &&
541 (theID == START_ID() || theID == END_ID() || theID == PASSED_POINT_ID())) {
542 data()->blockSendAttributeUpdated(true);
544 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
545 int aNbInitialized = 0;
546 for (int i = 1; i <= 3; ++i) {
547 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
548 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
549 if (aCurPnt->isInitialized())
550 aPoints[aNbInitialized++] = aCurPnt->pnt();
553 if (aNbInitialized == 1)
554 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
555 else if (aNbInitialized == 2) {
556 // calculate center point, which gives a quarter of circle for the given start and end points
557 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aPoints[0];
558 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aPoints[1];
559 std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy())->multiplied(0.5);
560 double x = aDir->x();
561 double y = aDir->y();
564 std::shared_ptr<GeomAPI_XY> aCenter = aStartPnt->xy()->added(aDir);
565 double aRadius = sqrt(aDir->dot(aDir));
567 aCenterAttr->setValue(aCenter->x(), aCenter->y());
568 aRadiusAttr->setValue(aRadius);
569 anAngleAttr->setValue(90.0);
572 std::shared_ptr<GeomAPI_Circ2d> aCircle(
573 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
575 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
577 aCenterAttr->setValue(aCenter);
578 if (theID == START_ID() || theID == END_ID())
579 updateDependentAttributes();
581 calculateArcAngleRadius(aCircle, aPoints[0], aPoints[1], aPoints[2],
582 anAngleAttr, aRadiusAttr);
586 data()->blockSendAttributeUpdated(false);
588 else if (anArcType == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
589 SketchPlugin_Sketch* aSketch = sketch();
590 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
591 data()->attribute(TANGENT_POINT_ID()));
593 if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
594 data()->blockSendAttributeUpdated(true);
595 // compute orthogonal direction
596 std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
597 std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
598 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
599 std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
600 FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
601 std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
602 aTangFeature->lastResult()->shape());
603 if (aTangEdge->isLine()) {
604 std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
605 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
606 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
607 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
609 else if (aTangEdge->isArc()) {
610 std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
611 std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
612 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
613 new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
616 // compute parameters of the middle perpendicular
617 std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
618 std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
619 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
620 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
621 new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
623 // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
624 std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
625 std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
626 std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
628 aCenterAttr->setValue(aCenter);
629 aStartAttr->setValue(aTangPnt2d);
630 updateDependentAttributes();
633 data()->blockSendAttributeUpdated(false);
635 if (theID == TANGENT_POINT_ID())
636 tangencyArcConstraints();
641 void SketchPlugin_Arc::setReversed(bool isReversed)
643 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
647 bool SketchPlugin_Arc::isReversed()
649 return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
652 void SketchPlugin_Arc::tangencyArcConstraints()
657 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
658 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
659 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
660 attribute(TANGENT_POINT_ID()));
661 if (!aTangPtAttr->attr())
664 FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
665 ObjectPtr aThisArc = aFeature->lastResult();
666 aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
667 ObjectPtr aTangFeature = aFeature->lastResult();
669 // trying to find constraints to fix the tangency of the arc
670 std::set<FeaturePtr> aCoincidence;
671 std::set<FeaturePtr> aTangency;
673 AttributeRefAttrPtr aRefAttrA, aRefAttrB;
674 std::set<AttributePtr> aRefs = data()->refsToMe();
675 const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
676 aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
677 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
678 for (; aRefIt != aRefs.end(); ++aRefIt) {
679 FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
680 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
681 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
682 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
683 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
684 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
685 if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
686 (aRefAttrB && aRefAttrB->attr() == aStartAttr))
687 aCoincidence.insert(aConstrFeature);
689 else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
690 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
691 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
692 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
693 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
694 if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
695 (aRefAttrB && aRefAttrB->object() == aThisArc))
696 aTangency.insert(aConstrFeature);
699 // search applicable pair of constraints
700 bool isFound = false;
701 FeaturePtr aPrevCoincidence, aPrevTangency;
702 std::set<FeaturePtr>::const_iterator aCIt, aTIt;
703 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
704 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
705 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
706 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
707 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
708 AttributePtr anOtherPoint =
709 aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
710 for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
711 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
712 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
713 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
714 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
715 ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
716 aRefAttrB->object() : aRefAttrA->object();
717 if (anOtherPoint->owner() == anOtherObject) {
719 aPrevCoincidence = *aCIt;
720 aPrevTangency = *aTIt;
726 // update previous constraints
727 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
728 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
729 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
730 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
731 if (aRefAttrA->attr() == aStartAttr)
732 aRefAttrB->setAttr(aTangPtAttr->attr());
734 aRefAttrA->setAttr(aTangPtAttr->attr());
736 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
737 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
738 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
739 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
740 if (aRefAttrA->object() == aThisArc)
741 aRefAttrB->setObject(aTangFeature);
743 aRefAttrA->setObject(aTangFeature);
745 // Wait all constraints being removed, then send update events
746 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
747 bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
749 Events_Loop::loop()->setFlushed(aDeleteEvent, false);
750 // Remove all obtained constraints which use current arc, because
751 // there is no information which of them were used to build tangency arc.
752 DocumentPtr aDoc = sketch()->document();
753 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
754 aDoc->removeFeature(*aCIt);
755 for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
756 aDoc->removeFeature(*aTIt);
757 // Send events to update the sub-features by the solver.
759 Events_Loop::loop()->setFlushed(aDeleteEvent, true);
761 Events_Loop::loop()->flush(aDeleteEvent);
763 // Wait all constraints being created, then send update events
764 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
765 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
767 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
769 // Create new constraints
770 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
771 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
772 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
773 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
774 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
775 aRefAttrA->setAttr(aStartAttr);
776 aRefAttrB->setAttr(aTangPtAttr->attr());
777 aConstraint->execute();
778 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
780 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
781 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
782 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
783 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
784 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
785 aRefAttrA->setObject(aThisArc);
786 aRefAttrB->setObject(aTangFeature);
787 aConstraint->execute();
788 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
790 // Send events to update the sub-features by the solver.
792 Events_Loop::loop()->setFlushed(anUpdateEvent, true);