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*>(aFactory->validator("GeomValidators_ShapeType"));
77 std::list<std::string> anArguments;
78 anArguments.push_back("circle");
79 Events_InfoMessage aCircleError;
80 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
81 // the circle line is not a valid case
83 theError = "Circle can not be used in distance constraint";
88 anArguments.push_back("line");
89 Events_InfoMessage aLineError;
90 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
91 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
93 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
94 // If it is a line then we have to check that first attribute id not a line
95 std::shared_ptr<SketchPlugin_Feature> aSFeature =
96 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
97 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
98 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
99 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
100 aFeature->data(), aParamA, aPlane);
102 theError = "One of parameters of distance constraint should be a point";
111 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
113 FeaturePtr aCoincidenceFeature = SketchPlugin_ConstraintCoincidence::findCoincidenceFeature
114 (theFeature1, theFeature2);
115 return aCoincidenceFeature.get() != NULL;
118 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
119 const std::list<std::string>& theArguments,
120 Events_InfoMessage& theError) const
122 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
123 theError = "The attribute with the %1 type is not processed";
124 theError.arg(theAttribute->attributeType());
128 // there is a check whether the feature contains a point and a linear edge or two point values
129 std::string aParamA = theArguments.front();
130 SessionPtr aMgr = ModelAPI_Session::get();
131 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
133 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
134 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
136 bool isObject = aRefAttr->isObject();
137 ObjectPtr anObject = aRefAttr->object();
138 if (isObject && anObject.get()) {
139 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
141 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
142 ObjectPtr aOtherObject = aOtherAttr->object();
143 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
147 if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
148 aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
149 !hasCoincidentPoint(aRefFea, aOtherFea))
152 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
153 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
154 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
155 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
156 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
157 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
161 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
162 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
163 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
164 theError = "It refers to an %1, but %2 is not a %3 or an %4";
165 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
166 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID());
170 else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
171 if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
172 theError = "It refers to an %1, but %2 is not a %3";
173 theError.arg(SketchPlugin_Circle::ID()).arg(aParamA)
174 .arg(SketchPlugin_Line::ID());
179 theError = "It refers to %1, but should refer to %2 or %3 or %4";
180 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
181 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
187 theError = "It uses an empty object";
194 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
195 const std::list<std::string>& theArguments,
196 Events_InfoMessage& theError) const
198 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
199 theError = "The attribute with the %1 type is not processed";
200 theError.arg(theAttribute->attributeType());
204 std::shared_ptr<SketchPlugin_Feature> aFeature =
205 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
209 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
211 SketchPlugin_Sketch* aSketch = aFeature->sketch();
212 int aNbFeatures = aSketch->numberOfSubs();
213 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
214 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
215 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
217 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
218 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
219 if (aRefAttr->isObject()) {
220 if (aRefAttr->object() == aRAttr->object()) {
221 ObjectPtr anObject = aRefAttr->object();
222 std::string aName = anObject.get() ? anObject->data()->name() : "";
223 theError = "The object %1 has been already fixed.";
228 else if (aRefAttr->attr() == aRAttr->attr()) {
229 AttributePtr anAttribute = aRefAttr->attr();
230 std::string aName = anAttribute.get() ? anAttribute->id() : "";
231 theError = "The attribute %1 has been already fixed.";
239 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
240 const std::list<std::string>& theArguments,
241 Events_InfoMessage& theError) const
243 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
244 theError = "The attribute with the %1 type is not processed";
245 theError.arg(theAttribute->attributeType());
249 std::string aParamA = theArguments.front();
250 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
251 AttributeRefAttrPtr aRefAttr[2];
252 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
253 aRefAttr[1] = aFeature->data()->refattr(aParamA);
255 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
256 theError = "Attributes can not be used in equal constraint";
260 std::string aType[2];
261 std::list<std::string> anArguments;
262 for (int i = 0; i < 2; i++) {
263 ObjectPtr anObject = aRefAttr[i]->object();
264 if (!anObject.get()) {
265 theError = "An empty object is used.";
269 aFeature = ModelAPI_Feature::feature(anObject);
270 if (!aFeature.get()) {
271 theError = "An empty feature is used.";
275 aType[i] = aFeature->getKind();
276 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
277 aFeature->getKind() != SketchPlugin_Circle::ID() &&
278 aFeature->getKind() != SketchPlugin_Arc::ID()) {
279 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
280 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
281 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
282 // wrong type of attribute
287 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
288 aType[0] != aType[1]) {
289 theError = "Feature with kinds %1 and %2 can not be equal.";
290 theError.arg(aType[0]).arg(aType[1]);
296 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
297 const std::list<std::string>& theArguments,
298 Events_InfoMessage& theError) const
300 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
301 theError = "The attribute with the %1 type is not processed";
302 theError.arg(theAttribute->attributeType());
306 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
307 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
309 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
310 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
311 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
313 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
314 ObjectPtr aSelObject = aSelAttr->object(anInd);
315 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
316 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
317 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
318 if (aSelObject == *aMirIter) {
319 theError = "The object %1 is a result of mirror";
327 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
328 const std::list<std::string>& theArguments,
329 Events_InfoMessage& theError) const
331 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
332 theError = "The attribute with the %1 type is not processed";
333 theError.arg(theAttribute->attributeType());
337 // there is a check whether the feature contains a point and a linear edge or two point values
338 std::string aParamA = theArguments.front();
339 SessionPtr aMgr = ModelAPI_Session::get();
340 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
342 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
343 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
345 theError = "The %1 attribute should be %2";
346 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
350 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
352 // first attribute is a point, it may coincide with any object
353 if (!aRefAttrA->isObject())
356 ObjectPtr anObject = aRefAttrA->object();
357 if (!anObject.get()) {
358 theError = "%1 attribute has an empty object";
359 theError.arg(aParamA);
362 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
363 if (!aFeature.get()) {
364 theError = "%1 attribute has an empty feature";
365 theError.arg(aParamA);
369 if (aFeature->getKind() == SketchPlugin_Point::ID())
373 // second attribute is a point, it may coincide with any object
374 if (!aRefAttrB->isObject())
377 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
379 theError = "%1 attribute has an empty object";
380 theError.arg(theAttribute->id());
383 if (aFeature->getKind() == SketchPlugin_Point::ID())
386 theError = "There is no an attribute filled by a point";
391 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
392 const std::list<std::string>& theArguments,
393 Events_InfoMessage& theError) const
395 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
396 theError = "The attribute with the %1 type is not processed";
397 theError.arg(theAttribute->attributeType());
401 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
402 AttributeRefListPtr aSelAttr =
403 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
405 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
406 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
407 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
408 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
409 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
410 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
412 std::list<ObjectPtr>::iterator anObjIter;
413 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
414 ObjectPtr aSelObject = aSelAttr->object(anInd);
415 anObjIter = anInitialObjects.begin();
416 for (; anObjIter != anInitialObjects.end(); anObjIter++)
417 if (aSelObject == *anObjIter)
419 if (anObjIter != anInitialObjects.end())
421 anObjIter = aCopiedObjects.begin();
422 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
423 if (aSelObject == *anObjIter) {
424 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
425 theError = "The object %1 is a result of copy";
433 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
434 const std::list<std::string>& theArguments,
435 Events_InfoMessage& theError) const
437 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
439 if (!aAttributeString->value().empty()) {
440 theError = aAttributeString->value();
447 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
452 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList, const FeaturePtr theFeature)
454 for(std::set<AttributePtr>::const_iterator anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
455 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
456 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
457 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
458 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
459 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
460 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
461 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
462 if(anAttrRefA.get()) {
463 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
465 DocumentPtr aDoc = aResA->document();
467 FeaturePtr aFeatureA = aDoc->feature(aResA);
468 if(aFeatureA.get() && aFeatureA == theFeature) {
474 if(anAttrRefB.get()) {
475 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
477 DocumentPtr aDoc = aResB->document();
479 FeaturePtr aFeatureB = aDoc->feature(aResB);
480 if(aFeatureB.get() && aFeatureB == theFeature) {
491 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
492 const std::list<std::string>& theArguments,
493 Events_InfoMessage& theError) const
495 std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature = std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
496 AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
497 if(aPointsRefList->size() == 0) {
498 theError = "Error: List of points is empty.";
502 std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap = aFilletFeature->pointsFeaturesMap();
503 std::set<AttributePtr> aSetOfPointsOnResultEdges;
504 for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator aPointsIter = aPointsFeaturesMap.begin();
505 aPointsIter != aPointsFeaturesMap.end();
507 const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
508 const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
509 for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
510 aResultIter != aResultEdges.cend();
512 FeaturePtr aResultFeature = *aResultIter;
513 if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
514 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
515 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
516 } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
517 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
518 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
523 std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
524 for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
525 ObjectPtr anObject = (*aPointsIt).first;
526 AttributePtr aPointAttribute = (*aPointsIt).second;
527 if (!aPointAttribute.get())
529 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
531 // If we alredy have some result then:
532 // - if it is the same point all ok, just skip it
533 // - if it is point on the fillet result edge then it is not valid
534 if(!aPointsFeaturesMap.empty()) {
535 if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
539 // Check that selected point not on the one of the fillet result edge.
540 if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
545 // Obtain constraint coincidence for the fillet point.
546 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
547 FeaturePtr aConstraintCoincidence;
548 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
549 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
550 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
551 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
552 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
553 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
554 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
555 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
556 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
557 AttributePtr anAttrA = anAttrRefA->attr();
558 if(aPointAttribute == anAttrA) {
559 aConstraintCoincidence = aConstrFeature;
563 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
564 AttributePtr anAttrB = anAttrRefB->attr();
565 if(aPointAttribute == anAttrB) {
566 aConstraintCoincidence = aConstrFeature;
573 if(!aConstraintCoincidence.get()) {
574 theError = "Error: one of the selected point does not have coicidence.";
578 // Get coincides from constraint.
579 std::set<FeaturePtr> aCoinsides;
580 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
581 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
583 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
584 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
587 // Remove points from set of coincides.
588 std::set<FeaturePtr> aNewSetOfCoincides;
589 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
590 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
591 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
594 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
595 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
596 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
597 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
598 if(aDistSelectedArcCenter < tolerance) {
602 aNewSetOfCoincides.insert(*anIt);
604 aCoinsides = aNewSetOfCoincides;
606 // If we still have more than two coincides remove auxilary entities from set of coincides.
607 if(aCoinsides.size() > 2) {
608 aNewSetOfCoincides.clear();
609 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
610 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
611 aNewSetOfCoincides.insert(*anIt);
614 aCoinsides = aNewSetOfCoincides;
617 if(aCoinsides.size() != 2) {
618 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
622 // Check that selected edges don't have tangent constraint.
623 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
624 FeaturePtr aFirstFeature = *anIt++;
625 FeaturePtr aSecondFeature = *anIt;
626 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
627 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
628 theError = "Error: Edges in selected point has tangent constraint.";
632 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
633 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin(); aResIt != aFirstResults.end(); ++aResIt) {
634 ResultPtr aRes = *aResIt;
635 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
636 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
637 theError = "Error: Edges in selected point has tangent constraint.";
642 // Check that lines not collinear
643 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
644 std::string aStartAttr = SketchPlugin_Line::START_ID();
645 std::string anEndAttr = SketchPlugin_Line::END_ID();
646 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
647 aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
648 aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
649 aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
650 aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
651 double aCheck1 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
652 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
653 double aCheck2 = abs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
654 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
655 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
664 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
665 const std::list<std::string>& theArguments,
666 Events_InfoMessage& theError) const
668 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
669 theError = "The attribute with the %1 type is not processed";
670 theError.arg(theAttribute->attributeType());
674 // there is a check whether the feature contains a point and a linear edge or two point values
675 std::string aParamA = theArguments.front();
676 SessionPtr aMgr = ModelAPI_Session::get();
677 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
679 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
680 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
681 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
683 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
686 for (int i = 0; i < 2; ++i) {
687 if (!aRefAttrs[i]->isObject())
690 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
692 if (aNbPoints + aNbLines != 0)
697 if (aFeature->getKind() == SketchPlugin_Point::ID())
699 else if (aFeature->getKind() == SketchPlugin_Line::ID())
704 if (aNbPoints != 1 || aNbLines != 1) {
705 theError = "Middle point constraint allows points and lines only";
711 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
712 const std::list<std::string>& /*theArguments*/,
713 Events_InfoMessage& theError) const
715 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
716 theError = "The attribute with the %1 type is not processed";
717 theError.arg(theAttribute->attributeType());
720 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
721 AttributePtr anAttr = aRefAttr->attr();
723 theError = "The attribute %1 should be a point";
724 theError.arg(theAttribute->id());
728 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
729 const std::string& aFeatureType = anAttrFeature->getKind();
730 if (aFeatureType == SketchPlugin_Arc::ID()) {
731 // selected point should not be a center of arc
732 const std::string& aPntId = anAttr->id();
733 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
734 theError = "The attribute %1 is not supported";
735 theError.arg(aPntId);
739 else if (aFeatureType == SketchPlugin_Line::ID()) {
740 // selected point should be bound point of line
741 const std::string& aPntId = anAttr->id();
742 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
743 theError = "The attribute %1 is not supported";
744 theError.arg(aPntId);
749 theError = "Unable to build tangent arc on %1";
750 theError.arg(anAttrFeature->getKind());
754 // Check the tangent point is equal to arc end
755 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
756 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
757 anArc->attribute(SketchPlugin_Arc::END_ID()));
758 if (anEndPoint->isInitialized()) {
759 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
760 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
761 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
762 theError = "Unable to build arc on same points";
770 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
771 const std::list<std::string>& theArguments,
772 Events_InfoMessage& theError) const
774 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
775 theError = "The attribute with the %1 type is not processed";
776 theError.arg(theAttribute->attributeType());
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 %1 should be a line";
790 theError.arg(theAttribute->id());
794 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
797 std::shared_ptr<SketchPlugin_Sketch> aSketch;
798 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
799 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
800 for (; anIt != aRefs.end(); ++anIt) {
801 CompositeFeaturePtr aComp =
802 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
803 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
804 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
809 theError = "There is no sketch referring to the current feature";
813 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
814 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
815 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
818 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
819 const std::list<std::string>& theArguments,
820 Events_InfoMessage& theError) const
824 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
825 theError = "The attribute with the %1 type is not processed";
826 theError.arg(theAttribute->attributeType());
829 AttributeReferencePtr aFeatureAttr =
830 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
832 ObjectPtr anAttrObject = aFeatureAttr->value();
833 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
837 std::string aKind = anAttrFeature->getKind();
838 if (aKind == SketchPlugin_Line::ID() ||
839 aKind == SketchPlugin_Arc::ID() ||
840 aKind == SketchPlugin_Circle::ID()) {
842 std::set<GeomShapePtr> anEdgeShapes;
843 ModelAPI_Tools::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
844 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
847 // coincidences to the feature
848 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
849 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature, SketchPlugin_ConstraintCoincidence::ID(),
850 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
852 GeomShapePtr anAttrShape = *anEdgeShapes.begin();
853 std::shared_ptr<SketchPlugin_Feature> aSFeature =
854 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
855 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
857 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
858 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
859 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
860 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
861 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
862 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
863 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
864 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
866 std::set<std::shared_ptr<GeomAPI_Pnt> > aPoints;
867 std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
868 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
869 aX->dir(), aDirY, aPoints, aPointToAttributes);
871 int aCoincidentToFeature = (int)aPoints.size();
872 if (aKind == SketchPlugin_Circle::ID())
873 aValid = aCoincidentToFeature >= 2;
875 aValid = aCoincidentToFeature >= 1;
881 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
882 const std::list<std::string>& theArguments,
883 Events_InfoMessage& theError) const
885 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
886 theError = "The attribute with the %1 type is not processed";
887 theError.arg(theAttribute->attributeType());
891 AttributeSelectionPtr aFeatureAttr =
892 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
893 std::shared_ptr<GeomAPI_Edge> anEdge;
894 if(aFeatureAttr && aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
895 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
896 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
897 aFeatureAttr->context()->shape()->isEdge()) {
898 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
902 theError = "The attribute %1 should be an edge";
903 theError.arg(theAttribute->id());
908 std::shared_ptr<SketchPlugin_Sketch> aSketch;
909 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
910 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
911 for (; anIt != aRefs.end(); ++anIt) {
912 CompositeFeaturePtr aComp =
913 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
914 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
915 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
920 theError = "There is no sketch referring to the current feature";
924 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
925 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
926 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
928 if (anEdge->isLine()) {
929 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
930 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
931 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
932 double aDot = aNormal->dot(aLineDir);
933 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
934 return (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
935 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
937 else if (anEdge->isCircle() || anEdge->isArc()) {
938 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
939 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
940 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
941 double aDot = fabs(aNormal->dot(aCircNormal));
942 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
943 return fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;