Created a validator to avoid selection of non-sketch features as objects of the Length constraints
aFactory->registerValidator("SketchPlugin_HasNoConstraint", new SketchPlugin_HasNoConstraint);
aFactory->registerValidator("SketchPlugin_ReplicationReference",
new SketchPlugin_ReplicationReferenceValidator);
+ aFactory->registerValidator("SketchPlugin_SketchFeatureValidator",
+ new SketchPlugin_SketchFeatureValidator);
// register this plugin
ModelAPI_Session::get()->registerPlugin(this);
return true;
}
+
+bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
+ theError = "The attribute with the %1 type is not processed";
+ theError.arg(theAttribute->attributeType());
+ return false;
+ }
+
+ // check the attribute refers to a sketch feature
+ AttributeRefAttrPtr aRefAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ bool isSketchFeature = aRefAttr->isObject();
+ if (isSketchFeature) {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+ isSketchFeature = aFeature;
+ if (isSketchFeature) {
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+ isSketchFeature = aSketchFeature;
+ }
+ }
+
+ if (isSketchFeature)
+ return true;
+
+ theError = "The object selected is not a sketch feature";
+ return false;
+}
Events_InfoMessage& theError) const;
};
+/**\class SketchPlugin_SketchFeatureValidator
+ * \ingroup Validators
+ * \brief Validator for checking whether the feature referred by attribute is a sketch feature.
+ */
+class SketchPlugin_SketchFeatureValidator: public ModelAPI_AttributeValidator
+{
+ public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ //! \param theError error message
+ virtual bool isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const;
+};
+
#endif
<label title="Select a line on which to calculate length" tooltip="Select a line on which to calculate length"/>
<shape_selector id="ConstraintEntityA" label="Line" tooltip="Select a line" shape_types="edge" >
<validator id="GeomValidators_ShapeType" parameters="line"/>
+ <validator id="SketchPlugin_SketchFeatureValidator"/>
</shape_selector>
<sketch-2dpoint_flyout_selector id="ConstraintFlyoutValuePnt" default="computed" internal="1" obligatory="0"/>
<doublevalue_editor label="Value" tooltip="Length" id="ConstraintValue" default="computed">