]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Create dimension presentations
[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 "SketchPlugin_ConstraintCoincidence.h"
10 #include "SketchPlugin_Line.h"
11 #include "SketchPlugin_Arc.h"
12
13 #include "SketcherPrs_Tools.h"
14
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_Session.h>
20
21 #include <GeomValidators_Edge.h>
22
23 #include <GeomDataAPI_Point2D.h>
24
25
26 bool SketchPlugin_DistanceAttrValidator::isValid(
27   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
28 {
29   // there is a check whether the feature contains a point and a linear edge or two point values
30   std::string aParamA = theArguments.front();
31   SessionPtr aMgr = ModelAPI_Session::get();
32   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
33
34   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
35   if (!aRefAttr)
36     return false;
37
38   bool isObject = aRefAttr->isObject();
39   if (!isObject) {
40     // an attribute is a point. A point value is valid always for the distance
41     return true;
42   } else {
43     // 1. check whether the references object is a linear
44     ObjectPtr anObject = aRefAttr->object();
45
46     const ModelAPI_AttributeValidator* anEdgeValidator = 
47       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
48     std::list<std::string> anArguments;
49     anArguments.push_back("circle");
50     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
51     // the circle line is not a valid case
52     if (anEdgeValid)
53       return false;
54       
55     anArguments.clear();
56     anArguments.push_back("line");
57     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
58     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
59     if (!anEdgeValid)
60       return true;
61
62     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
63     // If it is a line then we have to check that first attribute id not a line
64     std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
65     if (aPoint)
66       return true;
67   }
68   return false;
69 }
70
71
72
73 bool SketchPlugin_TangentAttrValidator::isValid(
74   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
75 {
76   // there is a check whether the feature contains a point and a linear edge or two point values
77   std::string aParamA = theArguments.front();
78   SessionPtr aMgr = ModelAPI_Session::get();
79   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
80
81   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
82   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
83   if (!aRefAttr)
84     return false;
85
86   bool isObject = aRefAttr->isObject();
87   ObjectPtr anObject = aRefAttr->object();
88   if (isObject && anObject) {
89     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
90
91     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
92     ObjectPtr aOtherObject = aOtherAttr->object();
93     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
94
95     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
96       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
97         return false;
98     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
99       if (aOtherFea->getKind() != SketchPlugin_Line::ID())
100         return false;
101     } else
102       return false;
103
104     // check that both have coincidence
105     FeaturePtr aConstrFeature;
106     std::set<FeaturePtr> aCoinList;
107     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
108     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
109     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
110       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
111       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
112       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
113         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
114         AttributePtr aAR = aRAttr->attr();
115         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
116           aCoinList.insert(aConstrFeature);
117       }
118     }
119     // if there is no coincidence then it is not valid
120     if (aCoinList.size() == 0)
121       return false;
122
123     // find that coincedence is the same
124     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
125     std::set<FeaturePtr>::const_iterator aCoinsIt;
126     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
127       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
128       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
129       aCoinsIt = aCoinList.find(aConstrFeature);
130       if (aCoinsIt != aCoinList.end()) {
131         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
132         AttributePtr aAR = aRAttr->attr();
133         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
134           return true;
135       }
136     }
137   }
138   return false;
139 }
140
141
142