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_Fillet.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>
68 bool isObject = aRefAttr->isObject();
70 // an attribute is a point. A point value is valid always for the distance
73 // 1. check whether the references object is a linear
74 ObjectPtr anObject = aRefAttr->object();
76 const ModelAPI_AttributeValidator* aShapeValidator =
77 dynamic_cast<const ModelAPI_AttributeValidator*>(
78 aFactory->validator("GeomValidators_ShapeType"));
79 std::list<std::string> anArguments;
80 anArguments.push_back("circle");
81 Events_InfoMessage aCircleError;
82 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
83 // the circle line is not a valid case
85 theError = "Circle can not be used in distance constraint";
90 anArguments.push_back("line");
91 Events_InfoMessage aLineError;
92 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
93 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
95 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
96 // If it is a line then we have to check that first attribute id not a line
97 std::shared_ptr<SketchPlugin_Feature> aSFeature =
98 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
99 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
100 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
101 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
102 aFeature->data(), aParamA, aPlane);
104 theError = "One of parameters of distance constraint should be a point";
112 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
113 const std::list<std::string>& theArguments,
114 Events_InfoMessage& theError) const
116 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
117 theError = "The attribute with the %1 type is not processed";
118 theError.arg(theAttribute->attributeType());
122 // there is a check whether the feature contains a point and a linear edge or two point values
123 std::string aParamA = theArguments.front();
124 SessionPtr aMgr = ModelAPI_Session::get();
125 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
127 FeaturePtr anAttributeFeature =
128 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
129 AttributeRefAttrPtr aRefAttr =
130 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
132 bool isObject = aRefAttr->isObject();
133 ObjectPtr anObject = aRefAttr->object();
134 if (isObject && anObject.get()) {
135 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
137 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
138 ObjectPtr aOtherObject = aOtherAttr->object();
139 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
143 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
144 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
145 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
146 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
147 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
148 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
152 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
153 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
154 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
155 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
156 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
157 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
158 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
159 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
160 .arg(SketchPlugin_Circle::ID());
165 theError = "It refers to %1, but should refer to %2 or %3 or %4";
166 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
167 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
173 theError = "It uses an empty object";
180 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
181 const std::list<std::string>& theArguments,
182 Events_InfoMessage& theError) const
184 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
185 theError = "The attribute with the %1 type is not processed";
186 theError.arg(theAttribute->attributeType());
190 std::shared_ptr<SketchPlugin_Feature> aFeature =
191 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
195 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
197 SketchPlugin_Sketch* aSketch = aFeature->sketch();
198 int aNbFeatures = aSketch->numberOfSubs();
199 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
200 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
201 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
203 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
204 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
205 if (aRefAttr->isObject()) {
206 if (aRefAttr->object() == aRAttr->object()) {
207 ObjectPtr anObject = aRefAttr->object();
208 std::string aName = anObject.get() ? anObject->data()->name() : "";
209 theError = "The object %1 has been already fixed.";
214 else if (aRefAttr->attr() == aRAttr->attr()) {
215 AttributePtr anAttribute = aRefAttr->attr();
216 std::string aName = anAttribute.get() ? anAttribute->id() : "";
217 theError = "The attribute %1 has been already fixed.";
225 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
226 const std::list<std::string>& theArguments,
227 Events_InfoMessage& theError) const
229 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
230 theError = "The attribute with the %1 type is not processed";
231 theError.arg(theAttribute->attributeType());
235 std::string aParamA = theArguments.front();
236 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
237 AttributeRefAttrPtr aRefAttr[2];
238 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
239 aRefAttr[1] = aFeature->data()->refattr(aParamA);
241 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
242 theError = "Attributes can not be used in equal constraint";
246 std::string aType[2];
247 std::list<std::string> anArguments;
248 for (int i = 0; i < 2; i++) {
249 ObjectPtr anObject = aRefAttr[i]->object();
250 if (!anObject.get()) {
251 theError = "An empty object is used.";
255 aFeature = ModelAPI_Feature::feature(anObject);
256 if (!aFeature.get()) {
257 theError = "An empty feature is used.";
261 aType[i] = aFeature->getKind();
262 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
263 aFeature->getKind() != SketchPlugin_Circle::ID() &&
264 aFeature->getKind() != SketchPlugin_Arc::ID()) {
265 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
266 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
267 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
268 // wrong type of attribute
273 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
274 aType[0] != aType[1]) {
275 theError = "Feature with kinds %1 and %2 can not be equal.";
276 theError.arg(aType[0]).arg(aType[1]);
282 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
283 const std::list<std::string>& theArguments,
284 Events_InfoMessage& theError) const
286 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
287 theError = "The attribute with the %1 type is not processed";
288 theError.arg(theAttribute->attributeType());
292 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
293 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
295 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
296 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
297 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
299 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
300 ObjectPtr aSelObject = aSelAttr->object(anInd);
301 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
302 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
303 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
304 if (aSelObject == *aMirIter) {
305 theError = "The object %1 is a result of mirror";
313 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
314 const std::list<std::string>& theArguments,
315 Events_InfoMessage& theError) const
317 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
318 theError = "The attribute with the %1 type is not processed";
319 theError.arg(theAttribute->attributeType());
323 // there is a check whether the feature contains a point and a linear edge or two point values
324 std::string aParamA = theArguments.front();
325 SessionPtr aMgr = ModelAPI_Session::get();
326 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
328 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
329 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
331 theError = "The %1 attribute should be %2";
332 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
336 AttributeRefAttrPtr aRefAttrB =
337 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
339 // first attribute is a point, it may coincide with any object
340 if (!aRefAttrA->isObject())
343 ObjectPtr anObject = aRefAttrA->object();
344 if (!anObject.get()) {
345 theError = "%1 attribute has an empty object";
346 theError.arg(aParamA);
349 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
350 if (!aFeature.get()) {
351 theError = "%1 attribute has an empty feature";
352 theError.arg(aParamA);
356 if (aFeature->getKind() == SketchPlugin_Point::ID())
360 // second attribute is a point, it may coincide with any object
361 if (!aRefAttrB->isObject())
364 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
366 theError = "%1 attribute has an empty object";
367 theError.arg(theAttribute->id());
370 if (aFeature->getKind() == SketchPlugin_Point::ID())
373 theError = "There is no an attribute filled by a point";
378 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
379 const std::list<std::string>& theArguments,
380 Events_InfoMessage& theError) const
382 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
383 theError = "The attribute with the %1 type is not processed";
384 theError.arg(theAttribute->attributeType());
388 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
389 AttributeRefListPtr aSelAttr =
390 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
392 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
393 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
394 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
395 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
396 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
397 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
399 std::list<ObjectPtr>::iterator anObjIter;
400 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
401 ObjectPtr aSelObject = aSelAttr->object(anInd);
402 anObjIter = anInitialObjects.begin();
403 for (; anObjIter != anInitialObjects.end(); anObjIter++)
404 if (aSelObject == *anObjIter)
406 if (anObjIter != anInitialObjects.end())
408 anObjIter = aCopiedObjects.begin();
409 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
410 if (aSelObject == *anObjIter) {
411 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
412 theError = "The object %1 is a result of copy";
420 bool SketchPlugin_SolverErrorValidator::isValid(
421 const std::shared_ptr<ModelAPI_Feature>& theFeature,
422 const std::list<std::string>& theArguments,
423 Events_InfoMessage& theError) const
425 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
427 if (!aAttributeString->value().empty()) {
428 theError = aAttributeString->value();
435 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
436 std::string theAttribute)
441 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
442 const FeaturePtr theFeature)
444 for(std::set<AttributePtr>::const_iterator
445 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
446 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
447 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
448 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
449 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
450 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
451 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
452 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
453 if(anAttrRefA.get()) {
454 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
456 DocumentPtr aDoc = aResA->document();
458 FeaturePtr aFeatureA = aDoc->feature(aResA);
459 if(aFeatureA.get() && aFeatureA == theFeature) {
465 if(anAttrRefB.get()) {
466 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
468 DocumentPtr aDoc = aResB->document();
470 FeaturePtr aFeatureB = aDoc->feature(aResB);
471 if(aFeatureB.get() && aFeatureB == theFeature) {
482 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
483 const std::list<std::string>& theArguments,
484 Events_InfoMessage& theError) const
486 AttributeRefAttrPtr aPointRefAttr =
487 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
488 if(!aPointRefAttr.get()) {
489 theError = "Error: Point not selected.";
493 AttributePtr aPointAttribute = aPointRefAttr->attr();
494 if (!aPointAttribute.get()) {
495 theError = "Error: Bad point selected.";
498 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
499 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
501 // Obtain constraint coincidence for the fillet point.
502 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
503 FeaturePtr aConstraintCoincidence;
504 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
505 anIt != aRefsList.cend(); ++anIt) {
506 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
507 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
508 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
509 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
510 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
511 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
512 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
513 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
514 AttributePtr anAttrA = anAttrRefA->attr();
515 if(aPointAttribute == anAttrA) {
516 aConstraintCoincidence = aConstrFeature;
520 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
521 AttributePtr anAttrB = anAttrRefB->attr();
522 if(aPointAttribute == anAttrB) {
523 aConstraintCoincidence = aConstrFeature;
530 if(!aConstraintCoincidence.get()) {
531 theError = "Error: one of the selected point does not have coicidence.";
535 // Get coincides from constraint.
536 std::set<FeaturePtr> aCoinsides;
537 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
538 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
540 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
541 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
544 // Remove points from set of coincides.
545 std::set<FeaturePtr> aNewSetOfCoincides;
546 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
547 anIt != aCoinsides.end(); ++anIt) {
548 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
549 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
550 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
553 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
554 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
557 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
558 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
559 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
560 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
561 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
562 if(aDistSelectedArcCenter < tolerance) {
566 aNewSetOfCoincides.insert(*anIt);
568 aCoinsides = aNewSetOfCoincides;
570 // If we still have more than two coincides remove auxilary entities from set of coincides.
571 if(aCoinsides.size() > 2) {
572 aNewSetOfCoincides.clear();
573 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
574 anIt != aCoinsides.end(); ++anIt) {
575 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
576 aNewSetOfCoincides.insert(*anIt);
579 aCoinsides = aNewSetOfCoincides;
582 if(aCoinsides.size() != 2) {
583 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
587 // Check that selected edges don't have tangent constraint.
588 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
589 FeaturePtr aFirstFeature = *anIt++;
590 FeaturePtr aSecondFeature = *anIt;
591 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
592 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
593 theError = "Error: Edges in selected point has tangent constraint.";
597 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
598 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
599 aResIt != aFirstResults.end(); ++aResIt) {
600 ResultPtr aRes = *aResIt;
601 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
602 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
603 theError = "Error: Edges in selected point has tangent constraint.";
608 // Check that lines not collinear
609 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
610 aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
611 std::string aStartAttr = SketchPlugin_Line::START_ID();
612 std::string anEndAttr = SketchPlugin_Line::END_ID();
613 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
615 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
616 aFirstFeature->attribute(aStartAttr))->pnt();
618 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
620 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
621 aSecondFeature->attribute(aStartAttr))->pnt();
623 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
624 aSecondFeature->attribute(anEndAttr))->pnt();
626 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
627 (aSecondStartPnt->y() - aFirstStartPnt->y()) -
628 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
630 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
631 (aSecondEndPnt->y() - aFirstStartPnt->y()) -
632 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
633 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
642 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
643 const std::list<std::string>& theArguments,
644 Events_InfoMessage& theError) const
646 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
647 theError = "The attribute with the %1 type is not processed";
648 theError.arg(theAttribute->attributeType());
652 // there is a check whether the feature contains a point and a linear edge or two point values
653 std::string aParamA = theArguments.front();
654 SessionPtr aMgr = ModelAPI_Session::get();
655 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
657 FeaturePtr anAttributeFeature =
658 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
659 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
660 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
662 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
665 for (int i = 0; i < 2; ++i) {
666 if (!aRefAttrs[i]->isObject())
669 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
671 if (aNbPoints + aNbLines != 0)
676 if (aFeature->getKind() == SketchPlugin_Point::ID())
678 else if (aFeature->getKind() == SketchPlugin_Line::ID())
683 if (aNbPoints != 1 || aNbLines != 1) {
684 theError = "Middle point constraint allows points and lines only";
690 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
691 const std::list<std::string>& /*theArguments*/,
692 Events_InfoMessage& theError) const
694 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
695 theError = "The attribute with the %1 type is not processed";
696 theError.arg(theAttribute->attributeType());
699 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
700 AttributePtr anAttr = aRefAttr->attr();
702 theError = "The attribute %1 should be a point";
703 theError.arg(theAttribute->id());
707 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
708 const std::string& aFeatureType = anAttrFeature->getKind();
709 if (aFeatureType == SketchPlugin_Arc::ID()) {
710 // selected point should not be a center of arc
711 const std::string& aPntId = anAttr->id();
712 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
713 theError = "The attribute %1 is not supported";
714 theError.arg(aPntId);
718 else if (aFeatureType == SketchPlugin_Line::ID()) {
719 // selected point should be bound point of line
720 const std::string& aPntId = anAttr->id();
721 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
722 theError = "The attribute %1 is not supported";
723 theError.arg(aPntId);
728 theError = "Unable to build tangent arc on %1";
729 theError.arg(anAttrFeature->getKind());
733 // Check the tangent point is equal to arc end
734 FeaturePtr anArc = std::dynamic_pointer_cast<ModelAPI_Feature>(aRefAttr->owner());
735 std::shared_ptr<GeomDataAPI_Point2D> anEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
736 anArc->attribute(SketchPlugin_Arc::END_ID()));
737 if (anEndPoint->isInitialized()) {
738 std::shared_ptr<GeomDataAPI_Point2D> aTangPt =
739 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
740 if (aTangPt->pnt()->distance(anEndPoint->pnt()) < tolerance) {
741 theError = "Unable to build arc on same points";
749 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
750 const std::list<std::string>& theArguments,
751 Events_InfoMessage& theError) const
753 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
754 theError = "The attribute with the %1 type is not processed";
755 theError.arg(theAttribute->attributeType());
758 AttributeSelectionPtr aLineAttr =
759 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
760 std::shared_ptr<GeomAPI_Edge> anEdge;
761 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
762 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
763 } else if(aLineAttr->context() &&
764 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
765 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
768 if (!anEdge || !anEdge->isLine()) {
769 theError = "The attribute %1 should be a line";
770 theError.arg(theAttribute->id());
774 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
777 std::shared_ptr<SketchPlugin_Sketch> aSketch;
778 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
779 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
780 for (; anIt != aRefs.end(); ++anIt) {
781 CompositeFeaturePtr aComp =
782 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
783 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
784 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
789 theError = "There is no sketch referring to the current feature";
793 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
794 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
795 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
798 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
799 const std::list<std::string>& theArguments,
800 Events_InfoMessage& theError) const
804 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
805 theError = "The attribute with the %1 type is not processed";
806 theError.arg(theAttribute->attributeType());
809 AttributeReferencePtr aFeatureAttr =
810 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
812 ObjectPtr anAttrObject = aFeatureAttr->value();
813 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
817 std::string aKind = anAttrFeature->getKind();
818 if (aKind == SketchPlugin_Line::ID() ||
819 aKind == SketchPlugin_Arc::ID() ||
820 aKind == SketchPlugin_Circle::ID()) {
822 std::set<ResultPtr> anEdgeShapes;
823 ModelAPI_Tools::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
824 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
827 // coincidences to the feature
828 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
829 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
830 SketchPlugin_ConstraintCoincidence::ID(),
831 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
833 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
834 std::shared_ptr<SketchPlugin_Feature> aSFeature =
835 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
836 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
838 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
839 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
840 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
841 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
842 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
843 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
844 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
845 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
847 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
848 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
849 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
850 PointToRefsMap aPointsInfo;
852 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
853 aX->dir(), aDirY, aPointsInfo);
854 int aCoincidentToFeature = (int)aPointsInfo.size();
855 if (aKind == SketchPlugin_Circle::ID())
856 aValid = aCoincidentToFeature >= 2;
858 aValid = aCoincidentToFeature >= 1;
864 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
865 const std::list<std::string>& theArguments,
866 Events_InfoMessage& theError) const
868 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
869 theError = "The attribute with the %1 type is not processed";
870 theError.arg(theAttribute->attributeType());
874 AttributeSelectionPtr aFeatureAttr =
875 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
876 std::shared_ptr<GeomAPI_Edge> anEdge;
877 if (aFeatureAttr.get()) {
878 GeomShapePtr aVal = aFeatureAttr->value();
879 ResultPtr aRes = aFeatureAttr->context();
880 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
881 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
882 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
883 aFeatureAttr->context()->shape()->isEdge()) {
884 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
888 theError = "The attribute %1 should be an edge";
889 theError.arg(theAttribute->id());
894 std::shared_ptr<SketchPlugin_Sketch> aSketch;
895 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
896 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
897 for (; anIt != aRefs.end(); ++anIt) {
898 CompositeFeaturePtr aComp =
899 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
900 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
901 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
906 theError = "There is no sketch referring to the current feature";
910 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
911 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
912 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
914 if (anEdge->isLine()) {
915 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
916 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
917 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
918 double aDot = aNormal->dot(aLineDir);
919 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
920 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
921 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
923 theError = "Error: Edge is already in the sketch plane.";
926 else if (anEdge->isCircle() || anEdge->isArc()) {
927 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
928 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
929 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
930 double aDot = fabs(aNormal->dot(aCircNormal));
931 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
932 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
934 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
935 : "Error: Arc is already in the sketch plane.");
939 theError = "Error: Selected object is not line, circle or arc.";