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 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
39 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
43 // there is a check whether the feature contains a point and a linear edge or two point values
44 std::string aParamA = theArguments.front();
45 SessionPtr aMgr = ModelAPI_Session::get();
46 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
48 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
49 bool isObject = aRefAttr->isObject();
51 // an attribute is a point. A point value is valid always for the distance
54 // 1. check whether the references object is a linear
55 ObjectPtr anObject = aRefAttr->object();
57 const ModelAPI_AttributeValidator* aShapeValidator =
58 dynamic_cast<const GeomValidators_ShapeType*>(aFactory->validator("GeomValidators_ShapeType"));
59 std::list<std::string> anArguments;
60 anArguments.push_back("circle");
61 std::string aCircleError;
62 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
63 // the circle line is not a valid case
65 theError = "Circle can not be used in distance constraint";
70 anArguments.push_back("line");
71 std::string aLineError;
72 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
73 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
75 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
76 // If it is a line then we have to check that first attribute id not a line
77 std::shared_ptr<SketchPlugin_Feature> aSFeature =
78 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
79 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
80 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
81 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
82 aFeature->data(), aParamA, aPlane);
84 theError = "One of parameters of distance constraint should be a point";
92 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
93 const std::list<std::string>& theArguments,
94 std::string& theError) const
96 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
97 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
101 // there is a check whether the feature contains a point and a linear edge or two point values
102 std::string aParamA = theArguments.front();
103 SessionPtr aMgr = ModelAPI_Session::get();
104 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
106 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
107 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
109 bool isObject = aRefAttr->isObject();
110 ObjectPtr anObject = aRefAttr->object();
111 if (isObject && anObject.get()) {
112 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
114 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
115 ObjectPtr aOtherObject = aOtherAttr->object();
116 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
118 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
119 if (aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
120 theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is not an "
121 + SketchPlugin_Arc::ID();
125 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
126 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
127 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
128 theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
129 + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
134 theError = "It refers to " + aRefFea->getKind() + "but should refer to " + SketchPlugin_Line::ID()
135 + " or " + SketchPlugin_Arc::ID();
141 theError = "It uses an empty object";
148 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
149 const std::list<std::string>& theArguments,
150 std::string& theError) const
152 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
153 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
157 std::shared_ptr<SketchPlugin_Feature> aFeature =
158 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
162 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
164 SketchPlugin_Sketch* aSketch = aFeature->sketch();
165 int aNbFeatures = aSketch->numberOfSubs();
166 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
167 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
168 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
170 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
171 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
172 if (aRefAttr->isObject()) {
173 if (aRefAttr->object() == aRAttr->object()) {
174 ObjectPtr anObject = aRefAttr->object();
175 std::string aName = anObject.get() ? anObject->data()->name() : "";
176 theError = "The object " + aName + " has been already fixed.";
180 else if (aRefAttr->attr() == aRAttr->attr()) {
181 AttributePtr anAttribute = aRefAttr->attr();
182 std::string aName = anAttribute.get() ? anAttribute->id() : "";
183 theError = "The attribute " + aName + " has been already fixed.";
190 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
191 const std::list<std::string>& theArguments,
192 std::string& theError) const
194 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
195 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
199 std::string aParamA = theArguments.front();
200 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
201 AttributeRefAttrPtr aRefAttr[2];
202 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
203 aRefAttr[1] = aFeature->data()->refattr(aParamA);
205 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
206 theError = "Attributes can not be used in equal constraint";
210 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
211 std::list<std::string> anArguments;
212 for (int i = 0; i < 2; i++) {
213 ObjectPtr anObject = aRefAttr[i]->object();
214 if (!anObject.get()) {
215 theError = "An empty object is used.";
219 aFeature = ModelAPI_Feature::feature(anObject);
220 if (!aFeature.get()) {
221 theError = "An empty feature is used.";
225 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
229 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
233 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
237 theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
238 SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " +
239 SketchPlugin_Arc::ID();
240 // wrong type of attribute
244 if ((aType[0] == 1 && aType[1] == 2) ||
245 (aType[0] == 2 && aType[1] == 1)) {
246 theError = "Feature with kinds " + SketchPlugin_Line::ID() + " and " +
247 SketchPlugin_Circle::ID() + "can not be equal.";
253 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
254 const std::list<std::string>& theArguments,
255 std::string& theError) const
257 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
258 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
262 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
263 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
265 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
266 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
267 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
269 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
270 ObjectPtr aSelObject = aSelAttr->object(anInd);
271 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
272 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
273 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
274 if (aSelObject == *aMirIter) {
275 theError = "The object " + aName + " is a result of mirror";
283 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
284 const std::list<std::string>& theArguments,
285 std::string& theError) const
287 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
288 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
292 // there is a check whether the feature contains a point and a linear edge or two point values
293 std::string aParamA = theArguments.front();
294 SessionPtr aMgr = ModelAPI_Session::get();
295 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
297 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
298 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
300 theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
304 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
306 // first attribute is a point, it may coincide with any object
307 if (!aRefAttrA->isObject())
310 ObjectPtr anObject = aRefAttrA->object();
311 if (!anObject.get()) {
312 theError = aParamA + " attribute has an empty object";
315 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
316 if (!aFeature.get()) {
317 theError = aParamA + " attribute has an empty feature";
321 if (aFeature->getKind() == SketchPlugin_Point::ID())
325 // second attribute is a point, it may coincide with any object
326 if (!aRefAttrB->isObject())
329 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
331 theError = theAttribute->id() + " attribute has an empty object";
334 if (aFeature->getKind() == SketchPlugin_Point::ID())
337 theError = "There is no an attribute filled by a point";
342 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
343 const std::list<std::string>& theArguments,
344 std::string& theError) const
346 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
347 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
351 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
352 AttributeRefListPtr aSelAttr =
353 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
355 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
356 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
357 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
358 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
359 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
360 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
362 std::list<ObjectPtr>::iterator anObjIter;
363 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
364 ObjectPtr aSelObject = aSelAttr->object(anInd);
365 anObjIter = anInitialObjects.begin();
366 for (; anObjIter != anInitialObjects.end(); anObjIter++)
367 if (aSelObject == *anObjIter)
369 if (anObjIter != anInitialObjects.end())
371 anObjIter = aCopiedObjects.begin();
372 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
373 if (aSelObject == *anObjIter) {
374 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
375 theError = "The object " + aName + " is a result of copy";
382 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
383 const std::list<std::string>& theArguments,
384 std::string& theError) const
386 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
388 if (!aAttributeString->value().empty()) {
389 theError = aAttributeString->value();
396 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)