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"
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Circle.h"
11 #include "SketchPlugin_ConstraintCoincidence.h"
12 #include "SketchPlugin_ConstraintDistance.h"
13 #include "SketchPlugin_ConstraintRigid.h"
14 #include "SketchPlugin_Line.h"
15 #include "SketchPlugin_Point.h"
16 #include "SketchPlugin_Sketch.h"
18 #include "SketcherPrs_Tools.h"
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Session.h>
29 #include <GeomValidators_ShapeType.h>
31 #include <GeomDataAPI_Point2D.h>
34 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
35 const std::list<std::string>& theArguments,
36 std::string& theError) const
38 // there is a check whether the feature contains a point and a linear edge or two point values
39 std::string aParamA = theArguments.front();
40 SessionPtr aMgr = ModelAPI_Session::get();
41 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
43 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
47 bool isObject = aRefAttr->isObject();
49 // an attribute is a point. A point value is valid always for the distance
52 // 1. check whether the references object is a linear
53 ObjectPtr anObject = aRefAttr->object();
55 const ModelAPI_AttributeValidator* aShapeValidator =
56 dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
57 std::list<std::string> anArguments;
58 anArguments.push_back("circle");
59 std::string aCircleError;
60 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
61 // the circle line is not a valid case
66 anArguments.push_back("line");
67 std::string aLineError;
68 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
69 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
73 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
74 // If it is a line then we have to check that first attribute id not a line
75 std::shared_ptr<SketchPlugin_Feature> aSFeature =
76 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
77 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
78 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
79 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
80 aFeature->data(), aParamA, aPlane);
87 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
88 const std::list<std::string>& theArguments,
89 std::string& theError) const
91 // there is a check whether the feature contains a point and a linear edge or two point values
92 std::string aParamA = theArguments.front();
93 SessionPtr aMgr = ModelAPI_Session::get();
94 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
96 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
97 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
101 bool isObject = aRefAttr->isObject();
102 ObjectPtr anObject = aRefAttr->object();
103 if (isObject && anObject) {
104 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
106 AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
107 ObjectPtr aOtherObject = aOtherAttr->object();
108 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
110 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
111 if (aOtherFea->getKind() != SketchPlugin_Arc::ID())
113 } else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
114 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
115 aOtherFea->getKind() != SketchPlugin_Arc::ID())
125 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
126 const std::list<std::string>& theArguments,
127 std::string& theError) const
129 std::shared_ptr<SketchPlugin_Feature> aFeature =
130 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
134 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
138 SketchPlugin_Sketch* aSketch = aFeature->sketch();
139 int aNbFeatures = aSketch->numberOfSubs();
140 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
141 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
142 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
144 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
145 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
146 if (aRefAttr->isObject()) {
147 if (aRefAttr->object() == aRAttr->object())
149 } else if (aRefAttr->attr() == aRAttr->attr())
155 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
156 const std::list<std::string>& theArguments,
157 std::string& theError) const
159 std::string aParamA = theArguments.front();
160 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
161 AttributeRefAttrPtr aRefAttr[2];
162 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
165 aRefAttr[1] = aFeature->data()->refattr(aParamA);
167 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject())
170 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
171 std::list<std::string> anArguments;
172 for (int i = 0; i < 2; i++) {
173 ObjectPtr anObject = aRefAttr[i]->object();
174 aFeature = ModelAPI_Feature::feature(anObject);
178 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
182 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
186 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
190 // wrong type of attribute
194 if ((aType[0] == 1 && aType[1] == 2) ||
195 (aType[0] == 2 && aType[1] == 1))
200 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
201 const std::list<std::string>& theArguments,
202 std::string& theError) const
204 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
205 AttributeRefListPtr aSelAttr =
206 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
210 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
211 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
212 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
214 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
215 ObjectPtr aSelObject = aSelAttr->object(anInd);
216 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
217 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
218 if (aSelObject == *aMirIter)
225 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
226 const std::list<std::string>& theArguments,
227 std::string& theError) const
229 // there is a check whether the feature contains a point and a linear edge or two point values
230 std::string aParamA = theArguments.front();
231 SessionPtr aMgr = ModelAPI_Session::get();
232 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
234 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
235 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
239 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
243 // first attribute is a point, it may coincide with any object
244 if (!aRefAttrA->isObject())
247 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
250 if (aFeature->getKind() == SketchPlugin_Point::ID())
254 // second attribute is a point, it may coincide with any object
255 if (!aRefAttrB->isObject())
258 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
261 if (aFeature->getKind() == SketchPlugin_Point::ID())
269 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
270 const std::list<std::string>& theArguments,
271 std::string& theError) const
273 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
274 AttributeRefListPtr aSelAttr =
275 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
279 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
280 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
281 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
282 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
283 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
284 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
286 std::list<ObjectPtr>::iterator anObjIter;
287 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
288 ObjectPtr aSelObject = aSelAttr->object(anInd);
289 anObjIter = anInitialObjects.begin();
290 for (; anObjIter != anInitialObjects.end(); anObjIter++)
291 if (aSelObject == *anObjIter)
293 if (anObjIter != anInitialObjects.end())
295 anObjIter = aCopiedObjects.begin();
296 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
297 if (aSelObject == *anObjIter)
303 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
304 const std::list<std::string>& theArguments,
305 std::string& theError) const
307 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
309 if (!aAttributeString->value().empty()) {
310 theError = aAttributeString->value();
317 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)