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();
882 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
886 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
887 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
888 if (!aSketchFeature.get() || aSketchFeature->isCopy())
891 std::string aKind = aBaseFeature->getKind();
892 if (aKind != SketchPlugin_Line::ID() &&
893 aKind != SketchPlugin_Arc::ID() &&
894 aKind != SketchPlugin_Circle::ID())
898 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
899 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
901 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
903 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
904 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
905 anAttributePnt2d->y());
907 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
908 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
909 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
910 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
911 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
912 aCashedShapes, anObjectToPoints);
913 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
915 return aShapes.size() > 1;
918 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
919 const std::list<std::string>& theArguments,
920 Events_InfoMessage& theError) const
922 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
923 theError = "The attribute with the %1 type is not processed";
924 theError.arg(theAttribute->attributeType());
928 AttributeSelectionPtr aFeatureAttr =
929 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
930 std::shared_ptr<GeomAPI_Edge> anEdge;
931 if (aFeatureAttr.get()) {
932 GeomShapePtr aVal = aFeatureAttr->value();
933 ResultPtr aRes = aFeatureAttr->context();
934 if(aFeatureAttr->value() && aFeatureAttr->value()->isEdge()) {
935 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
936 } else if(aFeatureAttr->context() && aFeatureAttr->context()->shape() &&
937 aFeatureAttr->context()->shape()->isEdge()) {
938 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
942 theError = "The attribute %1 should be an edge";
943 theError.arg(theAttribute->id());
948 std::shared_ptr<SketchPlugin_Sketch> aSketch;
949 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
950 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
951 for (; anIt != aRefs.end(); ++anIt) {
952 CompositeFeaturePtr aComp =
953 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
954 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
955 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
960 theError = "There is no sketch referring to the current feature";
964 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
965 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
966 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
968 if (anEdge->isLine()) {
969 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
970 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
971 std::shared_ptr<GeomAPI_Pnt> aLineLoc = aLine->location();
972 double aDot = aNormal->dot(aLineDir);
973 double aDist = aLineLoc->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
974 bool aValid = (fabs(aDot) >= tolerance && fabs(aDot) < 1.0 - tolerance) ||
975 (fabs(aDot) < tolerance && fabs(aDist) > tolerance);
977 theError = "Error: Edge is already in the sketch plane.";
980 else if (anEdge->isCircle() || anEdge->isArc()) {
981 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
982 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
983 std::shared_ptr<GeomAPI_Pnt> aCircCenter = aCircle->center();
984 double aDot = fabs(aNormal->dot(aCircNormal));
985 double aDist = aCircCenter->xyz()->decreased(anOrigin->xyz())->dot(aNormal->xyz());
986 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance && fabs(aDist) > tolerance;
988 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
989 : "Error: Arc is already in the sketch plane.");
993 theError = "Error: Selected object is not line, circle or arc.";
998 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
999 const std::set<FeaturePtr>& theSet2)
1001 std::set<FeaturePtr> aCommon;
1002 if (theSet1.empty() || theSet2.empty())
1005 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1006 for (; anIt2 != theSet2.end(); ++anIt2)
1007 if (theSet1.find(*anIt2) != theSet1.end())
1008 aCommon.insert(*anIt2);
1012 bool SketchPlugin_DifferentReferenceValidator::isValid(
1013 const AttributePtr& theAttribute,
1014 const std::list<std::string>& theArguments,
1015 Events_InfoMessage& theError) const
1017 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1019 int aNbFeaturesReferred = 0;
1020 int aNbAttributesReferred = 0;
1021 std::set<FeaturePtr> aCommonReferredFeatures;
1023 // find all features referred by attributes listed in theArguments
1024 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1025 for (; anArgIt != theArguments.end(); ++anArgIt) {
1026 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1030 std::set<FeaturePtr> aCoincidentFeatures;
1031 if (aRefAttr->isObject()) {
1032 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1034 aCoincidentFeatures.insert(aFeature);
1035 aNbFeaturesReferred += 1;
1038 AttributePoint2DPtr aPoint =
1039 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1041 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1042 aNbAttributesReferred += 1;
1046 if (aCommonReferredFeatures.empty())
1047 aCommonReferredFeatures = aCoincidentFeatures;
1049 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1051 if (aCommonReferredFeatures.empty())
1055 bool isOk = aNbFeaturesReferred < 1;
1056 if (aNbFeaturesReferred == 1) {
1057 if (aCommonReferredFeatures.size() == 1) {
1058 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1059 isOk = aNbAttributesReferred <= 1 ||
1060 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1061 aFeature->getKind() == SketchPlugin_Arc::ID();
1067 theError = "Attributes are referred to the same feature";
1071 bool SketchPlugin_CirclePassedPointValidator::isValid(
1072 const AttributePtr& theAttribute,
1073 const std::list<std::string>&,
1074 Events_InfoMessage& theError) const
1076 static const std::string aErrorMessage(
1077 "Passed point refers to the same feature as a center point");
1079 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1081 AttributeRefAttrPtr aCenterRef =
1082 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1083 AttributeRefAttrPtr aPassedRef =
1084 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1086 if (!aPassedRef->isObject())
1089 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1090 if (!aPassedFeature)
1093 if (aCenterRef->isObject()) {
1094 FeaturePtr aCenterFeature = ModelAPI_Feature::feature(aCenterRef->object());
1095 if (aCenterFeature == aPassedFeature) {
1096 theError = aErrorMessage;
1100 AttributePoint2DPtr aCenterPoint =
1101 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1103 std::set<FeaturePtr> aCoincidentFeatures =
1104 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1105 // check one of coincident features is a feature referred by passed point
1106 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1107 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1108 if (*anIt == aPassedFeature) {
1109 theError = aErrorMessage;
1117 bool SketchPlugin_ThirdPointValidator::isValid(
1118 const AttributePtr& theAttribute,
1119 const std::list<std::string>& theArguments,
1120 Events_InfoMessage& theError) const
1122 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1123 return arePointsNotOnLine(anOwner, theError) &&
1124 arePointsNotSeparated(anOwner, theArguments, theError);
1127 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1128 const std::string& thePointAttrName,
1129 const std::string& theRefPointAttrName)
1131 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1132 theMacroCircle->attribute(thePointAttrName));
1133 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1135 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1137 if (aRefAttr->isObject()) {
1138 // project a point onto selected feature
1139 std::shared_ptr<SketchPlugin_Feature> aFeature =
1140 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1141 ModelAPI_Feature::feature(aRefAttr->object()));
1143 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1144 std::shared_ptr<GeomAPI_Edge> anEdge =
1145 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1147 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1148 if (anEdge->isLine())
1149 aPoint3D = anEdge->line()->project(aPoint3D);
1150 else if (anEdge->isCircle())
1151 aPoint3D = anEdge->circle()->project(aPoint3D);
1153 aPoint = aSketch->to2D(aPoint3D);
1157 AttributePoint2DPtr anOtherPoint =
1158 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1160 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1167 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1168 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1170 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1171 thePoints[0] = toPoint(theMacroFeature,
1172 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1173 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1174 thePoints[1] = toPoint(theMacroFeature,
1175 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1176 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1177 thePoints[2] = toPoint(theMacroFeature,
1178 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1179 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1180 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1181 thePoints[0] = toPoint(theMacroFeature,
1182 SketchPlugin_MacroArc::START_POINT_2_ID(),
1183 SketchPlugin_MacroArc::START_POINT_REF_ID());
1184 thePoints[1] = toPoint(theMacroFeature,
1185 SketchPlugin_MacroArc::END_POINT_2_ID(),
1186 SketchPlugin_MacroArc::END_POINT_REF_ID());
1187 thePoints[2] = toPoint(theMacroFeature,
1188 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1189 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1193 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1194 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1195 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1197 static const double aTolerance = 1.e-7;
1198 if (thePoint1->distance(thePoint2) < aTolerance ||
1199 thePoint1->distance(thePoint3) < aTolerance)
1202 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1203 thePoint2->y() - thePoint1->y()));
1204 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1205 thePoint3->y() - thePoint1->y()));
1206 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1209 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1210 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1211 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1213 static const double aTolerance = 1.e-7;
1214 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1215 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1216 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(thePoint1->xyz()->decreased(aLineLoc)));
1217 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(thePoint2->xyz()->decreased(aLineLoc)));
1218 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1221 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1222 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1223 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1225 static const double aTolerance = 1.e-7;
1226 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1227 double aDistP1C = thePoint1->distance(aCenter);
1228 double aDistP2C = thePoint2->distance(aCenter);
1229 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1232 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1233 const FeaturePtr& theMacroFeature,
1234 Events_InfoMessage& theError) const
1236 static const std::string aErrorPointsOnLine(
1237 "Selected points are on the same line");
1239 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1240 threePointsOfFeature(theMacroFeature, aPoints);
1242 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1243 theError = aErrorPointsOnLine;
1249 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1250 const FeaturePtr& theMacroFeature,
1251 const std::list<std::string>& theArguments,
1252 Events_InfoMessage& theError) const
1254 static const std::string aErrorPointsApart(
1255 "Selected entity is lying between first two points");
1257 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1258 FeaturePtr aRefByThird;
1259 if (aThirdPointRef->isObject())
1260 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1264 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1265 threePointsOfFeature(theMacroFeature, aPoints);
1267 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1268 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1272 SketchPlugin_Sketch* aSketch =
1273 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1274 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1275 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1278 if (aThirdShape->isLine())
1279 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1280 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1281 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1284 theError = aErrorPointsApart;
1288 bool SketchPlugin_ArcEndPointValidator::isValid(
1289 const AttributePtr& theAttribute,
1290 const std::list<std::string>& theArguments,
1291 Events_InfoMessage& theError) const
1293 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1294 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1296 if(!anEndPointRef.get()) {
1300 ObjectPtr anObject = anEndPointRef->object();
1301 AttributePtr anAttr = anEndPointRef->attr();
1302 if(!anObject.get() && !anAttr.get()) {
1306 if(anEndPointRef->attr().get()) {
1310 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1312 GeomShapePtr aShape = aResult->shape();
1313 if(aShape.get() && aShape->isVertex()) {
1318 aFeature = ModelAPI_Feature::feature(anObject);
1319 if(aFeature.get()) {
1320 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1328 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1329 const AttributePtr& theAttribute,
1330 const std::list<std::string>& theArguments,
1331 Events_InfoMessage& theError) const
1333 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1334 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1335 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1337 if(!anEndPointRef.get()) {
1341 GeomShapePtr anArcShape = anArcFeature->getArcShape(false);
1343 if(!anArcShape.get() || anArcShape->isNull()) {
1347 ObjectPtr anObject = anEndPointRef->object();
1348 AttributePtr anAttr = anEndPointRef->attr();
1349 if(!anObject.get() && !anAttr.get()) {
1353 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1355 GeomShapePtr aShape = aResult->shape();
1356 if(aShape.get() && !aShape->isNull()) {
1357 GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1358 if(anIntersection.get() && !anIntersection->isNull()) {
1364 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1365 if(aSelectedFeature.get()) {
1366 std::list<ResultPtr> aResults = aSelectedFeature->results();
1367 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1368 anIt != aResults.cend();
1371 GeomShapePtr aShape = (*anIt)->shape();
1372 if(aShape.get() && !aShape->isNull()) {
1373 GeomShapePtr anIntersection = anArcShape->intersect(aShape);
1374 if(anIntersection.get() && !anIntersection->isNull()) {