Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Validators.cpp
4 // Created:     01 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketchPlugin_Validators.h"
8 #include "SketchPlugin_ConstraintDistance.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13 #include <ModelAPI_Session.h>
14
15 #include <GeomValidators_Edge.h>
16
17 #include <GeomDataAPI_Point2D.h>
18
19 bool SketchPlugin_DistanceAttrValidator::isValid(
20   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
21 {
22   // there is a check whether the feature contains a point and a linear edge or two point values
23   std::string aParamA = theArguments.front();
24   SessionPtr aMgr = ModelAPI_Session::get();
25   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
26
27   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
28   if (!aRefAttr)
29     return false;
30
31   bool isObject = aRefAttr->isObject();
32   if (!isObject) {
33     // an attribute is a point. A point value is valid always for the distance
34     return true;
35   } else {
36     // 1. check whether the references object is a linear
37     ObjectPtr anObject = aRefAttr->object();
38
39     const ModelAPI_AttributeValidator* anEdgeValidator = 
40       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
41     std::list<std::string> anArguments;
42     anArguments.push_back("circle");
43     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
44     // the circle line is not a valid case
45     if (anEdgeValid)
46       return false;
47       
48     anArguments.clear();
49     anArguments.push_back("line");
50     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
51     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
52     if (!anEdgeValid)
53       return true;
54
55     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
56     // If it is a line then we have to check that first attribute id not a line
57     std::shared_ptr<GeomDataAPI_Point2D> aPoint = getFeaturePoint(aFeature->data(), aParamA);
58     if (aPoint)
59       return true;
60   }
61   return false;
62 }