Compute default value by moving the mouse of the viewer. Check that the default value for the creation a constraint by preselection is computed.
/// Computes or recomputes the results
virtual void execute() = 0;
+ /// Computes the attribute value on the base of other attributes if the value can be computed
+ /// \param theAttributeId an attribute index to be computed
+ /// \return a boolean value about it is computed
+ virtual bool compute(const std::string& theAttributeId) { return false; };
+
/// Registers error during the execution, causes the ExecutionFailed state
virtual void setError(const std::string& theError) {
data()->setError(theError);
myParentId(theParentId)
{
myDefaultValue = theData->getProperty(ATTR_DEFAULT);
- myIsComputedDefault = false;
+ myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
myAttributeID = theData ? theData->widgetId() : "";
connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
void ModuleBase_ModelWidget::activate()
{
- if (!isEditingMode()) {
- // the control value is stored to the mode by the focus in on the widget
- // we need the value is initialized in order to enable the apply button in the property panel
- // it should happens only in the creation mode because during edition all fields are filled
- storeValue();
+ // the control value is stored to the mode by the focus in on the widget
+ // we need the value is initialized in order to enable the apply button in the property panel.
+ // It should happens in the creation mode only because all fields are filled in the edition mode
+ if (!isEditingMode()/* && !myFeature->data()->attribute(myAttributeID)->isInitialized()*/) {
+ if (isComputedDefault()) {
+ if (myFeature->compute(myAttributeID)) {
+ restoreValue();
+ }
+ }
+ else {
+ storeValue();
+ }
}
activateCustom();
}
/// Returns true, if default value of the widget should be computed
/// on operation's execute, like radius for circle's constraint (can not be zero)
- bool isComputedDefault() { return myIsComputedDefault; }
+ bool isComputedDefault() const { return myIsComputedDefault; }
/// Returns true, if default value of the widget is defined in the XML and it is not the
/// computed value
/// \return the boolean result
- bool isValueDefault() { return !myDefaultValue.empty(); }
+ std::string getDefaultValue() const { return myDefaultValue; }
/// Defines if it is supposed that the widget should interact with the viewer.
virtual bool isViewerSelector() { return false; }
/// A feature which is processing by active operation
FeaturePtr myFeature;
+ /// Flag which shows that current operation is in editing mode
+ bool myIsEditing;
+
+private:
/// Value should be computed on execute, like radius for circle's constraint (can not be zero)
bool myIsComputedDefault;
/// the default value, which is defined in the XML for this attribute
std::string myDefaultValue;
-
- /// Flag which shows that current operation is in editing mode
- bool myIsEditing;
};
#endif
mySpinBox->setSingleStep(aStepVal);
}
- double aDefVal = QString::fromStdString(myDefaultValue).toDouble(&isOk);
+ double aDefVal = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
if (isOk) {
mySpinBox->setValue(aDefVal);
- } else if (theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED){
- myIsComputedDefault = true;
}
QString aTTip = QString::fromStdString(theData->widgetTooltip());
void ModuleBase_WidgetDoubleValue::reset()
{
- if (myIsComputedDefault)
+ if (isComputedDefault()) {
return;
-
- bool isOk;
- double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
- ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
+ //if (myFeature->compute(myAttributeID))
+ // restoreValue();
+ }
+ else {
+ bool isOk;
+ double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
+ ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
+ }
}
bool ModuleBase_WidgetDoubleValue::storeValue() const
void PartSet_WidgetPoint2D::reset()
{
- bool isOk;
- double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
- // it is important to block the spin box control in order to do not through out the
- // locking of the validating state.
- ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
- ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
+ if (isComputedDefault()) {
+ //return;
+ if (myFeature->compute(myAttributeID))
+ restoreValue();
+ }
+ else {
+ bool isOk;
+ double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
+ // it is important to block the spin box control in order to do not through out the
+ // locking of the validating state.
+ ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
+ ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
+ }
}
PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
void PartSet_WidgetPoint2dDistance::reset()
{
bool isOk;
- double aDefValue = QString::fromStdString(myDefaultValue).toDouble(&isOk);
+ double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
}
}
}
+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);
+ }
+ }
+ if (!aPnt_A || !aPnt_B)
+ return false;
+
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
+
+ std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
+ std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
+ std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
+ double aDist = aPoint1->distance(aPoint2)/5.;
+ std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
+ aFlyOutAttr->setValue(aFPnt);
+
+ return true;
+}
+
//*************************************************************************************
AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
{
std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
- std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
- if(aFlyOutAttr->isInitialized()) {
- aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
- } else {
- std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
- double aDist = aPoint1->distance(aPoint2)/5.;
- std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
- aFlyOutAttr->setValue(aFPnt);
- aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
- }
+ if(!aFlyOutAttr->isInitialized())
+ compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+ std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()/*aFPnt->x(), aFPnt->y()*/);
+
// value calculation
std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
/// \brief Creates a new part document if needed
SKETCHPLUGIN_EXPORT virtual void execute();
+ /// Computes the attribute value on the base of other attributes if the value can be computed
+ /// \param theAttributeId an attribute index to be computed
+ /// \return a boolean value about it is computed
+ SKETCHPLUGIN_EXPORT virtual bool compute(const std::string& theAttributeId);
+
/// \brief Request for initialization of data model of the feature: adding all attributes
SKETCHPLUGIN_EXPORT virtual void initAttributes();
}
}
+bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
+{
+ if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
+ return false;
+
+ std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
+ std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
+ if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
+ return false;
+
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
+
+ std::shared_ptr<GeomAPI_Lin2d> aLine =
+ std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
+ double aDist = aPoint1->distance(aPoint2)/5.;
+ std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
+ aFlyOutAttr->setValue(aFPnt);
+
+ return true;
+}
+
bool SketchPlugin_ConstraintLength::getPoints(
std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
return thePrevious; // not possible to show length because points are not defined
- std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
- std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
- std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
- if (aFlyOutAttr->isInitialized()) {
- aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
- } else {
- std::shared_ptr<GeomAPI_Lin2d> aLine =
- std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
- double aDist = aPoint1->distance(aPoint2)/5.;
- std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
- aFlyOutAttr->setValue(aFPnt);
- aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
+ AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+ if (!aFlyOutAttribute->isInitialized()) {
+ if (!compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))
+ return thePrevious; // not possible to show length because points are not defined
}
+ std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(aFlyOutAttribute);
+ std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
// value calculation
// TODO: has to be calculated on definition of reference object
double aDistance = aPoint1->distance(aPoint2);
AISObjectPtr anAIS = thePrevious;
if (!anAIS)
anAIS = AISObjectPtr(new GeomAPI_AISObject);
+ std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
// Set color from preferences
/// \brief Creates a new part document if needed
SKETCHPLUGIN_EXPORT virtual void execute();
+ /// Computes the attribute value on the base of other attributes if the value can be computed
+ /// \param theAttributeId an attribute index to be computed
+ /// \return a boolean value about it is computed
+ SKETCHPLUGIN_EXPORT virtual bool compute(const std::string& theAttributeId);
+
/// \brief Request for initialization of data model of the feature: adding all attributes
SKETCHPLUGIN_EXPORT virtual void initAttributes();
}
}
+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.;
// Flyout point
std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
- std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt;
- if (aFlyoutAttr->isInitialized()) {
- aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
- }
+ if (!aFlyoutAttr->isInitialized() && !compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))
+ return thePrevious; // can not create a good presentation
+ std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
// Prepare a circle
std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
- if (!aFlyoutPnt) {
- 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);
- aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
- }
} else { // arc
aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
- if (!aFlyoutPnt) {
- std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
- GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
- aFlyoutAttr->setValue(aStartAttr->pnt());
- aFlyoutPnt = sketch()->to3D(aStartAttr->pnt()->x(), aStartAttr->pnt()->y());
- }
}
-
std::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
/// \brief Creates a new part document if needed
SKETCHPLUGIN_EXPORT virtual void execute();
+ /// Computes the attribute value on the base of other attributes if the value can be computed
+ /// \param theAttributeId an attribute index to be computed
+ /// \return a boolean value about it is computed
+ SKETCHPLUGIN_EXPORT virtual bool compute(const std::string& theAttributeId);
+
/// \brief Request for initialization of data model of the feature: adding all attributes
SKETCHPLUGIN_EXPORT virtual void initAttributes();
<selection_filter id="MultiFilter" parameters="line,vertex"/>
</sketch_shape_selector>
- <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" internal="1" obligatory="0"/>
+ <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
<doublevalue_editor label="Value" tooltip="Distance" id="ConstraintValue" default="computed" min="0">
<validator id="GeomValidators_Positive"/>
<validator id="SketchPlugin_ResultLine"/>
<selection_filter id="EdgeFilter" parameters="line"/>
</shape_selector>
- <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" internal="1" obligatory="0"/>
+ <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
<doublevalue_editor label="Value" tooltip="Length" id="ConstraintValue" default="computed">
<validator id="GeomValidators_Positive"/>
</doublevalue_editor>
<validator id="SketchPlugin_ResultArc"/>
<selection_filter id="EdgeFilter" parameters="circle"/>
</shape_selector>
- <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" internal="1" obligatory="0"/>
+ <sketch-2dpoint_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
<doublevalue_editor label="Value" tooltip="Radius" id="ConstraintValue" default="computed"/>
<validator id="PartSet_RadiusValidator"/>
</feature>
QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
bool isStoreValue = !theOperation->isEditOperation() &&
- aWidget->isValueDefault() && !aWidget->isComputedDefault();
+ !aWidget->getDefaultValue().empty() && !aWidget->isComputedDefault();
aWidget->setFeature(theOperation->feature(), isStoreValue);
aWidget->enableFocusProcessing();
}