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_ConstraintFillet.h"
14 #include "SketchPlugin_ConstraintRigid.h"
15 #include "SketchPlugin_ConstraintTangent.h"
16 #include "SketchPlugin_Line.h"
17 #include "SketchPlugin_Point.h"
18 #include "SketchPlugin_Sketch.h"
19 #include "SketchPlugin_Tools.h"
21 #include "SketcherPrs_Tools.h"
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeRefAttr.h>
28 #include <ModelAPI_AttributeRefAttrList.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultConstruction.h>
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_Edge.h>
37 #include <GeomAPI_Vertex.h>
38 #include <GeomDataAPI_Point2D.h>
43 const double tolerance = 1.e-7;
45 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
46 const std::list<std::string>& theArguments,
47 std::string& theError) const
49 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
50 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
54 // there is a check whether the feature contains a point and a linear edge or two point values
55 std::string aParamA = theArguments.front();
56 SessionPtr aMgr = ModelAPI_Session::get();
57 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
59 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
60 bool isObject = aRefAttr->isObject();
62 // an attribute is a point. A point value is valid always for the distance
65 // 1. check whether the references object is a linear
66 ObjectPtr anObject = aRefAttr->object();
68 const ModelAPI_AttributeValidator* aShapeValidator =
69 dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
70 std::list<std::string> anArguments;
71 anArguments.push_back("circle");
72 std::string aCircleError;
73 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
74 // the circle line is not a valid case
76 theError = "Circle can not be used in distance constraint";
81 anArguments.push_back("line");
82 std::string aLineError;
83 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
84 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
86 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
87 // If it is a line then we have to check that first attribute id not a line
88 std::shared_ptr<SketchPlugin_Feature> aSFeature =
89 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
90 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
91 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
92 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
93 aFeature->data(), aParamA, aPlane);
95 theError = "One of parameters of distance constraint should be a point";
104 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
106 FeaturePtr aConstrFeature;
107 std::list<AttributePtr> anAttrList;
108 if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
109 theFeature2->getKind() == SketchPlugin_Circle::ID())
111 if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
112 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
113 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
114 } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
115 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
116 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
119 const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
120 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
121 for (; aRefIt != aRefsList.end(); ++aRefIt) {
122 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
123 if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
125 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
126 AttributePtr anAttr = aRefAttr->attr();
127 if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
130 anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
131 if (anAttr == *aRefIt)
132 anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
134 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
137 anAttr = aRefAttr->attr();
138 for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
139 anIt != anAttrList.end(); ++anIt)
146 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
147 const std::list<std::string>& theArguments,
148 std::string& theError) const
150 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
151 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
155 // there is a check whether the feature contains a point and a linear edge or two point values
156 std::string aParamA = theArguments.front();
157 SessionPtr aMgr = ModelAPI_Session::get();
158 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
160 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
161 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
163 bool isObject = aRefAttr->isObject();
164 ObjectPtr anObject = aRefAttr->object();
165 if (isObject && anObject.get()) {
166 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
168 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
169 ObjectPtr aOtherObject = aOtherAttr->object();
170 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
174 if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
175 aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
176 !hasCoincidentPoint(aRefFea, aOtherFea))
179 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
180 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
181 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
182 theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is neither an "
183 + SketchPlugin_Arc::ID() + " nor " + SketchPlugin_Circle::ID();
187 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
188 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
189 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
190 theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
191 + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
195 else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
196 if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
197 theError = "It refers to an " + SketchPlugin_Circle::ID() + ", but " + aParamA + " is not a "
198 + SketchPlugin_Line::ID();
203 theError = "It refers to " + aRefFea->getKind() + ", but should refer to " + SketchPlugin_Line::ID()
204 + " or " + SketchPlugin_Arc::ID() + " or " + SketchPlugin_Circle::ID();
210 theError = "It uses an empty object";
217 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
218 const std::list<std::string>& theArguments,
219 std::string& theError) const
221 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
222 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
226 std::shared_ptr<SketchPlugin_Feature> aFeature =
227 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
231 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
233 SketchPlugin_Sketch* aSketch = aFeature->sketch();
234 int aNbFeatures = aSketch->numberOfSubs();
235 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
236 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
237 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
239 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
240 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
241 if (aRefAttr->isObject()) {
242 if (aRefAttr->object() == aRAttr->object()) {
243 ObjectPtr anObject = aRefAttr->object();
244 std::string aName = anObject.get() ? anObject->data()->name() : "";
245 theError = "The object " + aName + " has been already fixed.";
249 else if (aRefAttr->attr() == aRAttr->attr()) {
250 AttributePtr anAttribute = aRefAttr->attr();
251 std::string aName = anAttribute.get() ? anAttribute->id() : "";
252 theError = "The attribute " + aName + " has been already fixed.";
259 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
260 const std::list<std::string>& theArguments,
261 std::string& theError) const
263 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
264 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
268 std::string aParamA = theArguments.front();
269 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
270 AttributeRefAttrPtr aRefAttr[2];
271 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
272 aRefAttr[1] = aFeature->data()->refattr(aParamA);
274 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
275 theError = "Attributes can not be used in equal constraint";
279 std::string aType[2];
280 std::list<std::string> anArguments;
281 for (int i = 0; i < 2; i++) {
282 ObjectPtr anObject = aRefAttr[i]->object();
283 if (!anObject.get()) {
284 theError = "An empty object is used.";
288 aFeature = ModelAPI_Feature::feature(anObject);
289 if (!aFeature.get()) {
290 theError = "An empty feature is used.";
294 aType[i] = aFeature->getKind();
295 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
296 aFeature->getKind() != SketchPlugin_Circle::ID() &&
297 aFeature->getKind() != SketchPlugin_Arc::ID()) {
298 theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
299 SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " +
300 SketchPlugin_Arc::ID();
301 // wrong type of attribute
306 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
307 aType[0] != aType[1]) {
308 theError = "Feature with kinds " + aType[0] + " and " + aType[1] + "can not be equal.";
314 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
315 const std::list<std::string>& theArguments,
316 std::string& theError) const
318 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
319 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
323 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
324 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
326 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
327 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
328 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
330 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
331 ObjectPtr aSelObject = aSelAttr->object(anInd);
332 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
333 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
334 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
335 if (aSelObject == *aMirIter) {
336 theError = "The object " + aName + " is a result of mirror";
343 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
344 const std::list<std::string>& theArguments,
345 std::string& theError) const
347 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
348 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
352 // there is a check whether the feature contains a point and a linear edge or two point values
353 std::string aParamA = theArguments.front();
354 SessionPtr aMgr = ModelAPI_Session::get();
355 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
357 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
358 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
360 theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
364 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
366 // first attribute is a point, it may coincide with any object
367 if (!aRefAttrA->isObject())
370 ObjectPtr anObject = aRefAttrA->object();
371 if (!anObject.get()) {
372 theError = aParamA + " attribute has an empty object";
375 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
376 if (!aFeature.get()) {
377 theError = aParamA + " attribute has an empty feature";
381 if (aFeature->getKind() == SketchPlugin_Point::ID())
385 // second attribute is a point, it may coincide with any object
386 if (!aRefAttrB->isObject())
389 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
391 theError = theAttribute->id() + " attribute has an empty object";
394 if (aFeature->getKind() == SketchPlugin_Point::ID())
397 theError = "There is no an attribute filled by a point";
402 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
403 const std::list<std::string>& theArguments,
404 std::string& theError) const
406 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
407 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
411 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
412 AttributeRefListPtr aSelAttr =
413 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
415 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
416 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
417 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
418 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
419 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
420 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
422 std::list<ObjectPtr>::iterator anObjIter;
423 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
424 ObjectPtr aSelObject = aSelAttr->object(anInd);
425 anObjIter = anInitialObjects.begin();
426 for (; anObjIter != anInitialObjects.end(); anObjIter++)
427 if (aSelObject == *anObjIter)
429 if (anObjIter != anInitialObjects.end())
431 anObjIter = aCopiedObjects.begin();
432 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
433 if (aSelObject == *anObjIter) {
434 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
435 theError = "The object " + aName + " is a result of copy";
442 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
443 const std::list<std::string>& theArguments,
444 std::string& theError) const
446 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
448 if (!aAttributeString->value().empty()) {
449 theError = aAttributeString->value();
456 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
461 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList, const FeaturePtr theFeature)
463 for(std::set<AttributePtr>::const_iterator anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
464 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
465 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
466 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
467 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
468 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
469 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
470 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
471 if(anAttrRefA.get()) {
472 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
474 DocumentPtr aDoc = aResA->document();
476 FeaturePtr aFeatureA = aDoc->feature(aResA);
477 if(aFeatureA.get() && aFeatureA == theFeature) {
483 if(anAttrRefB.get()) {
484 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
486 DocumentPtr aDoc = aResB->document();
488 FeaturePtr aFeatureB = aDoc->feature(aResB);
489 if(aFeatureB.get() && aFeatureB == theFeature) {
500 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
501 const std::list<std::string>& theArguments,
502 std::string& theError) const
504 std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature = std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
505 AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
506 if(aPointsRefList->size() == 0) {
507 theError = "Error: List of points is empty.";
511 std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap = aFilletFeature->pointsFeaturesMap();
512 std::set<AttributePtr> aSetOfPointsOnResultEdges;
513 for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator aPointsIter = aPointsFeaturesMap.begin();
514 aPointsIter != aPointsFeaturesMap.end();
516 const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
517 const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
518 for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
519 aResultIter != aResultEdges.cend();
521 FeaturePtr aResultFeature = *aResultIter;
522 if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
523 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
524 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
525 } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
526 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
527 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
532 std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
533 for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
534 ObjectPtr anObject = (*aPointsIt).first;
535 AttributePtr aPointAttribute = (*aPointsIt).second;
536 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
538 // If we alredy have some result then:
539 // - if it is the same point all ok, just skip it
540 // - if it is point on the fillet result edge then it is not valid
541 if(!aPointsFeaturesMap.empty()) {
542 if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
546 // Check that selected point not on the one of the fillet result edge.
547 if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
552 // Obtain constraint coincidence for the fillet point.
553 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
554 FeaturePtr aConstraintCoincidence;
555 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
556 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
557 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
558 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
559 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
560 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
561 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
562 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
563 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
564 AttributePtr anAttrA = anAttrRefA->attr();
565 if(aPointAttribute == anAttrA) {
566 aConstraintCoincidence = aConstrFeature;
570 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
571 AttributePtr anAttrB = anAttrRefB->attr();
572 if(aPointAttribute == anAttrB) {
573 aConstraintCoincidence = aConstrFeature;
580 if(!aConstraintCoincidence.get()) {
581 theError = "Error: one of the selected point does not have coicidence.";
585 // Get coincides from constraint.
586 std::set<FeaturePtr> aCoinsides;
587 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
588 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
590 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
591 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
594 // Remove points from set of coincides.
595 std::set<FeaturePtr> aNewSetOfCoincides;
596 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
597 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
598 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
601 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
602 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
603 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
604 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
605 if(aDistSelectedArcCenter < tolerance) {
609 aNewSetOfCoincides.insert(*anIt);
611 aCoinsides = aNewSetOfCoincides;
613 // If we still have more than two coincides remove auxilary entities from set of coincides.
614 if(aCoinsides.size() > 2) {
615 aNewSetOfCoincides.clear();
616 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
617 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
618 aNewSetOfCoincides.insert(*anIt);
621 aCoinsides = aNewSetOfCoincides;
624 if(aCoinsides.size() != 2) {
625 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
629 // Check that selected edges don't have tangent constraint.
630 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
631 FeaturePtr aFirstFeature = *anIt++;
632 FeaturePtr aSecondFeature = *anIt;
633 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
634 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
635 theError = "Error: Edges in selected point has tangent constraint.";
639 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
640 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin(); aResIt != aFirstResults.end(); ++aResIt) {
641 ResultPtr aRes = *aResIt;
642 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
643 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
644 theError = "Error: Edges in selected point has tangent constraint.";
649 // Check that lines not collinear
650 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
651 std::string aStartAttr = SketchPlugin_Line::START_ID();
652 std::string anEndAttr = SketchPlugin_Line::END_ID();
653 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
654 aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
655 aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
656 aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
657 aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
658 double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
659 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
660 double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
661 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
662 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
671 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
672 const std::list<std::string>& theArguments,
673 std::string& theError) const
675 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
676 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
680 // there is a check whether the feature contains a point and a linear edge or two point values
681 std::string aParamA = theArguments.front();
682 SessionPtr aMgr = ModelAPI_Session::get();
683 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
685 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
686 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
687 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
689 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
692 for (int i = 0; i < 2; ++i) {
693 if (!aRefAttrs[i]->isObject())
696 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
698 if (aNbPoints + aNbLines != 0)
703 if (aFeature->getKind() == SketchPlugin_Point::ID())
705 else if (aFeature->getKind() == SketchPlugin_Line::ID())
710 if (aNbPoints != 1 || aNbLines != 1) {
711 theError = "Middle point constraint allows points and lines only";
717 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
718 const std::list<std::string>& /*theArguments*/,
719 std::string& theError) const
721 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
722 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
725 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
726 AttributePtr anAttr = aRefAttr->attr();
728 theError = "The attribute " + theAttribute->id() + " should be a point";
732 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
733 const std::string& aFeatureType = anAttrFeature->getKind();
734 if (aFeatureType == SketchPlugin_Arc::ID()) {
735 // selected point should not be a center of arc
736 const std::string& aPntId = anAttr->id();
737 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
738 theError = "The attribute " + aPntId + " is not supported";
742 else if (aFeatureType == SketchPlugin_Line::ID()) {
743 // selected point should be bound point of line
744 const std::string& aPntId = anAttr->id();
745 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
746 theError = "The attribute " + aPntId + " is not supported";
751 theError = "Unable to build tangent arc on " + anAttrFeature->getKind();
755 // Check the tangent point is equal to arc end
756 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
757 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
758 anArc->attribute(SketchPlugin_Arc::END_ID()));
759 if (anEndPoint->isInitialized()) {
760 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
761 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
762 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
763 theError = "Unable to build arc on same points";
771 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
772 const std::list<std::string>& theArguments,
773 std::string& theError) const
775 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
776 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
779 AttributeSelectionPtr aLineAttr =
780 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
781 std::shared_ptr<GeomAPI_Edge> anEdge;
782 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
783 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
784 } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
785 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
788 if (!anEdge || !anEdge->isLine()) {
789 theError = "The attribute " + theAttribute->id() + " should be a line";
793 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
796 std::shared_ptr<SketchPlugin_Sketch> aSketch;
797 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
798 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
799 for (; anIt != aRefs.end(); ++anIt) {
800 CompositeFeaturePtr aComp =
801 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
802 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
803 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
808 theError = "There is no sketch referring to the current feature";
812 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
813 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
814 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;