+ bool aValid = false;
+
+ if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
+ theError = "The attribute with the %1 type is not processed";
+ theError.arg(theAttribute->attributeType());
+ return aValid;
+ }
+ AttributeReferencePtr aFeatureAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+
+ ObjectPtr anAttrObject = aFeatureAttr->value();
+ FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
+ if (!anAttrFeature)
+ return aValid;
+
+ std::string aKind = anAttrFeature->getKind();
+ if (aKind == SketchPlugin_Line::ID() ||
+ aKind == SketchPlugin_Arc::ID() ||
+ aKind == SketchPlugin_Circle::ID()) {
+
+ std::set<ResultPtr> anEdgeShapes;
+ ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
+ if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
+ return aValid;
+
+ // coincidences to the feature
+ std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
+ ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
+ SketchPlugin_ConstraintCoincidence::ID(),
+ aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
+
+ GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
+ std::shared_ptr<SketchPlugin_Feature> aSFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
+ SketchPlugin_Sketch* aSketch = aSFeature->sketch();
+
+ std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
+ std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+ aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
+ std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+ aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+ std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
+
+ typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
+ std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
+ std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
+ PointToRefsMap aPointsInfo;
+
+ ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
+ aX->dir(), aDirY, aPointsInfo);
+ int aCoincidentToFeature = (int)aPointsInfo.size();
+ if (aKind == SketchPlugin_Circle::ID())
+ aValid = aCoincidentToFeature >= 2;
+ else
+ aValid = aCoincidentToFeature >= 1;
+ }
+
+ return aValid;
+}
+
+bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ bool aValid = false;
+
+ if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
+ theError = "The attribute with the %1 type is not processed";
+ theError.arg(theAttribute->attributeType());
+ return aValid;
+ }
+ AttributeReferencePtr aBaseObjectAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+
+ std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
+
+ ObjectPtr aBaseObject = aBaseObjectAttr->value();
+ if (!aBaseObject) {
+ AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
+ aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
+ aBaseObject = aBaseObjectAttr->value();
+ }
+
+ FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
+ if (!aBaseFeature)
+ return aValid;
+
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
+ if (!aSketchFeature.get() || aSketchFeature->isCopy())
+ return aValid;
+
+ std::string aKind = aBaseFeature->getKind();
+ if (aKind != SketchPlugin_Line::ID() &&
+ aKind != SketchPlugin_Arc::ID() &&
+ aKind != SketchPlugin_Circle::ID())
+ return aValid;
+
+ // point on feature
+ AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+ aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
+
+ SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
+
+ std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
+ std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
+ anAttributePnt2d->y());
+
+ std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
+ std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
+ std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
+ std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
+ SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
+ aCashedShapes, anObjectToPoints);
+ const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
+
+ return aShapes.size() > 1;
+}
+
+bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
+ theError = "The attribute with the %1 type is not processed";
+ theError.arg(theAttribute->attributeType());
+ return false;
+ }
+
+ AttributeSelectionPtr aFeatureAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ std::shared_ptr<GeomAPI_Edge> anEdge;
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
+ if (aFeatureAttr.get()) {
+ GeomShapePtr aVal = aFeatureAttr->value();
+ ResultPtr aRes = aFeatureAttr->context();
+ if (aVal && aVal->isVertex())
+ return true; // vertex is always could be projected
+ if (aVal && aVal->isEdge()) {
+ anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
+ } else if(aRes && aRes->shape()) {
+ if (aRes->shape()->isVertex())
+ return true; // vertex is always could be projected
+ else if (aRes->shape()->isEdge())
+ anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
+ }
+
+ // try to convert result to sketch feature
+ if (aRes) {
+ aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
+ }
+ }
+ if (!anEdge) {
+ theError = "The attribute %1 should be an edge or vertex";
+ theError.arg(theAttribute->id());
+ return false;
+ }
+
+ // find a sketch
+ std::shared_ptr<SketchPlugin_Sketch> aSketch;
+ std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
+ std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
+ for (; anIt != aRefs.end(); ++anIt) {
+ CompositeFeaturePtr aComp =
+ std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
+ if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
+ aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
+ break;
+ }
+ }
+ if (!aSketch) {
+ theError = "There is no sketch referring to the current feature";
+ return false;
+ }
+ if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
+ theError = "Unable to project feature from the same sketch";
+ return false;
+ }
+
+ std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
+ std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
+ std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
+
+ if (anEdge->isLine()) {
+ std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
+ std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
+ double aDot = fabs(aNormal->dot(aLineDir));
+ bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
+ if (!aValid)
+ theError = "Error: Edge is already in the sketch plane.";
+ return aValid;
+ }
+ else if (anEdge->isCircle() || anEdge->isArc()) {
+ std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
+ std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
+ double aDot = fabs(aNormal->dot(aCircNormal));
+ bool aValid = fabs(aDot - 1.0) < tolerance * tolerance;
+ if (!aValid)
+ theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
+ : "Error: Arc is already in the sketch plane.");
+ return aValid;
+ }
+
+ theError = "Error: Selected object is not line, circle or arc.";
+ return false;
+}
+
+
+static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
+ const std::set<FeaturePtr>& theSet2)
+{
+ std::set<FeaturePtr> aCommon;
+ if (theSet1.empty() || theSet2.empty())
+ return aCommon;
+
+ std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
+ for (; anIt2 != theSet2.end(); ++anIt2)
+ if (theSet1.find(*anIt2) != theSet1.end())
+ aCommon.insert(*anIt2);
+ return aCommon;
+}
+
+bool SketchPlugin_DifferentReferenceValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+
+ int aNbFeaturesReferred = 0;
+ int aNbAttributesReferred = 0;
+ std::set<FeaturePtr> aCommonReferredFeatures;
+
+ // find all features referred by attributes listed in theArguments
+ std::list<std::string>::const_iterator anArgIt = theArguments.begin();
+ for (; anArgIt != theArguments.end(); ++anArgIt) {
+ AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
+ if (!aRefAttr)
+ continue;
+
+ std::set<FeaturePtr> aCoincidentFeatures;
+ if (aRefAttr->isObject()) {
+ FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+ if (aFeature) {
+ aCoincidentFeatures.insert(aFeature);
+ aNbFeaturesReferred += 1;
+ }
+ } else {
+ AttributePoint2DPtr aPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+ if (aPoint) {
+ aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
+ aNbAttributesReferred += 1;
+ }
+ }
+
+ if (aCommonReferredFeatures.empty())
+ aCommonReferredFeatures = aCoincidentFeatures;
+ else
+ aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
+
+ if (aCommonReferredFeatures.empty())
+ return true;
+ }
+
+ bool isOk = aNbFeaturesReferred < 1;
+ if (aNbFeaturesReferred == 1) {
+ if (aCommonReferredFeatures.size() == 1) {
+ FeaturePtr aFeature = *aCommonReferredFeatures.begin();
+ isOk = aNbAttributesReferred <= 1 ||
+ aFeature->getKind() == SketchPlugin_Circle::ID() ||
+ aFeature->getKind() == SketchPlugin_Arc::ID();
+ } else
+ isOk = false;
+ }
+
+ if (!isOk)
+ theError = "Attributes are referred to the same feature";
+ return isOk;
+}
+
+bool SketchPlugin_DifferentPointReferenceValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>& theArguments,
+ Events_InfoMessage& theError) const
+{
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+ std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
+
+ // find all points referred by attributes listed in theArguments
+ bool hasRefsToPoints = false;
+ std::list<std::string>::const_iterator anArgIt = theArguments.begin();
+ for (; anArgIt != theArguments.end(); ++anArgIt) {
+ AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
+ if (!aRefAttr)
+ continue;
+
+ if (!aRefAttr->isObject()) {
+ AttributePoint2DPtr aPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+ if (aReferredCoincidentPoints.empty())
+ aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
+ else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
+ return true; // non-coincident point has been found
+ else
+ hasRefsToPoints = true;
+ }
+ }
+
+ if (hasRefsToPoints)
+ theError = "Attributes are referred to the same point";
+ return !hasRefsToPoints;
+}
+
+bool SketchPlugin_CirclePassedPointValidator::isValid(
+ const AttributePtr& theAttribute,
+ const std::list<std::string>&,
+ Events_InfoMessage& theError) const
+{
+ static const std::string aErrorMessage(
+ "Passed point refers to the same feature as a center point");
+
+ FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+
+ AttributeRefAttrPtr aCenterRef =
+ anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
+ AttributeRefAttrPtr aPassedRef =
+ anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
+
+ if (!aPassedRef->isObject())
+ return true;
+
+ FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
+ if (!aPassedFeature)
+ return true;
+
+ if (aCenterRef->isObject()) {
+ if (aCenterRef->object() == aPassedRef->object()) {
+ theError = aErrorMessage;
+ return false;
+ }
+ } else {
+ AttributePoint2DPtr aCenterPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
+ if (aCenterPoint) {
+ std::set<FeaturePtr> aCoincidentFeatures =
+ SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
+ // check one of coincident features is a feature referred by passed point
+ std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
+ for(; anIt != aCoincidentFeatures.end(); ++anIt)
+ if (*anIt == aPassedFeature) {
+ theError = aErrorMessage;