void SketchPlugin_ConstraintAngle::execute()
{
std::shared_ptr<ModelAPI_Data> aData = data();
- AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
- aData->attribute(SketchPlugin_Constraint::VALUE()));
- if(anAttrValue->isInitialized())
+ std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
+ std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
+ if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
return;
- double anAngle = calculateAngle();
- anAttrValue->setValue(anAngle);
+ AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aData->attribute(SketchPlugin_Constraint::VALUE()));
+
+ if (!anAttrValue->isInitialized()) {
+ double anAngle = calculateAngle();
+ anAttrValue->setValue(anAngle);
+ }
+ // 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());
}
AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
{
- if (theID == SketchPlugin_Constraint::ENTITY_A() ||
- theID == SketchPlugin_Constraint::ENTITY_B()) {
- std::shared_ptr<ModelAPI_Data> aData = data();
- if (!aData)
- return;
+ std::shared_ptr<ModelAPI_Data> aData = data();
+ if (!aData)
+ return;
FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
- if (!aLineA || !aLineB)
- return;
+ if (!aLineA || !aLineB)
+ return;
+ if (theID == SketchPlugin_Constraint::ENTITY_A() ||
+ theID == SketchPlugin_Constraint::ENTITY_B()) {
std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
}
} else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
// Recalculate flyout point in local coordinates
+ // coordinates are calculated according to the center of shapes intersection
std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
}
aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
myFlyoutUpdate = false;
}
+
+
+bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
+{
+ if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
+ return false;
+ if (!sketch())
+ return false;
+
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(attribute(theAttributeId));
+ if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
+ return false;
+
+ DataPtr aData = data();
+ std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
+ FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+ FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
+
+ if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
+ return false;
+
+ // Start and end points of lines
+ std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aLineA->attribute(SketchPlugin_Line::START_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aLineA->attribute(SketchPlugin_Line::END_ID()));
+
+ std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
+ std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
+ if (aStartA->distance(aEndA) < tolerance)
+ return false;
+
+ myFlyoutUpdate = true;
+ double aX = (aStartA->x() + aEndA->x()) / 2.;
+ double aY = (aStartA->y() + aEndA->y()) / 2.;
+
+ aFlyOutAttr->setValue(aX, aY);
+ myFlyoutUpdate = false;
+
+ return true;
+}
\ No newline at end of file
<doublevalue_editor label="Value" tooltip="Radius" id="ConstraintValue" default="computed"/>
<validator id="PartSet_RadiusSelection"/>
</feature>
-
- <!-- SketchConstraintParallel -->
+
+ <!-- SketchConstraintAngle -->
+ <feature id="SketchConstraintAngle" title="Angle" tooltip="Set fixed angle between two line segments" icon=":icons/angle_constr.png">
+ <shape_selector id="ConstraintEntityA" label="Line" tooltip="Select an line" shape_types="edge" >
+ <validator id="GeomValidators_ShapeType" parameters="line"/>
+ <validator id="PartSet_DifferentObjects"/>
+ <validator id="SketchPlugin_ExternalValidator" parameters="ConstraintEntityB"/>
+ </shape_selector>
+ <shape_selector id="ConstraintEntityB" label="Line" tooltip="Select an line" shape_types="edge" >
+ <validator id="GeomValidators_ShapeType" parameters="line"/>
+ <validator id="PartSet_DifferentObjects"/>
+ <validator id="SketchPlugin_ExternalValidator" parameters="ConstraintEntityA"/>
+ </shape_selector>
+ <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
+ <doublevalue_editor label="Value" tooltip="Angle" id="ConstraintValue" default="computed" />
+ <validator id="PartSet_AngleSelection"/>
+ </feature>
+
+ <!-- SketchConstraintParallel -->
<feature id="SketchConstraintParallel" title="Parallel" tooltip="Create constraint defining two parallel lines" icon=":icons/parallel.png">
<sketch_shape_selector id="ConstraintEntityA"
label="First line" tooltip="Select a line" shape_types="edge">
</sketch_shape_selector>
<validator id="PartSet_TangentSelection"/>
</feature>
-
-
- <!-- SketchConstraintAngle -->
- <feature id="SketchConstraintAngle" title="Angle" tooltip="Set fixed angle between two line segments">
- <shape_selector id="ConstraintEntityA" label="Line" tooltip="Select an line" shape_types="edge" >
- <validator id="GeomValidators_ShapeType" parameters="line"/>
- <validator id="PartSet_DifferentObjects"/>
- <validator id="SketchPlugin_ExternalValidator" parameters="ConstraintEntityB"/>
- </shape_selector>
- <shape_selector id="ConstraintEntityB" label="Line" tooltip="Select an line" shape_types="edge" >
- <validator id="GeomValidators_ShapeType" parameters="line"/>
- <validator id="PartSet_DifferentObjects"/>
- <validator id="SketchPlugin_ExternalValidator" parameters="ConstraintEntityA"/>
- </shape_selector>
- <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
- <doublevalue_editor label="Value" tooltip="Angle" id="ConstraintValue" default="computed" />
- <validator id="PartSet_AngleSelection"/>
- </feature>
-
</group>