1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Validators.h"
22 #include "SketchPlugin_Arc.h"
23 #include "SketchPlugin_BSpline.h"
24 #include "SketchPlugin_BSplinePeriodic.h"
25 #include "SketchPlugin_Circle.h"
26 #include "SketchPlugin_ConstraintCoincidence.h"
27 #include "SketchPlugin_ConstraintCoincidenceInternal.h"
28 #include "SketchPlugin_ConstraintDistance.h"
29 #include "SketchPlugin_ConstraintRigid.h"
30 #include "SketchPlugin_ConstraintTangent.h"
31 #include "SketchPlugin_Ellipse.h"
32 #include "SketchPlugin_EllipticArc.h"
33 #include "SketchPlugin_Fillet.h"
34 #include "SketchPlugin_CurveFitting.h"
35 #include "SketchPlugin_Line.h"
36 #include "SketchPlugin_MacroArc.h"
37 #include "SketchPlugin_MacroCircle.h"
38 #include "SketchPlugin_MultiRotation.h"
39 #include "SketchPlugin_Point.h"
40 #include "SketchPlugin_Sketch.h"
41 #include "SketchPlugin_Trim.h"
42 #include "SketchPlugin_Tools.h"
44 #include "SketcherPrs_Tools.h"
46 #include <Events_InfoMessage.h>
48 #include <ModelAPI_Data.h>
49 #include <ModelAPI_Validator.h>
50 #include <ModelAPI_AttributeDouble.h>
51 #include <ModelAPI_AttributeInteger.h>
52 #include <ModelAPI_AttributeRefAttr.h>
53 #include <ModelAPI_AttributeRefAttrList.h>
54 #include <ModelAPI_AttributeRefList.h>
55 #include <ModelAPI_AttributeSelectionList.h>
56 #include <ModelAPI_AttributeString.h>
57 #include <ModelAPI_Session.h>
58 #include <ModelAPI_Tools.h>
59 #include <ModelAPI_ResultConstruction.h>
61 #include <ModelGeomAlgo_Point2D.h>
62 #include <ModelGeomAlgo_Shape.h>
64 #include <GeomAlgoAPI_EdgeBuilder.h>
65 #include <GeomAlgoAPI_ShapeTools.h>
67 #include <GeomAPI_Circ.h>
68 #include <GeomAPI_Dir2d.h>
69 #include <GeomAPI_Ellipse.h>
70 #include <GeomAPI_Lin.h>
71 #include <GeomAPI_Edge.h>
72 #include <GeomAPI_Vertex.h>
74 #include <GeomDataAPI_Point2D.h>
75 #include <GeomDataAPI_Point2DArray.h>
81 #pragma warning(disable: 4100)
84 const double tolerance = 1.e-7;
86 static bool isSpline(FeaturePtr theFeature)
88 return theFeature && (theFeature->getKind() == SketchPlugin_BSpline::ID() ||
89 theFeature->getKind() == SketchPlugin_BSplinePeriodic::ID());
93 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
94 const std::list<std::string>& theArguments,
95 Events_InfoMessage& theError) const
97 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
98 theError = "The attribute with the %1 type is not processed";
99 theError.arg(theAttribute->attributeType());
103 // there is a check whether the feature contains a point and a linear edge or two point values
104 std::string aParamA = theArguments.front();
105 SessionPtr aMgr = ModelAPI_Session::get();
106 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
108 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
110 bool isObject = aRefAttr->isObject();
112 // an attribute is a point. A point value is valid always for the distance
115 // 1. check whether the references object is a linear
116 ObjectPtr anObject = aRefAttr->object();
118 const ModelAPI_AttributeValidator* aShapeValidator =
119 dynamic_cast<const ModelAPI_AttributeValidator*>(
120 aFactory->validator("GeomValidators_ShapeType"));
121 std::list<std::string> anArguments;
122 anArguments.push_back("circle");
123 Events_InfoMessage aCircleError;
124 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
125 // the circle line is not a valid case
127 theError = "Circle can not be used in distance constraint";
132 anArguments.push_back("line");
133 Events_InfoMessage aLineError;
134 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
135 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
137 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
138 // If it is a line then we have to check that first attribute id not a line
139 std::shared_ptr<SketchPlugin_Feature> aSFeature =
140 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
141 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
142 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
143 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
144 aFeature->data(), aParamA, aPlane);
146 theError = "One of parameters of distance constraint should be a point";
154 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
155 const std::list<std::string>& theArguments,
156 Events_InfoMessage& theError) const
158 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
159 theError = "The attribute with the %1 type is not processed";
160 theError.arg(theAttribute->attributeType());
164 // there is a check whether the feature contains a point and a linear edge or two point values
165 std::string aParamA = theArguments.front();
167 FeaturePtr anAttributeFeature =
168 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
169 AttributeRefAttrPtr aRefAttr =
170 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
172 bool isObject = aRefAttr->isObject();
173 ObjectPtr anObject = aRefAttr->object();
174 if (!isObject || !anObject.get()) {
175 theError = "It uses an empty object";
179 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
181 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
182 ObjectPtr aOtherObject = aOtherAttr->object();
183 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
187 if (aRefFea->getKind() == SketchPlugin_Line::ID() &&
188 aOtherFea->getKind() == SketchPlugin_Line::ID()) {
189 theError = "Two segments cannot be tangent";
192 else if (isSpline(aRefFea) && isSpline(aOtherFea)) {
193 theError = "Two B-splines cannot be tangent";
198 bool hasSpline = isSpline(aRefFea);
199 if (!hasSpline && isSpline(aOtherFea)) {
201 std::swap(aRefFea, aOtherFea);
204 auto isApplicableCoincidence = [](FeaturePtr theFeature, const std::string& theAttrName) {
205 AttributeRefAttrPtr aRefAttr = theFeature->refattr(theAttrName);
206 if (aRefAttr->isObject())
208 AttributePtr anAttr = aRefAttr->attr();
209 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
210 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
212 return anOwner->getKind() == SketchPlugin_BSpline::ID() &&
213 (aPointAttr->id() == SketchPlugin_BSpline::START_ID() ||
214 aPointAttr->id() == SketchPlugin_BSpline::END_ID());
217 AttributePoint2DArrayPtr aPntArray =
218 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(anAttr);
219 if (aPntArray && anOwner->getKind() == SketchPlugin_BSpline::ID()) {
220 // check index of the pole
221 AttributeIntegerPtr anIndex = theAttrName == SketchPlugin_Constraint::ENTITY_A() ?
222 theFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A()) :
223 theFeature->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
224 return anIndex && (anIndex->value() == 0 || anIndex->value() == aPntArray->size() - 1);
230 AttributePoint2DArrayPtr aBSplinePoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
231 aRefFea->attribute(SketchPlugin_BSplineBase::POLES_ID()));
232 // additional check the B-spline edge and the other edge have a coincident boundary point
233 std::set<FeaturePtr> aCoincidences = SketchPlugin_Tools::findCoincidentConstraints(aRefFea);
234 for (std::set<FeaturePtr>::iterator anIt = aCoincidences.begin();
235 anIt != aCoincidences.end() && !isValid; ++anIt) {
236 std::set<FeaturePtr> aCoinc;
237 if (isApplicableCoincidence(*anIt, SketchPlugin_Constraint::ENTITY_A()))
238 SketchPlugin_Tools::findCoincidences(*anIt, SketchPlugin_Constraint::ENTITY_B(),
240 else if (isApplicableCoincidence(*anIt, SketchPlugin_Constraint::ENTITY_B()))
241 SketchPlugin_Tools::findCoincidences(*anIt, SketchPlugin_Constraint::ENTITY_A(),
244 isValid = aCoinc.find(aOtherFea) != aCoinc.end();
251 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
252 const std::list<std::string>& theArguments,
253 Events_InfoMessage& theError) const
255 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
256 theError = "The attribute with the %1 type is not processed";
257 theError.arg(theAttribute->attributeType());
261 std::string aParamA = theArguments.front();
263 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
264 AttributeRefAttrPtr aRefAttr =
265 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
267 bool isObject = aRefAttr->isObject();
268 ObjectPtr anObject = aRefAttr->object();
269 if (isObject && anObject.get()) {
270 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
272 AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
273 ObjectPtr aOtherObject = aOtherAttr->object();
274 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
276 // at least one feature should be a line
277 if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
278 aOtherFea && aOtherFea->getKind() != SketchPlugin_Line::ID()) {
279 theError = "At least one feature should be a line";
282 else if (isSpline(aRefFea) || isSpline(aOtherFea)) {
283 theError = "B-spline is not supported";
288 theError = "It uses an empty object";
295 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
296 const std::list<std::string>& theArguments,
297 Events_InfoMessage& theError) const
299 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
300 theError = "The attribute with the %1 type is not processed";
301 theError.arg(theAttribute->attributeType());
305 std::shared_ptr<SketchPlugin_Feature> aFeature =
306 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
310 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
312 SketchPlugin_Sketch* aSketch = aFeature->sketch();
313 int aNbFeatures = aSketch->numberOfSubs();
314 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
315 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
316 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
318 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
319 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
320 if (aRefAttr->isObject()) {
321 if (aRefAttr->object() == aRAttr->object()) {
322 ObjectPtr anObject = aRefAttr->object();
323 std::string aName = anObject.get() ? anObject->data()->name() : "";
324 theError = "The object %1 has been already fixed.";
329 else if (aRefAttr->attr() == aRAttr->attr()) {
330 AttributePtr anAttribute = aRefAttr->attr();
331 std::string aName = anAttribute.get() ? anAttribute->id() : "";
332 theError = "The attribute %1 has been already fixed.";
340 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
341 const std::list<std::string>& theArguments,
342 Events_InfoMessage& theError) const
344 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
345 theError = "The attribute with the %1 type is not processed";
346 theError.arg(theAttribute->attributeType());
350 std::string aParamA = theArguments.front();
351 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
352 AttributeRefAttrPtr aRefAttr[2];
353 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
354 aRefAttr[1] = aFeature->data()->refattr(aParamA);
356 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
357 theError = "Attributes can not be used in equal constraint";
361 std::string aType[2];
362 std::list<std::string> anArguments;
363 for (int i = 0; i < 2; i++) {
364 aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
368 aType[i] = aFeature->getKind();
369 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
370 aFeature->getKind() != SketchPlugin_Circle::ID() &&
371 aFeature->getKind() != SketchPlugin_Arc::ID() &&
372 aFeature->getKind() != SketchPlugin_Ellipse::ID() &&
373 aFeature->getKind() != SketchPlugin_EllipticArc::ID()) {
374 theError = "The %1 feature is not supported by the Equal constraint.";
375 theError.arg(aFeature->getKind());
376 // wrong type of attribute
381 bool isOk = aType[0] == aType[1];
383 // circle and arc may be equal
384 isOk = (aType[0] == SketchPlugin_Arc::ID() && aType[1] == SketchPlugin_Circle::ID())
385 || (aType[0] == SketchPlugin_Circle::ID() && aType[1] == SketchPlugin_Arc::ID());
388 // ellipse and elliptic arc may be equal
389 isOk = (aType[0] == SketchPlugin_EllipticArc::ID() && aType[1] == SketchPlugin_Ellipse::ID())
390 || (aType[0] == SketchPlugin_Ellipse::ID() && aType[1] == SketchPlugin_EllipticArc::ID());
393 theError = "Features with kinds %1 and %2 can not be equal.";
394 theError.arg(aType[0]).arg(aType[1]);
400 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
401 const std::list<std::string>& theArguments,
402 Events_InfoMessage& theError) const
404 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
405 theError = "The attribute with the %1 type is not processed";
406 theError.arg(theAttribute->attributeType());
410 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
411 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
413 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
414 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
415 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
417 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
418 ObjectPtr aSelObject = aSelAttr->object(anInd);
420 // B-splines are not supported in Mirror yet
421 FeaturePtr aSelFeature = ModelAPI_Feature::feature(aSelObject);
422 if (aSelFeature && (aSelFeature->getKind() == SketchPlugin_BSpline::ID() ||
423 aSelFeature->getKind() == SketchPlugin_BSplinePeriodic::ID())) {
424 theError = "Not supported";
428 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
429 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
430 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
431 if (aSelObject == *aMirIter) {
432 theError = "The object %1 is a result of mirror";
440 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
441 const std::list<std::string>& theArguments,
442 Events_InfoMessage& theError) const
444 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
445 theError = "The attribute with the %1 type is not processed";
446 theError.arg(theAttribute->attributeType());
450 // there is a check whether the feature contains a point and a linear edge or two point values
451 std::string aParamA = theArguments.front();
453 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
454 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
456 theError = "The %1 attribute should be %2";
457 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
461 AttributeRefAttrPtr aRefAttrB =
462 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
464 // first attribute is a point, it may coincide with any object
465 if (!aRefAttrA->isObject())
468 ObjectPtr anObject = aRefAttrA->object();
469 if (!anObject.get()) {
470 theError = "%1 attribute has an empty object";
471 theError.arg(aParamA);
474 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
475 if (!aFeature.get()) {
476 theError = "%1 attribute has an empty feature";
477 theError.arg(aParamA);
481 if (aFeature->getKind() == SketchPlugin_Point::ID())
485 // second attribute is a point, it may coincide with any object
486 if (!aRefAttrB->isObject())
489 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
491 theError = "%1 attribute has an empty object";
492 theError.arg(theAttribute->id());
495 if (aFeature->getKind() == SketchPlugin_Point::ID())
498 theError = "There is no an attribute filled by a point";
503 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
504 const std::list<std::string>& theArguments,
505 Events_InfoMessage& theError) const
507 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
508 theError = "The attribute with the %1 type is not processed";
509 theError.arg(theAttribute->attributeType());
513 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
514 AttributeRefListPtr aSelAttr =
515 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
517 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
518 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
519 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
520 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
521 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
522 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
524 std::list<ObjectPtr>::iterator anObjIter;
525 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
526 ObjectPtr aSelObject = aSelAttr->object(anInd);
527 anObjIter = anInitialObjects.begin();
528 for (; anObjIter != anInitialObjects.end(); anObjIter++)
529 if (aSelObject == *anObjIter)
531 if (anObjIter != anInitialObjects.end())
534 // B-splines are not supported in Copying features
535 FeaturePtr aSelFeature = ModelAPI_Feature::feature(aSelObject);
536 if (aSelFeature && (aSelFeature->getKind() == SketchPlugin_BSpline::ID() ||
537 aSelFeature->getKind() == SketchPlugin_BSplinePeriodic::ID())) {
538 theError = "Not supported";
542 anObjIter = aCopiedObjects.begin();
543 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
544 if (aSelObject == *anObjIter) {
545 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
546 theError = "The object %1 is a result of copy";
554 bool SketchPlugin_SolverErrorValidator::isValid(
555 const std::shared_ptr<ModelAPI_Feature>& theFeature,
556 const std::list<std::string>& theArguments,
557 Events_InfoMessage& theError) const
559 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
561 if (!aAttributeString->value().empty()) {
562 theError = aAttributeString->value();
569 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
570 std::string theAttribute)
575 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
576 const FeaturePtr theFeature)
578 for(std::set<AttributePtr>::const_iterator
579 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
580 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
581 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
584 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
585 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
586 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
587 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
588 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
589 if(anAttrRefA.get()) {
590 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
592 DocumentPtr aDoc = aResA->document();
594 FeaturePtr aFeatureA = aDoc->feature(aResA);
595 if(aFeatureA.get() && aFeatureA == theFeature) {
601 if(anAttrRefB.get()) {
602 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
604 DocumentPtr aDoc = aResB->document();
606 FeaturePtr aFeatureB = aDoc->feature(aResB);
607 if(aFeatureB.get() && aFeatureB == theFeature) {
618 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
620 AttributeRefAttrPtr aRefAttr[2] = {
621 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
622 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
625 bool arePoints = true;
626 for (int i = 0; i < 2 && arePoints; ++i) {
627 if (aRefAttr[i]->isObject()) {
628 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
629 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
631 arePoints = aRefAttr[i]->attr().get() != NULL;
636 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
637 const std::list<std::string>& theArguments,
638 Events_InfoMessage& theError) const
640 AttributeRefAttrPtr aPointRefAttr =
641 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
642 if(!aPointRefAttr.get()) {
643 theError = "Error: Point not selected.";
647 AttributePtr aPointAttribute = aPointRefAttr->attr();
648 if (!aPointAttribute.get()) {
649 theError = "Error: Bad point selected.";
652 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
653 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
655 // Obtain constraint coincidence for the fillet point.
656 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
657 FeaturePtr aConstraintCoincidence;
658 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
659 anIt != aRefsList.cend(); ++anIt) {
660 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
661 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
662 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
663 if (!isPointPointCoincidence(aConstrFeature))
666 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
667 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
668 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
669 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
671 AttributePtr anAttrA = anAttrRefA->attr();
672 if(aPointAttribute == anAttrA) {
673 aConstraintCoincidence = aConstrFeature;
677 AttributePtr anAttrB = anAttrRefB->attr();
678 if(aPointAttribute == anAttrB) {
679 aConstraintCoincidence = aConstrFeature;
685 if(!aConstraintCoincidence.get()) {
686 theError = "Error: one of the selected point does not have coincidence.";
690 // Get coincides from constraint.
691 std::set<FeaturePtr> aCoinsides;
692 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
693 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
696 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
697 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
701 // Remove points and external lines from set of coincides.
702 std::set<FeaturePtr> aNewSetOfCoincides;
703 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
704 anIt != aCoinsides.end(); ++anIt) {
705 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
706 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
707 if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
710 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
711 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
714 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
715 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
716 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
717 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
718 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
719 if(aDistSelectedArcCenter < tolerance) {
723 aNewSetOfCoincides.insert(*anIt);
725 aCoinsides = aNewSetOfCoincides;
727 // If we still have more than two coincides remove auxilary entities from set of coincides.
728 if(aCoinsides.size() > 2) {
729 aNewSetOfCoincides.clear();
730 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
731 anIt != aCoinsides.end(); ++anIt) {
732 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
733 aNewSetOfCoincides.insert(*anIt);
736 aCoinsides = aNewSetOfCoincides;
739 if(aCoinsides.size() != 2) {
740 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
744 // Check that selected edges don't have tangent constraint.
745 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
746 FeaturePtr aFirstFeature = *anIt++;
747 FeaturePtr aSecondFeature = *anIt;
748 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
749 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
750 theError = "Error: Edges in selected point has tangent constraint.";
754 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
755 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
756 aResIt != aFirstResults.end(); ++aResIt) {
757 ResultPtr aRes = *aResIt;
758 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
759 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
760 theError = "Error: Edges in selected point has tangent constraint.";
765 // Check the features are not tangent
766 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
767 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
768 if (!aFirstShape || !aFirstShape->isEdge() ||
769 !aSecondShape || !aSecondShape->isEdge()) {
770 theError = "Error: At least on of the features is not an edge";
774 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
775 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
777 static const double TOL = 1.e-7;
778 if (anEdge1->isLine() && anEdge2->isLine()) {
779 // Check that lines not collinear
780 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
781 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
782 double aCross = aDir1->cross(aDir2)->squareModulus();
783 if (aCross < TOL * TOL)
785 } else if (anEdge1->isArc() && anEdge2->isArc()) {
786 // check the circles are not tangent
787 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
788 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
789 double aDistCC = aCirc1->center()->distance(aCirc2->center());
790 double aRadSum = aCirc1->radius() + aCirc2->radius();
791 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
792 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
795 // check whether line and arc are tangent
796 std::shared_ptr<GeomAPI_Circ> aCirc;
797 std::shared_ptr<GeomAPI_Lin> aLine;
798 if (anEdge1->isLine()) {
799 aLine = anEdge1->line();
800 aCirc = anEdge2->circle();
802 aCirc = anEdge1->circle();
803 aLine = anEdge2->line();
806 double aDistCL = aLine->distance(aCirc->center());
807 if (fabs(aDistCL - aCirc->radius()) < TOL)
814 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
815 const std::list<std::string>& theArguments,
816 Events_InfoMessage& theError) const
818 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
819 theError = "The attribute with the %1 type is not processed";
820 theError.arg(theAttribute->attributeType());
824 // there is a check whether the feature contains a point and a linear edge or two point values
825 std::string aParamA = theArguments.front();
827 FeaturePtr anAttributeFeature =
828 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
829 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
830 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
832 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
835 for (int i = 0; i < 2; ++i) {
836 if (!aRefAttrs[i]->isObject())
839 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
841 if (aNbPoints + aNbLines != 0)
846 if (aFeature->getKind() == SketchPlugin_Point::ID())
848 else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
849 aFeature->getKind() == SketchPlugin_Arc::ID() ||
850 aFeature->getKind() == SketchPlugin_EllipticArc::ID())
855 if (aNbPoints != 1 || aNbLines != 1) {
856 theError = "Middle point constraint allows points and lines only";
862 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
863 const std::list<std::string>& /*theArguments*/,
864 Events_InfoMessage& theError) const
866 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
867 theError = "The attribute with the %1 type is not processed";
868 theError.arg(theAttribute->attributeType());
871 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
872 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
873 if (anArcTypeAttr && anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE())
874 return true; // not applicable for non-tangent arcs
876 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
877 AttributePtr anAttr = aRefAttr->attr();
879 theError = "The attribute %1 should be a point";
880 theError.arg(theAttribute->id());
884 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
885 const std::string& aFeatureType = anAttrFeature->getKind();
886 if (aFeatureType == SketchPlugin_Arc::ID()) {
887 // selected point should not be a center of arc
888 const std::string& aPntId = anAttr->id();
889 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
890 theError = "The attribute %1 is not supported";
891 theError.arg(aPntId);
895 else if (aFeatureType == SketchPlugin_Line::ID()) {
896 // selected point should be bound point of line
897 const std::string& aPntId = anAttr->id();
898 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
899 theError = "The attribute %1 is not supported";
900 theError.arg(aPntId);
905 theError = "Unable to build tangent arc on %1";
906 theError.arg(anAttrFeature->getKind());
913 bool SketchPlugin_ArcTransversalPointValidator::isValid(
914 const AttributePtr& theAttribute,
915 const std::list<std::string>& /*theArguments*/,
916 Events_InfoMessage& theError) const
918 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
919 theError = "The attribute with the %1 type is not processed";
920 theError.arg(theAttribute->attributeType());
923 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
924 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
926 anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TRANSVERSAL_LINE())
927 return true; // not applicable for non-transversal arcs
929 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
930 AttributePtr anAttr = aRefAttr->attr();
932 theError = "The attribute %1 should be a point";
933 theError.arg(theAttribute->id());
937 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
938 const std::string& aFeatureType = anAttrFeature->getKind();
939 if (aFeatureType == SketchPlugin_Line::ID()) {
940 // selected point should be bound point of line
941 const std::string& aPntId = anAttr->id();
942 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
943 theError = "The attribute %1 is not supported";
944 theError.arg(aPntId);
949 theError = "Unable to build perpendicular arc on %1";
950 theError.arg(anAttrFeature->getKind());
957 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
958 const std::list<std::string>& theArguments,
959 Events_InfoMessage& theError) const
961 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
962 theError = "The attribute with the %1 type is not processed";
963 theError.arg(theAttribute->attributeType());
966 AttributeSelectionPtr anExternalAttr =
967 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
968 std::shared_ptr<GeomAPI_Edge> anEdge;
969 if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
970 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
971 } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
972 anExternalAttr->context()->shape()->isEdge()) {
973 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
977 theError = "The attribute %1 should be an edge";
978 theError.arg(theAttribute->id());
983 std::shared_ptr<SketchPlugin_Sketch> aSketch;
984 std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
985 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
986 for (; anIt != aRefs.end(); ++anIt) {
987 CompositeFeaturePtr aComp =
988 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
989 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
990 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
995 theError = "There is no sketch referring to the current feature";
999 // check the edge is intersected with sketch plane
1000 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1002 std::list<GeomPointPtr> anIntersectionsPoints;
1003 anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
1004 if (anIntersectionsPoints.empty()) {
1005 theError = "The edge is not intersected with sketch plane";
1011 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
1012 const std::list<std::string>& theArguments,
1013 Events_InfoMessage& theError) const
1015 bool aValid = false;
1017 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1018 theError = "The attribute with the %1 type is not processed";
1019 theError.arg(theAttribute->attributeType());
1022 AttributeReferencePtr aFeatureAttr =
1023 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1024 std::shared_ptr<SketchPlugin_Feature> aSplitFeature =
1025 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1027 ObjectPtr anAttrObject = aFeatureAttr->value();
1028 if (!anAttrObject) {
1029 AttributePtr aPreviewAttr = aSplitFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1030 aFeatureAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1031 anAttrObject = aFeatureAttr->value();
1034 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
1038 // B-splines are not supported by the Split yet
1039 if (anAttrFeature->getKind() == SketchPlugin_BSpline::ID() ||
1040 anAttrFeature->getKind() == SketchPlugin_BSplinePeriodic::ID()) {
1041 theError = "Not supported";
1045 std::set<ResultPtr> anEdgeShapes;
1046 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1047 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
1050 // coincidences to the feature
1051 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
1052 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
1053 SketchPlugin_ConstraintCoincidence::ID(),
1054 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1056 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
1057 std::shared_ptr<SketchPlugin_Feature> aSFeature =
1058 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
1059 if (!aSFeature || aSFeature->isCopy())
1061 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
1063 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
1064 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1065 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1066 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1067 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1068 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1069 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1070 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1072 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
1073 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1074 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
1075 PointToRefsMap aPointsInfo;
1077 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
1078 aX->dir(), aDirY, aPointsInfo);
1079 int aCoincidentToFeature = (int)aPointsInfo.size();
1080 if (anAttrFeature->getKind() == SketchPlugin_Circle::ID() ||
1081 anAttrFeature->getKind() == SketchPlugin_Ellipse::ID())
1082 aValid = aCoincidentToFeature >= 2;
1084 aValid = aCoincidentToFeature >= 1;
1089 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
1090 const std::list<std::string>& theArguments,
1091 Events_InfoMessage& theError) const
1093 bool aValid = false;
1095 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1096 theError = "The attribute with the %1 type is not processed";
1097 theError.arg(theAttribute->attributeType());
1100 AttributeReferencePtr aBaseObjectAttr =
1101 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1103 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1104 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1106 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1108 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1109 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1110 aBaseObject = aBaseObjectAttr->value();
1113 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1117 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1118 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1119 if (!aSketchFeature.get() || aSketchFeature->isCopy())
1122 // B-splines are not supported by the Trim yet
1123 if (aBaseFeature->getKind() == SketchPlugin_BSpline::ID() ||
1124 aBaseFeature->getKind() == SketchPlugin_BSplinePeriodic::ID()) {
1125 theError = "Not supported";
1130 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1131 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1133 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1135 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1136 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1137 anAttributePnt2d->y());
1139 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1140 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1141 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1142 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1143 SketchPlugin_SegmentationTools::fillObjectShapes(
1144 aTrimFeature.get(), aBaseObject, aCashedShapes, anObjectToPoints);
1145 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1147 return aShapes.size() > 1;
1150 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1151 const std::list<std::string>& theArguments,
1152 Events_InfoMessage& theError) const
1154 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1155 theError = "The attribute with the %1 type is not processed";
1156 theError.arg(theAttribute->attributeType());
1160 AttributeSelectionPtr aFeatureAttr =
1161 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1162 std::shared_ptr<GeomAPI_Vertex> aVertex;
1163 std::shared_ptr<GeomAPI_Edge> anEdge;
1164 std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1165 if (aFeatureAttr.get()) {
1166 GeomShapePtr aVal = aFeatureAttr->value();
1167 ResultPtr aRes = aFeatureAttr->context();
1168 if (aVal && aVal->isVertex())
1169 aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aVal));
1170 else if (aVal && aVal->isEdge()) {
1171 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aVal));
1172 } else if(aRes && aRes->shape()) {
1173 if (aRes->shape()->isVertex())
1174 aVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aRes->shape()));
1175 else if (aRes->shape()->isEdge())
1176 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aRes->shape()));
1179 // try to convert result to sketch feature
1182 std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1185 if (!aVertex && !anEdge) {
1186 theError = "The attribute %1 should be an edge or vertex";
1187 theError.arg(theAttribute->id());
1192 std::shared_ptr<SketchPlugin_Sketch> aSketch;
1193 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1194 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1195 for (; anIt != aRefs.end(); ++anIt) {
1196 CompositeFeaturePtr aComp =
1197 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1198 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1199 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1204 theError = "There is no sketch referring to the current feature";
1207 if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1208 theError = "Unable to project feature from the same sketch";
1212 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1213 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1214 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1218 aValid = true; // vertex is always could be projected
1219 else if (anEdge->isLine()) {
1220 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1221 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1222 double aDot = fabs(aNormal->dot(aLineDir));
1223 aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1225 theError = "Error: Line is orthogonal to the sketch plane.";
1227 else if (anEdge->isCircle() || anEdge->isArc()) {
1228 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1229 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1230 double aDot = fabs(aNormal->dot(aCircNormal));
1231 aValid = aDot >= tolerance * tolerance;
1233 theError.arg(anEdge->isCircle() ? "Error: Circle is orthogonal to the sketch plane."
1234 : "Error: Arc is orthogonal to the sketch plane.");
1236 else if (anEdge->isEllipse()) {
1237 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
1238 std::shared_ptr<GeomAPI_Dir> anEllipseNormal = anEllipse->normal();
1239 double aDot = fabs(aNormal->dot(anEllipseNormal));
1240 aValid = fabs(aDot - 1.0) <= tolerance * tolerance;
1242 theError.arg(anEdge->isClosed() ? "Error: Ellipse is orthogonal to the sketch plane."
1243 : "Error: Elliptic Arc is orthogonal to the sketch plane.");
1250 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1251 const std::set<FeaturePtr>& theSet2)
1253 std::set<FeaturePtr> aCommon;
1254 if (theSet1.empty() || theSet2.empty())
1257 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1258 for (; anIt2 != theSet2.end(); ++anIt2)
1259 if (theSet1.find(*anIt2) != theSet1.end())
1260 aCommon.insert(*anIt2);
1264 bool SketchPlugin_DifferentReferenceValidator::isValid(
1265 const AttributePtr& theAttribute,
1266 const std::list<std::string>& theArguments,
1267 Events_InfoMessage& theError) const
1269 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1271 int aNbFeaturesReferred = 0;
1272 int aNbAttributesReferred = 0;
1273 std::set<FeaturePtr> aCommonReferredFeatures;
1275 // find all features referred by attributes listed in theArguments
1276 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1277 for (; anArgIt != theArguments.end(); ++anArgIt) {
1278 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1282 std::set<FeaturePtr> aCoincidentFeatures;
1283 if (aRefAttr->isObject()) {
1284 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1286 aCoincidentFeatures.insert(aFeature);
1287 aNbFeaturesReferred += 1;
1290 AttributePoint2DPtr aPoint =
1291 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1293 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1294 aNbAttributesReferred += 1;
1298 if (aCommonReferredFeatures.empty())
1299 aCommonReferredFeatures = aCoincidentFeatures;
1301 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1303 if (aCommonReferredFeatures.empty())
1307 bool isOk = aNbFeaturesReferred < 1;
1308 if (aNbFeaturesReferred == 1) {
1309 if (aCommonReferredFeatures.size() == 1) {
1310 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1311 isOk = aNbAttributesReferred <= 1 ||
1312 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1313 aFeature->getKind() == SketchPlugin_Arc::ID();
1319 theError = "Attributes are referred to the same feature";
1323 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1324 const AttributePtr& theAttribute,
1325 const std::list<std::string>& theArguments,
1326 Events_InfoMessage& theError) const
1328 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1329 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1331 // find all points referred by attributes listed in theArguments
1332 bool hasRefsToPoints = false;
1333 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1334 for (; anArgIt != theArguments.end(); ++anArgIt) {
1335 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1339 if (!aRefAttr->isObject()) {
1340 AttributePoint2DPtr aPoint =
1341 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1342 if (aReferredCoincidentPoints.empty())
1343 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1344 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1345 return true; // non-coincident point has been found
1347 hasRefsToPoints = true;
1351 if (hasRefsToPoints)
1352 theError = "Attributes are referred to the same point";
1353 return !hasRefsToPoints;
1356 bool SketchPlugin_CirclePassedPointValidator::isValid(
1357 const AttributePtr& theAttribute,
1358 const std::list<std::string>&,
1359 Events_InfoMessage& theError) const
1361 static const std::string aErrorMessage(
1362 "Passed point refers to the same feature as a center point");
1364 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1366 AttributeRefAttrPtr aCenterRef =
1367 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1368 AttributeRefAttrPtr aPassedRef =
1369 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1371 if (!aPassedRef->isObject())
1374 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1375 if (!aPassedFeature)
1378 if (aCenterRef->isObject()) {
1379 if (aCenterRef->object() == aPassedRef->object()) {
1380 theError = aErrorMessage;
1384 AttributePoint2DPtr aCenterPoint =
1385 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1387 std::set<FeaturePtr> aCoincidentFeatures =
1388 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1389 // check one of coincident features is a feature referred by passed point
1390 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1391 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1392 if (*anIt == aPassedFeature) {
1393 theError = aErrorMessage;
1401 bool SketchPlugin_ThirdPointValidator::isValid(
1402 const AttributePtr& theAttribute,
1403 const std::list<std::string>& theArguments,
1404 Events_InfoMessage& theError) const
1406 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1407 return arePointsNotOnLine(anOwner, theError) &&
1408 arePointsNotSeparated(anOwner, theArguments, theError);
1411 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1412 const std::string& thePointAttrName,
1413 const std::string& theRefPointAttrName)
1415 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1416 theMacroCircle->attribute(thePointAttrName));
1417 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1419 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1421 if (aRefAttr->isObject()) {
1422 // project a point onto selected feature
1423 std::shared_ptr<SketchPlugin_Feature> aFeature =
1424 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1425 ModelAPI_Feature::feature(aRefAttr->object()));
1427 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1428 std::shared_ptr<GeomAPI_Edge> anEdge =
1429 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1431 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1432 if (anEdge->isLine())
1433 aPoint3D = anEdge->line()->project(aPoint3D);
1434 else if (anEdge->isCircle())
1435 aPoint3D = anEdge->circle()->project(aPoint3D);
1437 aPoint = aSketch->to2D(aPoint3D);
1441 AttributePoint2DPtr anOtherPoint =
1442 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1444 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1451 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1452 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1454 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1455 thePoints[0] = toPoint(theMacroFeature,
1456 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1457 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1458 thePoints[1] = toPoint(theMacroFeature,
1459 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1460 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1461 thePoints[2] = toPoint(theMacroFeature,
1462 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1463 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1464 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1465 thePoints[0] = toPoint(theMacroFeature,
1466 SketchPlugin_MacroArc::START_POINT_2_ID(),
1467 SketchPlugin_MacroArc::START_POINT_REF_ID());
1468 thePoints[1] = toPoint(theMacroFeature,
1469 SketchPlugin_MacroArc::END_POINT_2_ID(),
1470 SketchPlugin_MacroArc::END_POINT_REF_ID());
1471 thePoints[2] = toPoint(theMacroFeature,
1472 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1473 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1477 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1478 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1479 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1481 static const double aTolerance = 1.e-7;
1482 if (thePoint1->distance(thePoint2) < aTolerance ||
1483 thePoint1->distance(thePoint3) < aTolerance)
1486 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1487 thePoint2->y() - thePoint1->y()));
1488 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1489 thePoint3->y() - thePoint1->y()));
1490 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1493 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1494 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1495 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1497 static const double aTolerance = 1.e-7;
1498 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1499 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1501 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1502 // the first point is on the line
1503 if (aVec1->squareModulus() < aTolerance * aTolerance)
1505 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1506 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1507 // the second point is on the line
1508 if (aVec2->squareModulus() < aTolerance * aTolerance)
1510 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1512 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1515 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1516 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1517 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1519 static const double aTolerance = 1.e-7;
1520 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1521 double aDistP1C = thePoint1->distance(aCenter);
1522 double aDistP2C = thePoint2->distance(aCenter);
1523 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1526 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1527 const FeaturePtr& theMacroFeature,
1528 Events_InfoMessage& theError) const
1530 static const std::string aErrorPointsOnLine(
1531 "Selected points are on the same line");
1533 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1534 threePointsOfFeature(theMacroFeature, aPoints);
1536 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1537 theError = aErrorPointsOnLine;
1543 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1544 const FeaturePtr& theMacroFeature,
1545 const std::list<std::string>& theArguments,
1546 Events_InfoMessage& theError) const
1548 static const std::string aErrorPointsApart(
1549 "Selected entity is lying between first two points");
1551 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1552 FeaturePtr aRefByThird;
1553 if (aThirdPointRef->isObject())
1554 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1558 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1559 threePointsOfFeature(theMacroFeature, aPoints);
1561 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1562 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1566 SketchPlugin_Sketch* aSketch =
1567 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1568 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1569 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1572 if (aThirdShape->isLine())
1573 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1574 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1575 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1578 theError = aErrorPointsApart;
1582 bool SketchPlugin_ArcEndPointValidator::isValid(
1583 const AttributePtr& theAttribute,
1584 const std::list<std::string>& theArguments,
1585 Events_InfoMessage& theError) const
1587 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1588 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1590 if(!anEndPointRef.get()) {
1594 ObjectPtr anObject = anEndPointRef->object();
1595 AttributePtr anAttr = anEndPointRef->attr();
1596 if(!anObject.get() && !anAttr.get()) {
1600 if(anEndPointRef->attr().get()) {
1604 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1606 GeomShapePtr aShape = aResult->shape();
1607 if(aShape.get() && aShape->isVertex()) {
1612 aFeature = ModelAPI_Feature::feature(anObject);
1613 if(aFeature.get()) {
1614 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1622 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1624 if(!theShape.get()) {
1628 if(!theShape->isEdge()) {
1632 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1638 if(anEdge->isLine()) {
1639 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1640 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1644 if(anEdge->isCircle() || anEdge->isArc()) {
1645 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1646 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1653 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1654 const AttributePtr& theAttribute,
1655 const std::list<std::string>& theArguments,
1656 Events_InfoMessage& theError) const
1658 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1659 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1660 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1662 if(!anEndPointRef.get()) {
1666 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1668 if(!anArcShape.get() || anArcShape->isNull()) {
1672 ObjectPtr anObject = anEndPointRef->object();
1673 AttributePtr anAttr = anEndPointRef->attr();
1674 if(!anObject.get() && !anAttr.get()) {
1678 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1680 GeomShapePtr aShape = aResult->shape();
1681 if (!aShape->isEdge())
1683 aShape = toInfiniteEdge(aShape);
1684 if(aShape.get() && !aShape->isNull()) {
1685 if(anArcShape->isIntersect(aShape)) {
1691 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1692 if(aSelectedFeature.get()) {
1693 std::list<ResultPtr> aResults = aSelectedFeature->results();
1694 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1695 anIt != aResults.cend();
1698 GeomShapePtr aShape = (*anIt)->shape();
1699 if (!aShape->isEdge())
1701 aShape = toInfiniteEdge(aShape);
1702 if(aShape.get() && !aShape->isNull()) {
1703 if(anArcShape->isIntersect(aShape)) {
1713 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1714 const std::list<std::string>& theArguments,
1715 Events_InfoMessage& theError) const
1717 std::set<std::string> aFeatureKinds;
1718 for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1719 anArgIt != theArguments.end(); anArgIt++) {
1720 aFeatureKinds.insert(*anArgIt);
1723 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1724 theError = "The attribute with the %1 type is not processed";
1725 theError.arg(theAttribute->attributeType());
1729 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1731 bool isObject = aRefAttr->isObject();
1733 theError = "It uses an empty object";
1736 ObjectPtr anObject = aRefAttr->object();
1737 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1738 if (!aFeature.get()) {
1739 theError = "The feature of the checked attribute is empty";
1743 FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1745 std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1746 std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1747 for (; anIt != aRefsList.end(); anIt++) {
1748 FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1749 if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1750 aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1751 return false; // constraint is found, that means that the check is not valid
1756 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1757 const AttributePtr& theAttribute,
1758 const std::list<std::string>& theArguments,
1759 Events_InfoMessage& theError) const
1761 AttributeRefAttrPtr aRefAttr =
1762 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1765 theError = "Incorrect attribute";
1770 if (aRefAttr->isObject())
1771 anOwner = aRefAttr->object();
1774 AttributePtr anAttr = aRefAttr->attr();
1775 anOwner = anAttr->owner();
1777 FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1778 if (!anAttrOwnerFeature)
1780 AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1781 if (!aCopyAttr || !aCopyAttr->value())
1782 return true; // feature is not a copy, thus valid
1784 // check the copy feature is already referred by the "Multi" feature
1785 FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1786 AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1787 for (int i = 0; i < aRefList->size(); ++i)
1789 FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1790 if (aRefOwner == anAttrOwnerFeature)
1792 theError = "Attribute refers to the object generated by this feature";
1800 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1801 const std::list<std::string>& theArguments,
1802 Events_InfoMessage& theError) const
1804 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId() &&
1805 theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1806 theError = "The attribute with the %1 type is not processed";
1807 theError.arg(theAttribute->attributeType());
1811 // check the attribute refers to a sketch feature
1812 bool isSketchFeature = false;
1813 AttributeRefAttrPtr aRefAttr =
1814 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1816 isSketchFeature = aRefAttr->isObject();
1817 if (isSketchFeature) {
1818 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1819 isSketchFeature = aFeature.get() != NULL;
1820 if (isSketchFeature) {
1821 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1822 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1823 isSketchFeature = aSketchFeature.get() != NULL;
1828 AttributeReferencePtr aReference =
1829 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1831 FeaturePtr aFeature = ModelAPI_Feature::feature(aReference->value());
1832 isSketchFeature = aFeature.get() && aFeature->getKind() == SketchPlugin_Sketch::ID();
1836 if (!isSketchFeature)
1837 theError = "The object selected is not a sketch feature";
1838 return isSketchFeature;
1841 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1842 const std::list<std::string>& theArguments,
1843 Events_InfoMessage& theError) const
1845 if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1846 theError = "The attribute with the %1 type is not processed";
1847 theError.arg(theAttribute->attributeType());
1851 AttributeDoublePtr anAngleAttr =
1852 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1854 FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1855 AttributeStringPtr anAngleType =
1856 aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1857 AttributeIntegerPtr aNbCopies =
1858 aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1860 if (anAngleType->value() != "FullAngle")
1862 double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1863 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1865 theError = "Rotation single angle should produce full angle less than 360 degree";
1871 double aFullAngleValue = anAngleAttr->value();
1872 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1874 theError = "Rotation full angle should be in range [0, 360]";
1882 bool SketchPlugin_BSplineValidator::isValid(const AttributePtr& theAttribute,
1883 const std::list<std::string>& theArguments,
1884 Events_InfoMessage& theError) const
1886 AttributePoint2DArrayPtr aPolesArray =
1887 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
1891 if (aPolesArray->size() < 2) {
1892 theError = "Number of B-spline poles should be 2 or more";
1899 bool SketchPlugin_CurveFittingValidator::isValid(const FeaturePtr& theFeature,
1900 const std::list<std::string>& theArguments,
1901 Events_InfoMessage& theError) const
1903 AttributeRefAttrListPtr aRefAttrList =
1904 theFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
1905 AttributeBooleanPtr aPeriodicAttr =
1906 theFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
1908 // check number of selected entities
1909 int aMinNbPoints = aPeriodicAttr->value() ? 3 : 2;
1910 if (aRefAttrList->size() < aMinNbPoints) {
1911 theError = "Not enough points selected. Need at least %1 points.";
1912 theError.arg(aMinNbPoints);