1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Validators.cpp
4 // Created: 01 Aug 2014
5 // Author: Vitaly SMETANNIKOV
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 #include "SketchPlugin_Point.h"
16 #include "SketcherPrs_Tools.h"
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Session.h>
26 #include <GeomValidators_ShapeType.h>
28 #include <GeomDataAPI_Point2D.h>
31 bool SketchPlugin_DistanceAttrValidator::isValid(
32 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
34 // there is a check whether the feature contains a point and a linear edge or two point values
35 std::string aParamA = theArguments.front();
36 SessionPtr aMgr = ModelAPI_Session::get();
37 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
39 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
43 bool isObject = aRefAttr->isObject();
45 // an attribute is a point. A point value is valid always for the distance
48 // 1. check whether the references object is a linear
49 ObjectPtr anObject = aRefAttr->object();
51 const ModelAPI_AttributeValidator* aShapeValidator =
52 dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
53 std::list<std::string> anArguments;
54 anArguments.push_back("circle");
55 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
56 // the circle line is not a valid case
61 anArguments.push_back("line");
62 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments);
63 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
67 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
68 // If it is a line then we have to check that first attribute id not a line
69 std::shared_ptr<SketchPlugin_Feature> aSFeature =
70 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
71 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
72 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
73 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
74 aFeature->data(), aParamA, aPlane);
81 bool SketchPlugin_CoincidentAttr::isValid(
82 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
84 // there is a check whether the feature contains a point and a linear edge or two point values
85 std::string aParamA = theArguments.front();
86 SessionPtr aMgr = ModelAPI_Session::get();
87 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
89 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
90 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
94 bool isObject = aRefAttr->isObject();
95 ObjectPtr anObject = aRefAttr->object();
96 if (isObject && anObject) {
97 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
98 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
99 ObjectPtr aOtherObject = aOtherAttr->object();
100 // if the other attribute is not filled still, the result is true
101 if (!aOtherObject.get())
103 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
105 // check that both have coincidence
106 FeaturePtr aConstrFeature;
107 std::set<FeaturePtr> aCoinList;
108 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
109 std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
110 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
111 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
112 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
113 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
114 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
115 AttributePtr aAR = aRAttr->attr();
116 if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
117 aCoinList.insert(aConstrFeature);
120 // if there is no coincidence then it is not valid
121 if (aCoinList.size() == 0)
124 // find that coincedence is the same
125 const std::set<std::shared_ptr<ModelAPI_Attribute>>& aOtherList = aOtherFea->data()->refsToMe();
126 std::set<FeaturePtr>::const_iterator aCoinsIt;
127 for (aIt = aOtherList.cbegin(); aIt != aOtherList.cend(); ++aIt) {
128 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
129 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
130 aCoinsIt = aCoinList.find(aConstrFeature);
131 if (aCoinsIt != aCoinList.end()) {
132 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
133 AttributePtr aAR = aRAttr->attr();
134 if (aAR->id() != SketchPlugin_Arc::CENTER_ID())
142 bool SketchPlugin_TangentAttrValidator::isValid(
143 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
145 // there is a check whether the feature contains a point and a linear edge or two point values
146 std::string aParamA = theArguments.front();
147 SessionPtr aMgr = ModelAPI_Session::get();
148 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
150 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
151 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
155 bool isObject = aRefAttr->isObject();
156 ObjectPtr anObject = aRefAttr->object();
157 if (isObject && anObject) {
158 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
160 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
161 ObjectPtr aOtherObject = aOtherAttr->object();
162 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
164 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
165 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
167 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
168 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
169 aOtherFea->getKind() != SketchPlugin_Arc::ID())
179 bool SketchPlugin_NotFixedValidator::isValid(
180 const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
182 std::shared_ptr<SketchPlugin_Feature> aFeature =
183 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
187 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
191 SketchPlugin_Sketch* aSketch = aFeature->sketch();
192 int aNbFeatures = aSketch->numberOfSubs();
193 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
194 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
195 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
197 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
198 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
199 if (aRefAttr->isObject()) {
200 if (aRefAttr->object() == aRAttr->object())
202 } else if (aRefAttr->attr() == aRAttr->attr())
208 bool SketchPlugin_EqualAttrValidator::isValid(
209 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
211 std::string aParamA = theArguments.front();
212 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
213 AttributeRefAttrPtr aRefAttr[2];
214 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
217 aRefAttr[1] = aFeature->data()->refattr(aParamA);
219 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
222 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
223 std::list<std::string> anArguments;
224 for (int i = 0; i < 2; i++) {
225 ObjectPtr anObject = aRefAttr[i]->object();
226 aFeature = ModelAPI_Feature::feature(anObject);
230 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
234 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
238 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
242 // wrong type of attribute
246 if ((aType[0] == 1 && aType[1] == 2) ||
247 (aType[0] == 2 && aType[1] == 1))
252 bool SketchPlugin_MirrorAttrValidator::isValid(
253 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
255 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
256 AttributeSelectionListPtr aSelAttr =
257 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
261 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
262 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
263 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
265 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
266 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
267 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
268 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
269 if (aSelect->context() == *aMirIter)
276 bool SketchPlugin_CoincidenceAttrValidator::isValid(
277 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
279 // there is a check whether the feature contains a point and a linear edge or two point values
280 std::string aParamA = theArguments.front();
281 SessionPtr aMgr = ModelAPI_Session::get();
282 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
284 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
285 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
289 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
293 // first attribute is a point, it may coincide with any object
294 if (!aRefAttrA->isObject())
297 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
300 if (aFeature->getKind() == SketchPlugin_Point::ID())
304 // second attribute is a point, it may coincide with any object
305 if (!aRefAttrB->isObject())
308 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
311 if (aFeature->getKind() == SketchPlugin_Point::ID())
319 bool SketchPlugin_CopyValidator::isValid(
320 const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
322 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
323 AttributeSelectionListPtr aSelAttr =
324 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
328 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
329 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
330 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
331 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
332 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
333 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
335 std::list<ObjectPtr>::iterator anObjIter;
336 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
337 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
338 anObjIter = anInitialObjects.begin();
339 for (; anObjIter != anInitialObjects.end(); anObjIter++)
340 if (aSelect->context() == *anObjIter)
342 if (anObjIter != anInitialObjects.end())
344 anObjIter = aCopiedObjects.begin();
345 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
346 if (aSelect->context() == *anObjIter)