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 <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomAlgoAPI_ShapeTools.h>
47 #include <GeomAPI_Circ.h>
48 #include <GeomAPI_Dir2d.h>
49 #include <GeomAPI_Lin.h>
50 #include <GeomAPI_Edge.h>
51 #include <GeomAPI_Vertex.h>
52 #include <GeomDataAPI_Point2D.h>
57 const double tolerance = 1.e-7;
59 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
60 const std::list<std::string>& theArguments,
61 Events_InfoMessage& theError) const
63 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
64 theError = "The attribute with the %1 type is not processed";
65 theError.arg(theAttribute->attributeType());
69 // there is a check whether the feature contains a point and a linear edge or two point values
70 std::string aParamA = theArguments.front();
71 SessionPtr aMgr = ModelAPI_Session::get();
72 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
76 bool isObject = aRefAttr->isObject();
78 // an attribute is a point. A point value is valid always for the distance
81 // 1. check whether the references object is a linear
82 ObjectPtr anObject = aRefAttr->object();
84 const ModelAPI_AttributeValidator* aShapeValidator =
85 dynamic_cast<const ModelAPI_AttributeValidator*>(
86 aFactory->validator("GeomValidators_ShapeType"));
87 std::list<std::string> anArguments;
88 anArguments.push_back("circle");
89 Events_InfoMessage aCircleError;
90 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
91 // the circle line is not a valid case
93 theError = "Circle can not be used in distance constraint";
98 anArguments.push_back("line");
99 Events_InfoMessage aLineError;
100 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
101 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
103 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
104 // If it is a line then we have to check that first attribute id not a line
105 std::shared_ptr<SketchPlugin_Feature> aSFeature =
106 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
107 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
108 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
109 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
110 aFeature->data(), aParamA, aPlane);
112 theError = "One of parameters of distance constraint should be a point";
120 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
121 const std::list<std::string>& theArguments,
122 Events_InfoMessage& theError) const
124 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
125 theError = "The attribute with the %1 type is not processed";
126 theError.arg(theAttribute->attributeType());
130 // there is a check whether the feature contains a point and a linear edge or two point values
131 std::string aParamA = theArguments.front();
132 SessionPtr aMgr = ModelAPI_Session::get();
133 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
135 FeaturePtr anAttributeFeature =
136 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
137 AttributeRefAttrPtr aRefAttr =
138 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
140 bool isObject = aRefAttr->isObject();
141 ObjectPtr anObject = aRefAttr->object();
142 if (isObject && anObject.get()) {
143 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
145 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
146 ObjectPtr aOtherObject = aOtherAttr->object();
147 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
151 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
152 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
153 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
154 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
155 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
156 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
160 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
161 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
162 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
163 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
164 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
165 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
166 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
167 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
168 .arg(SketchPlugin_Circle::ID());
173 theError = "It refers to %1, but should refer to %2 or %3 or %4";
174 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
175 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
181 theError = "It uses an empty object";
188 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
189 const std::list<std::string>& theArguments,
190 Events_InfoMessage& theError) const
192 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
193 theError = "The attribute with the %1 type is not processed";
194 theError.arg(theAttribute->attributeType());
198 std::shared_ptr<SketchPlugin_Feature> aFeature =
199 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
203 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
205 SketchPlugin_Sketch* aSketch = aFeature->sketch();
206 int aNbFeatures = aSketch->numberOfSubs();
207 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
208 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
209 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
211 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
212 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
213 if (aRefAttr->isObject()) {
214 if (aRefAttr->object() == aRAttr->object()) {
215 ObjectPtr anObject = aRefAttr->object();
216 std::string aName = anObject.get() ? anObject->data()->name() : "";
217 theError = "The object %1 has been already fixed.";
222 else if (aRefAttr->attr() == aRAttr->attr()) {
223 AttributePtr anAttribute = aRefAttr->attr();
224 std::string aName = anAttribute.get() ? anAttribute->id() : "";
225 theError = "The attribute %1 has been already fixed.";
233 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
234 const std::list<std::string>& theArguments,
235 Events_InfoMessage& theError) const
237 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
238 theError = "The attribute with the %1 type is not processed";
239 theError.arg(theAttribute->attributeType());
243 std::string aParamA = theArguments.front();
244 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
245 AttributeRefAttrPtr aRefAttr[2];
246 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
247 aRefAttr[1] = aFeature->data()->refattr(aParamA);
249 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
250 theError = "Attributes can not be used in equal constraint";
254 std::string aType[2];
255 std::list<std::string> anArguments;
256 for (int i = 0; i < 2; i++) {
257 ObjectPtr anObject = aRefAttr[i]->object();
258 if (!anObject.get()) {
259 theError = "An empty object is used.";
263 aFeature = ModelAPI_Feature::feature(anObject);
264 if (!aFeature.get()) {
265 theError = "An empty feature is used.";
269 aType[i] = aFeature->getKind();
270 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
271 aFeature->getKind() != SketchPlugin_Circle::ID() &&
272 aFeature->getKind() != SketchPlugin_Arc::ID()) {
273 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
274 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
275 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
276 // wrong type of attribute
281 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
282 aType[0] != aType[1]) {
283 theError = "Feature with kinds %1 and %2 can not be equal.";
284 theError.arg(aType[0]).arg(aType[1]);
290 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
291 const std::list<std::string>& theArguments,
292 Events_InfoMessage& theError) const
294 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
295 theError = "The attribute with the %1 type is not processed";
296 theError.arg(theAttribute->attributeType());
300 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
301 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
303 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
304 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
305 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
307 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
308 ObjectPtr aSelObject = aSelAttr->object(anInd);
309 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
310 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
311 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
312 if (aSelObject == *aMirIter) {
313 theError = "The object %1 is a result of mirror";
321 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
322 const std::list<std::string>& theArguments,
323 Events_InfoMessage& theError) const
325 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
326 theError = "The attribute with the %1 type is not processed";
327 theError.arg(theAttribute->attributeType());
331 // there is a check whether the feature contains a point and a linear edge or two point values
332 std::string aParamA = theArguments.front();
333 SessionPtr aMgr = ModelAPI_Session::get();
334 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
336 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
337 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
339 theError = "The %1 attribute should be %2";
340 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
344 AttributeRefAttrPtr aRefAttrB =
345 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
347 // first attribute is a point, it may coincide with any object
348 if (!aRefAttrA->isObject())
351 ObjectPtr anObject = aRefAttrA->object();
352 if (!anObject.get()) {
353 theError = "%1 attribute has an empty object";
354 theError.arg(aParamA);
357 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
358 if (!aFeature.get()) {
359 theError = "%1 attribute has an empty feature";
360 theError.arg(aParamA);
364 if (aFeature->getKind() == SketchPlugin_Point::ID())
368 // second attribute is a point, it may coincide with any object
369 if (!aRefAttrB->isObject())
372 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
374 theError = "%1 attribute has an empty object";
375 theError.arg(theAttribute->id());
378 if (aFeature->getKind() == SketchPlugin_Point::ID())
381 theError = "There is no an attribute filled by a point";
386 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
387 const std::list<std::string>& theArguments,
388 Events_InfoMessage& theError) const
390 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
391 theError = "The attribute with the %1 type is not processed";
392 theError.arg(theAttribute->attributeType());
396 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
397 AttributeRefListPtr aSelAttr =
398 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
400 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
401 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
402 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
403 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
404 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
405 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
407 std::list<ObjectPtr>::iterator anObjIter;
408 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
409 ObjectPtr aSelObject = aSelAttr->object(anInd);
410 anObjIter = anInitialObjects.begin();
411 for (; anObjIter != anInitialObjects.end(); anObjIter++)
412 if (aSelObject == *anObjIter)
414 if (anObjIter != anInitialObjects.end())
416 anObjIter = aCopiedObjects.begin();
417 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
418 if (aSelObject == *anObjIter) {
419 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
420 theError = "The object %1 is a result of copy";
428 bool SketchPlugin_SolverErrorValidator::isValid(
429 const std::shared_ptr<ModelAPI_Feature>& theFeature,
430 const std::list<std::string>& theArguments,
431 Events_InfoMessage& theError) const
433 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
435 if (!aAttributeString->value().empty()) {
436 theError = aAttributeString->value();
443 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
444 std::string theAttribute)
449 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
450 const FeaturePtr theFeature)
452 for(std::set<AttributePtr>::const_iterator
453 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
454 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
455 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
456 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
457 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
458 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
459 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
460 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
461 if(anAttrRefA.get()) {
462 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
464 DocumentPtr aDoc = aResA->document();
466 FeaturePtr aFeatureA = aDoc->feature(aResA);
467 if(aFeatureA.get() && aFeatureA == theFeature) {
473 if(anAttrRefB.get()) {
474 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
476 DocumentPtr aDoc = aResB->document();
478 FeaturePtr aFeatureB = aDoc->feature(aResB);
479 if(aFeatureB.get() && aFeatureB == theFeature) {
490 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
492 AttributeRefAttrPtr aRefAttr[2] = {
493 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
494 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
497 bool arePoints = true;
498 for (int i = 0; i < 2 && arePoints; ++i) {
499 if (aRefAttr[i]->isObject()) {
500 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
501 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
503 arePoints = aRefAttr[i]->attr().get() != NULL;
508 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
509 const std::list<std::string>& theArguments,
510 Events_InfoMessage& theError) const
512 AttributeRefAttrPtr aPointRefAttr =
513 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
514 if(!aPointRefAttr.get()) {
515 theError = "Error: Point not selected.";
519 AttributePtr aPointAttribute = aPointRefAttr->attr();
520 if (!aPointAttribute.get()) {
521 theError = "Error: Bad point selected.";
524 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
525 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
527 // Obtain constraint coincidence for the fillet point.
528 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
529 FeaturePtr aConstraintCoincidence;
530 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
531 anIt != aRefsList.cend(); ++anIt) {
532 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
533 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
534 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
535 if (!isPointPointCoincidence(aConstrFeature))
538 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
539 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
540 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
541 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
543 AttributePtr anAttrA = anAttrRefA->attr();
544 if(aPointAttribute == anAttrA) {
545 aConstraintCoincidence = aConstrFeature;
549 AttributePtr anAttrB = anAttrRefB->attr();
550 if(aPointAttribute == anAttrB) {
551 aConstraintCoincidence = aConstrFeature;
557 if(!aConstraintCoincidence.get()) {
558 theError = "Error: one of the selected point does not have coicidence.";
562 // Get coincides from constraint.
563 std::set<FeaturePtr> aCoinsides;
564 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
565 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
567 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
568 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
571 // Remove points from set of coincides.
572 std::set<FeaturePtr> aNewSetOfCoincides;
573 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
574 anIt != aCoinsides.end(); ++anIt) {
575 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
576 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
577 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
580 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
581 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
584 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
585 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
586 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
587 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
588 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
589 if(aDistSelectedArcCenter < tolerance) {
593 aNewSetOfCoincides.insert(*anIt);
595 aCoinsides = aNewSetOfCoincides;
597 // If we still have more than two coincides remove auxilary entities from set of coincides.
598 if(aCoinsides.size() > 2) {
599 aNewSetOfCoincides.clear();
600 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
601 anIt != aCoinsides.end(); ++anIt) {
602 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
603 aNewSetOfCoincides.insert(*anIt);
606 aCoinsides = aNewSetOfCoincides;
609 if(aCoinsides.size() != 2) {
610 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
614 // Check that selected edges don't have tangent constraint.
615 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
616 FeaturePtr aFirstFeature = *anIt++;
617 FeaturePtr aSecondFeature = *anIt;
618 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
619 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
620 theError = "Error: Edges in selected point has tangent constraint.";
624 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
625 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
626 aResIt != aFirstResults.end(); ++aResIt) {
627 ResultPtr aRes = *aResIt;
628 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
629 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
630 theError = "Error: Edges in selected point has tangent constraint.";
635 // Check the features are not tangent
636 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
637 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
638 if (!aFirstShape || !aFirstShape->isEdge() ||
639 !aSecondShape || !aSecondShape->isEdge()) {
640 theError = "Error: At least on of the features is not an edge";
644 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
645 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
647 static const double TOL = 1.e-7;
648 if (anEdge1->isLine() && anEdge2->isLine()) {
649 // Check that lines not collinear
650 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
651 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
652 double aCross = aDir1->cross(aDir2)->squareModulus();
653 if (aCross < TOL * TOL)
655 } else if (anEdge1->isArc() && anEdge2->isArc()) {
656 // check the circles are not tangent
657 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
658 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
659 double aDistCC = aCirc1->center()->distance(aCirc2->center());
660 double aRadSum = aCirc1->radius() + aCirc2->radius();
661 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
662 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
665 // check whether line and arc are tangent
666 std::shared_ptr<GeomAPI_Circ> aCirc;
667 std::shared_ptr<GeomAPI_Lin> aLine;
668 if (anEdge1->isLine()) {
669 aLine = anEdge1->line();
670 aCirc = anEdge2->circle();
672 aCirc = anEdge1->circle();
673 aLine = anEdge2->line();
676 double aDistCL = aLine->distance(aCirc->center());
677 if (fabs(aDistCL - aCirc->radius()) < TOL)
684 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
685 const std::list<std::string>& theArguments,
686 Events_InfoMessage& theError) const
688 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
689 theError = "The attribute with the %1 type is not processed";
690 theError.arg(theAttribute->attributeType());
694 // there is a check whether the feature contains a point and a linear edge or two point values
695 std::string aParamA = theArguments.front();
696 SessionPtr aMgr = ModelAPI_Session::get();
697 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
699 FeaturePtr anAttributeFeature =
700 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
701 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
702 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
704 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
707 for (int i = 0; i < 2; ++i) {
708 if (!aRefAttrs[i]->isObject())
711 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
713 if (aNbPoints + aNbLines != 0)
718 if (aFeature->getKind() == SketchPlugin_Point::ID())
720 else if (aFeature->getKind() == SketchPlugin_Line::ID())
725 if (aNbPoints != 1 || aNbLines != 1) {
726 theError = "Middle point constraint allows points and lines only";
732 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
733 const std::list<std::string>& /*theArguments*/,
734 Events_InfoMessage& theError) const
736 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
737 theError = "The attribute with the %1 type is not processed";
738 theError.arg(theAttribute->attributeType());
741 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
742 AttributePtr anAttr = aRefAttr->attr();
744 theError = "The attribute %1 should be a point";
745 theError.arg(theAttribute->id());
749 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
750 const std::string& aFeatureType = anAttrFeature->getKind();
751 if (aFeatureType == SketchPlugin_Arc::ID()) {
752 // selected point should not be a center of arc
753 const std::string& aPntId = anAttr->id();
754 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
755 theError = "The attribute %1 is not supported";
756 theError.arg(aPntId);
760 else if (aFeatureType == SketchPlugin_Line::ID()) {
761 // selected point should be bound point of line
762 const std::string& aPntId = anAttr->id();
763 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
764 theError = "The attribute %1 is not supported";
765 theError.arg(aPntId);
770 theError = "Unable to build tangent arc on %1";
771 theError.arg(anAttrFeature->getKind());
778 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
779 const std::list<std::string>& theArguments,
780 Events_InfoMessage& theError) const
782 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
783 theError = "The attribute with the %1 type is not processed";
784 theError.arg(theAttribute->attributeType());
787 AttributeSelectionPtr aLineAttr =
788 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
789 std::shared_ptr<GeomAPI_Edge> anEdge;
790 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
791 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
792 } else if(aLineAttr->context() &&
793 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
794 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
797 if (!anEdge || !anEdge->isLine()) {
798 theError = "The attribute %1 should be a line";
799 theError.arg(theAttribute->id());
803 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
806 std::shared_ptr<SketchPlugin_Sketch> aSketch;
807 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
808 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
809 for (; anIt != aRefs.end(); ++anIt) {
810 CompositeFeaturePtr aComp =
811 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
812 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
813 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
818 theError = "There is no sketch referring to the current feature";
822 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
823 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
824 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
827 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
828 const std::list<std::string>& theArguments,
829 Events_InfoMessage& theError) const
833 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
834 theError = "The attribute with the %1 type is not processed";
835 theError.arg(theAttribute->attributeType());
838 AttributeReferencePtr aFeatureAttr =
839 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
841 ObjectPtr anAttrObject = aFeatureAttr->value();
842 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
846 std::string aKind = anAttrFeature->getKind();
847 if (aKind == SketchPlugin_Line::ID() ||
848 aKind == SketchPlugin_Arc::ID() ||
849 aKind == SketchPlugin_Circle::ID()) {
851 std::set<ResultPtr> anEdgeShapes;
852 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
853 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
856 // coincidences to the feature
857 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
858 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
859 SketchPlugin_ConstraintCoincidence::ID(),
860 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
862 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
863 std::shared_ptr<SketchPlugin_Feature> aSFeature =
864 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
865 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
867 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
868 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
869 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
870 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
871 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
872 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
873 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
874 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
876 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
877 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
878 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
879 PointToRefsMap aPointsInfo;
881 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
882 aX->dir(), aDirY, aPointsInfo);
883 int aCoincidentToFeature = (int)aPointsInfo.size();
884 if (aKind == SketchPlugin_Circle::ID())
885 aValid = aCoincidentToFeature >= 2;
887 aValid = aCoincidentToFeature >= 1;
893 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
894 const std::list<std::string>& theArguments,
895 Events_InfoMessage& theError) const
899 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
900 theError = "The attribute with the %1 type is not processed";
901 theError.arg(theAttribute->attributeType());
904 AttributeReferencePtr aBaseObjectAttr =
905 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
907 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
908 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
910 ObjectPtr aBaseObject = aBaseObjectAttr->value();
912 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
913 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
914 aBaseObject = aBaseObjectAttr->value();
917 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
921 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
922 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
923 if (!aSketchFeature.get() || aSketchFeature->isCopy())
926 std::string aKind = aBaseFeature->getKind();
927 if (aKind != SketchPlugin_Line::ID() &&
928 aKind != SketchPlugin_Arc::ID() &&
929 aKind != SketchPlugin_Circle::ID())
933 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
934 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
936 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
938 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
939 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
940 anAttributePnt2d->y());
942 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
943 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
944 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
945 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
946 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
947 aCashedShapes, anObjectToPoints);
948 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
950 return aShapes.size() > 1;
953 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
954 const std::list<std::string>& theArguments,
955 Events_InfoMessage& theError) const
957 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
958 theError = "The attribute with the %1 type is not processed";
959 theError.arg(theAttribute->attributeType());
963 AttributeSelectionPtr aFeatureAttr =
964 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
965 std::shared_ptr<GeomAPI_Edge> anEdge;
966 if (aFeatureAttr.get()) {
967 GeomShapePtr aVal = aFeatureAttr->value();
968 ResultPtr aRes = aFeatureAttr->context();
969 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
970 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
971 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
972 aFeatureAttr->context()->shape()->isEdge()) {
973 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
977 theError = "The attribute %1 should be an edge";
978 theError.arg(theAttribute->id());
983 std::shared_ptr<SketchPlugin_Sketch> aSketch;
984 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
985 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
986 for (; anIt != aRefs.end(); ++anIt) {
987 CompositeFeaturePtr aComp =
988 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
989 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
990 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
995 theError = "There is no sketch referring to the current feature";
999 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1000 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1001 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1003 if (anEdge->isLine()) {
1004 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1005 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1006 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
1007 double aDot = aNormal->dot(aLineDir);
1008 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1009 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
1010 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
1012 theError = "Error: Edge is already in the sketch plane.";
1015 else if (anEdge->isCircle() || anEdge->isArc()) {
1016 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1017 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1018 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
1019 double aDot = fabs(aNormal->dot(aCircNormal));
1020 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
1021 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
1023 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1024 : "Error: Arc is already in the sketch plane.");
1028 theError = "Error: Selected object is not line, circle or arc.";
1033 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1034 const std::set<FeaturePtr>& theSet2)
1036 std::set<FeaturePtr> aCommon;
1037 if (theSet1.empty() || theSet2.empty())
1040 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1041 for (; anIt2 != theSet2.end(); ++anIt2)
1042 if (theSet1.find(*anIt2) != theSet1.end())
1043 aCommon.insert(*anIt2);
1047 bool SketchPlugin_DifferentReferenceValidator::isValid(
1048 const AttributePtr& theAttribute,
1049 const std::list<std::string>& theArguments,
1050 Events_InfoMessage& theError) const
1052 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1054 int aNbFeaturesReferred = 0;
1055 int aNbAttributesReferred = 0;
1056 std::set<FeaturePtr> aCommonReferredFeatures;
1058 // find all features referred by attributes listed in theArguments
1059 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1060 for (; anArgIt != theArguments.end(); ++anArgIt) {
1061 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1065 std::set<FeaturePtr> aCoincidentFeatures;
1066 if (aRefAttr->isObject()) {
1067 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1069 aCoincidentFeatures.insert(aFeature);
1070 aNbFeaturesReferred += 1;
1073 AttributePoint2DPtr aPoint =
1074 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1076 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1077 aNbAttributesReferred += 1;
1081 if (aCommonReferredFeatures.empty())
1082 aCommonReferredFeatures = aCoincidentFeatures;
1084 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1086 if (aCommonReferredFeatures.empty())
1090 bool isOk = aNbFeaturesReferred < 1;
1091 if (aNbFeaturesReferred == 1) {
1092 if (aCommonReferredFeatures.size() == 1) {
1093 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1094 isOk = aNbAttributesReferred <= 1 ||
1095 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1096 aFeature->getKind() == SketchPlugin_Arc::ID();
1102 theError = "Attributes are referred to the same feature";
1106 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1107 const AttributePtr& theAttribute,
1108 const std::list<std::string>& theArguments,
1109 Events_InfoMessage& theError) const
1111 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1112 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1114 // find all points referred by attributes listed in theArguments
1115 bool hasRefsToPoints = false;
1116 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1117 for (; anArgIt != theArguments.end(); ++anArgIt) {
1118 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1122 if (!aRefAttr->isObject()) {
1123 AttributePoint2DPtr aPoint =
1124 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1125 if (aReferredCoincidentPoints.empty())
1126 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1127 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1128 return true; // non-coincident point has been found
1130 hasRefsToPoints = true;
1134 if (hasRefsToPoints)
1135 theError = "Attributes are referred to the same point";
1136 return !hasRefsToPoints;
1139 bool SketchPlugin_CirclePassedPointValidator::isValid(
1140 const AttributePtr& theAttribute,
1141 const std::list<std::string>&,
1142 Events_InfoMessage& theError) const
1144 static const std::string aErrorMessage(
1145 "Passed point refers to the same feature as a center point");
1147 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1149 AttributeRefAttrPtr aCenterRef =
1150 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1151 AttributeRefAttrPtr aPassedRef =
1152 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1154 if (!aPassedRef->isObject())
1157 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1158 if (!aPassedFeature)
1161 if (aCenterRef->isObject()) {
1162 if (aCenterRef->object() == aPassedRef->object()) {
1163 theError = aErrorMessage;
1167 AttributePoint2DPtr aCenterPoint =
1168 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1170 std::set<FeaturePtr> aCoincidentFeatures =
1171 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1172 // check one of coincident features is a feature referred by passed point
1173 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1174 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1175 if (*anIt == aPassedFeature) {
1176 theError = aErrorMessage;
1184 bool SketchPlugin_ThirdPointValidator::isValid(
1185 const AttributePtr& theAttribute,
1186 const std::list<std::string>& theArguments,
1187 Events_InfoMessage& theError) const
1189 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1190 return arePointsNotOnLine(anOwner, theError) &&
1191 arePointsNotSeparated(anOwner, theArguments, theError);
1194 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1195 const std::string& thePointAttrName,
1196 const std::string& theRefPointAttrName)
1198 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1199 theMacroCircle->attribute(thePointAttrName));
1200 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1202 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1204 if (aRefAttr->isObject()) {
1205 // project a point onto selected feature
1206 std::shared_ptr<SketchPlugin_Feature> aFeature =
1207 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1208 ModelAPI_Feature::feature(aRefAttr->object()));
1210 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1211 std::shared_ptr<GeomAPI_Edge> anEdge =
1212 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1214 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1215 if (anEdge->isLine())
1216 aPoint3D = anEdge->line()->project(aPoint3D);
1217 else if (anEdge->isCircle())
1218 aPoint3D = anEdge->circle()->project(aPoint3D);
1220 aPoint = aSketch->to2D(aPoint3D);
1224 AttributePoint2DPtr anOtherPoint =
1225 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1227 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1234 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1235 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1237 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1238 thePoints[0] = toPoint(theMacroFeature,
1239 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1240 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1241 thePoints[1] = toPoint(theMacroFeature,
1242 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1243 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1244 thePoints[2] = toPoint(theMacroFeature,
1245 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1246 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1247 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1248 thePoints[0] = toPoint(theMacroFeature,
1249 SketchPlugin_MacroArc::START_POINT_2_ID(),
1250 SketchPlugin_MacroArc::START_POINT_REF_ID());
1251 thePoints[1] = toPoint(theMacroFeature,
1252 SketchPlugin_MacroArc::END_POINT_2_ID(),
1253 SketchPlugin_MacroArc::END_POINT_REF_ID());
1254 thePoints[2] = toPoint(theMacroFeature,
1255 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1256 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1260 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1261 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1262 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1264 static const double aTolerance = 1.e-7;
1265 if (thePoint1->distance(thePoint2) < aTolerance ||
1266 thePoint1->distance(thePoint3) < aTolerance)
1269 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1270 thePoint2->y() - thePoint1->y()));
1271 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1272 thePoint3->y() - thePoint1->y()));
1273 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1276 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1277 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1278 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1280 static const double aTolerance = 1.e-7;
1281 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1282 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1284 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1285 // the first point is on the line
1286 if (aVec1->squareModulus() < aTolerance * aTolerance)
1288 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1289 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1290 // the second point is on the line
1291 if (aVec2->squareModulus() < aTolerance * aTolerance)
1293 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1295 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1298 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1299 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1300 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1302 static const double aTolerance = 1.e-7;
1303 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1304 double aDistP1C = thePoint1->distance(aCenter);
1305 double aDistP2C = thePoint2->distance(aCenter);
1306 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1309 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1310 const FeaturePtr& theMacroFeature,
1311 Events_InfoMessage& theError) const
1313 static const std::string aErrorPointsOnLine(
1314 "Selected points are on the same line");
1316 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1317 threePointsOfFeature(theMacroFeature, aPoints);
1319 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1320 theError = aErrorPointsOnLine;
1326 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1327 const FeaturePtr& theMacroFeature,
1328 const std::list<std::string>& theArguments,
1329 Events_InfoMessage& theError) const
1331 static const std::string aErrorPointsApart(
1332 "Selected entity is lying between first two points");
1334 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1335 FeaturePtr aRefByThird;
1336 if (aThirdPointRef->isObject())
1337 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1341 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1342 threePointsOfFeature(theMacroFeature, aPoints);
1344 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1345 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1349 SketchPlugin_Sketch* aSketch =
1350 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1351 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1352 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1355 if (aThirdShape->isLine())
1356 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1357 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1358 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1361 theError = aErrorPointsApart;
1365 bool SketchPlugin_ArcEndPointValidator::isValid(
1366 const AttributePtr& theAttribute,
1367 const std::list<std::string>& theArguments,
1368 Events_InfoMessage& theError) const
1370 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1371 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1373 if(!anEndPointRef.get()) {
1377 ObjectPtr anObject = anEndPointRef->object();
1378 AttributePtr anAttr = anEndPointRef->attr();
1379 if(!anObject.get() && !anAttr.get()) {
1383 if(anEndPointRef->attr().get()) {
1387 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1389 GeomShapePtr aShape = aResult->shape();
1390 if(aShape.get() && aShape->isVertex()) {
1395 aFeature = ModelAPI_Feature::feature(anObject);
1396 if(aFeature.get()) {
1397 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1405 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1407 if(!theShape.get()) {
1411 if(!theShape->isEdge()) {
1415 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1421 if(anEdge->isLine()) {
1422 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1423 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1427 if(anEdge->isCircle() || anEdge->isArc()) {
1428 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1429 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1436 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1437 const AttributePtr& theAttribute,
1438 const std::list<std::string>& theArguments,
1439 Events_InfoMessage& theError) const
1441 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1442 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1443 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1445 if(!anEndPointRef.get()) {
1449 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1451 if(!anArcShape.get() || anArcShape->isNull()) {
1455 ObjectPtr anObject = anEndPointRef->object();
1456 AttributePtr anAttr = anEndPointRef->attr();
1457 if(!anObject.get() && !anAttr.get()) {
1461 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1463 GeomShapePtr aShape = aResult->shape();
1464 if (!aShape->isEdge())
1466 aShape = toInfiniteEdge(aShape);
1467 if(aShape.get() && !aShape->isNull()) {
1468 if(anArcShape->isIntersect(aShape)) {
1474 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1475 if(aSelectedFeature.get()) {
1476 std::list<ResultPtr> aResults = aSelectedFeature->results();
1477 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1478 anIt != aResults.cend();
1481 GeomShapePtr aShape = (*anIt)->shape();
1482 if (!aShape->isEdge())
1484 aShape = toInfiniteEdge(aShape);
1485 if(aShape.get() && !aShape->isNull()) {
1486 if(anArcShape->isIntersect(aShape)) {