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_Circ.h>
36 #include <GeomAPI_Lin.h>
37 #include <GeomAPI_Edge.h>
38 #include <GeomAPI_Vertex.h>
39 #include <GeomDataAPI_Point2D.h>
44 const double tolerance = 1.e-7;
46 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
47 const std::list<std::string>& theArguments,
48 std::string& theError) const
50 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
51 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
55 // there is a check whether the feature contains a point and a linear edge or two point values
56 std::string aParamA = theArguments.front();
57 SessionPtr aMgr = ModelAPI_Session::get();
58 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
60 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
61 bool isObject = aRefAttr->isObject();
63 // an attribute is a point. A point value is valid always for the distance
66 // 1. check whether the references object is a linear
67 ObjectPtr anObject = aRefAttr->object();
69 const ModelAPI_AttributeValidator* aShapeValidator =
70 dynamic_cast<const ModelAPI_AttributeValidator*>(aFactory->validator("GeomValidators_ShapeType"));
71 std::list<std::string> anArguments;
72 anArguments.push_back("circle");
73 std::string aCircleError;
74 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
75 // the circle line is not a valid case
77 theError = "Circle can not be used in distance constraint";
82 anArguments.push_back("line");
83 std::string aLineError;
84 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
85 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
87 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
88 // If it is a line then we have to check that first attribute id not a line
89 std::shared_ptr<SketchPlugin_Feature> aSFeature =
90 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
91 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
92 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
93 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
94 aFeature->data(), aParamA, aPlane);
96 theError = "One of parameters of distance constraint should be a point";
105 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
107 FeaturePtr aConstrFeature;
108 std::list<AttributePtr> anAttrList;
109 if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
110 theFeature2->getKind() == SketchPlugin_Circle::ID())
112 if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
113 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
114 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
115 } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
116 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
117 anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
120 const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
121 std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
122 for (; aRefIt != aRefsList.end(); ++aRefIt) {
123 aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
124 if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
126 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
127 AttributePtr anAttr = aRefAttr->attr();
128 if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
131 anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
132 if (anAttr == *aRefIt)
133 anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
135 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
138 anAttr = aRefAttr->attr();
139 for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
140 anIt != anAttrList.end(); ++anIt)
147 bool SketchPlugin_TangentAttrValidator::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 // there is a check whether the feature contains a point and a linear edge or two point values
157 std::string aParamA = theArguments.front();
158 SessionPtr aMgr = ModelAPI_Session::get();
159 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
161 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
162 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
164 bool isObject = aRefAttr->isObject();
165 ObjectPtr anObject = aRefAttr->object();
166 if (isObject && anObject.get()) {
167 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
169 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
170 ObjectPtr aOtherObject = aOtherAttr->object();
171 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
175 if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
176 aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
177 !hasCoincidentPoint(aRefFea, aOtherFea))
180 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
181 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
182 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
183 theError = "It refers to a " + SketchPlugin_Line::ID() + ", but " + aParamA + " is neither an "
184 + SketchPlugin_Arc::ID() + " nor " + SketchPlugin_Circle::ID();
188 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
189 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
190 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
191 theError = "It refers to an " + SketchPlugin_Arc::ID() + ", but " + aParamA + " is not a "
192 + SketchPlugin_Line::ID() + " or an " + SketchPlugin_Arc::ID();
196 else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
197 if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
198 theError = "It refers to an " + SketchPlugin_Circle::ID() + ", but " + aParamA + " is not a "
199 + SketchPlugin_Line::ID();
204 theError = "It refers to " + aRefFea->getKind() + ", but should refer to " + SketchPlugin_Line::ID()
205 + " or " + SketchPlugin_Arc::ID() + " or " + SketchPlugin_Circle::ID();
211 theError = "It uses an empty object";
218 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
219 const std::list<std::string>& theArguments,
220 std::string& theError) const
222 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
223 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
227 std::shared_ptr<SketchPlugin_Feature> aFeature =
228 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
232 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
234 SketchPlugin_Sketch* aSketch = aFeature->sketch();
235 int aNbFeatures = aSketch->numberOfSubs();
236 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
237 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
238 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
240 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
241 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
242 if (aRefAttr->isObject()) {
243 if (aRefAttr->object() == aRAttr->object()) {
244 ObjectPtr anObject = aRefAttr->object();
245 std::string aName = anObject.get() ? anObject->data()->name() : "";
246 theError = "The object " + aName + " has been already fixed.";
250 else if (aRefAttr->attr() == aRAttr->attr()) {
251 AttributePtr anAttribute = aRefAttr->attr();
252 std::string aName = anAttribute.get() ? anAttribute->id() : "";
253 theError = "The attribute " + aName + " has been already fixed.";
260 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
261 const std::list<std::string>& theArguments,
262 std::string& theError) const
264 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
265 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
269 std::string aParamA = theArguments.front();
270 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
271 AttributeRefAttrPtr aRefAttr[2];
272 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
273 aRefAttr[1] = aFeature->data()->refattr(aParamA);
275 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
276 theError = "Attributes can not be used in equal constraint";
280 std::string aType[2];
281 std::list<std::string> anArguments;
282 for (int i = 0; i < 2; i++) {
283 ObjectPtr anObject = aRefAttr[i]->object();
284 if (!anObject.get()) {
285 theError = "An empty object is used.";
289 aFeature = ModelAPI_Feature::feature(anObject);
290 if (!aFeature.get()) {
291 theError = "An empty feature is used.";
295 aType[i] = aFeature->getKind();
296 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
297 aFeature->getKind() != SketchPlugin_Circle::ID() &&
298 aFeature->getKind() != SketchPlugin_Arc::ID()) {
299 theError = "The " + aFeature->getKind() + " feature kind of attribute is wrong. It should be " +
300 SketchPlugin_Line::ID() + " or " + SketchPlugin_Circle::ID() + " or " +
301 SketchPlugin_Arc::ID();
302 // wrong type of attribute
307 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
308 aType[0] != aType[1]) {
309 theError = "Feature with kinds " + aType[0] + " and " + aType[1] + "can not be equal.";
315 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
316 const std::list<std::string>& theArguments,
317 std::string& theError) const
319 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
320 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
324 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
325 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
327 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
328 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
329 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
331 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
332 ObjectPtr aSelObject = aSelAttr->object(anInd);
333 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
334 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
335 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
336 if (aSelObject == *aMirIter) {
337 theError = "The object " + aName + " is a result of mirror";
344 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
345 const std::list<std::string>& theArguments,
346 std::string& theError) const
348 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
349 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
353 // there is a check whether the feature contains a point and a linear edge or two point values
354 std::string aParamA = theArguments.front();
355 SessionPtr aMgr = ModelAPI_Session::get();
356 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
358 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
359 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
361 theError = "The " + aParamA + " attribute " + " should be " + ModelAPI_AttributeRefAttr::typeId();
365 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
367 // first attribute is a point, it may coincide with any object
368 if (!aRefAttrA->isObject())
371 ObjectPtr anObject = aRefAttrA->object();
372 if (!anObject.get()) {
373 theError = aParamA + " attribute has an empty object";
376 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
377 if (!aFeature.get()) {
378 theError = aParamA + " attribute has an empty feature";
382 if (aFeature->getKind() == SketchPlugin_Point::ID())
386 // second attribute is a point, it may coincide with any object
387 if (!aRefAttrB->isObject())
390 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
392 theError = theAttribute->id() + " attribute has an empty object";
395 if (aFeature->getKind() == SketchPlugin_Point::ID())
398 theError = "There is no an attribute filled by a point";
403 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
404 const std::list<std::string>& theArguments,
405 std::string& theError) const
407 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
408 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
412 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
413 AttributeRefListPtr aSelAttr =
414 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
416 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
417 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
418 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
419 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
420 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
421 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
423 std::list<ObjectPtr>::iterator anObjIter;
424 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
425 ObjectPtr aSelObject = aSelAttr->object(anInd);
426 anObjIter = anInitialObjects.begin();
427 for (; anObjIter != anInitialObjects.end(); anObjIter++)
428 if (aSelObject == *anObjIter)
430 if (anObjIter != anInitialObjects.end())
432 anObjIter = aCopiedObjects.begin();
433 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
434 if (aSelObject == *anObjIter) {
435 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
436 theError = "The object " + aName + " is a result of copy";
443 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
444 const std::list<std::string>& theArguments,
445 std::string& theError) const
447 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
449 if (!aAttributeString->value().empty()) {
450 theError = aAttributeString->value();
457 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
462 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList, const FeaturePtr theFeature)
464 for(std::set<AttributePtr>::const_iterator anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
465 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
466 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
467 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
468 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
469 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
470 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
471 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
472 if(anAttrRefA.get()) {
473 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
475 DocumentPtr aDoc = aResA->document();
477 FeaturePtr aFeatureA = aDoc->feature(aResA);
478 if(aFeatureA.get() && aFeatureA == theFeature) {
484 if(anAttrRefB.get()) {
485 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
487 DocumentPtr aDoc = aResB->document();
489 FeaturePtr aFeatureB = aDoc->feature(aResB);
490 if(aFeatureB.get() && aFeatureB == theFeature) {
501 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
502 const std::list<std::string>& theArguments,
503 std::string& theError) const
505 std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature = std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
506 AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
507 if(aPointsRefList->size() == 0) {
508 theError = "Error: List of points is empty.";
512 std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap = aFilletFeature->pointsFeaturesMap();
513 std::set<AttributePtr> aSetOfPointsOnResultEdges;
514 for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator aPointsIter = aPointsFeaturesMap.begin();
515 aPointsIter != aPointsFeaturesMap.end();
517 const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
518 const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
519 for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
520 aResultIter != aResultEdges.cend();
522 FeaturePtr aResultFeature = *aResultIter;
523 if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
524 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
525 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
526 } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
527 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
528 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
533 std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
534 for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
535 ObjectPtr anObject = (*aPointsIt).first;
536 AttributePtr aPointAttribute = (*aPointsIt).second;
537 if (!aPointAttribute.get())
539 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
541 // If we alredy have some result then:
542 // - if it is the same point all ok, just skip it
543 // - if it is point on the fillet result edge then it is not valid
544 if(!aPointsFeaturesMap.empty()) {
545 if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
549 // Check that selected point not on the one of the fillet result edge.
550 if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
555 // Obtain constraint coincidence for the fillet point.
556 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
557 FeaturePtr aConstraintCoincidence;
558 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
559 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
560 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
561 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
562 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
563 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
564 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
565 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
566 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
567 AttributePtr anAttrA = anAttrRefA->attr();
568 if(aPointAttribute == anAttrA) {
569 aConstraintCoincidence = aConstrFeature;
573 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
574 AttributePtr anAttrB = anAttrRefB->attr();
575 if(aPointAttribute == anAttrB) {
576 aConstraintCoincidence = aConstrFeature;
583 if(!aConstraintCoincidence.get()) {
584 theError = "Error: one of the selected point does not have coicidence.";
588 // Get coincides from constraint.
589 std::set<FeaturePtr> aCoinsides;
590 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
591 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
593 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
594 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
597 // Remove points from set of coincides.
598 std::set<FeaturePtr> aNewSetOfCoincides;
599 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
600 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
601 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
604 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
605 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
606 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
607 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
608 if(aDistSelectedArcCenter < tolerance) {
612 aNewSetOfCoincides.insert(*anIt);
614 aCoinsides = aNewSetOfCoincides;
616 // If we still have more than two coincides remove auxilary entities from set of coincides.
617 if(aCoinsides.size() > 2) {
618 aNewSetOfCoincides.clear();
619 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
620 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
621 aNewSetOfCoincides.insert(*anIt);
624 aCoinsides = aNewSetOfCoincides;
627 if(aCoinsides.size() != 2) {
628 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
632 // Check that selected edges don't have tangent constraint.
633 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
634 FeaturePtr aFirstFeature = *anIt++;
635 FeaturePtr aSecondFeature = *anIt;
636 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
637 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
638 theError = "Error: Edges in selected point has tangent constraint.";
642 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
643 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin(); aResIt != aFirstResults.end(); ++aResIt) {
644 ResultPtr aRes = *aResIt;
645 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
646 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
647 theError = "Error: Edges in selected point has tangent constraint.";
652 // Check that lines not collinear
653 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
654 std::string aStartAttr = SketchPlugin_Line::START_ID();
655 std::string anEndAttr = SketchPlugin_Line::END_ID();
656 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
657 aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
658 aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
659 aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
660 aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
661 double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
662 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
663 double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
664 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
665 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
674 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
675 const std::list<std::string>& theArguments,
676 std::string& theError) const
678 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
679 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
683 // there is a check whether the feature contains a point and a linear edge or two point values
684 std::string aParamA = theArguments.front();
685 SessionPtr aMgr = ModelAPI_Session::get();
686 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
688 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
689 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
690 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
692 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
695 for (int i = 0; i < 2; ++i) {
696 if (!aRefAttrs[i]->isObject())
699 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
701 if (aNbPoints + aNbLines != 0)
706 if (aFeature->getKind() == SketchPlugin_Point::ID())
708 else if (aFeature->getKind() == SketchPlugin_Line::ID())
713 if (aNbPoints != 1 || aNbLines != 1) {
714 theError = "Middle point constraint allows points and lines only";
720 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
721 const std::list<std::string>& /*theArguments*/,
722 std::string& theError) const
724 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
725 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
728 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
729 AttributePtr anAttr = aRefAttr->attr();
731 theError = "The attribute " + theAttribute->id() + " should be a point";
735 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
736 const std::string& aFeatureType = anAttrFeature->getKind();
737 if (aFeatureType == SketchPlugin_Arc::ID()) {
738 // selected point should not be a center of arc
739 const std::string& aPntId = anAttr->id();
740 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
741 theError = "The attribute " + aPntId + " is not supported";
745 else if (aFeatureType == SketchPlugin_Line::ID()) {
746 // selected point should be bound point of line
747 const std::string& aPntId = anAttr->id();
748 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
749 theError = "The attribute " + aPntId + " is not supported";
754 theError = "Unable to build tangent arc on " + anAttrFeature->getKind();
758 // Check the tangent point is equal to arc end
759 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
760 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
761 anArc->attribute(SketchPlugin_Arc::END_ID()));
762 if (anEndPoint->isInitialized()) {
763 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
764 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
765 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
766 theError = "Unable to build arc on same points";
774 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
775 const std::list<std::string>& theArguments,
776 std::string& theError) const
778 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
779 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
782 AttributeSelectionPtr aLineAttr =
783 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
784 std::shared_ptr<GeomAPI_Edge> anEdge;
785 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
786 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
787 } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
788 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
791 if (!anEdge || !anEdge->isLine()) {
792 theError = "The attribute " + theAttribute->id() + " should be a line";
796 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
799 std::shared_ptr<SketchPlugin_Sketch> aSketch;
800 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
801 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
802 for (; anIt != aRefs.end(); ++anIt) {
803 CompositeFeaturePtr aComp =
804 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
805 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
806 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
811 theError = "There is no sketch referring to the current feature";
815 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
816 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
817 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
820 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
821 const std::list<std::string>& theArguments,
822 std::string& theError) const
824 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
825 theError = "The attribute with the " + theAttribute->attributeType() + " type is not processed";
829 AttributeSelectionPtr aFeatureAttr =
830 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
831 std::shared_ptr<GeomAPI_Edge> anEdge;
832 if(aFeatureAttr && aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
833 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
834 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
835 aFeatureAttr->context()->shape()->isEdge()) {
836 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
840 theError = "The attribute " + theAttribute->id() + " should be an edge";
845 std::shared_ptr<SketchPlugin_Sketch> aSketch;
846 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
847 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
848 for (; anIt != aRefs.end(); ++anIt) {
849 CompositeFeaturePtr aComp =
850 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
851 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
852 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
857 theError = "There is no sketch referring to the current feature";
861 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
862 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
864 if (anEdge->isLine()) {
865 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
866 double aDot = aNormal->dot(aLineDir);
867 return aDot > -1.0 + tolerance && aDot < 1.0 - tolerance;
869 else if (anEdge->isCircle() || anEdge->isArc()) {
870 std::shared_ptr<GeomAPI_Dir> aCircNormal = anEdge->circle()->normal();
871 double aDot = fabs(aNormal->dot(aCircNormal));
872 return fabs(aDot - 1.0) < tolerance * tolerance;