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";
110 static bool isCoincident(FeaturePtr theFeature1, FeaturePtr theFeature2)
112 AttributePtr aFeature1PointAttr[2];
113 if(theFeature1->getKind() == SketchPlugin_Line::ID()) {
114 aFeature1PointAttr[0] = theFeature1->attribute(SketchPlugin_Line::START_ID());
115 aFeature1PointAttr[1] = theFeature1->attribute(SketchPlugin_Line::END_ID());
116 } else if(theFeature1->getKind() == SketchPlugin_Arc::ID()) {
117 aFeature1PointAttr[0] = theFeature1->attribute(SketchPlugin_Arc::START_ID());
118 aFeature1PointAttr[1] = theFeature1->attribute(SketchPlugin_Arc::END_ID());
121 std::set<AttributePtr> aRefsList = theFeature1->data()->refsToMe();
122 for(std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
123 aRefIt != aRefsList.end();
126 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
127 if(aRefFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
129 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
130 AttributePtr anAttr = aRefAttr->attr();
131 if(anAttr != aFeature1PointAttr[0] && anAttr != aFeature1PointAttr[1])
134 // Get coincides from constraint.
135 std::set<FeaturePtr> aCoinsides;
136 SketchPlugin_Tools::findCoincidences(aRefFeature,
137 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
139 SketchPlugin_Tools::findCoincidences(aRefFeature,
140 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
143 if(aCoinsides.find(theFeature2) != aCoinsides.end()) {
151 static bool hasCoincidentPoint(FeaturePtr theFeature1, FeaturePtr theFeature2)
153 if(theFeature1->getKind() == SketchPlugin_Circle::ID() ||
154 theFeature2->getKind() == SketchPlugin_Circle::ID()) {
158 return (isCoincident(theFeature1, theFeature2) && isCoincident(theFeature2, theFeature1));
161 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
162 const std::list<std::string>& theArguments,
163 Events_InfoMessage& theError) const
165 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
166 theError = "The attribute with the %1 type is not processed";
167 theError.arg(theAttribute->attributeType());
171 // there is a check whether the feature contains a point and a linear edge or two point values
172 std::string aParamA = theArguments.front();
173 SessionPtr aMgr = ModelAPI_Session::get();
174 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
176 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
177 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
179 bool isObject = aRefAttr->isObject();
180 ObjectPtr anObject = aRefAttr->object();
181 if (isObject && anObject.get()) {
182 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
184 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
185 ObjectPtr aOtherObject = aOtherAttr->object();
186 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
190 if ((aRefFea->getKind() == SketchPlugin_Arc::ID() ||
191 aOtherFea->getKind() == SketchPlugin_Arc::ID()) &&
192 !hasCoincidentPoint(aRefFea, aOtherFea))
195 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
196 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
197 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
198 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
199 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
200 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
204 else if (aRefFea->getKind() == SketchPlugin_Arc::ID()) {
205 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
206 aOtherFea->getKind() != SketchPlugin_Arc::ID()) {
207 theError = "It refers to an %1, but %2 is not a %3 or an %4";
208 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
209 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID());
213 else if (aRefFea->getKind() == SketchPlugin_Circle::ID()) {
214 if (aOtherFea->getKind() != SketchPlugin_Line::ID()) {
215 theError = "It refers to an %1, but %2 is not a %3";
216 theError.arg(SketchPlugin_Circle::ID()).arg(aParamA)
217 .arg(SketchPlugin_Line::ID());
222 theError = "It refers to %1, but should refer to %2 or %3 or %4";
223 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
224 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
230 theError = "It uses an empty object";
237 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
238 const std::list<std::string>& theArguments,
239 Events_InfoMessage& theError) const
241 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
242 theError = "The attribute with the %1 type is not processed";
243 theError.arg(theAttribute->attributeType());
247 std::shared_ptr<SketchPlugin_Feature> aFeature =
248 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
252 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
254 SketchPlugin_Sketch* aSketch = aFeature->sketch();
255 int aNbFeatures = aSketch->numberOfSubs();
256 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
257 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
258 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
260 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
261 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
262 if (aRefAttr->isObject()) {
263 if (aRefAttr->object() == aRAttr->object()) {
264 ObjectPtr anObject = aRefAttr->object();
265 std::string aName = anObject.get() ? anObject->data()->name() : "";
266 theError = "The object %1 has been already fixed.";
271 else if (aRefAttr->attr() == aRAttr->attr()) {
272 AttributePtr anAttribute = aRefAttr->attr();
273 std::string aName = anAttribute.get() ? anAttribute->id() : "";
274 theError = "The attribute %1 has been already fixed.";
282 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
283 const std::list<std::string>& theArguments,
284 Events_InfoMessage& theError) const
286 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
287 theError = "The attribute with the %1 type is not processed";
288 theError.arg(theAttribute->attributeType());
292 std::string aParamA = theArguments.front();
293 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
294 AttributeRefAttrPtr aRefAttr[2];
295 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
296 aRefAttr[1] = aFeature->data()->refattr(aParamA);
298 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
299 theError = "Attributes can not be used in equal constraint";
303 std::string aType[2];
304 std::list<std::string> anArguments;
305 for (int i = 0; i < 2; i++) {
306 ObjectPtr anObject = aRefAttr[i]->object();
307 if (!anObject.get()) {
308 theError = "An empty object is used.";
312 aFeature = ModelAPI_Feature::feature(anObject);
313 if (!aFeature.get()) {
314 theError = "An empty feature is used.";
318 aType[i] = aFeature->getKind();
319 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
320 aFeature->getKind() != SketchPlugin_Circle::ID() &&
321 aFeature->getKind() != SketchPlugin_Arc::ID()) {
322 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
323 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
324 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
325 // wrong type of attribute
330 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
331 aType[0] != aType[1]) {
332 theError = "Feature with kinds %1 and %2 can not be equal.";
333 theError.arg(aType[0]).arg(aType[1]);
339 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
340 const std::list<std::string>& theArguments,
341 Events_InfoMessage& theError) const
343 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
344 theError = "The attribute with the %1 type is not processed";
345 theError.arg(theAttribute->attributeType());
349 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
350 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
352 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
353 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
354 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
356 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
357 ObjectPtr aSelObject = aSelAttr->object(anInd);
358 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
359 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
360 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
361 if (aSelObject == *aMirIter) {
362 theError = "The object %1 is a result of mirror";
370 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
371 const std::list<std::string>& theArguments,
372 Events_InfoMessage& theError) const
374 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
375 theError = "The attribute with the %1 type is not processed";
376 theError.arg(theAttribute->attributeType());
380 // there is a check whether the feature contains a point and a linear edge or two point values
381 std::string aParamA = theArguments.front();
382 SessionPtr aMgr = ModelAPI_Session::get();
383 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
385 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
386 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
388 theError = "The %1 attribute should be %2";
389 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
393 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
395 // first attribute is a point, it may coincide with any object
396 if (!aRefAttrA->isObject())
399 ObjectPtr anObject = aRefAttrA->object();
400 if (!anObject.get()) {
401 theError = "%1 attribute has an empty object";
402 theError.arg(aParamA);
405 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
406 if (!aFeature.get()) {
407 theError = "%1 attribute has an empty feature";
408 theError.arg(aParamA);
412 if (aFeature->getKind() == SketchPlugin_Point::ID())
416 // second attribute is a point, it may coincide with any object
417 if (!aRefAttrB->isObject())
420 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
422 theError = "%1 attribute has an empty object";
423 theError.arg(theAttribute->id());
426 if (aFeature->getKind() == SketchPlugin_Point::ID())
429 theError = "There is no an attribute filled by a point";
434 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
435 const std::list<std::string>& theArguments,
436 Events_InfoMessage& theError) const
438 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
439 theError = "The attribute with the %1 type is not processed";
440 theError.arg(theAttribute->attributeType());
444 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
445 AttributeRefListPtr aSelAttr =
446 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
448 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
449 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
450 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
451 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
452 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
453 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
455 std::list<ObjectPtr>::iterator anObjIter;
456 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
457 ObjectPtr aSelObject = aSelAttr->object(anInd);
458 anObjIter = anInitialObjects.begin();
459 for (; anObjIter != anInitialObjects.end(); anObjIter++)
460 if (aSelObject == *anObjIter)
462 if (anObjIter != anInitialObjects.end())
464 anObjIter = aCopiedObjects.begin();
465 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
466 if (aSelObject == *anObjIter) {
467 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
468 theError = "The object %1 is a result of copy";
476 bool SketchPlugin_SolverErrorValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
477 const std::list<std::string>& theArguments,
478 Events_InfoMessage& theError) const
480 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
482 if (!aAttributeString->value().empty()) {
483 theError = aAttributeString->value();
490 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
495 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList, const FeaturePtr theFeature)
497 for(std::set<AttributePtr>::const_iterator anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
498 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
499 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
500 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
501 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
502 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
503 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
504 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
505 if(anAttrRefA.get()) {
506 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
508 DocumentPtr aDoc = aResA->document();
510 FeaturePtr aFeatureA = aDoc->feature(aResA);
511 if(aFeatureA.get() && aFeatureA == theFeature) {
517 if(anAttrRefB.get()) {
518 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
520 DocumentPtr aDoc = aResB->document();
522 FeaturePtr aFeatureB = aDoc->feature(aResB);
523 if(aFeatureB.get() && aFeatureB == theFeature) {
534 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
535 const std::list<std::string>& theArguments,
536 Events_InfoMessage& theError) const
538 std::shared_ptr<SketchPlugin_ConstraintFillet> aFilletFeature = std::dynamic_pointer_cast<SketchPlugin_ConstraintFillet>(theAttribute->owner());
539 AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
540 if(aPointsRefList->size() == 0) {
541 theError = "Error: List of points is empty.";
545 std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures> aPointsFeaturesMap = aFilletFeature->pointsFeaturesMap();
546 std::set<AttributePtr> aSetOfPointsOnResultEdges;
547 for(std::map<AttributePtr, SketchPlugin_ConstraintFillet::FilletFeatures>::iterator aPointsIter = aPointsFeaturesMap.begin();
548 aPointsIter != aPointsFeaturesMap.end();
550 const SketchPlugin_ConstraintFillet::FilletFeatures& aFeatures = aPointsIter->second;
551 const std::list<FeaturePtr>& aResultEdges = aFeatures.resultEdges;
552 for(std::list<FeaturePtr>::const_iterator aResultIter = aResultEdges.cbegin();
553 aResultIter != aResultEdges.cend();
555 FeaturePtr aResultFeature = *aResultIter;
556 if(aResultFeature->getKind() == SketchPlugin_Line::ID()) {
557 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::START_ID()));
558 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Line::END_ID()));
559 } else if(aResultFeature->getKind() == SketchPlugin_Arc::ID()) {
560 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::START_ID()));
561 aSetOfPointsOnResultEdges.insert(aResultFeature->attribute(SketchPlugin_Arc::END_ID()));
566 std::list<std::pair<ObjectPtr, AttributePtr>> aPointsList = aPointsRefList->list();
567 for(std::list<std::pair<ObjectPtr, AttributePtr>>::const_iterator aPointsIt = aPointsList.cbegin(); aPointsIt != aPointsList.cend(); aPointsIt++) {
568 ObjectPtr anObject = (*aPointsIt).first;
569 AttributePtr aPointAttribute = (*aPointsIt).second;
570 if (!aPointAttribute.get())
572 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
574 // If we alredy have some result then:
575 // - if it is the same point all ok, just skip it
576 // - if it is point on the fillet result edge then it is not valid
577 if(!aPointsFeaturesMap.empty()) {
578 if(aPointsFeaturesMap.find(aPointAttribute) != aPointsFeaturesMap.end()) {
582 // Check that selected point not on the one of the fillet result edge.
583 if(aSetOfPointsOnResultEdges.find(aPointAttribute) != aSetOfPointsOnResultEdges.end()) {
588 // Obtain constraint coincidence for the fillet point.
589 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
590 FeaturePtr aConstraintCoincidence;
591 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
592 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
593 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
594 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
595 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
596 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
597 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
598 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
599 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
600 AttributePtr anAttrA = anAttrRefA->attr();
601 if(aPointAttribute == anAttrA) {
602 aConstraintCoincidence = aConstrFeature;
606 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
607 AttributePtr anAttrB = anAttrRefB->attr();
608 if(aPointAttribute == anAttrB) {
609 aConstraintCoincidence = aConstrFeature;
616 if(!aConstraintCoincidence.get()) {
617 theError = "Error: one of the selected point does not have coicidence.";
621 // Get coincides from constraint.
622 std::set<FeaturePtr> aCoinsides;
623 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
624 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
626 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
627 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
630 // Remove points from set of coincides.
631 std::set<FeaturePtr> aNewSetOfCoincides;
632 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
633 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
634 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
637 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
638 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
639 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
640 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
641 if(aDistSelectedArcCenter < tolerance) {
645 aNewSetOfCoincides.insert(*anIt);
647 aCoinsides = aNewSetOfCoincides;
649 // If we still have more than two coincides remove auxilary entities from set of coincides.
650 if(aCoinsides.size() > 2) {
651 aNewSetOfCoincides.clear();
652 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
653 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
654 aNewSetOfCoincides.insert(*anIt);
657 aCoinsides = aNewSetOfCoincides;
660 if(aCoinsides.size() != 2) {
661 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
665 // Check that selected edges don't have tangent constraint.
666 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
667 FeaturePtr aFirstFeature = *anIt++;
668 FeaturePtr aSecondFeature = *anIt;
669 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
670 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
671 theError = "Error: Edges in selected point has tangent constraint.";
675 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
676 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin(); aResIt != aFirstResults.end(); ++aResIt) {
677 ResultPtr aRes = *aResIt;
678 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
679 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
680 theError = "Error: Edges in selected point has tangent constraint.";
685 // Check that lines not collinear
686 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() && aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
687 std::string aStartAttr = SketchPlugin_Line::START_ID();
688 std::string anEndAttr = SketchPlugin_Line::END_ID();
689 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
690 aFirstStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(aStartAttr))->pnt();
691 aFirstEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
692 aSecondStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(aStartAttr))->pnt();
693 aSecondEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSecondFeature->attribute(anEndAttr))->pnt();
694 double aCheck1 = fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondStartPnt->y() - aFirstStartPnt->y()) -
695 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
696 double aCheck2 = fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) * (aSecondEndPnt->y() - aFirstStartPnt->y()) -
697 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
698 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
707 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
708 const std::list<std::string>& theArguments,
709 Events_InfoMessage& theError) const
711 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
712 theError = "The attribute with the %1 type is not processed";
713 theError.arg(theAttribute->attributeType());
717 // there is a check whether the feature contains a point and a linear edge or two point values
718 std::string aParamA = theArguments.front();
719 SessionPtr aMgr = ModelAPI_Session::get();
720 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
722 FeaturePtr anAttributeFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
723 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
724 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
726 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
729 for (int i = 0; i < 2; ++i) {
730 if (!aRefAttrs[i]->isObject())
733 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
735 if (aNbPoints + aNbLines != 0)
740 if (aFeature->getKind() == SketchPlugin_Point::ID())
742 else if (aFeature->getKind() == SketchPlugin_Line::ID())
747 if (aNbPoints != 1 || aNbLines != 1) {
748 theError = "Middle point constraint allows points and lines only";
754 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
755 const std::list<std::string>& /*theArguments*/,
756 Events_InfoMessage& theError) const
758 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
759 theError = "The attribute with the %1 type is not processed";
760 theError.arg(theAttribute->attributeType());
763 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
764 AttributePtr anAttr = aRefAttr->attr();
766 theError = "The attribute %1 should be a point";
767 theError.arg(theAttribute->id());
771 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
772 const std::string& aFeatureType = anAttrFeature->getKind();
773 if (aFeatureType == SketchPlugin_Arc::ID()) {
774 // selected point should not be a center of arc
775 const std::string& aPntId = anAttr->id();
776 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
777 theError = "The attribute %1 is not supported";
778 theError.arg(aPntId);
782 else if (aFeatureType == SketchPlugin_Line::ID()) {
783 // selected point should be bound point of line
784 const std::string& aPntId = anAttr->id();
785 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
786 theError = "The attribute %1 is not supported";
787 theError.arg(aPntId);
792 theError = "Unable to build tangent arc on %1";
793 theError.arg(anAttrFeature->getKind());
797 // Check the tangent point is equal to arc end
798 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
799 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
800 anArc->attribute(SketchPlugin_Arc::END_ID()));
801 if (anEndPoint->isInitialized()) {
802 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
803 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
804 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
805 theError = "Unable to build arc on same points";
813 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
814 const std::list<std::string>& theArguments,
815 Events_InfoMessage& theError) const
817 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
818 theError = "The attribute with the %1 type is not processed";
819 theError.arg(theAttribute->attributeType());
822 AttributeSelectionPtr aLineAttr =
823 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
824 std::shared_ptr<GeomAPI_Edge> anEdge;
825 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
826 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
827 } else if(aLineAttr->context() && aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
828 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
831 if (!anEdge || !anEdge->isLine()) {
832 theError = "The attribute %1 should be a line";
833 theError.arg(theAttribute->id());
837 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
840 std::shared_ptr<SketchPlugin_Sketch> aSketch;
841 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
842 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
843 for (; anIt != aRefs.end(); ++anIt) {
844 CompositeFeaturePtr aComp =
845 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
846 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
847 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
852 theError = "There is no sketch referring to the current feature";
856 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
857 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
858 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
861 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
862 const std::list<std::string>& theArguments,
863 Events_InfoMessage& theError) const
867 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
868 theError = "The attribute with the %1 type is not processed";
869 theError.arg(theAttribute->attributeType());
872 AttributeReferencePtr aFeatureAttr =
873 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
875 ObjectPtr anAttrObject = aFeatureAttr->value();
876 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
880 std::string aKind = anAttrFeature->getKind();
881 if (aKind == SketchPlugin_Line::ID() ||
882 aKind == SketchPlugin_Arc::ID() ||
883 aKind == SketchPlugin_Circle::ID()) {
885 std::set<GeomShapePtr> anEdgeShapes;
886 ModelAPI_Tools::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
887 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
890 // coincidences to the feature
891 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
892 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature, SketchPlugin_ConstraintCoincidence::ID(),
893 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
895 GeomShapePtr anAttrShape = *anEdgeShapes.begin();
896 std::shared_ptr<SketchPlugin_Feature> aSFeature =
897 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
898 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
900 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
901 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
902 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
903 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
904 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
905 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
906 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
907 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
909 std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
910 std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
911 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
912 aX->dir(), aDirY, aPoints, aPointToAttributes);
914 int aCoincidentToFeature = (int)aPoints.size();
915 if (aKind == SketchPlugin_Circle::ID())
916 aValid = aCoincidentToFeature >= 2;
918 aValid = aCoincidentToFeature >= 1;
924 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
925 const std::list<std::string>& theArguments,
926 Events_InfoMessage& theError) const
928 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
929 theError = "The attribute with the %1 type is not processed";
930 theError.arg(theAttribute->attributeType());
934 AttributeSelectionPtr aFeatureAttr =
935 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
936 std::shared_ptr<GeomAPI_Edge> anEdge;
937 if (aFeatureAttr.get()) {
938 GeomShapePtr aVal = aFeatureAttr->value();
939 ResultPtr aRes = aFeatureAttr->context();
940 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
941 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
942 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
943 aFeatureAttr->context()->shape()->isEdge()) {
944 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
948 theError = "The attribute %1 should be an edge";
949 theError.arg(theAttribute->id());
954 std::shared_ptr<SketchPlugin_Sketch> aSketch;
955 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
956 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
957 for (; anIt != aRefs.end(); ++anIt) {
958 CompositeFeaturePtr aComp =
959 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
960 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
961 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
966 theError = "There is no sketch referring to the current feature";
970 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
971 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
972 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
974 if (anEdge->isLine()) {
975 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
976 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
977 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
978 double aDot = aNormal->dot(aLineDir);
979 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
980 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
981 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
983 theError = "Error: Edge is already in the sketch plane.";
986 else if (anEdge->isCircle() || anEdge->isArc()) {
987 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
988 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
989 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
990 double aDot = fabs(aNormal->dot(aCircNormal));
991 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
992 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
994 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
995 : "Error: Arc is already in the sketch plane.");
999 theError = "Error: Selected object is not line, circle or arc.";