+ std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
+ if (aFeature) {
+ double aRadius = 0;
+ std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
+ if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
+ AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
+ if (anAttribute)
+ aRadius = anAttribute->value();
+ } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
+ if (aCenterAttr && aStartAttr)
+ aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
+ }
+ //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+ // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+ //if(!aValueAttr->isInitialized()) {
+ // aValueAttr->setValue(aRadius);
+ //}
+
+ // the value should to be computed here, not in the getAISObject in order to change the model value
+ // inside the object transaction. This is important for creating a constraint by preselection.
+ // The display of the presentation in this case happens after the transaction commit
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+ if (!aFlyoutAttr->isInitialized())
+ compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+ }
+}
+
+bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
+{
+ if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
+ return false;
+
+ std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
+ double aRadius = circleRadius(aCyrcFeature);
+ if (aRadius < 0)
+ return false;
+
+ // Flyout point
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
+ // Prepare a circle
+ if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
+ double aShift = aRadius * 1.1;
+ std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
+ std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
+ std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
+ aFlyoutAttr->setValue(aFPnt);
+ } else { // arc
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
+ aFlyoutAttr->setValue(aStartAttr->pnt());
+ }
+ return true;
+}
+
+double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
+{
+ static const double kErrorResult = -1.;
+ if (!sketch())
+ return kErrorResult;
+
+ std::shared_ptr<ModelAPI_Data> aData = data();
+ std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
+ ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+ if (!anAttr)
+ return kErrorResult;
+ theCirc = ModelAPI_Feature::feature(anAttr->object());
+ std::string aKind = theCirc ? theCirc->getKind() : "";
+ if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
+ return kErrorResult;
+
+ DataPtr aCircData = theCirc->data();
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
+ if (aKind == SketchPlugin_Circle::ID()) {
+ AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
+ return aCircRadius->value();
+ } else {
+ aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
+ return aCenterAttr->pnt()->distance(aStartAttr->pnt());
+ }
+ return kErrorResult;