+
+bool SketchPlugin_Ellipse::fillCharacteristicPoints()
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
+
+ AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
+ AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
+
+ if (!aCenterAttr->isInitialized() ||
+ !aFocusAttr->isInitialized() ||
+ !aMajorRadiusAttr->isInitialized() ||
+ !aMinorRadiusAttr->isInitialized()) {
+ return false;
+ }
+
+ double aMajorRadius = aMajorRadiusAttr->value();
+ double aMinorRadius = aMinorRadiusAttr->value();
+ if (aMajorRadius < tolerance || aMinorRadius < tolerance) {
+ return false;
+ }
+
+ data()->blockSendAttributeUpdated(true);
+ GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
+ GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
+ GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
+ aFocus2d->y() - aCenter2d->y()));
+ GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
+ ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
+ ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
+ aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
+ ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
+ aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
+ ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
+ aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
+ ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
+ aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
+ data()->blockSendAttributeUpdated(false);
+
+ return true;
+}
+
+void SketchPlugin_Ellipse::createEllipse(SketchPlugin_Sketch* theSketch, const int theResultIndex)
+{
+ // Compute a ellipse in 3D view.
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
+
+ double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
+ double aMinorRadius = real(MINOR_RADIUS_ID())->value();
+
+ std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
+
+ GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
+ GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
+ GeomDirPtr aNormal = aNDir->dir();
+ std::shared_ptr<GeomAPI_Shape> anEllipseShape;
+ if (aFocus->distance(aCenter) > tolerance) {
+ GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
+ aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
+
+ anEllipseShape =
+ GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
+ }
+ else {
+ // build circle instead of ellipse
+ anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
+ }
+
+ ResultConstructionPtr aResult = document()->createConstruction(data(), theResultIndex);
+ aResult->setShape(anEllipseShape);
+ aResult->setIsInHistory(false);
+ setResult(aResult, theResultIndex);
+}