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>
22 #include <ModelAPI_Tools.h>
24 #include <GeomAPI_Ax2.h>
25 #include <GeomAPI_Circ2d.h>
26 #include <GeomAPI_Circ.h>
27 #include <GeomAPI_Dir2d.h>
28 #include <GeomAPI_Dir.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomAPI_Pnt2d.h>
32 #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>
41 const double tolerance = 1e-7;
42 const double paramTolerance = 1.e-4;
43 const double PI =3.141592653589793238463;
46 static const std::string& POINT_ID(int theIndex)
49 case 1: return SketchPlugin_Arc::START_ID();
50 case 2: return SketchPlugin_Arc::END_ID();
51 case 3: return SketchPlugin_Arc::PASSED_POINT_ID();
54 static const std::string DUMMY;
61 SketchPlugin_Arc::SketchPlugin_Arc()
62 : SketchPlugin_SketchEntity()
64 myStartUpdate = false;
73 void SketchPlugin_Arc::initDerivedClassAttributes()
75 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
76 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
77 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
78 GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
79 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
82 AttributeBooleanPtr isInversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
83 data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
85 // get the initial values
86 if (anEndAttr->isInitialized()) {
87 myXEndBefore = anEndAttr->x();
88 myYEndBefore = anEndAttr->y();
91 AttributeStringPtr anArcType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
92 data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId()));
94 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
95 data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
96 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
97 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
99 // set after all to avoid in attributeChanged reference to not existing attributes
100 if (!isInversed->isInitialized())
101 isInversed->setValue(false);
102 anArcType->setValue(ARC_TYPE_CENTER_START_END());
105 void SketchPlugin_Arc::execute()
107 SketchPlugin_Sketch* aSketch = sketch();
108 // result for the arc is set only when all obligatory attributes are initialized,
109 // otherwise AIS object is used to visualize the arc's preview
110 if (aSketch && isFeatureValid()) {
111 bool hasResult = lastResult().get() != NULL;
113 // compute a circle point in 3D view
114 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
115 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
116 // compute the arc start point
117 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
118 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
120 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
121 // make a visible point
122 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
123 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
125 aConstr1->setShape(aCenterPointShape);
126 aConstr1->setIsInHistory(false);
127 setResult(aConstr1, 0);
129 // make a visible circle
130 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
131 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
132 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
133 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
135 // compute and change the arc end point
136 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
137 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
138 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
139 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
140 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
141 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
142 anEndAttr->setValue(aProjection);
143 std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
144 AttributeBooleanPtr isInversed =
145 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
147 std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
148 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
149 std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
150 double aParameterNew = 0.0;
151 if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
152 if(0 <= myParamBefore && myParamBefore <= PI / 2.0
153 && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
154 isInversed->setValue(true);
155 } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
156 && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
157 isInversed->setValue(false);
160 myParamBefore = aParameterNew;
162 std::shared_ptr<GeomAPI_Shape> aCircleShape;
163 if(!isInversed->value()) {
164 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
166 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
170 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
172 aConstr2->setShape(aCircleShape);
173 aConstr2->setIsInHistory(false);
174 setResult(aConstr2, 1);
179 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
181 SketchPlugin_Sketch* aSketch = sketch();
183 // if the feature is valid, the execute() method should be performed, AIS object is empty
184 if (!isFeatureValid()) {
185 // compute a circle point in 3D view
186 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
187 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
189 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
190 if (aCenterAttr->isInitialized()) {
191 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
193 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
194 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
195 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<
196 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
197 AttributeStringPtr aTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
198 data()->attribute(ARC_TYPE()));
200 if (aStartAttr->isInitialized()) {
201 // make a visible circle
202 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
203 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
204 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
206 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
207 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
208 std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
209 if (aTypeAttr && aTypeAttr->isInitialized() &&
210 aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized() &&
211 aEndAttr->pnt()->distance(aStartAttr->pnt()) > tolerance) {
212 aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
213 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
214 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
215 if (!aPassedAttr->isInitialized()) { // calculate the appropriate center for the presentation
216 std::shared_ptr<GeomAPI_XY> aDir =
217 aEndAttr->pnt()->xy()->decreased(aStartAttr->pnt()->xy())->multiplied(0.5);
218 double x = aDir->x();
219 double y = aDir->y();
222 std::shared_ptr<GeomAPI_XY> aCenterXY = aStartAttr->pnt()->xy()->added(aDir);
223 aCenter = aSketch->to3D(aCenterXY->x(), aCenterXY->y());
227 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
228 aCenter, aStartPoint, aEndPoint, aNormal);
230 aShapes.push_back(aCircleShape);
233 // make a visible point
234 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
235 aShapes.push_back(aCenterPointShape);
237 if (!aShapes.empty()) {
238 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
239 AISObjectPtr anAIS = thePrevious;
241 anAIS = AISObjectPtr(new GeomAPI_AISObject);
242 anAIS->createShape(aCompound);
248 return AISObjectPtr();
251 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
253 std::shared_ptr<ModelAPI_Data> aData = data();
254 if (!aData->isValid())
257 aData->blockSendAttributeUpdated(true);
259 myStartUpdate = true;
261 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
262 aData->attribute(SketchPlugin_Arc::START_ID()));
263 aPoint2->move(theDeltaX, theDeltaY);
265 std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
266 aData->attribute(SketchPlugin_Arc::END_ID()));
267 aPoint3->move(theDeltaX, theDeltaY);
268 myStartUpdate = false;
271 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
273 aPoint1->move(theDeltaX, theDeltaY);
275 std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
276 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
277 aPassedPoint->move(theDeltaX, theDeltaY);
278 aData->blockSendAttributeUpdated(false);
281 bool SketchPlugin_Arc::isFixed() {
282 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
285 bool SketchPlugin_Arc::isFeatureValid()
287 AttributeStringPtr anArcTypeAttr =
288 std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
291 std::string anArcType = anArcTypeAttr->value();
293 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
294 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
295 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
296 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
297 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
298 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
299 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
300 GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
302 bool isValid = false;
303 if (anArcType == ARC_TYPE_THREE_POINTS())
304 isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
306 isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
311 static inline void adjustPeriod(double& theParam)
313 static const double PERIOD = 2.0 * PI;
314 while (theParam < 0.0) theParam += PERIOD;
315 while (theParam >= PERIOD) theParam -= PERIOD;
318 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
320 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
321 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
322 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
323 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
324 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
325 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
326 // the second condition for unability to move external segments anywhere
327 if (theID == EXTERNAL_ID() || isFixed()) {
328 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
329 // update arguments due to the selection value
330 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
331 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
332 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
334 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
335 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
336 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
341 AttributeStringPtr aTypeAttr =
342 std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
344 // this is before others since here end attribute may be changed, but with the special behavior
345 if (aTypeAttr->value() == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
346 SketchPlugin_Sketch* aSketch = sketch();
347 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
348 data()->attribute(TANGENT_POINT_ID()));
350 if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
351 // compute orthogonal direction
352 std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
353 std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
354 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
355 std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
356 FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
357 std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
358 aTangFeature->lastResult()->shape());
359 if (aTangEdge->isLine()) {
360 std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
361 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
362 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
363 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
365 else if (aTangEdge->isArc()) {
366 std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
367 std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
368 anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
369 new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
372 // compute parameters of the middle perpendicular
373 std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
374 std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
375 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
376 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
377 new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
379 // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
380 std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
381 std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
382 std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
384 data()->blockSendAttributeUpdated(true);
385 aCenterAttr->setValue(aCenter);
386 aStartAttr->setValue(aTangPnt2d);
387 data()->blockSendAttributeUpdated(false);
390 tangencyArcConstraints();
395 // if changed the base attributes, update all other (is necessary) without recursion
396 if (theID == CENTER_ID() || theID == START_ID() || theID == END_ID() || theID == ARC_TYPE()) {
397 if (!isFeatureValid())
399 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
400 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
401 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
402 double aRadius = aCenter->distance(aStart);
403 if (aRadius < tolerance)
405 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
407 data()->blockSendAttributeUpdated(true);
408 if (theID == END_ID() && isStable()) {
409 // The arc is under construction, so its end point projected
410 // on the circle formed by center and start points
411 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
412 if (aProjection && anEnd->distance(aProjection) > tolerance) {
413 anEndAttr->setValue(aProjection);
417 // update all other attributes due to the base attributes values
418 if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // update passed point due to start, end and center
419 if (aCenter->distance(aStart) > tolerance && aCenter->distance(anEnd) > tolerance) {
420 // project passed point t othe circle
421 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
422 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
423 if (aPassedAttr->isInitialized()) {
424 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aPassedAttr->pnt());
425 if (aProjection && aPassedAttr->pnt()->distance(aProjection) > tolerance) {
426 aPassedAttr->setValue(aProjection);
428 } else { // initialize it by some middle - value
429 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
430 aStart->xy()->decreased(aCenter->xy())));
431 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
432 anEnd->xy()->decreased(aCenter->xy())));
433 std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
434 if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
435 // start and end directions are opposite, so middle direction will be orthogonal
436 aMidDirXY->setX(-aStartDir->y());
437 aMidDirXY->setY(aStartDir->x());
439 std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
440 if ((aStartDir->cross(aMidDir) > 0) ^ !isReversed())
442 std::shared_ptr<GeomAPI_XY> aPassedPnt =
443 aCenter->xy()->added(aMidDir->xy()->multiplied(aCenter->distance(aStart)));
444 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()))->
445 setValue(aPassedPnt->x(), aPassedPnt->y());
449 // update radius and angle
450 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
451 data()->attribute(RADIUS_ID()));
452 aRadiusAttr->setValue(aRadius);
453 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
454 data()->attribute(ANGLE_ID()));
455 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aStart));
456 double aStartParam, aEndParam;
457 aCircle->parameter(aStart, paramTolerance, aStartParam);
458 aCircle->parameter(anEnd, paramTolerance, aEndParam);
459 adjustPeriod(aStartParam);
460 adjustPeriod(aEndParam);
461 if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // use the passed point for the angle calculation
462 std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
463 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
465 aCircle->parameter(aPassedAttr->pnt(), paramTolerance, aPassedParam);
466 adjustPeriod(aPassedParam);
467 double aNewAngle = aPassedParam >= aStartParam && aPassedParam <= aEndParam ?
468 ((aEndParam - aStartParam) * 180.0 / PI) :
469 ((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
470 if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
471 anAngleAttr->setValue(aNewAngle);
473 double aNewAngle = (aEndParam - aStartParam) * 180.0 / PI;
474 if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
475 anAngleAttr->setValue(aNewAngle);
477 // do not need to inform that other parameters were changed in this basis mode: these arguments
479 data()->blockSendAttributeUpdated(false, false);
483 if (theID == PASSED_POINT_ID()) {
484 data()->blockSendAttributeUpdated(true);
486 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
487 int aNbInitialized = 0;
488 for (int i = 1; i <= 3; ++i) {
489 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
490 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
491 if (aCurPnt->isInitialized())
492 aPoints[aNbInitialized++] = aCurPnt->pnt();
495 if (aNbInitialized == 3) {
496 std::shared_ptr<GeomAPI_Circ2d> aCircle(
497 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
499 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
501 aCenterAttr->setValue(aCenter);
504 data()->blockSendAttributeUpdated(false);
508 if (theID == RADIUS_ID()) {
509 if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized() || !aCenterAttr->isInitialized())
511 // move center and passed point
512 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
513 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
514 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
515 if (aStart->distance(aCenter) < tolerance || anEnd->distance(aCenter) < tolerance)
517 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
518 data()->attribute(RADIUS_ID()));
519 double aRadius = aRadiusAttr->value();
521 data()->blockSendAttributeUpdated(true);
522 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
523 std::shared_ptr<GeomAPI_XY> aNewStart = aStartDir->xy()->multiplied(aRadius)->added(aCenter->xy());
524 aStartAttr->setValue(aNewStart->x(), aNewStart->y());
525 std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEnd->xy()->decreased(aCenter->xy())));
526 std::shared_ptr<GeomAPI_XY> aNewEnd = anEndDir->xy()->multiplied(aRadius)->added(aCenter->xy());
527 anEndAttr->setValue(aNewEnd->x(), aNewEnd->y());
528 data()->blockSendAttributeUpdated(false);
531 if (theID == ANGLE_ID()) {
532 if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
534 AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
535 data()->attribute(ANGLE_ID()));
536 data()->blockSendAttributeUpdated(true);
537 // move end point and passed point
538 std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
539 double anAngle = anAngleAttr->value() * PI / 180.0;
540 double sinA = sin(anAngle);
541 double cosA = cos(anAngle);
542 std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
543 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
544 aStartDir->x() * cosA - aStartDir->y() * sinA,
545 aStartDir->x() * sinA + aStartDir->y() * cosA));
546 anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
547 data()->blockSendAttributeUpdated(false);
552 void SketchPlugin_Arc::setReversed(bool isReversed)
554 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
558 bool SketchPlugin_Arc::isReversed()
560 return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
563 void SketchPlugin_Arc::tangencyArcConstraints()
568 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
569 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
570 AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
571 attribute(TANGENT_POINT_ID()));
572 if (!aTangPtAttr->attr())
575 FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
576 ObjectPtr aThisArc = aFeature->lastResult();
577 aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
578 ObjectPtr aTangFeature = aFeature->lastResult();
580 // trying to find constraints to fix the tangency of the arc
581 std::set<FeaturePtr> aCoincidence;
582 std::set<FeaturePtr> aTangency;
584 AttributeRefAttrPtr aRefAttrA, aRefAttrB;
585 std::set<AttributePtr> aRefs = data()->refsToMe();
586 const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
587 aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
588 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
589 for (; aRefIt != aRefs.end(); ++aRefIt) {
590 FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
591 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
592 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
593 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
594 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
595 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
596 if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
597 (aRefAttrB && aRefAttrB->attr() == aStartAttr))
598 aCoincidence.insert(aConstrFeature);
600 else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
601 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
602 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
603 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
604 aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
605 if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
606 (aRefAttrB && aRefAttrB->object() == aThisArc))
607 aTangency.insert(aConstrFeature);
610 // search applicable pair of constraints
611 bool isFound = false;
612 FeaturePtr aPrevCoincidence, aPrevTangency;
613 std::set<FeaturePtr>::const_iterator aCIt, aTIt;
614 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
615 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
616 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
617 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
618 (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
619 AttributePtr anOtherPoint =
620 aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
621 for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
622 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
623 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
624 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
625 (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
626 ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
627 aRefAttrB->object() : aRefAttrA->object();
628 FeaturePtr anOtherFeature = ModelAPI_Feature::feature(anOtherObject);
629 if (anOtherPoint->owner() == anOtherFeature) {
631 aPrevCoincidence = *aCIt;
632 aPrevTangency = *aTIt;
638 // update previous constraints
639 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
640 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
641 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
642 aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
643 if (aRefAttrA->attr() == aStartAttr)
644 aRefAttrB->setAttr(aTangPtAttr->attr());
646 aRefAttrA->setAttr(aTangPtAttr->attr());
648 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
649 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
650 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
651 aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
652 if (aRefAttrA->object() == aThisArc)
653 aRefAttrB->setObject(aTangFeature);
655 aRefAttrA->setObject(aTangFeature);
657 // Wait all constraints being removed, then send update events
658 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
659 bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
661 Events_Loop::loop()->setFlushed(aDeleteEvent, false);
662 // Remove all obtained constraints which use current arc, because
663 // there is no information which of them were used to build tangency arc.
664 DocumentPtr aDoc = sketch()->document();
665 std::set<FeaturePtr> aFeaturesToBeRemoved;
666 for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
667 aFeaturesToBeRemoved.insert(*aCIt);
668 for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
669 aFeaturesToBeRemoved.insert(*aTIt);
670 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
671 // Send events to update the sub-features by the solver.
673 Events_Loop::loop()->setFlushed(aDeleteEvent, true);
675 Events_Loop::loop()->flush(aDeleteEvent);
677 // Wait all constraints being created, then send update events
678 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
679 bool isCreateFlushed = Events_Loop::loop()->isFlushed(aCreateEvent);
681 Events_Loop::loop()->setFlushed(aCreateEvent, false);
683 // Create new constraints
684 FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
685 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
686 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
687 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
688 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
689 aRefAttrA->setAttr(aStartAttr);
690 aRefAttrB->setAttr(aTangPtAttr->attr());
691 aConstraint->execute();
692 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
694 aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
695 aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
696 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
697 aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
698 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
699 aRefAttrA->setObject(aThisArc);
700 aRefAttrB->setObject(aTangFeature);
701 aConstraint->execute();
702 ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
704 // Send events to update the sub-features by the solver.
706 Events_Loop::loop()->setFlushed(aCreateEvent, true);
708 Events_Loop::loop()->flush(aCreateEvent);