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_ConstraintRigid.h"
14 #include "SketchPlugin_ConstraintTangent.h"
15 #include "SketchPlugin_Fillet.h"
16 #include "SketchPlugin_Line.h"
17 #include "SketchPlugin_MacroArc.h"
18 #include "SketchPlugin_MacroCircle.h"
19 #include "SketchPlugin_Point.h"
20 #include "SketchPlugin_Sketch.h"
21 #include "SketchPlugin_Trim.h"
22 #include "SketchPlugin_Tools.h"
24 #include "SketcherPrs_Tools.h"
26 #include <Events_InfoMessage.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_AttributeRefAttrList.h>
34 #include <ModelAPI_AttributeRefList.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeString.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_ResultConstruction.h>
41 #include <ModelGeomAlgo_Point2D.h>
42 #include <ModelGeomAlgo_Shape.h>
44 #include <GeomAPI_Circ.h>
45 #include <GeomAPI_Dir2d.h>
46 #include <GeomAPI_Lin.h>
47 #include <GeomAPI_Edge.h>
48 #include <GeomAPI_Vertex.h>
49 #include <GeomDataAPI_Point2D.h>
54 const double tolerance = 1.e-7;
56 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
57 const std::list<std::string>& theArguments,
58 Events_InfoMessage& theError) const
60 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
61 theError = "The attribute with the %1 type is not processed";
62 theError.arg(theAttribute->attributeType());
66 // there is a check whether the feature contains a point and a linear edge or two point values
67 std::string aParamA = theArguments.front();
68 SessionPtr aMgr = ModelAPI_Session::get();
69 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
71 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
73 bool isObject = aRefAttr->isObject();
75 // an attribute is a point. A point value is valid always for the distance
78 // 1. check whether the references object is a linear
79 ObjectPtr anObject = aRefAttr->object();
81 const ModelAPI_AttributeValidator* aShapeValidator =
82 dynamic_cast<const ModelAPI_AttributeValidator*>(
83 aFactory->validator("GeomValidators_ShapeType"));
84 std::list<std::string> anArguments;
85 anArguments.push_back("circle");
86 Events_InfoMessage aCircleError;
87 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
88 // the circle line is not a valid case
90 theError = "Circle can not be used in distance constraint";
95 anArguments.push_back("line");
96 Events_InfoMessage aLineError;
97 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
98 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
100 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
101 // If it is a line then we have to check that first attribute id not a line
102 std::shared_ptr<SketchPlugin_Feature> aSFeature =
103 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
104 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
105 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
106 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
107 aFeature->data(), aParamA, aPlane);
109 theError = "One of parameters of distance constraint should be a point";
117 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
118 const std::list<std::string>& theArguments,
119 Events_InfoMessage& theError) const
121 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
122 theError = "The attribute with the %1 type is not processed";
123 theError.arg(theAttribute->attributeType());
127 // there is a check whether the feature contains a point and a linear edge or two point values
128 std::string aParamA = theArguments.front();
129 SessionPtr aMgr = ModelAPI_Session::get();
130 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
132 FeaturePtr anAttributeFeature =
133 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
134 AttributeRefAttrPtr aRefAttr =
135 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
137 bool isObject = aRefAttr->isObject();
138 ObjectPtr anObject = aRefAttr->object();
139 if (isObject && anObject.get()) {
140 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
142 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
143 ObjectPtr aOtherObject = aOtherAttr->object();
144 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
148 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
149 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
150 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
151 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
152 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
153 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
157 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
158 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
159 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
160 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
161 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
162 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
163 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
164 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
165 .arg(SketchPlugin_Circle::ID());
170 theError = "It refers to %1, but should refer to %2 or %3 or %4";
171 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
172 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
178 theError = "It uses an empty object";
185 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
186 const std::list<std::string>& theArguments,
187 Events_InfoMessage& theError) const
189 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
190 theError = "The attribute with the %1 type is not processed";
191 theError.arg(theAttribute->attributeType());
195 std::shared_ptr<SketchPlugin_Feature> aFeature =
196 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
200 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
202 SketchPlugin_Sketch* aSketch = aFeature->sketch();
203 int aNbFeatures = aSketch->numberOfSubs();
204 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
205 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
206 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
208 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
209 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
210 if (aRefAttr->isObject()) {
211 if (aRefAttr->object() == aRAttr->object()) {
212 ObjectPtr anObject = aRefAttr->object();
213 std::string aName = anObject.get() ? anObject->data()->name() : "";
214 theError = "The object %1 has been already fixed.";
219 else if (aRefAttr->attr() == aRAttr->attr()) {
220 AttributePtr anAttribute = aRefAttr->attr();
221 std::string aName = anAttribute.get() ? anAttribute->id() : "";
222 theError = "The attribute %1 has been already fixed.";
230 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
231 const std::list<std::string>& theArguments,
232 Events_InfoMessage& theError) const
234 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
235 theError = "The attribute with the %1 type is not processed";
236 theError.arg(theAttribute->attributeType());
240 std::string aParamA = theArguments.front();
241 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
242 AttributeRefAttrPtr aRefAttr[2];
243 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
244 aRefAttr[1] = aFeature->data()->refattr(aParamA);
246 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
247 theError = "Attributes can not be used in equal constraint";
251 std::string aType[2];
252 std::list<std::string> anArguments;
253 for (int i = 0; i < 2; i++) {
254 ObjectPtr anObject = aRefAttr[i]->object();
255 if (!anObject.get()) {
256 theError = "An empty object is used.";
260 aFeature = ModelAPI_Feature::feature(anObject);
261 if (!aFeature.get()) {
262 theError = "An empty feature is used.";
266 aType[i] = aFeature->getKind();
267 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
268 aFeature->getKind() != SketchPlugin_Circle::ID() &&
269 aFeature->getKind() != SketchPlugin_Arc::ID()) {
270 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
271 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
272 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
273 // wrong type of attribute
278 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
279 aType[0] != aType[1]) {
280 theError = "Feature with kinds %1 and %2 can not be equal.";
281 theError.arg(aType[0]).arg(aType[1]);
287 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
288 const std::list<std::string>& theArguments,
289 Events_InfoMessage& theError) const
291 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
292 theError = "The attribute with the %1 type is not processed";
293 theError.arg(theAttribute->attributeType());
297 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
298 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
300 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
301 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
302 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
304 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
305 ObjectPtr aSelObject = aSelAttr->object(anInd);
306 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
307 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
308 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
309 if (aSelObject == *aMirIter) {
310 theError = "The object %1 is a result of mirror";
318 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
319 const std::list<std::string>& theArguments,
320 Events_InfoMessage& theError) const
322 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
323 theError = "The attribute with the %1 type is not processed";
324 theError.arg(theAttribute->attributeType());
328 // there is a check whether the feature contains a point and a linear edge or two point values
329 std::string aParamA = theArguments.front();
330 SessionPtr aMgr = ModelAPI_Session::get();
331 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
333 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
334 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
336 theError = "The %1 attribute should be %2";
337 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
341 AttributeRefAttrPtr aRefAttrB =
342 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
344 // first attribute is a point, it may coincide with any object
345 if (!aRefAttrA->isObject())
348 ObjectPtr anObject = aRefAttrA->object();
349 if (!anObject.get()) {
350 theError = "%1 attribute has an empty object";
351 theError.arg(aParamA);
354 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
355 if (!aFeature.get()) {
356 theError = "%1 attribute has an empty feature";
357 theError.arg(aParamA);
361 if (aFeature->getKind() == SketchPlugin_Point::ID())
365 // second attribute is a point, it may coincide with any object
366 if (!aRefAttrB->isObject())
369 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
371 theError = "%1 attribute has an empty object";
372 theError.arg(theAttribute->id());
375 if (aFeature->getKind() == SketchPlugin_Point::ID())
378 theError = "There is no an attribute filled by a point";
383 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
384 const std::list<std::string>& theArguments,
385 Events_InfoMessage& theError) const
387 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
388 theError = "The attribute with the %1 type is not processed";
389 theError.arg(theAttribute->attributeType());
393 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
394 AttributeRefListPtr aSelAttr =
395 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
397 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
398 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
399 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
400 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
401 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
402 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
404 std::list<ObjectPtr>::iterator anObjIter;
405 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
406 ObjectPtr aSelObject = aSelAttr->object(anInd);
407 anObjIter = anInitialObjects.begin();
408 for (; anObjIter != anInitialObjects.end(); anObjIter++)
409 if (aSelObject == *anObjIter)
411 if (anObjIter != anInitialObjects.end())
413 anObjIter = aCopiedObjects.begin();
414 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
415 if (aSelObject == *anObjIter) {
416 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
417 theError = "The object %1 is a result of copy";
425 bool SketchPlugin_SolverErrorValidator::isValid(
426 const std::shared_ptr<ModelAPI_Feature>& theFeature,
427 const std::list<std::string>& theArguments,
428 Events_InfoMessage& theError) const
430 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
432 if (!aAttributeString->value().empty()) {
433 theError = aAttributeString->value();
440 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
441 std::string theAttribute)
446 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
447 const FeaturePtr theFeature)
449 for(std::set<AttributePtr>::const_iterator
450 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
451 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
452 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
453 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
454 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
455 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
456 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
457 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
458 if(anAttrRefA.get()) {
459 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
461 DocumentPtr aDoc = aResA->document();
463 FeaturePtr aFeatureA = aDoc->feature(aResA);
464 if(aFeatureA.get() && aFeatureA == theFeature) {
470 if(anAttrRefB.get()) {
471 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
473 DocumentPtr aDoc = aResB->document();
475 FeaturePtr aFeatureB = aDoc->feature(aResB);
476 if(aFeatureB.get() && aFeatureB == theFeature) {
487 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
488 const std::list<std::string>& theArguments,
489 Events_InfoMessage& theError) const
491 AttributeRefAttrPtr aPointRefAttr =
492 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
493 if(!aPointRefAttr.get()) {
494 theError = "Error: Point not selected.";
498 AttributePtr aPointAttribute = aPointRefAttr->attr();
499 if (!aPointAttribute.get()) {
500 theError = "Error: Bad point selected.";
503 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
504 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
506 // Obtain constraint coincidence for the fillet point.
507 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
508 FeaturePtr aConstraintCoincidence;
509 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
510 anIt != aRefsList.cend(); ++anIt) {
511 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
512 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
513 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
514 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
515 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
516 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
517 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
518 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
519 AttributePtr anAttrA = anAttrRefA->attr();
520 if(aPointAttribute == anAttrA) {
521 aConstraintCoincidence = aConstrFeature;
525 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
526 AttributePtr anAttrB = anAttrRefB->attr();
527 if(aPointAttribute == anAttrB) {
528 aConstraintCoincidence = aConstrFeature;
535 if(!aConstraintCoincidence.get()) {
536 theError = "Error: one of the selected point does not have coicidence.";
540 // Get coincides from constraint.
541 std::set<FeaturePtr> aCoinsides;
542 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
543 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
545 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
546 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
549 // Remove points from set of coincides.
550 std::set<FeaturePtr> aNewSetOfCoincides;
551 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
552 anIt != aCoinsides.end(); ++anIt) {
553 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
554 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
555 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
558 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
559 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
562 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
563 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
564 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
565 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
566 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
567 if(aDistSelectedArcCenter < tolerance) {
571 aNewSetOfCoincides.insert(*anIt);
573 aCoinsides = aNewSetOfCoincides;
575 // If we still have more than two coincides remove auxilary entities from set of coincides.
576 if(aCoinsides.size() > 2) {
577 aNewSetOfCoincides.clear();
578 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
579 anIt != aCoinsides.end(); ++anIt) {
580 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
581 aNewSetOfCoincides.insert(*anIt);
584 aCoinsides = aNewSetOfCoincides;
587 if(aCoinsides.size() != 2) {
588 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
592 // Check that selected edges don't have tangent constraint.
593 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
594 FeaturePtr aFirstFeature = *anIt++;
595 FeaturePtr aSecondFeature = *anIt;
596 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
597 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
598 theError = "Error: Edges in selected point has tangent constraint.";
602 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
603 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
604 aResIt != aFirstResults.end(); ++aResIt) {
605 ResultPtr aRes = *aResIt;
606 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
607 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
608 theError = "Error: Edges in selected point has tangent constraint.";
613 // Check that lines not collinear
614 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
615 aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
616 std::string aStartAttr = SketchPlugin_Line::START_ID();
617 std::string anEndAttr = SketchPlugin_Line::END_ID();
618 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
620 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
621 aFirstFeature->attribute(aStartAttr))->pnt();
623 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
625 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
626 aSecondFeature->attribute(aStartAttr))->pnt();
628 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
629 aSecondFeature->attribute(anEndAttr))->pnt();
631 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
632 (aSecondStartPnt->y() - aFirstStartPnt->y()) -
633 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
635 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
636 (aSecondEndPnt->y() - aFirstStartPnt->y()) -
637 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
638 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
647 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
648 const std::list<std::string>& theArguments,
649 Events_InfoMessage& theError) const
651 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
652 theError = "The attribute with the %1 type is not processed";
653 theError.arg(theAttribute->attributeType());
657 // there is a check whether the feature contains a point and a linear edge or two point values
658 std::string aParamA = theArguments.front();
659 SessionPtr aMgr = ModelAPI_Session::get();
660 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
662 FeaturePtr anAttributeFeature =
663 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
664 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
665 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
667 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
670 for (int i = 0; i < 2; ++i) {
671 if (!aRefAttrs[i]->isObject())
674 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
676 if (aNbPoints + aNbLines != 0)
681 if (aFeature->getKind() == SketchPlugin_Point::ID())
683 else if (aFeature->getKind() == SketchPlugin_Line::ID())
688 if (aNbPoints != 1 || aNbLines != 1) {
689 theError = "Middle point constraint allows points and lines only";
695 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
696 const std::list<std::string>& /*theArguments*/,
697 Events_InfoMessage& theError) const
699 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
700 theError = "The attribute with the %1 type is not processed";
701 theError.arg(theAttribute->attributeType());
704 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
705 AttributePtr anAttr = aRefAttr->attr();
707 theError = "The attribute %1 should be a point";
708 theError.arg(theAttribute->id());
712 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
713 const std::string& aFeatureType = anAttrFeature->getKind();
714 if (aFeatureType == SketchPlugin_Arc::ID()) {
715 // selected point should not be a center of arc
716 const std::string& aPntId = anAttr->id();
717 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
718 theError = "The attribute %1 is not supported";
719 theError.arg(aPntId);
723 else if (aFeatureType == SketchPlugin_Line::ID()) {
724 // selected point should be bound point of line
725 const std::string& aPntId = anAttr->id();
726 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
727 theError = "The attribute %1 is not supported";
728 theError.arg(aPntId);
733 theError = "Unable to build tangent arc on %1";
734 theError.arg(anAttrFeature->getKind());
741 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
742 const std::list<std::string>& theArguments,
743 Events_InfoMessage& theError) const
745 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
746 theError = "The attribute with the %1 type is not processed";
747 theError.arg(theAttribute->attributeType());
750 AttributeSelectionPtr aLineAttr =
751 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
752 std::shared_ptr<GeomAPI_Edge> anEdge;
753 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
754 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
755 } else if(aLineAttr->context() &&
756 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
757 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
760 if (!anEdge || !anEdge->isLine()) {
761 theError = "The attribute %1 should be a line";
762 theError.arg(theAttribute->id());
766 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
769 std::shared_ptr<SketchPlugin_Sketch> aSketch;
770 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
771 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
772 for (; anIt != aRefs.end(); ++anIt) {
773 CompositeFeaturePtr aComp =
774 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
775 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
776 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
781 theError = "There is no sketch referring to the current feature";
785 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
786 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
787 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
790 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
791 const std::list<std::string>& theArguments,
792 Events_InfoMessage& theError) const
796 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
797 theError = "The attribute with the %1 type is not processed";
798 theError.arg(theAttribute->attributeType());
801 AttributeReferencePtr aFeatureAttr =
802 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
804 ObjectPtr anAttrObject = aFeatureAttr->value();
805 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
809 std::string aKind = anAttrFeature->getKind();
810 if (aKind == SketchPlugin_Line::ID() ||
811 aKind == SketchPlugin_Arc::ID() ||
812 aKind == SketchPlugin_Circle::ID()) {
814 std::set<ResultPtr> anEdgeShapes;
815 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
816 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
819 // coincidences to the feature
820 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
821 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
822 SketchPlugin_ConstraintCoincidence::ID(),
823 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
825 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
826 std::shared_ptr<SketchPlugin_Feature> aSFeature =
827 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
828 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
830 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
831 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
832 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
833 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
834 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
835 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
836 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
837 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
839 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
840 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
841 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
842 PointToRefsMap aPointsInfo;
844 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
845 aX->dir(), aDirY, aPointsInfo);
846 int aCoincidentToFeature = (int)aPointsInfo.size();
847 if (aKind == SketchPlugin_Circle::ID())
848 aValid = aCoincidentToFeature >= 2;
850 aValid = aCoincidentToFeature >= 1;
856 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
857 const std::list<std::string>& theArguments,
858 Events_InfoMessage& theError) const
862 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
863 theError = "The attribute with the %1 type is not processed";
864 theError.arg(theAttribute->attributeType());
867 AttributeReferencePtr aBaseObjectAttr =
868 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
870 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
871 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
873 ObjectPtr aBaseObject = aBaseObjectAttr->value();
875 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
876 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
877 aBaseObject = aBaseObjectAttr->value();
882 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
886 std::string aKind = aBaseFeature->getKind();
887 if (aKind != SketchPlugin_Line::ID() &&
888 aKind != SketchPlugin_Arc::ID() &&
889 aKind != SketchPlugin_Circle::ID())
893 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
894 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
896 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
898 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
899 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
900 anAttributePnt2d->y());
902 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
903 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
904 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
905 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
906 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
907 aCashedShapes, anObjectToPoints);
908 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
910 return aShapes.size() > 1;
913 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
914 const std::list<std::string>& theArguments,
915 Events_InfoMessage& theError) const
917 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
918 theError = "The attribute with the %1 type is not processed";
919 theError.arg(theAttribute->attributeType());
923 AttributeSelectionPtr aFeatureAttr =
924 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
925 std::shared_ptr<GeomAPI_Edge> anEdge;
926 if (aFeatureAttr.get()) {
927 GeomShapePtr aVal = aFeatureAttr->value();
928 ResultPtr aRes = aFeatureAttr->context();
929 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
930 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
931 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
932 aFeatureAttr->context()->shape()->isEdge()) {
933 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
937 theError = "The attribute %1 should be an edge";
938 theError.arg(theAttribute->id());
943 std::shared_ptr<SketchPlugin_Sketch> aSketch;
944 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
945 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
946 for (; anIt != aRefs.end(); ++anIt) {
947 CompositeFeaturePtr aComp =
948 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
949 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
950 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
955 theError = "There is no sketch referring to the current feature";
959 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
960 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
961 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
963 if (anEdge->isLine()) {
964 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
965 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
966 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
967 double aDot = aNormal->dot(aLineDir);
968 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
969 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
970 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
972 theError = "Error: Edge is already in the sketch plane.";
975 else if (anEdge->isCircle() || anEdge->isArc()) {
976 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
977 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
978 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
979 double aDot = fabs(aNormal->dot(aCircNormal));
980 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
981 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
983 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
984 : "Error: Arc is already in the sketch plane.");
988 theError = "Error: Selected object is not line, circle or arc.";
993 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
994 const std::set<FeaturePtr>& theSet2)
996 std::set<FeaturePtr> aCommon;
997 if (theSet1.empty() || theSet2.empty())
1000 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1001 for (; anIt2 != theSet2.end(); ++anIt2)
1002 if (theSet1.find(*anIt2) != theSet1.end())
1003 aCommon.insert(*anIt2);
1007 bool SketchPlugin_DifferentReferenceValidator::isValid(
1008 const AttributePtr& theAttribute,
1009 const std::list<std::string>& theArguments,
1010 Events_InfoMessage& theError) const
1012 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1014 int aNbFeaturesReferred = 0;
1015 int aNbAttributesReferred = 0;
1016 std::set<FeaturePtr> aCommonReferredFeatures;
1018 // find all features referred by attributes listed in theArguments
1019 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1020 for (; anArgIt != theArguments.end(); ++anArgIt) {
1021 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1025 std::set<FeaturePtr> aCoincidentFeatures;
1026 if (aRefAttr->isObject()) {
1027 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1029 aCoincidentFeatures.insert(aFeature);
1030 aNbFeaturesReferred += 1;
1033 AttributePoint2DPtr aPoint =
1034 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1036 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1037 aNbAttributesReferred += 1;
1041 if (aCommonReferredFeatures.empty())
1042 aCommonReferredFeatures = aCoincidentFeatures;
1044 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1046 if (aCommonReferredFeatures.empty())
1050 bool isOk = aNbFeaturesReferred < 1;
1051 if (aNbFeaturesReferred == 1) {
1052 if (aCommonReferredFeatures.size() == 1) {
1053 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1054 isOk = aNbAttributesReferred <= 1 ||
1055 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1056 aFeature->getKind() == SketchPlugin_Arc::ID();
1062 theError = "Attributes are referred to the same feature";
1066 bool SketchPlugin_CirclePassedPointValidator::isValid(
1067 const AttributePtr& theAttribute,
1068 const std::list<std::string>&,
1069 Events_InfoMessage& theError) const
1071 static const std::string aErrorMessage(
1072 "Passed point refers to the same feature as a center point");
1074 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1076 AttributeRefAttrPtr aCenterRef =
1077 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1078 AttributeRefAttrPtr aPassedRef =
1079 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1081 if (!aPassedRef->isObject())
1084 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1085 if (!aPassedFeature)
1088 if (aCenterRef->isObject()) {
1089 FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
1090 if (aCenterFeature == aPassedFeature) {
1091 theError = aErrorMessage;
1095 AttributePoint2DPtr aCenterPoint =
1096 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1098 std::set<FeaturePtr> aCoincidentFeatures =
1099 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1100 // check one of coincident features is a feature referred by passed point
1101 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1102 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1103 if (*anIt == aPassedFeature) {
1104 theError = aErrorMessage;
1112 bool SketchPlugin_ThirdPointValidator::isValid(
1113 const AttributePtr& theAttribute,
1114 const std::list<std::string>& theArguments,
1115 Events_InfoMessage& theError) const
1117 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1118 return arePointsNotOnLine(anOwner, theError) &&
1119 arePointsNotSeparated(anOwner, theArguments, theError);
1122 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1123 const std::string& thePointAttrName,
1124 const std::string& theRefPointAttrName)
1126 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1127 theMacroCircle->attribute(thePointAttrName));
1128 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1130 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1132 if (aRefAttr->isObject()) {
1133 // project a point onto selected feature
1134 std::shared_ptr<SketchPlugin_Feature> aFeature =
1135 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1136 ModelAPI_Feature::feature(aRefAttr->object()));
1138 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1139 std::shared_ptr<GeomAPI_Edge> anEdge =
1140 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1142 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1143 if (anEdge->isLine())
1144 aPoint3D = anEdge->line()->project(aPoint3D);
1145 else if (anEdge->isCircle())
1146 aPoint3D = anEdge->circle()->project(aPoint3D);
1147 aPoint = aSketch->to2D(aPoint3D);
1151 AttributePoint2DPtr anOtherPoint =
1152 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1154 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1161 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1162 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1164 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1165 thePoints[0] = toPoint(theMacroFeature,
1166 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1167 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1168 thePoints[1] = toPoint(theMacroFeature,
1169 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1170 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1171 thePoints[2] = toPoint(theMacroFeature,
1172 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1173 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1174 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1175 thePoints[0] = toPoint(theMacroFeature,
1176 SketchPlugin_MacroArc::START_POINT_ID(),
1177 SketchPlugin_MacroArc::START_POINT_REF_ID());
1178 thePoints[1] = toPoint(theMacroFeature,
1179 SketchPlugin_MacroArc::END_POINT_ID(),
1180 SketchPlugin_MacroArc::END_POINT_REF_ID());
1181 thePoints[2] = toPoint(theMacroFeature,
1182 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1183 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1187 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1188 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1189 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1191 static const double aTolerance = 1.e-7;
1192 if (thePoint1->distance(thePoint2) < aTolerance ||
1193 thePoint1->distance(thePoint3) < aTolerance)
1196 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1197 thePoint2->y() - thePoint1->y()));
1198 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1199 thePoint3->y() - thePoint1->y()));
1200 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1203 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1204 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1205 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1207 static const double aTolerance = 1.e-7;
1208 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1209 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1210 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
1211 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
1212 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1215 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1216 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1217 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1219 static const double aTolerance = 1.e-7;
1220 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1221 double aDistP1C = thePoint1->distance(aCenter);
1222 double aDistP2C = thePoint2->distance(aCenter);
1223 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1226 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1227 const FeaturePtr& theMacroFeature,
1228 Events_InfoMessage& theError) const
1230 static const std::string aErrorPointsOnLine(
1231 "Selected points are on the same line");
1233 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1234 threePointsOfFeature(theMacroFeature, aPoints);
1236 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1237 theError = aErrorPointsOnLine;
1243 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1244 const FeaturePtr& theMacroFeature,
1245 const std::list<std::string>& theArguments,
1246 Events_InfoMessage& theError) const
1248 static const std::string aErrorPointsApart(
1249 "Selected entity is lying between first two points");
1251 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1252 FeaturePtr aRefByThird;
1253 if (aThirdPointRef->isObject())
1254 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1258 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1259 threePointsOfFeature(theMacroFeature, aPoints);
1261 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1262 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1266 SketchPlugin_Sketch* aSketch =
1267 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1268 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1269 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1272 if (aThirdShape->isLine())
1273 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1274 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1275 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1278 theError = aErrorPointsApart;