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 <Events_InfoMessage.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeRefAttrList.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultConstruction.h>
38 #include <ModelGeomAlgo_Point2D.h>
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_Lin.h>
42 #include <GeomAPI_Edge.h>
43 #include <GeomAPI_Vertex.h>
44 #include <GeomDataAPI_Point2D.h>
49 const double tolerance = 1.e-7;
51 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
52 const std::list<std::string>& theArguments,
53 Events_InfoMessage& theError) const
55 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
56 theError = "The attribute with the %1 type is not processed";
57 theError.arg(theAttribute->attributeType());
61 // there is a check whether the feature contains a point and a linear edge or two point values
62 std::string aParamA = theArguments.front();
63 SessionPtr aMgr = ModelAPI_Session::get();
64 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
66 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
67 bool isObject = aRefAttr->isObject();
69 // an attribute is a point. A point value is valid always for the distance
72 // 1. check whether the references object is a linear
73 ObjectPtr anObject = aRefAttr->object();
75 const ModelAPI_AttributeValidator* aShapeValidator =
76 dynamic_cast<const ModelAPI_AttributeValidator*>(
77 aFactory->validator("GeomValidators_ShapeType"));
78 std::list<std::string> anArguments;
79 anArguments.push_back("circle");
80 Events_InfoMessage aCircleError;
81 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
82 // the circle line is not a valid case
84 theError = "Circle can not be used in distance constraint";
89 anArguments.push_back("line");
90 Events_InfoMessage aLineError;
91 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
92 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
94 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
95 // If it is a line then we have to check that first attribute id not a line
96 std::shared_ptr<SketchPlugin_Feature> aSFeature =
97 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
98 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
99 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
100 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
101 aFeature->data(), aParamA, aPlane);
103 theError = "One of parameters of distance constraint should be a point";
111 static bool isCoincident(FeaturePtr theFeature1, FeaturePtr theFeature2)
113 AttributePtr aFeature1PointAttr[2];
114 if(theFeature1->getKind() == SketchPlugin_Line::ID()) {
115 aFeature1PointAttr[0] = theFeature1->attribute(SketchPlugin_Line::START_ID());
116 aFeature1PointAttr[1] = theFeature1->attribute(SketchPlugin_Line::END_ID());
117 } else if(theFeature1->getKind() == SketchPlugin_Arc::ID()) {
118 aFeature1PointAttr[0] = theFeature1->attribute(SketchPlugin_Arc::START_ID());
119 aFeature1PointAttr[1] = theFeature1->attribute(SketchPlugin_Arc::END_ID());
122 std::set<AttributePtr> aRefsList = theFeature1->data()->refsToMe();
123 for(std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
124 aRefIt != aRefsList.end();
127 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
128 if(aRefFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
130 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
131 AttributePtr anAttr = aRefAttr->attr();
132 if(anAttr != aFeature1PointAttr[0] && anAttr != aFeature1PointAttr[1])
135 // Get coincides from constraint.
136 std::set<FeaturePtr> aCoinsides;
137 SketchPlugin_Tools::findCoincidences(aRefFeature,
138 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
140 SketchPlugin_Tools::findCoincidences(aRefFeature,
141 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
144 if(aCoinsides.find(theFeature2) != aCoinsides.end()) {
152 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
154 if(theFeature1->getKind() == SketchPlugin_Circle::ID() ||
155 theFeature2->getKind() == SketchPlugin_Circle::ID()) {
159 return (isCoincident(theFeature1, theFeature2) && isCoincident(theFeature2, theFeature1));
162 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
163 const std::list<std::string>& theArguments,
164 Events_InfoMessage& theError) const
166 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
167 theError = "The attribute with the %1 type is not processed";
168 theError.arg(theAttribute->attributeType());
172 // there is a check whether the feature contains a point and a linear edge or two point values
173 std::string aParamA = theArguments.front();
174 SessionPtr aMgr = ModelAPI_Session::get();
175 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
177 FeaturePtr anAttributeFeature =
178 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
179 AttributeRefAttrPtr aRefAttr =
180 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
182 bool isObject = aRefAttr->isObject();
183 ObjectPtr anObject = aRefAttr->object();
184 if (isObject && anObject.get()) {
185 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
187 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
188 ObjectPtr aOtherObject = aOtherAttr->object();
189 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
193 if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
194 aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
195 !hasCoincidentPoint(aRefFea, aOtherFea))
198 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
199 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
200 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
201 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
202 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
203 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
207 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
208 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
209 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
210 theError = "It refers to an %1, but %2 is not a %3 or an %4";
211 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
212 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID());
216 else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
217 if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
218 theError = "It refers to an %1, but %2 is not a %3";
219 theError.arg(SketchPlugin_Circle::ID()).arg(aParamA)
220 .arg(SketchPlugin_Line::ID());
225 theError = "It refers to %1, but should refer to %2 or %3 or %4";
226 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
227 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
233 theError = "It uses an empty object";
240 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
241 const std::list<std::string>& theArguments,
242 Events_InfoMessage& theError) const
244 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
245 theError = "The attribute with the %1 type is not processed";
246 theError.arg(theAttribute->attributeType());
250 std::shared_ptr<SketchPlugin_Feature> aFeature =
251 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
255 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
257 SketchPlugin_Sketch* aSketch = aFeature->sketch();
258 int aNbFeatures = aSketch->numberOfSubs();
259 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
260 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
261 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
263 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
264 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
265 if (aRefAttr->isObject()) {
266 if (aRefAttr->object() == aRAttr->object()) {
267 ObjectPtr anObject = aRefAttr->object();
268 std::string aName = anObject.get() ? anObject->data()->name() : "";
269 theError = "The object %1 has been already fixed.";
274 else if (aRefAttr->attr() == aRAttr->attr()) {
275 AttributePtr anAttribute = aRefAttr->attr();
276 std::string aName = anAttribute.get() ? anAttribute->id() : "";
277 theError = "The attribute %1 has been already fixed.";
285 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
286 const std::list<std::string>& theArguments,
287 Events_InfoMessage& theError) const
289 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
290 theError = "The attribute with the %1 type is not processed";
291 theError.arg(theAttribute->attributeType());
295 std::string aParamA = theArguments.front();
296 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
297 AttributeRefAttrPtr aRefAttr[2];
298 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
299 aRefAttr[1] = aFeature->data()->refattr(aParamA);
301 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
302 theError = "Attributes can not be used in equal constraint";
306 std::string aType[2];
307 std::list<std::string> anArguments;
308 for (int i = 0; i < 2; i++) {
309 ObjectPtr anObject = aRefAttr[i]->object();
310 if (!anObject.get()) {
311 theError = "An empty object is used.";
315 aFeature = ModelAPI_Feature::feature(anObject);
316 if (!aFeature.get()) {
317 theError = "An empty feature is used.";
321 aType[i] = aFeature->getKind();
322 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
323 aFeature->getKind() != SketchPlugin_Circle::ID() &&
324 aFeature->getKind() != SketchPlugin_Arc::ID()) {
325 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
326 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
327 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
328 // wrong type of attribute
333 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
334 aType[0] != aType[1]) {
335 theError = "Feature with kinds %1 and %2 can not be equal.";
336 theError.arg(aType[0]).arg(aType[1]);
342 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
343 const std::list<std::string>& theArguments,
344 Events_InfoMessage& theError) const
346 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
347 theError = "The attribute with the %1 type is not processed";
348 theError.arg(theAttribute->attributeType());
352 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
353 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
355 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
356 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
357 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
359 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
360 ObjectPtr aSelObject = aSelAttr->object(anInd);
361 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
362 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
363 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
364 if (aSelObject == *aMirIter) {
365 theError = "The object %1 is a result of mirror";
373 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
374 const std::list<std::string>& theArguments,
375 Events_InfoMessage& theError) const
377 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
378 theError = "The attribute with the %1 type is not processed";
379 theError.arg(theAttribute->attributeType());
383 // there is a check whether the feature contains a point and a linear edge or two point values
384 std::string aParamA = theArguments.front();
385 SessionPtr aMgr = ModelAPI_Session::get();
386 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
388 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
389 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
391 theError = "The %1 attribute should be %2";
392 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
396 AttributeRefAttrPtr aRefAttrB =
397 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
399 // first attribute is a point, it may coincide with any object
400 if (!aRefAttrA->isObject())
403 ObjectPtr anObject = aRefAttrA->object();
404 if (!anObject.get()) {
405 theError = "%1 attribute has an empty object";
406 theError.arg(aParamA);
409 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
410 if (!aFeature.get()) {
411 theError = "%1 attribute has an empty feature";
412 theError.arg(aParamA);
416 if (aFeature->getKind() == SketchPlugin_Point::ID())
420 // second attribute is a point, it may coincide with any object
421 if (!aRefAttrB->isObject())
424 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
426 theError = "%1 attribute has an empty object";
427 theError.arg(theAttribute->id());
430 if (aFeature->getKind() == SketchPlugin_Point::ID())
433 theError = "There is no an attribute filled by a point";
438 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
439 const std::list<std::string>& theArguments,
440 Events_InfoMessage& theError) const
442 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
443 theError = "The attribute with the %1 type is not processed";
444 theError.arg(theAttribute->attributeType());
448 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
449 AttributeRefListPtr aSelAttr =
450 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
452 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
453 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
454 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
455 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
456 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
457 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
459 std::list<ObjectPtr>::iterator anObjIter;
460 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
461 ObjectPtr aSelObject = aSelAttr->object(anInd);
462 anObjIter = anInitialObjects.begin();
463 for (; anObjIter != anInitialObjects.end(); anObjIter++)
464 if (aSelObject == *anObjIter)
466 if (anObjIter != anInitialObjects.end())
468 anObjIter = aCopiedObjects.begin();
469 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
470 if (aSelObject == *anObjIter) {
471 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
472 theError = "The object %1 is a result of copy";
480 bool SketchPlugin_SolverErrorValidator::isValid(
481 const std::shared_ptr<ModelAPI_Feature>& theFeature,
482 const std::list<std::string>& theArguments,
483 Events_InfoMessage& theError) const
485 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
487 if (!aAttributeString->value().empty()) {
488 theError = aAttributeString->value();
495 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
496 std::string theAttribute)
501 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
502 const FeaturePtr theFeature)
504 for(std::set<AttributePtr>::const_iterator
505 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
506 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
507 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
508 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
509 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
510 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
511 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
512 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
513 if(anAttrRefA.get()) {
514 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
516 DocumentPtr aDoc = aResA->document();
518 FeaturePtr aFeatureA = aDoc->feature(aResA);
519 if(aFeatureA.get() && aFeatureA == theFeature) {
525 if(anAttrRefB.get()) {
526 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
528 DocumentPtr aDoc = aResB->document();
530 FeaturePtr aFeatureB = aDoc->feature(aResB);
531 if(aFeatureB.get() && aFeatureB == theFeature) {
542 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
543 const std::list<std::string>& theArguments,
544 Events_InfoMessage& theError) const
546 std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature =
547 std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
548 AttributeRefAttrListPtr aPointsRefList =
549 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
550 if(aPointsRefList->size() == 0) {
551 theError = "Error: List of points is empty.";
555 std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap =
556 aFilletFeature->pointsFeaturesMap();
557 std::set<AttributePtr> aSetOfPointsOnResultEdges;
558 for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator
559 aPointsIter = aPointsFeaturesMap.begin();
560 aPointsIter != aPointsFeaturesMap.end();
562 const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
563 const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
564 for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
565 aResultIter != aResultEdges.cend();
567 FeaturePtr aResultFeature = *aResultIter;
568 if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
569 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
570 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
571 } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
572 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
573 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
578 std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
579 for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator
580 aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
581 ObjectPtr anObject = (*aPointsIt).first;
582 AttributePtr aPointAttribute = (*aPointsIt).second;
583 if (!aPointAttribute.get())
585 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
586 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
588 // If we alredy have some result then:
589 // - if it is the same point all ok, just skip it
590 // - if it is point on the fillet result edge then it is not valid
591 if(!aPointsFeaturesMap.empty()) {
592 if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
596 // Check that selected point not on the one of the fillet result edge.
597 if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
602 // Obtain constraint coincidence for the fillet point.
603 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
604 FeaturePtr aConstraintCoincidence;
605 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
606 anIt != aRefsList.cend(); ++anIt) {
607 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
608 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
609 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
610 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
611 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
612 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
613 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
614 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
615 AttributePtr anAttrA = anAttrRefA->attr();
616 if(aPointAttribute == anAttrA) {
617 aConstraintCoincidence = aConstrFeature;
621 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
622 AttributePtr anAttrB = anAttrRefB->attr();
623 if(aPointAttribute == anAttrB) {
624 aConstraintCoincidence = aConstrFeature;
631 if(!aConstraintCoincidence.get()) {
632 theError = "Error: one of the selected point does not have coicidence.";
636 // Get coincides from constraint.
637 std::set<FeaturePtr> aCoinsides;
638 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
639 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
641 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
642 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
645 // Remove points from set of coincides.
646 std::set<FeaturePtr> aNewSetOfCoincides;
647 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
648 anIt != aCoinsides.end(); ++anIt) {
649 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
650 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
653 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
654 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
655 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
656 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
657 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
658 if(aDistSelectedArcCenter < tolerance) {
662 aNewSetOfCoincides.insert(*anIt);
664 aCoinsides = aNewSetOfCoincides;
666 // If we still have more than two coincides remove auxilary entities from set of coincides.
667 if(aCoinsides.size() > 2) {
668 aNewSetOfCoincides.clear();
669 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
670 anIt != aCoinsides.end(); ++anIt) {
671 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
672 aNewSetOfCoincides.insert(*anIt);
675 aCoinsides = aNewSetOfCoincides;
678 if(aCoinsides.size() != 2) {
679 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
683 // Check that selected edges don't have tangent constraint.
684 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
685 FeaturePtr aFirstFeature = *anIt++;
686 FeaturePtr aSecondFeature = *anIt;
687 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
688 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
689 theError = "Error: Edges in selected point has tangent constraint.";
693 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
694 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
695 aResIt != aFirstResults.end(); ++aResIt) {
696 ResultPtr aRes = *aResIt;
697 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
698 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
699 theError = "Error: Edges in selected point has tangent constraint.";
704 // Check that lines not collinear
705 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
706 aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
707 std::string aStartAttr = SketchPlugin_Line::START_ID();
708 std::string anEndAttr = SketchPlugin_Line::END_ID();
709 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
711 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
712 aFirstFeature->attribute(aStartAttr))->pnt();
714 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
716 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
717 aSecondFeature->attribute(aStartAttr))->pnt();
719 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
720 aSecondFeature->attribute(anEndAttr))->pnt();
722 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
723 (aSecondStartPnt->y() - aFirstStartPnt->y()) -
724 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
726 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
727 (aSecondEndPnt->y() - aFirstStartPnt->y()) -
728 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
729 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
738 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
739 const std::list<std::string>& theArguments,
740 Events_InfoMessage& theError) const
742 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
743 theError = "The attribute with the %1 type is not processed";
744 theError.arg(theAttribute->attributeType());
748 // there is a check whether the feature contains a point and a linear edge or two point values
749 std::string aParamA = theArguments.front();
750 SessionPtr aMgr = ModelAPI_Session::get();
751 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
753 FeaturePtr anAttributeFeature =
754 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
755 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
756 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
758 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
761 for (int i = 0; i < 2; ++i) {
762 if (!aRefAttrs[i]->isObject())
765 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
767 if (aNbPoints + aNbLines != 0)
772 if (aFeature->getKind() == SketchPlugin_Point::ID())
774 else if (aFeature->getKind() == SketchPlugin_Line::ID())
779 if (aNbPoints != 1 || aNbLines != 1) {
780 theError = "Middle point constraint allows points and lines only";
786 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
787 const std::list<std::string>& /*theArguments*/,
788 Events_InfoMessage& theError) const
790 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
791 theError = "The attribute with the %1 type is not processed";
792 theError.arg(theAttribute->attributeType());
795 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
796 AttributePtr anAttr = aRefAttr->attr();
798 theError = "The attribute %1 should be a point";
799 theError.arg(theAttribute->id());
803 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
804 const std::string& aFeatureType = anAttrFeature->getKind();
805 if (aFeatureType == SketchPlugin_Arc::ID()) {
806 // selected point should not be a center of arc
807 const std::string& aPntId = anAttr->id();
808 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
809 theError = "The attribute %1 is not supported";
810 theError.arg(aPntId);
814 else if (aFeatureType == SketchPlugin_Line::ID()) {
815 // selected point should be bound point of line
816 const std::string& aPntId = anAttr->id();
817 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
818 theError = "The attribute %1 is not supported";
819 theError.arg(aPntId);
824 theError = "Unable to build tangent arc on %1";
825 theError.arg(anAttrFeature->getKind());
829 // Check the tangent point is equal to arc end
830 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
831 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
832 anArc->attribute(SketchPlugin_Arc::END_ID()));
833 if (anEndPoint->isInitialized()) {
834 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
835 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
836 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
837 theError = "Unable to build arc on same points";
845 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
846 const std::list<std::string>& theArguments,
847 Events_InfoMessage& theError) const
849 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
850 theError = "The attribute with the %1 type is not processed";
851 theError.arg(theAttribute->attributeType());
854 AttributeSelectionPtr aLineAttr =
855 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
856 std::shared_ptr<GeomAPI_Edge> anEdge;
857 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
858 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
859 } else if(aLineAttr->context() &&
860 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
861 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
864 if (!anEdge || !anEdge->isLine()) {
865 theError = "The attribute %1 should be a line";
866 theError.arg(theAttribute->id());
870 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
873 std::shared_ptr<SketchPlugin_Sketch> aSketch;
874 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
875 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
876 for (; anIt != aRefs.end(); ++anIt) {
877 CompositeFeaturePtr aComp =
878 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
879 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
880 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
885 theError = "There is no sketch referring to the current feature";
889 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
890 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
891 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
894 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
895 const std::list<std::string>& theArguments,
896 Events_InfoMessage& theError) const
900 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
901 theError = "The attribute with the %1 type is not processed";
902 theError.arg(theAttribute->attributeType());
905 AttributeReferencePtr aFeatureAttr =
906 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
908 ObjectPtr anAttrObject = aFeatureAttr->value();
909 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
913 std::string aKind = anAttrFeature->getKind();
914 if (aKind == SketchPlugin_Line::ID() ||
915 aKind == SketchPlugin_Arc::ID() ||
916 aKind == SketchPlugin_Circle::ID()) {
918 std::set<GeomShapePtr> anEdgeShapes;
919 ModelAPI_Tools::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
920 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
923 // coincidences to the feature
924 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
925 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
926 SketchPlugin_ConstraintCoincidence::ID(),
927 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
929 GeomShapePtr anAttrShape = *anEdgeShapes.begin();
930 std::shared_ptr<SketchPlugin_Feature> aSFeature =
931 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
932 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
934 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
935 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
936 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
937 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
938 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
939 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
940 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
941 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
943 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
944 std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> >
946 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
947 aX->dir(), aDirY, aPoints, aPointToAttributes);
949 int aCoincidentToFeature = (int)aPoints.size();
950 if (aKind == SketchPlugin_Circle::ID())
951 aValid = aCoincidentToFeature >= 2;
953 aValid = aCoincidentToFeature >= 1;
959 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
960 const std::list<std::string>& theArguments,
961 Events_InfoMessage& theError) const
963 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
964 theError = "The attribute with the %1 type is not processed";
965 theError.arg(theAttribute->attributeType());
969 AttributeSelectionPtr aFeatureAttr =
970 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
971 std::shared_ptr<GeomAPI_Edge> anEdge;
972 if (aFeatureAttr.get()) {
973 GeomShapePtr aVal = aFeatureAttr->value();
974 ResultPtr aRes = aFeatureAttr->context();
975 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
976 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
977 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
978 aFeatureAttr->context()->shape()->isEdge()) {
979 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
983 theError = "The attribute %1 should be an edge";
984 theError.arg(theAttribute->id());
989 std::shared_ptr<SketchPlugin_Sketch> aSketch;
990 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
991 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
992 for (; anIt != aRefs.end(); ++anIt) {
993 CompositeFeaturePtr aComp =
994 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
995 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
996 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1001 theError = "There is no sketch referring to the current feature";
1005 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1006 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1007 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1009 if (anEdge->isLine()) {
1010 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1011 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1012 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
1013 double aDot = aNormal->dot(aLineDir);
1014 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1015 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
1016 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
1018 theError = "Error: Edge is already in the sketch plane.";
1021 else if (anEdge->isCircle() || anEdge->isArc()) {
1022 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1023 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1024 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
1025 double aDot = fabs(aNormal->dot(aCircNormal));
1026 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1027 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
1029 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1030 : "Error: Arc is already in the sketch plane.");
1034 theError = "Error: Selected object is not line, circle or arc.";