]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Validators.cpp
Salome HOME
Update constraint Mirror.
[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 #include "SketchPlugin_Circle.h"
14
15 #include "SketcherPrs_Tools.h"
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Session.h>
24
25 #include <GeomValidators_Edge.h>
26
27 #include <GeomDataAPI_Point2D.h>
28
29
30 bool SketchPlugin_DistanceAttrValidator::isValid(
31   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
32 {
33   // there is a check whether the feature contains a point and a linear edge or two point values
34   std::string aParamA = theArguments.front();
35   SessionPtr aMgr = ModelAPI_Session::get();
36   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
37
38   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
39   if (!aRefAttr)
40     return false;
41
42   bool isObject = aRefAttr->isObject();
43   if (!isObject) {
44     // an attribute is a point. A point value is valid always for the distance
45     return true;
46   } else {
47     // 1. check whether the references object is a linear
48     ObjectPtr anObject = aRefAttr->object();
49
50     const ModelAPI_AttributeValidator* anEdgeValidator = 
51       dynamic_cast<const GeomValidators_Edge*>(aFactory->validator("GeomValidators_Edge"));
52     std::list<std::string> anArguments;
53     anArguments.push_back("circle");
54     bool anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
55     // the circle line is not a valid case
56     if (anEdgeValid)
57       return false;
58       
59     anArguments.clear();
60     anArguments.push_back("line");
61     anEdgeValid = anEdgeValidator->isValid(aRefAttr, anArguments);
62     // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
63     if (!anEdgeValid)
64       return true;
65
66     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
67     // If it is a line then we have to check that first attribute id not a line
68     std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(aFeature->data(), aParamA);
69     if (aPoint)
70       return true;
71   }
72   return false;
73 }
74 bool SketchPlugin_TangentAttrValidator::isValid(
75   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
76 {
77   // there is a check whether the feature contains a point and a linear edge or two point values
78   std::string aParamA = theArguments.front();
79   SessionPtr aMgr = ModelAPI_Session::get();
80   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
81
82   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
83   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
84   if (!aRefAttr)
85     return false;
86
87   bool isObject = aRefAttr->isObject();
88   ObjectPtr anObject = aRefAttr->object();
89   if (isObject && anObject) {
90     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
91
92     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
93     ObjectPtr aOtherObject = aOtherAttr->object();
94     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
95
96     if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
97       if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
98         return false;
99     } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
100       if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
101           aOtherFea->getKind() != SketchPlugin_Arc::ID())
102         return false;
103     } else
104       return false;
105
106     // check that both have coincidence
107     FeaturePtr aConstrFeature;
108     std::set<FeaturePtr> aCoinList;
109     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
110     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
111     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
112       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
113       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
114       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
115         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
116         AttributePtr aAR = aRAttr->attr();
117         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
118           aCoinList.insert(aConstrFeature);
119       }
120     }
121     // if there is no coincidence then it is not valid
122     if (aCoinList.size() == 0)
123       return false;
124
125     // find that coincedence is the same
126     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
127     std::set<FeaturePtr>::const_iterator aCoinsIt;
128     for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
129       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
130       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
131       aCoinsIt = aCoinList.find(aConstrFeature);
132       if (aCoinsIt != aCoinList.end()) {
133         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
134         AttributePtr aAR = aRAttr->attr();
135         if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
136           return true;
137       }
138     }
139   }
140   return false;
141 }
142
143 bool SketchPlugin_NotFixedValidator::isValid(
144     const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
145 {
146   std::shared_ptr<SketchPlugin_Feature> aFeature =
147       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
148   if (!aFeature)
149     return true;
150
151   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
152   if (!aRefAttr)
153     return false;
154
155   SketchPlugin_Sketch* aSketch = aFeature->sketch();
156   int aNbFeatures = aSketch->numberOfSubs();
157   for (int anInd = 0; anInd < aNbFeatures; anInd++) {
158     FeaturePtr aSubFeature = aSketch->subFeature(anInd);
159     if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
160       continue;
161     AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
162         aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
163     if (aRefAttr->isObject()) {
164       if (aRefAttr->object() == aRAttr->object())
165         return false;
166     } else if (aRefAttr->attr() == aRAttr->attr())
167       return false;
168   }
169   return true;
170 }
171
172 bool SketchPlugin_EqualAttrValidator::isValid(
173   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
174 {
175   std::string aParamA = theArguments.front();
176   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
177   AttributeRefAttrPtr aRefAttr[2];
178   aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
179   if (!aRefAttr)
180     return false;
181   aRefAttr[1] = aFeature->data()->refattr(aParamA);
182
183   if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
184     return false;
185
186   int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
187   std::list<std::string> anArguments;
188   for (int i = 0; i < 2; i++) {
189     ObjectPtr anObject = aRefAttr[i]->object();
190     aFeature = ModelAPI_Feature::feature(anObject);
191     if (!aFeature)
192       return false;
193
194     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
195       aType[i] = 1;
196       continue;
197     }
198     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
199       aType[i] = 2;
200       continue;
201     }
202     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
203       aType[i] = 3;
204       continue;
205     }
206     // wrong type of attribute
207     return false;
208   }
209
210   if ((aType[0] == 1 && aType[1] == 2) ||
211       (aType[0] == 2 && aType[1] == 1))
212     return false;
213   return true;
214 }
215
216 bool SketchPlugin_MirrorAttrValidator::isValid(
217   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
218 {
219   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
220   AttributeSelectionListPtr aSelAttr = 
221     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
222   if (!aSelAttr)
223     return false;
224
225   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226       aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
227   std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
228
229   for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
230     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
231     std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
232     for (; aMirIter != aMirroredObjects.end(); aMirIter++)
233       if (aSelect->context() == *aMirIter)
234         return false;
235   }
236   return true;
237 }
238