+ aData->blockSendAttributeUpdated(true);
+
+ myStartUpdate = true;
+ myEndUpdate = true;
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Arc::START_ID()));
+ aPoint2->move(theDeltaX, theDeltaY);
+
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Arc::END_ID()));
+ aPoint3->move(theDeltaX, theDeltaY);
+ myStartUpdate = false;
+ myEndUpdate = false;
+
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aData->attribute(SketchPlugin_Arc::CENTER_ID()));
+ aPoint1->move(theDeltaX, theDeltaY);
+
+ std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
+ aPassedPoint->move(theDeltaX, theDeltaY);
+ aData->blockSendAttributeUpdated(false);
+}
+
+bool SketchPlugin_Arc::isFixed() {
+ return data()->selection(EXTERNAL_ID())->context().get() != NULL;
+}
+
+bool SketchPlugin_Arc::isFeatureValid()
+{
+ AttributeStringPtr anArcTypeAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
+ if (!anArcTypeAttr)
+ return false;
+ std::string anArcType = anArcTypeAttr->value();
+
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
+
+ bool isValid = false;
+ if (anArcType == ARC_TYPE_THREE_POINTS())
+ isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
+ else
+ isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
+
+ return isValid;
+}
+
+static inline void adjustPeriod(double& theParam)
+{
+ static const double PERIOD = 2.0 * PI;
+ while (theParam < 0.0) theParam += PERIOD;
+ while (theParam >= PERIOD) theParam -= PERIOD;
+}
+
+static inline void calculateArcAngleRadius(
+ const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
+ const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
+ AttributeDoublePtr theAngleAttr,
+ AttributeDoublePtr theRadiusAttr)
+{
+ double aStartParam, aEndParam, aPassedParam;
+ theCircle->parameter(theStartPoint, paramTolerance, aStartParam);
+ theCircle->parameter(theEndPoint, paramTolerance, aEndParam);
+ theCircle->parameter(thePassedPoint, paramTolerance, aPassedParam);
+ adjustPeriod(aStartParam);
+ adjustPeriod(aEndParam);
+ adjustPeriod(aPassedParam);
+
+ if (aPassedParam >= aStartParam && aPassedParam <= aEndParam)
+ theAngleAttr->setValue((aEndParam - aStartParam) * 180.0 / PI);
+ else
+ theAngleAttr->setValue((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
+ theRadiusAttr->setValue(theCircle->radius());
+}
+
+static inline void calculatePassedPoint(
+ const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
+ const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
+ bool theArcReversed,
+ std::shared_ptr<GeomDataAPI_Point2D> thePassedPoint)
+{
+ std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
+ theStartPoint->xy()->decreased(theCenter->xy())));
+ std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
+ theEndPoint->xy()->decreased(theCenter->xy())));
+ std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
+ if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
+ // start and end directions are opposite, so middle direction will be orthogonal
+ aMidDirXY->setX(-aStartDir->y());
+ aMidDirXY->setY(aStartDir->x());
+ }
+ std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
+ if ((aStartDir->cross(aMidDir) > 0) ^ !theArcReversed)
+ aMidDir->reverse();
+
+ double aRadius = theCenter->distance(theStartPoint);
+ std::shared_ptr<GeomAPI_XY> aPassedPnt = theCenter->xy()->added( aMidDir->xy()->multiplied(aRadius) );
+ thePassedPoint->setValue(aPassedPnt->x(), aPassedPnt->y());
+}
+
+void SketchPlugin_Arc::updateDependentAttributes()
+{
+ data()->blockSendAttributeUpdated(true);
+
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(END_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
+ AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ data()->attribute(RADIUS_ID()));
+ AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ data()->attribute(ANGLE_ID()));
+
+ calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
+ isReversed(), aPassedPoint);
+ std::shared_ptr<GeomAPI_Circ2d> aCircle(
+ new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
+ calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
+ anAngleAttr, aRadiusAttr);
+ data()->blockSendAttributeUpdated(false);
+}
+
+
+void SketchPlugin_Arc::attributeChanged(const std::string& theID)
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(END_ID()));
+ // the second condition for unability to move external segments anywhere
+ if (theID == EXTERNAL_ID() || isFixed()) {
+ std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+ // update arguments due to the selection value
+ if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
+ std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+ std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
+ if (aCirc.get()) {
+ aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
+ anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
+ aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
+ }
+ }
+ return;
+ }
+
+ AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ data()->attribute(RADIUS_ID()));
+ AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ data()->attribute(ANGLE_ID()));
+
+ if (theID == RADIUS_ID()) {
+ if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
+ return;
+ // move center and passed point
+ std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
+ std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = anEndAttr->pnt();
+ double aDist = aStartPnt->distance(aEndPnt);
+ if (fabs(aDist) < tolerance)
+ return;
+ std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy());
+ std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
+ std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->xy()->added(aEndPnt->xy())->multiplied(0.5);
+
+ double anAngle = anAngleAttr->value() * PI / 180.0;
+ adjustPeriod(anAngle);
+ if (anAngle > PI)
+ aMidPerpDir->reverse();
+
+ double aRadius = aRadiusAttr->value();
+ aDist = sqrt(aRadius * aRadius - aDist * aDist / 4.0);
+
+ std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
+
+ data()->blockSendAttributeUpdated(true);
+ aCenterAttr->setValue(aCenter->x(), aCenter->y());
+ updateDependentAttributes();
+ data()->blockSendAttributeUpdated(false);
+ return;
+ }
+ if (theID == ANGLE_ID()) {
+ if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
+ return;
+ data()->blockSendAttributeUpdated(true);
+ // move end point and passed point
+ std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
+ double anAngle = anAngleAttr->value() * PI / 180.0;
+ double sinA = sin(anAngle);
+ double cosA = cos(anAngle);
+ std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
+ std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
+ aStartDir->x() * cosA - aStartDir->y() * sinA,
+ aStartDir->x() * sinA + aStartDir->y() * cosA));
+ anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());