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_ShapeTools.h>
46 #include <GeomAPI_Circ.h>
47 #include <GeomAPI_Dir2d.h>
48 #include <GeomAPI_Lin.h>
49 #include <GeomAPI_Edge.h>
50 #include <GeomAPI_Vertex.h>
51 #include <GeomDataAPI_Point2D.h>
56 const double tolerance = 1.e-7;
58 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
59 const std::list<std::string>& theArguments,
60 Events_InfoMessage& theError) const
62 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
63 theError = "The attribute with the %1 type is not processed";
64 theError.arg(theAttribute->attributeType());
68 // there is a check whether the feature contains a point and a linear edge or two point values
69 std::string aParamA = theArguments.front();
70 SessionPtr aMgr = ModelAPI_Session::get();
71 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
73 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
75 bool isObject = aRefAttr->isObject();
77 // an attribute is a point. A point value is valid always for the distance
80 // 1. check whether the references object is a linear
81 ObjectPtr anObject = aRefAttr->object();
83 const ModelAPI_AttributeValidator* aShapeValidator =
84 dynamic_cast<const ModelAPI_AttributeValidator*>(
85 aFactory->validator("GeomValidators_ShapeType"));
86 std::list<std::string> anArguments;
87 anArguments.push_back("circle");
88 Events_InfoMessage aCircleError;
89 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
90 // the circle line is not a valid case
92 theError = "Circle can not be used in distance constraint";
97 anArguments.push_back("line");
98 Events_InfoMessage aLineError;
99 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
100 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
102 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
103 // If it is a line then we have to check that first attribute id not a line
104 std::shared_ptr<SketchPlugin_Feature> aSFeature =
105 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
106 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
107 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
108 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
109 aFeature->data(), aParamA, aPlane);
111 theError = "One of parameters of distance constraint should be a point";
119 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
120 const std::list<std::string>& theArguments,
121 Events_InfoMessage& theError) const
123 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
124 theError = "The attribute with the %1 type is not processed";
125 theError.arg(theAttribute->attributeType());
129 // there is a check whether the feature contains a point and a linear edge or two point values
130 std::string aParamA = theArguments.front();
131 SessionPtr aMgr = ModelAPI_Session::get();
132 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
134 FeaturePtr anAttributeFeature =
135 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
136 AttributeRefAttrPtr aRefAttr =
137 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
139 bool isObject = aRefAttr->isObject();
140 ObjectPtr anObject = aRefAttr->object();
141 if (isObject && anObject.get()) {
142 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
144 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
145 ObjectPtr aOtherObject = aOtherAttr->object();
146 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
150 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
151 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
152 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
153 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
154 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
155 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
159 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
160 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
161 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
162 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
163 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
164 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
165 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
166 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
167 .arg(SketchPlugin_Circle::ID());
172 theError = "It refers to %1, but should refer to %2 or %3 or %4";
173 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
174 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
180 theError = "It uses an empty object";
187 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
188 const std::list<std::string>& theArguments,
189 Events_InfoMessage& theError) const
191 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
192 theError = "The attribute with the %1 type is not processed";
193 theError.arg(theAttribute->attributeType());
197 std::shared_ptr<SketchPlugin_Feature> aFeature =
198 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
202 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
204 SketchPlugin_Sketch* aSketch = aFeature->sketch();
205 int aNbFeatures = aSketch->numberOfSubs();
206 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
207 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
208 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
210 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
211 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
212 if (aRefAttr->isObject()) {
213 if (aRefAttr->object() == aRAttr->object()) {
214 ObjectPtr anObject = aRefAttr->object();
215 std::string aName = anObject.get() ? anObject->data()->name() : "";
216 theError = "The object %1 has been already fixed.";
221 else if (aRefAttr->attr() == aRAttr->attr()) {
222 AttributePtr anAttribute = aRefAttr->attr();
223 std::string aName = anAttribute.get() ? anAttribute->id() : "";
224 theError = "The attribute %1 has been already fixed.";
232 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
233 const std::list<std::string>& theArguments,
234 Events_InfoMessage& theError) const
236 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
237 theError = "The attribute with the %1 type is not processed";
238 theError.arg(theAttribute->attributeType());
242 std::string aParamA = theArguments.front();
243 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
244 AttributeRefAttrPtr aRefAttr[2];
245 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
246 aRefAttr[1] = aFeature->data()->refattr(aParamA);
248 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
249 theError = "Attributes can not be used in equal constraint";
253 std::string aType[2];
254 std::list<std::string> anArguments;
255 for (int i = 0; i < 2; i++) {
256 ObjectPtr anObject = aRefAttr[i]->object();
257 if (!anObject.get()) {
258 theError = "An empty object is used.";
262 aFeature = ModelAPI_Feature::feature(anObject);
263 if (!aFeature.get()) {
264 theError = "An empty feature is used.";
268 aType[i] = aFeature->getKind();
269 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
270 aFeature->getKind() != SketchPlugin_Circle::ID() &&
271 aFeature->getKind() != SketchPlugin_Arc::ID()) {
272 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
273 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
274 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
275 // wrong type of attribute
280 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
281 aType[0] != aType[1]) {
282 theError = "Feature with kinds %1 and %2 can not be equal.";
283 theError.arg(aType[0]).arg(aType[1]);
289 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
290 const std::list<std::string>& theArguments,
291 Events_InfoMessage& theError) const
293 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
294 theError = "The attribute with the %1 type is not processed";
295 theError.arg(theAttribute->attributeType());
299 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
300 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
302 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
303 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
304 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
306 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
307 ObjectPtr aSelObject = aSelAttr->object(anInd);
308 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
309 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
310 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
311 if (aSelObject == *aMirIter) {
312 theError = "The object %1 is a result of mirror";
320 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
321 const std::list<std::string>& theArguments,
322 Events_InfoMessage& theError) const
324 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
325 theError = "The attribute with the %1 type is not processed";
326 theError.arg(theAttribute->attributeType());
330 // there is a check whether the feature contains a point and a linear edge or two point values
331 std::string aParamA = theArguments.front();
332 SessionPtr aMgr = ModelAPI_Session::get();
333 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
335 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
336 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
338 theError = "The %1 attribute should be %2";
339 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
343 AttributeRefAttrPtr aRefAttrB =
344 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
346 // first attribute is a point, it may coincide with any object
347 if (!aRefAttrA->isObject())
350 ObjectPtr anObject = aRefAttrA->object();
351 if (!anObject.get()) {
352 theError = "%1 attribute has an empty object";
353 theError.arg(aParamA);
356 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
357 if (!aFeature.get()) {
358 theError = "%1 attribute has an empty feature";
359 theError.arg(aParamA);
363 if (aFeature->getKind() == SketchPlugin_Point::ID())
367 // second attribute is a point, it may coincide with any object
368 if (!aRefAttrB->isObject())
371 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
373 theError = "%1 attribute has an empty object";
374 theError.arg(theAttribute->id());
377 if (aFeature->getKind() == SketchPlugin_Point::ID())
380 theError = "There is no an attribute filled by a point";
385 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
386 const std::list<std::string>& theArguments,
387 Events_InfoMessage& theError) const
389 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
390 theError = "The attribute with the %1 type is not processed";
391 theError.arg(theAttribute->attributeType());
395 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
396 AttributeRefListPtr aSelAttr =
397 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
399 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
400 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
401 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
402 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
403 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
404 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
406 std::list<ObjectPtr>::iterator anObjIter;
407 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
408 ObjectPtr aSelObject = aSelAttr->object(anInd);
409 anObjIter = anInitialObjects.begin();
410 for (; anObjIter != anInitialObjects.end(); anObjIter++)
411 if (aSelObject == *anObjIter)
413 if (anObjIter != anInitialObjects.end())
415 anObjIter = aCopiedObjects.begin();
416 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
417 if (aSelObject == *anObjIter) {
418 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
419 theError = "The object %1 is a result of copy";
427 bool SketchPlugin_SolverErrorValidator::isValid(
428 const std::shared_ptr<ModelAPI_Feature>& theFeature,
429 const std::list<std::string>& theArguments,
430 Events_InfoMessage& theError) const
432 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
434 if (!aAttributeString->value().empty()) {
435 theError = aAttributeString->value();
442 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
443 std::string theAttribute)
448 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
449 const FeaturePtr theFeature)
451 for(std::set<AttributePtr>::const_iterator
452 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
453 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
454 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
455 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
456 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
457 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
458 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
459 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
460 if(anAttrRefA.get()) {
461 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
463 DocumentPtr aDoc = aResA->document();
465 FeaturePtr aFeatureA = aDoc->feature(aResA);
466 if(aFeatureA.get() && aFeatureA == theFeature) {
472 if(anAttrRefB.get()) {
473 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
475 DocumentPtr aDoc = aResB->document();
477 FeaturePtr aFeatureB = aDoc->feature(aResB);
478 if(aFeatureB.get() && aFeatureB == theFeature) {
489 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
490 const std::list<std::string>& theArguments,
491 Events_InfoMessage& theError) const
493 AttributeRefAttrPtr aPointRefAttr =
494 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
495 if(!aPointRefAttr.get()) {
496 theError = "Error: Point not selected.";
500 AttributePtr aPointAttribute = aPointRefAttr->attr();
501 if (!aPointAttribute.get()) {
502 theError = "Error: Bad point selected.";
505 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
506 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
508 // Obtain constraint coincidence for the fillet point.
509 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
510 FeaturePtr aConstraintCoincidence;
511 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
512 anIt != aRefsList.cend(); ++anIt) {
513 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
514 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
515 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
516 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
517 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
518 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
519 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
520 if(anAttrRefA.get() && !anAttrRefA->isObject()) {
521 AttributePtr anAttrA = anAttrRefA->attr();
522 if(aPointAttribute == anAttrA) {
523 aConstraintCoincidence = aConstrFeature;
527 if(anAttrRefB.get() && !anAttrRefB->isObject()) {
528 AttributePtr anAttrB = anAttrRefB->attr();
529 if(aPointAttribute == anAttrB) {
530 aConstraintCoincidence = aConstrFeature;
537 if(!aConstraintCoincidence.get()) {
538 theError = "Error: one of the selected point does not have coicidence.";
542 // Get coincides from constraint.
543 std::set<FeaturePtr> aCoinsides;
544 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
545 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
547 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
548 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
551 // Remove points from set of coincides.
552 std::set<FeaturePtr> aNewSetOfCoincides;
553 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
554 anIt != aCoinsides.end(); ++anIt) {
555 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
556 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
557 if(aSketchEntity.get() && aSketchEntity->isCopy()) {
560 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
561 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
564 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
565 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
566 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
567 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
568 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
569 if(aDistSelectedArcCenter < tolerance) {
573 aNewSetOfCoincides.insert(*anIt);
575 aCoinsides = aNewSetOfCoincides;
577 // If we still have more than two coincides remove auxilary entities from set of coincides.
578 if(aCoinsides.size() > 2) {
579 aNewSetOfCoincides.clear();
580 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
581 anIt != aCoinsides.end(); ++anIt) {
582 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
583 aNewSetOfCoincides.insert(*anIt);
586 aCoinsides = aNewSetOfCoincides;
589 if(aCoinsides.size() != 2) {
590 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
594 // Check that selected edges don't have tangent constraint.
595 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
596 FeaturePtr aFirstFeature = *anIt++;
597 FeaturePtr aSecondFeature = *anIt;
598 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
599 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
600 theError = "Error: Edges in selected point has tangent constraint.";
604 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
605 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
606 aResIt != aFirstResults.end(); ++aResIt) {
607 ResultPtr aRes = *aResIt;
608 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
609 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
610 theError = "Error: Edges in selected point has tangent constraint.";
615 // Check that lines not collinear
616 if(aFirstFeature->getKind() == SketchPlugin_Line::ID() &&
617 aSecondFeature->getKind() == SketchPlugin_Line::ID()) {
618 std::string aStartAttr = SketchPlugin_Line::START_ID();
619 std::string anEndAttr = SketchPlugin_Line::END_ID();
620 std::shared_ptr<GeomAPI_Pnt2d> aFirstStartPnt, aFirstEndPnt, aSecondStartPnt, aSecondEndPnt;
622 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
623 aFirstFeature->attribute(aStartAttr))->pnt();
625 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFirstFeature->attribute(anEndAttr))->pnt();
627 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
628 aSecondFeature->attribute(aStartAttr))->pnt();
630 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
631 aSecondFeature->attribute(anEndAttr))->pnt();
633 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
634 (aSecondStartPnt->y() - aFirstStartPnt->y()) -
635 (aSecondStartPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
637 fabs((aFirstEndPnt->x() - aFirstStartPnt->x()) *
638 (aSecondEndPnt->y() - aFirstStartPnt->y()) -
639 (aSecondEndPnt->x() - aFirstStartPnt->x()) * (aFirstEndPnt->y() - aFirstStartPnt->y()));
640 if(aCheck1 < 1.e-7 && aCheck2 < 1.e-7) {
649 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
650 const std::list<std::string>& theArguments,
651 Events_InfoMessage& theError) const
653 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
654 theError = "The attribute with the %1 type is not processed";
655 theError.arg(theAttribute->attributeType());
659 // there is a check whether the feature contains a point and a linear edge or two point values
660 std::string aParamA = theArguments.front();
661 SessionPtr aMgr = ModelAPI_Session::get();
662 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
664 FeaturePtr anAttributeFeature =
665 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
666 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
667 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
669 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
672 for (int i = 0; i < 2; ++i) {
673 if (!aRefAttrs[i]->isObject())
676 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
678 if (aNbPoints + aNbLines != 0)
683 if (aFeature->getKind() == SketchPlugin_Point::ID())
685 else if (aFeature->getKind() == SketchPlugin_Line::ID())
690 if (aNbPoints != 1 || aNbLines != 1) {
691 theError = "Middle point constraint allows points and lines only";
697 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
698 const std::list<std::string>& /*theArguments*/,
699 Events_InfoMessage& theError) const
701 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
702 theError = "The attribute with the %1 type is not processed";
703 theError.arg(theAttribute->attributeType());
706 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
707 AttributePtr anAttr = aRefAttr->attr();
709 theError = "The attribute %1 should be a point";
710 theError.arg(theAttribute->id());
714 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
715 const std::string& aFeatureType = anAttrFeature->getKind();
716 if (aFeatureType == SketchPlugin_Arc::ID()) {
717 // selected point should not be a center of arc
718 const std::string& aPntId = anAttr->id();
719 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
720 theError = "The attribute %1 is not supported";
721 theError.arg(aPntId);
725 else if (aFeatureType == SketchPlugin_Line::ID()) {
726 // selected point should be bound point of line
727 const std::string& aPntId = anAttr->id();
728 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
729 theError = "The attribute %1 is not supported";
730 theError.arg(aPntId);
735 theError = "Unable to build tangent arc on %1";
736 theError.arg(anAttrFeature->getKind());
743 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
744 const std::list<std::string>& theArguments,
745 Events_InfoMessage& theError) const
747 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
748 theError = "The attribute with the %1 type is not processed";
749 theError.arg(theAttribute->attributeType());
752 AttributeSelectionPtr aLineAttr =
753 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
754 std::shared_ptr<GeomAPI_Edge> anEdge;
755 if(aLineAttr && aLineAttr->value() && aLineAttr->value()->isEdge()) {
756 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->value()));
757 } else if(aLineAttr->context() &&
758 aLineAttr->context()->shape() && aLineAttr->context()->shape()->isEdge()) {
759 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLineAttr->context()->shape()));
762 if (!anEdge || !anEdge->isLine()) {
763 theError = "The attribute %1 should be a line";
764 theError.arg(theAttribute->id());
768 std::shared_ptr<GeomAPI_Dir> aLineDir = anEdge->line()->direction();
771 std::shared_ptr<SketchPlugin_Sketch> aSketch;
772 std::set<AttributePtr> aRefs = aLineAttr->owner()->data()->refsToMe();
773 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
774 for (; anIt != aRefs.end(); ++anIt) {
775 CompositeFeaturePtr aComp =
776 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
777 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
778 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
783 theError = "There is no sketch referring to the current feature";
787 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
788 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
789 return fabs(aNormal->dot(aLineDir)) > tolerance * tolerance;
792 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
793 const std::list<std::string>& theArguments,
794 Events_InfoMessage& theError) const
798 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
799 theError = "The attribute with the %1 type is not processed";
800 theError.arg(theAttribute->attributeType());
803 AttributeReferencePtr aFeatureAttr =
804 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
806 ObjectPtr anAttrObject = aFeatureAttr->value();
807 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
811 std::string aKind = anAttrFeature->getKind();
812 if (aKind == SketchPlugin_Line::ID() ||
813 aKind == SketchPlugin_Arc::ID() ||
814 aKind == SketchPlugin_Circle::ID()) {
816 std::set<ResultPtr> anEdgeShapes;
817 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
818 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
821 // coincidences to the feature
822 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
823 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
824 SketchPlugin_ConstraintCoincidence::ID(),
825 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
827 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
828 std::shared_ptr<SketchPlugin_Feature> aSFeature =
829 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
830 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
832 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
833 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
834 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
835 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
836 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
837 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
838 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
839 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
841 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
842 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
843 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
844 PointToRefsMap aPointsInfo;
846 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
847 aX->dir(), aDirY, aPointsInfo);
848 int aCoincidentToFeature = (int)aPointsInfo.size();
849 if (aKind == SketchPlugin_Circle::ID())
850 aValid = aCoincidentToFeature >= 2;
852 aValid = aCoincidentToFeature >= 1;
858 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
859 const std::list<std::string>& theArguments,
860 Events_InfoMessage& theError) const
864 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
865 theError = "The attribute with the %1 type is not processed";
866 theError.arg(theAttribute->attributeType());
869 AttributeReferencePtr aBaseObjectAttr =
870 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
872 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
873 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
875 ObjectPtr aBaseObject = aBaseObjectAttr->value();
877 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
878 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
879 aBaseObject = aBaseObjectAttr->value();
884 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
888 std::string aKind = aBaseFeature->getKind();
889 if (aKind != SketchPlugin_Line::ID() &&
890 aKind != SketchPlugin_Arc::ID() &&
891 aKind != SketchPlugin_Circle::ID())
895 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
896 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
898 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
900 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
901 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
902 anAttributePnt2d->y());
904 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
905 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
906 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
907 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
908 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
909 aCashedShapes, anObjectToPoints);
910 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
912 return aShapes.size() > 1;
915 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
916 const std::list<std::string>& theArguments,
917 Events_InfoMessage& theError) const
919 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
920 theError = "The attribute with the %1 type is not processed";
921 theError.arg(theAttribute->attributeType());
925 AttributeSelectionPtr aFeatureAttr =
926 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
927 std::shared_ptr<GeomAPI_Edge> anEdge;
928 if (aFeatureAttr.get()) {
929 GeomShapePtr aVal = aFeatureAttr->value();
930 ResultPtr aRes = aFeatureAttr->context();
931 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
932 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
933 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
934 aFeatureAttr->context()->shape()->isEdge()) {
935 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
939 theError = "The attribute %1 should be an edge";
940 theError.arg(theAttribute->id());
945 std::shared_ptr<SketchPlugin_Sketch> aSketch;
946 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
947 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
948 for (; anIt != aRefs.end(); ++anIt) {
949 CompositeFeaturePtr aComp =
950 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
951 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
952 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
957 theError = "There is no sketch referring to the current feature";
961 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
962 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
963 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
965 if (anEdge->isLine()) {
966 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
967 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
968 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
969 double aDot = aNormal->dot(aLineDir);
970 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
971 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
972 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
974 theError = "Error: Edge is already in the sketch plane.";
977 else if (anEdge->isCircle() || anEdge->isArc()) {
978 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
979 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
980 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
981 double aDot = fabs(aNormal->dot(aCircNormal));
982 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
983 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
985 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
986 : "Error: Arc is already in the sketch plane.");
990 theError = "Error: Selected object is not line, circle or arc.";
995 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
996 const std::set<FeaturePtr>& theSet2)
998 std::set<FeaturePtr> aCommon;
999 if (theSet1.empty() || theSet2.empty())
1002 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1003 for (; anIt2 != theSet2.end(); ++anIt2)
1004 if (theSet1.find(*anIt2) != theSet1.end())
1005 aCommon.insert(*anIt2);
1009 bool SketchPlugin_DifferentReferenceValidator::isValid(
1010 const AttributePtr& theAttribute,
1011 const std::list<std::string>& theArguments,
1012 Events_InfoMessage& theError) const
1014 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1016 int aNbFeaturesReferred = 0;
1017 int aNbAttributesReferred = 0;
1018 std::set<FeaturePtr> aCommonReferredFeatures;
1020 // find all features referred by attributes listed in theArguments
1021 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1022 for (; anArgIt != theArguments.end(); ++anArgIt) {
1023 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1027 std::set<FeaturePtr> aCoincidentFeatures;
1028 if (aRefAttr->isObject()) {
1029 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1031 aCoincidentFeatures.insert(aFeature);
1032 aNbFeaturesReferred += 1;
1035 AttributePoint2DPtr aPoint =
1036 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1038 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1039 aNbAttributesReferred += 1;
1043 if (aCommonReferredFeatures.empty())
1044 aCommonReferredFeatures = aCoincidentFeatures;
1046 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1048 if (aCommonReferredFeatures.empty())
1052 bool isOk = aNbFeaturesReferred < 1;
1053 if (aNbFeaturesReferred == 1) {
1054 if (aCommonReferredFeatures.size() == 1) {
1055 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1056 isOk = aNbAttributesReferred <= 1 ||
1057 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1058 aFeature->getKind() == SketchPlugin_Arc::ID();
1064 theError = "Attributes are referred to the same feature";
1068 bool SketchPlugin_CirclePassedPointValidator::isValid(
1069 const AttributePtr& theAttribute,
1070 const std::list<std::string>&,
1071 Events_InfoMessage& theError) const
1073 static const std::string aErrorMessage(
1074 "Passed point refers to the same feature as a center point");
1076 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1078 AttributeRefAttrPtr aCenterRef =
1079 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1080 AttributeRefAttrPtr aPassedRef =
1081 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1083 if (!aPassedRef->isObject())
1086 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1087 if (!aPassedFeature)
1090 if (aCenterRef->isObject()) {
1091 FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
1092 if (aCenterFeature == aPassedFeature) {
1093 theError = aErrorMessage;
1097 AttributePoint2DPtr aCenterPoint =
1098 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1100 std::set<FeaturePtr> aCoincidentFeatures =
1101 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1102 // check one of coincident features is a feature referred by passed point
1103 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1104 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1105 if (*anIt == aPassedFeature) {
1106 theError = aErrorMessage;
1114 bool SketchPlugin_ThirdPointValidator::isValid(
1115 const AttributePtr& theAttribute,
1116 const std::list<std::string>& theArguments,
1117 Events_InfoMessage& theError) const
1119 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1120 return arePointsNotOnLine(anOwner, theError) &&
1121 arePointsNotSeparated(anOwner, theArguments, theError);
1124 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1125 const std::string& thePointAttrName,
1126 const std::string& theRefPointAttrName)
1128 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1129 theMacroCircle->attribute(thePointAttrName));
1130 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1132 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1134 if (aRefAttr->isObject()) {
1135 // project a point onto selected feature
1136 std::shared_ptr<SketchPlugin_Feature> aFeature =
1137 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1138 ModelAPI_Feature::feature(aRefAttr->object()));
1140 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1141 std::shared_ptr<GeomAPI_Edge> anEdge =
1142 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1144 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1145 if (anEdge->isLine())
1146 aPoint3D = anEdge->line()->project(aPoint3D);
1147 else if (anEdge->isCircle())
1148 aPoint3D = anEdge->circle()->project(aPoint3D);
1149 aPoint = aSketch->to2D(aPoint3D);
1153 AttributePoint2DPtr anOtherPoint =
1154 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1156 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1163 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1164 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1166 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1167 thePoints[0] = toPoint(theMacroFeature,
1168 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1169 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1170 thePoints[1] = toPoint(theMacroFeature,
1171 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1172 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1173 thePoints[2] = toPoint(theMacroFeature,
1174 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1175 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1176 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1177 thePoints[0] = toPoint(theMacroFeature,
1178 SketchPlugin_MacroArc::START_POINT_2_ID(),
1179 SketchPlugin_MacroArc::START_POINT_REF_ID());
1180 thePoints[1] = toPoint(theMacroFeature,
1181 SketchPlugin_MacroArc::END_POINT_2_ID(),
1182 SketchPlugin_MacroArc::END_POINT_REF_ID());
1183 thePoints[2] = toPoint(theMacroFeature,
1184 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1185 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1189 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1190 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1191 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1193 static const double aTolerance = 1.e-7;
1194 if (thePoint1->distance(thePoint2) < aTolerance ||
1195 thePoint1->distance(thePoint3) < aTolerance)
1198 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1199 thePoint2->y() - thePoint1->y()));
1200 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1201 thePoint3->y() - thePoint1->y()));
1202 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1205 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1206 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1207 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1209 static const double aTolerance = 1.e-7;
1210 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1211 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1212 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
1213 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
1214 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1217 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1218 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1219 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1221 static const double aTolerance = 1.e-7;
1222 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1223 double aDistP1C = thePoint1->distance(aCenter);
1224 double aDistP2C = thePoint2->distance(aCenter);
1225 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1228 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1229 const FeaturePtr& theMacroFeature,
1230 Events_InfoMessage& theError) const
1232 static const std::string aErrorPointsOnLine(
1233 "Selected points are on the same line");
1235 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1236 threePointsOfFeature(theMacroFeature, aPoints);
1238 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1239 theError = aErrorPointsOnLine;
1245 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1246 const FeaturePtr& theMacroFeature,
1247 const std::list<std::string>& theArguments,
1248 Events_InfoMessage& theError) const
1250 static const std::string aErrorPointsApart(
1251 "Selected entity is lying between first two points");
1253 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1254 FeaturePtr aRefByThird;
1255 if (aThirdPointRef->isObject())
1256 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1260 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1261 threePointsOfFeature(theMacroFeature, aPoints);
1263 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1264 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1268 SketchPlugin_Sketch* aSketch =
1269 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1270 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1271 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1274 if (aThirdShape->isLine())
1275 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1276 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1277 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1280 theError = aErrorPointsApart;
1284 bool SketchPlugin_ArcEndPointValidator::isValid(
1285 const AttributePtr& theAttribute,
1286 const std::list<std::string>& theArguments,
1287 Events_InfoMessage& theError) const
1289 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1290 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1292 if(!anEndPointRef.get()) {
1296 ObjectPtr anObject = anEndPointRef->object();
1297 AttributePtr anAttr = anEndPointRef->attr();
1298 if(!anObject.get() && !anAttr.get()) {
1302 if(anEndPointRef->attr().get()) {
1306 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1308 GeomShapePtr aShape = aResult->shape();
1309 if(aShape.get() && aShape->isVertex()) {
1314 aFeature = ModelAPI_Feature::feature(anObject);
1315 if(aFeature.get()) {
1316 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1324 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1325 const AttributePtr& theAttribute,
1326 const std::list<std::string>& theArguments,
1327 Events_InfoMessage& theError) const
1329 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1330 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1331 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1333 if(!anEndPointRef.get()) {
1337 GeomShapePtr anArcShape = anArcFeature->getArcShape();
1339 if(!anArcShape.get() || anArcShape->isNull()) {
1343 ObjectPtr anObject = anEndPointRef->object();
1344 AttributePtr anAttr = anEndPointRef->attr();
1345 if(!anObject.get() && !anAttr.get()) {
1349 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1351 GeomShapePtr aShape = aResult->shape();
1352 if(aShape.get() && !aShape->isNull()) {
1353 GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1354 if(anIntersection.get() && !anIntersection->isNull()) {
1360 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1361 if(aSelectedFeature.get()) {
1362 std::list<ResultPtr> aResults = aSelectedFeature->results();
1363 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1364 anIt != aResults.cend();
1367 GeomShapePtr aShape = (*anIt)->shape();
1368 if(aShape.get() && !aShape->isNull()) {
1369 GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1370 if(anIntersection.get() && !anIntersection->isNull()) {