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"
17 #include "SketchPlugin_Tools.h"
19 #include "SketcherPrs_Tools.h"
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomDataAPI_Point2D.h>
33 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
34 const std::list<std::string>& theArguments,
35 std::string& theError) const
37 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
38 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
42 // there is a check whether the feature contains a point and a linear edge or two point values
43 std::string aParamA = theArguments.front();
44 SessionPtr aMgr = ModelAPI_Session::get();
45 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
47 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
48 bool isObject = aRefAttr->isObject();
50 // an attribute is a point. A point value is valid always for the distance
53 // 1. check whether the references object is a linear
54 ObjectPtr anObject = aRefAttr->object();
56 const ModelAPI_AttributeValidator* aShapeValidator =
57 dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
58 std::list<std::string> anArguments;
59 anArguments.push_back("circle");
60 std::string aCircleError;
61 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
62 // the circle line is not a valid case
64 theError = "Circle can not be used in distance constraint";
69 anArguments.push_back("line");
70 std::string aLineError;
71 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
72 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
74 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
75 // If it is a line then we have to check that first attribute id not a line
76 std::shared_ptr<SketchPlugin_Feature> aSFeature =
77 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
78 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
79 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
80 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
81 aFeature->data(), aParamA, aPlane);
83 theError = "One of parameters of distance constraint should be a point";
91 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
92 const std::list<std::string>& theArguments,
93 std::string& theError) const
95 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
96 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
100 // there is a check whether the feature contains a point and a linear edge or two point values
101 std::string aParamA = theArguments.front();
102 SessionPtr aMgr = ModelAPI_Session::get();
103 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
105 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
106 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
108 bool isObject = aRefAttr->isObject();
109 ObjectPtr anObject = aRefAttr->object();
110 if (isObject && anObject.get()) {
111 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
113 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
114 ObjectPtr aOtherObject = aOtherAttr->object();
115 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
117 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
118 if (aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
119 theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is not an "
120 + SketchPlugin_Arc::ID();
124 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
125 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
126 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
127 theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
128 + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
133 theError = "It refers to " + aRefFea->getKind() + "but should refer to " + SketchPlugin_Line::ID()
134 + " or " + SketchPlugin_Arc::ID();
140 theError = "It uses an empty object";
147 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
148 const std::list<std::string>& theArguments,
149 std::string& theError) const
151 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
152 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
156 std::shared_ptr<SketchPlugin_Feature> aFeature =
157 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
161 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
163 SketchPlugin_Sketch* aSketch = aFeature->sketch();
164 int aNbFeatures = aSketch->numberOfSubs();
165 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
166 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
167 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
169 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
170 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
171 if (aRefAttr->isObject()) {
172 if (aRefAttr->object() == aRAttr->object()) {
173 ObjectPtr anObject = aRefAttr->object();
174 std::string aName = anObject.get() ? anObject->data()->name() : "";
175 theError = "The object " + aName + " has been already fixed.";
179 else if (aRefAttr->attr() == aRAttr->attr()) {
180 AttributePtr anAttribute = aRefAttr->attr();
181 std::string aName = anAttribute.get() ? anAttribute->id() : "";
182 theError = "The attribute " + aName + " has been already fixed.";
189 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
190 const std::list<std::string>& theArguments,
191 std::string& theError) const
193 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
194 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
198 std::string aParamA = theArguments.front();
199 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
200 AttributeRefAttrPtr aRefAttr[2];
201 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
202 aRefAttr[1] = aFeature->data()->refattr(aParamA);
204 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
205 theError = "Attributes can not be used in equal constraint";
209 int aType[2] = {0, 0}; // types of attributes: 0 - incorrect, 1 - line, 2 - circle, 3 - arc
210 std::list<std::string> anArguments;
211 for (int i = 0; i < 2; i++) {
212 ObjectPtr anObject = aRefAttr[i]->object();
213 if (!anObject.get()) {
214 theError = "An empty object is used.";
218 aFeature = ModelAPI_Feature::feature(anObject);
219 if (!aFeature.get()) {
220 theError = "An empty feature is used.";
224 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
228 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
232 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
236 theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
237 SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " +
238 SketchPlugin_Arc::ID();
239 // wrong type of attribute
243 if ((aType[0] == 1 && aType[1] == 2) ||
244 (aType[0] == 2 && aType[1] == 1)) {
245 theError = "Feature with kinds " + SketchPlugin_Line::ID() + " and " +
246 SketchPlugin_Circle::ID() + "can not be equal.";
252 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
253 const std::list<std::string>& theArguments,
254 std::string& theError) const
256 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
257 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
261 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
262 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
264 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
265 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
266 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
268 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
269 ObjectPtr aSelObject = aSelAttr->object(anInd);
270 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
271 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
272 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
273 if (aSelObject == *aMirIter) {
274 theError = "The object " + aName + " is a result of mirror";
282 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
283 const std::list<std::string>& theArguments,
284 std::string& theError) const
286 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
287 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
291 // there is a check whether the feature contains a point and a linear edge or two point values
292 std::string aParamA = theArguments.front();
293 SessionPtr aMgr = ModelAPI_Session::get();
294 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
296 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
297 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
299 theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
303 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
305 // first attribute is a point, it may coincide with any object
306 if (!aRefAttrA->isObject())
309 ObjectPtr anObject = aRefAttrA->object();
310 if (!anObject.get()) {
311 theError = aParamA + " attribute has an empty object";
314 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
315 if (!aFeature.get()) {
316 theError = aParamA + " attribute has an empty feature";
320 if (aFeature->getKind() == SketchPlugin_Point::ID())
324 // second attribute is a point, it may coincide with any object
325 if (!aRefAttrB->isObject())
328 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
330 theError = theAttribute->id() + " attribute has an empty object";
333 if (aFeature->getKind() == SketchPlugin_Point::ID())
336 theError = "There is no an attribute filled by a point";
341 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
342 const std::list<std::string>& theArguments,
343 std::string& theError) const
345 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
346 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
350 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
351 AttributeRefListPtr aSelAttr =
352 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
354 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
355 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
356 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
357 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
358 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
359 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
361 std::list<ObjectPtr>::iterator anObjIter;
362 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
363 ObjectPtr aSelObject = aSelAttr->object(anInd);
364 anObjIter = anInitialObjects.begin();
365 for (; anObjIter != anInitialObjects.end(); anObjIter++)
366 if (aSelObject == *anObjIter)
368 if (anObjIter != anInitialObjects.end())
370 anObjIter = aCopiedObjects.begin();
371 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
372 if (aSelObject == *anObjIter) {
373 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
374 theError = "The object " + aName + " is a result of copy";
381 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
382 const std::list<std::string>& theArguments,
383 std::string& theError) const
385 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
387 if (!aAttributeString->value().empty()) {
388 theError = aAttributeString->value();
395 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
400 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
401 const std::list<std::string>& theArguments,
402 std::string& theError) const
404 if(!theAttribute.get()) {
408 AttributeRefAttrPtr aBase = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
409 if(aBase->isObject()) {
413 // If we alredy have some result then all ok
414 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
415 AttributePtr aBaseLinesAttribute = aFeature->attribute(SketchPlugin_Constraint::ENTITY_C());
416 AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aBaseLinesAttribute);
417 if(!aRefListOfBaseLines->list().empty()) {
421 AttributePtr anAttrBase = aBase->attr();
422 const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
423 std::set<AttributePtr>::const_iterator aIt;
424 FeaturePtr aCoincident;
425 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
426 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
427 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
428 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
429 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
430 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
431 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
432 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
433 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
434 AttributePtr anAttrA = anAttrRefA->attr();
435 if(anAttrBase == anAttrA) {
436 aCoincident = aConstrFeature;
440 if(anAttrRefA.get() && !anAttrRefB->isObject()) {
441 AttributePtr anAttrB = anAttrRefB->attr();
442 if(anAttrBase == anAttrB) {
443 aCoincident = aConstrFeature;
450 if(!aCoincident.get()) {
454 std::set<FeaturePtr> aCoinsideLines;
455 SketchPlugin_Tools::findCoincidences(aCoincident,
456 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
458 SketchPlugin_Tools::findCoincidences(aCoincident,
459 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
461 if(aCoinsideLines.size() < 2) {
465 // Remove auxilary lines
466 if(aCoinsideLines.size() > 2) {
467 std::set<FeaturePtr> aNewLines;
468 for(std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin(); anIt != aCoinsideLines.end(); ++anIt) {
469 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
470 aNewLines.insert(*anIt);
473 aCoinsideLines = aNewLines;
476 if(aCoinsideLines.size() != 2) {
480 // Check that lines not collinear
481 std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin();
482 FeaturePtr aFirstFeature = *anIt++;
483 FeaturePtr aSecondFeature = *anIt;
484 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
485 std::string aStartAttr = SketchPlugin_Line::START_ID();
486 std::string anEndAttr = SketchPlugin_Line::END_ID();
487 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
488 aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
489 aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
490 aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
491 aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
492 double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
493 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
494 double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
495 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
496 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {