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