- if (!anAttr_Value->isInitialized()) {
- boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
- getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
- boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
- getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
-
- if (aPoint_A && aPoint_B) { // both points
- anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
- } else {
- if (!aPoint_A && aPoint_B) { //Line and point
- boost::shared_ptr<SketchPlugin_Line> aLine =
- getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
- if (aLine)
- anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
- } else if (aPoint_A && !aPoint_B) { // Point and line
- boost::shared_ptr<SketchPlugin_Line> aLine =
- getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
- if(aLine)
- anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
- }
+ if(anAttrValue->isInitialized())
+ return;
+
+ double aDistance = calculateCurrentDistance();
+ if(aDistance >= 0) {
+ anAttrValue->setValue(aDistance);
+ }
+
+ // 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>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+ if(!aFlyOutAttr->isInitialized())
+ compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+}
+
+bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
+{
+ if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
+ return false;
+
+ if (!sketch())
+ return false;
+
+ DataPtr aData = data();
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
+ aData, SketchPlugin_Constraint::ENTITY_A());
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
+ aData, SketchPlugin_Constraint::ENTITY_B());
+
+ std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
+ std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
+
+ if (aPoint_A && aPoint_B) {
+ aPnt_A = aPoint_A->pnt();
+ aPnt_B = aPoint_B->pnt();
+ } else if (!aPoint_A && aPoint_B) {
+ std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
+ aData, SketchPlugin_Constraint::ENTITY_A());
+ if (aLine) {
+ aPnt_B = aPoint_B->pnt();
+ aPnt_A = getProjectionPoint(aLine, aPnt_B);
+ }
+ } else if (aPoint_A && !aPoint_B) {
+ std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
+ aData, SketchPlugin_Constraint::ENTITY_B());
+ if (aLine) {
+ aPnt_A = aPoint_A->pnt();
+ aPnt_B = getProjectionPoint(aLine, aPnt_A);