1 // Copyright (C) 2014-2019 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_Circle.h"
24 #include "SketchPlugin_ConstraintCoincidence.h"
25 #include "SketchPlugin_ConstraintDistance.h"
26 #include "SketchPlugin_ConstraintRigid.h"
27 #include "SketchPlugin_ConstraintTangent.h"
28 #include "SketchPlugin_Fillet.h"
29 #include "SketchPlugin_Line.h"
30 #include "SketchPlugin_MacroArc.h"
31 #include "SketchPlugin_MacroCircle.h"
32 #include "SketchPlugin_MultiRotation.h"
33 #include "SketchPlugin_Point.h"
34 #include "SketchPlugin_Sketch.h"
35 #include "SketchPlugin_Trim.h"
36 #include "SketchPlugin_Tools.h"
38 #include "SketcherPrs_Tools.h"
40 #include <Events_InfoMessage.h>
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Validator.h>
44 #include <ModelAPI_AttributeDouble.h>
45 #include <ModelAPI_AttributeInteger.h>
46 #include <ModelAPI_AttributeRefAttr.h>
47 #include <ModelAPI_AttributeRefList.h>
48 #include <ModelAPI_AttributeSelectionList.h>
49 #include <ModelAPI_AttributeString.h>
50 #include <ModelAPI_Session.h>
51 #include <ModelAPI_Tools.h>
52 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelGeomAlgo_Point2D.h>
55 #include <ModelGeomAlgo_Shape.h>
57 #include <GeomAlgoAPI_EdgeBuilder.h>
58 #include <GeomAlgoAPI_ShapeTools.h>
60 #include <GeomAPI_Circ.h>
61 #include <GeomAPI_Dir2d.h>
62 #include <GeomAPI_Lin.h>
63 #include <GeomAPI_Edge.h>
64 #include <GeomAPI_Vertex.h>
65 #include <GeomDataAPI_Point2D.h>
70 const double tolerance = 1.e-7;
72 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
73 const std::list<std::string>& theArguments,
74 Events_InfoMessage& theError) const
76 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
77 theError = "The attribute with the %1 type is not processed";
78 theError.arg(theAttribute->attributeType());
82 // there is a check whether the feature contains a point and a linear edge or two point values
83 std::string aParamA = theArguments.front();
84 SessionPtr aMgr = ModelAPI_Session::get();
85 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
89 bool isObject = aRefAttr->isObject();
91 // an attribute is a point. A point value is valid always for the distance
94 // 1. check whether the references object is a linear
95 ObjectPtr anObject = aRefAttr->object();
97 const ModelAPI_AttributeValidator* aShapeValidator =
98 dynamic_cast<const ModelAPI_AttributeValidator*>(
99 aFactory->validator("GeomValidators_ShapeType"));
100 std::list<std::string> anArguments;
101 anArguments.push_back("circle");
102 Events_InfoMessage aCircleError;
103 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
104 // the circle line is not a valid case
106 theError = "Circle can not be used in distance constraint";
111 anArguments.push_back("line");
112 Events_InfoMessage aLineError;
113 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
114 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
116 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
117 // If it is a line then we have to check that first attribute id not a line
118 std::shared_ptr<SketchPlugin_Feature> aSFeature =
119 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
120 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
121 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
122 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
123 aFeature->data(), aParamA, aPlane);
125 theError = "One of parameters of distance constraint should be a point";
133 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
134 const std::list<std::string>& theArguments,
135 Events_InfoMessage& theError) const
137 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
138 theError = "The attribute with the %1 type is not processed";
139 theError.arg(theAttribute->attributeType());
143 // there is a check whether the feature contains a point and a linear edge or two point values
144 std::string aParamA = theArguments.front();
145 SessionPtr aMgr = ModelAPI_Session::get();
146 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
148 FeaturePtr anAttributeFeature =
149 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
150 AttributeRefAttrPtr aRefAttr =
151 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
153 bool isObject = aRefAttr->isObject();
154 ObjectPtr anObject = aRefAttr->object();
155 if (isObject && anObject.get()) {
156 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
158 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
159 ObjectPtr aOtherObject = aOtherAttr->object();
160 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
164 if (aRefFea->getKind() == SketchPlugin_Line::ID()) {
165 if (aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
166 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
167 theError = "It refers to a %1, but %2 is neither an %3 nor %4";
168 theError.arg(SketchPlugin_Line::ID()).arg(aParamA)
169 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
173 else if (aRefFea->getKind() == SketchPlugin_Arc::ID() ||
174 aRefFea->getKind() == SketchPlugin_Circle::ID()) {
175 if (aOtherFea->getKind() != SketchPlugin_Line::ID() &&
176 aOtherFea->getKind() != SketchPlugin_Arc::ID() &&
177 aOtherFea->getKind() != SketchPlugin_Circle::ID()) {
178 theError = "It refers to an %1, but %2 is not a %3 or an %4 or a %5";
179 theError.arg(SketchPlugin_Arc::ID()).arg(aParamA)
180 .arg(SketchPlugin_Line::ID()).arg(SketchPlugin_Arc::ID())
181 .arg(SketchPlugin_Circle::ID());
186 theError = "It refers to %1, but should refer to %2 or %3 or %4";
187 theError.arg(aRefFea->getKind()).arg(SketchPlugin_Line::ID())
188 .arg(SketchPlugin_Arc::ID()).arg(SketchPlugin_Circle::ID());
194 theError = "It uses an empty object";
201 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
202 const std::list<std::string>& theArguments,
203 Events_InfoMessage& theError) const
205 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
206 theError = "The attribute with the %1 type is not processed";
207 theError.arg(theAttribute->attributeType());
211 std::string aParamA = theArguments.front();
212 SessionPtr aMgr = ModelAPI_Session::get();
213 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
215 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
216 AttributeRefAttrPtr aRefAttr =
217 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
219 bool isObject = aRefAttr->isObject();
220 ObjectPtr anObject = aRefAttr->object();
221 if (isObject && anObject.get()) {
222 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
224 AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
225 ObjectPtr aOtherObject = aOtherAttr->object();
226 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
230 // at least one feature should be a line
231 if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
232 aOtherFea->getKind() != SketchPlugin_Line::ID()) {
233 theError = "At least one feature should be a line";
238 theError = "It uses an empty object";
245 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
246 const std::list<std::string>& theArguments,
247 Events_InfoMessage& theError) const
249 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
250 theError = "The attribute with the %1 type is not processed";
251 theError.arg(theAttribute->attributeType());
255 std::shared_ptr<SketchPlugin_Feature> aFeature =
256 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
260 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
262 SketchPlugin_Sketch* aSketch = aFeature->sketch();
263 int aNbFeatures = aSketch->numberOfSubs();
264 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
265 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
266 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
268 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
269 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
270 if (aRefAttr->isObject()) {
271 if (aRefAttr->object() == aRAttr->object()) {
272 ObjectPtr anObject = aRefAttr->object();
273 std::string aName = anObject.get() ? anObject->data()->name() : "";
274 theError = "The object %1 has been already fixed.";
279 else if (aRefAttr->attr() == aRAttr->attr()) {
280 AttributePtr anAttribute = aRefAttr->attr();
281 std::string aName = anAttribute.get() ? anAttribute->id() : "";
282 theError = "The attribute %1 has been already fixed.";
290 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
291 const std::list<std::string>& theArguments,
292 Events_InfoMessage& theError) const
294 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
295 theError = "The attribute with the %1 type is not processed";
296 theError.arg(theAttribute->attributeType());
300 std::string aParamA = theArguments.front();
301 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
302 AttributeRefAttrPtr aRefAttr[2];
303 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
304 aRefAttr[1] = aFeature->data()->refattr(aParamA);
306 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
307 theError = "Attributes can not be used in equal constraint";
311 std::string aType[2];
312 std::list<std::string> anArguments;
313 for (int i = 0; i < 2; i++) {
314 ObjectPtr anObject = aRefAttr[i]->object();
315 if (!anObject.get()) {
316 theError = "An empty object is used.";
320 aFeature = ModelAPI_Feature::feature(anObject);
321 if (!aFeature.get()) {
322 theError = "An empty feature is used.";
326 aType[i] = aFeature->getKind();
327 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
328 aFeature->getKind() != SketchPlugin_Circle::ID() &&
329 aFeature->getKind() != SketchPlugin_Arc::ID()) {
330 theError = "The %1 feature kind of attribute is wrong. It should be %2 or %3 or %4";
331 theError.arg(aFeature->getKind()).arg(SketchPlugin_Line::ID())
332 .arg(SketchPlugin_Circle::ID()).arg(SketchPlugin_Arc::ID());
333 // wrong type of attribute
338 if ((aType[0] == SketchPlugin_Line::ID() || aType[1] == SketchPlugin_Line::ID()) &&
339 aType[0] != aType[1]) {
340 theError = "Feature with kinds %1 and %2 can not be equal.";
341 theError.arg(aType[0]).arg(aType[1]);
347 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
348 const std::list<std::string>& theArguments,
349 Events_InfoMessage& theError) const
351 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
352 theError = "The attribute with the %1 type is not processed";
353 theError.arg(theAttribute->attributeType());
357 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
358 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
360 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
361 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
362 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
364 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
365 ObjectPtr aSelObject = aSelAttr->object(anInd);
366 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
367 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
368 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
369 if (aSelObject == *aMirIter) {
370 theError = "The object %1 is a result of mirror";
378 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
379 const std::list<std::string>& theArguments,
380 Events_InfoMessage& theError) const
382 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
383 theError = "The attribute with the %1 type is not processed";
384 theError.arg(theAttribute->attributeType());
388 // there is a check whether the feature contains a point and a linear edge or two point values
389 std::string aParamA = theArguments.front();
390 SessionPtr aMgr = ModelAPI_Session::get();
391 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
393 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
394 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
396 theError = "The %1 attribute should be %2";
397 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
401 AttributeRefAttrPtr aRefAttrB =
402 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
404 // first attribute is a point, it may coincide with any object
405 if (!aRefAttrA->isObject())
408 ObjectPtr anObject = aRefAttrA->object();
409 if (!anObject.get()) {
410 theError = "%1 attribute has an empty object";
411 theError.arg(aParamA);
414 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
415 if (!aFeature.get()) {
416 theError = "%1 attribute has an empty feature";
417 theError.arg(aParamA);
421 if (aFeature->getKind() == SketchPlugin_Point::ID())
425 // second attribute is a point, it may coincide with any object
426 if (!aRefAttrB->isObject())
429 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
431 theError = "%1 attribute has an empty object";
432 theError.arg(theAttribute->id());
435 if (aFeature->getKind() == SketchPlugin_Point::ID())
438 theError = "There is no an attribute filled by a point";
443 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
444 const std::list<std::string>& theArguments,
445 Events_InfoMessage& theError) const
447 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
448 theError = "The attribute with the %1 type is not processed";
449 theError.arg(theAttribute->attributeType());
453 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
454 AttributeRefListPtr aSelAttr =
455 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
457 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
458 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
459 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
460 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
461 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
462 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
464 std::list<ObjectPtr>::iterator anObjIter;
465 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
466 ObjectPtr aSelObject = aSelAttr->object(anInd);
467 anObjIter = anInitialObjects.begin();
468 for (; anObjIter != anInitialObjects.end(); anObjIter++)
469 if (aSelObject == *anObjIter)
471 if (anObjIter != anInitialObjects.end())
473 anObjIter = aCopiedObjects.begin();
474 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
475 if (aSelObject == *anObjIter) {
476 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
477 theError = "The object %1 is a result of copy";
485 bool SketchPlugin_SolverErrorValidator::isValid(
486 const std::shared_ptr<ModelAPI_Feature>& theFeature,
487 const std::list<std::string>& theArguments,
488 Events_InfoMessage& theError) const
490 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
492 if (!aAttributeString->value().empty()) {
493 theError = aAttributeString->value();
500 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
501 std::string theAttribute)
506 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
507 const FeaturePtr theFeature)
509 for(std::set<AttributePtr>::const_iterator
510 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
511 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
512 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
515 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
516 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
517 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
518 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
519 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
520 if(anAttrRefA.get()) {
521 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
523 DocumentPtr aDoc = aResA->document();
525 FeaturePtr aFeatureA = aDoc->feature(aResA);
526 if(aFeatureA.get() && aFeatureA == theFeature) {
532 if(anAttrRefB.get()) {
533 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
535 DocumentPtr aDoc = aResB->document();
537 FeaturePtr aFeatureB = aDoc->feature(aResB);
538 if(aFeatureB.get() && aFeatureB == theFeature) {
549 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
551 AttributeRefAttrPtr aRefAttr[2] = {
552 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
553 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
556 bool arePoints = true;
557 for (int i = 0; i < 2 && arePoints; ++i) {
558 if (aRefAttr[i]->isObject()) {
559 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
560 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
562 arePoints = aRefAttr[i]->attr().get() != NULL;
567 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
568 const std::list<std::string>& theArguments,
569 Events_InfoMessage& theError) const
571 AttributeRefAttrPtr aPointRefAttr =
572 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
573 if(!aPointRefAttr.get()) {
574 theError = "Error: Point not selected.";
578 AttributePtr aPointAttribute = aPointRefAttr->attr();
579 if (!aPointAttribute.get()) {
580 theError = "Error: Bad point selected.";
583 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
584 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
586 // Obtain constraint coincidence for the fillet point.
587 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
588 FeaturePtr aConstraintCoincidence;
589 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
590 anIt != aRefsList.cend(); ++anIt) {
591 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
592 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
593 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
594 if (!isPointPointCoincidence(aConstrFeature))
597 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
598 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
599 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
600 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
602 AttributePtr anAttrA = anAttrRefA->attr();
603 if(aPointAttribute == anAttrA) {
604 aConstraintCoincidence = aConstrFeature;
608 AttributePtr anAttrB = anAttrRefB->attr();
609 if(aPointAttribute == anAttrB) {
610 aConstraintCoincidence = aConstrFeature;
616 if(!aConstraintCoincidence.get()) {
617 theError = "Error: one of the selected point does not have coicidence.";
621 // Get coincides from constraint.
622 std::set<FeaturePtr> aCoinsides;
623 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
624 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
627 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
628 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
632 // Remove points and external lines from set of coincides.
633 std::set<FeaturePtr> aNewSetOfCoincides;
634 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
635 anIt != aCoinsides.end(); ++anIt) {
636 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
637 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
638 if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
641 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
642 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
645 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
646 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
647 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
648 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
649 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
650 if(aDistSelectedArcCenter < tolerance) {
654 aNewSetOfCoincides.insert(*anIt);
656 aCoinsides = aNewSetOfCoincides;
658 // If we still have more than two coincides remove auxilary entities from set of coincides.
659 if(aCoinsides.size() > 2) {
660 aNewSetOfCoincides.clear();
661 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
662 anIt != aCoinsides.end(); ++anIt) {
663 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
664 aNewSetOfCoincides.insert(*anIt);
667 aCoinsides = aNewSetOfCoincides;
670 if(aCoinsides.size() != 2) {
671 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
675 // Check that selected edges don't have tangent constraint.
676 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
677 FeaturePtr aFirstFeature = *anIt++;
678 FeaturePtr aSecondFeature = *anIt;
679 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
680 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
681 theError = "Error: Edges in selected point has tangent constraint.";
685 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
686 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
687 aResIt != aFirstResults.end(); ++aResIt) {
688 ResultPtr aRes = *aResIt;
689 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
690 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
691 theError = "Error: Edges in selected point has tangent constraint.";
696 // Check the features are not tangent
697 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
698 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
699 if (!aFirstShape || !aFirstShape->isEdge() ||
700 !aSecondShape || !aSecondShape->isEdge()) {
701 theError = "Error: At least on of the features is not an edge";
705 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
706 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
708 static const double TOL = 1.e-7;
709 if (anEdge1->isLine() && anEdge2->isLine()) {
710 // Check that lines not collinear
711 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
712 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
713 double aCross = aDir1->cross(aDir2)->squareModulus();
714 if (aCross < TOL * TOL)
716 } else if (anEdge1->isArc() && anEdge2->isArc()) {
717 // check the circles are not tangent
718 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
719 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
720 double aDistCC = aCirc1->center()->distance(aCirc2->center());
721 double aRadSum = aCirc1->radius() + aCirc2->radius();
722 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
723 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
726 // check whether line and arc are tangent
727 std::shared_ptr<GeomAPI_Circ> aCirc;
728 std::shared_ptr<GeomAPI_Lin> aLine;
729 if (anEdge1->isLine()) {
730 aLine = anEdge1->line();
731 aCirc = anEdge2->circle();
733 aCirc = anEdge1->circle();
734 aLine = anEdge2->line();
737 double aDistCL = aLine->distance(aCirc->center());
738 if (fabs(aDistCL - aCirc->radius()) < TOL)
745 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
746 const std::list<std::string>& theArguments,
747 Events_InfoMessage& theError) const
749 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
750 theError = "The attribute with the %1 type is not processed";
751 theError.arg(theAttribute->attributeType());
755 // there is a check whether the feature contains a point and a linear edge or two point values
756 std::string aParamA = theArguments.front();
757 SessionPtr aMgr = ModelAPI_Session::get();
758 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
760 FeaturePtr anAttributeFeature =
761 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
762 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
763 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
765 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
768 for (int i = 0; i < 2; ++i) {
769 if (!aRefAttrs[i]->isObject())
772 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
774 if (aNbPoints + aNbLines != 0)
779 if (aFeature->getKind() == SketchPlugin_Point::ID())
781 else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
782 aFeature->getKind() == SketchPlugin_Arc::ID())
787 if (aNbPoints != 1 || aNbLines != 1) {
788 theError = "Middle point constraint allows points and lines only";
794 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
795 const std::list<std::string>& /*theArguments*/,
796 Events_InfoMessage& theError) const
798 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
799 theError = "The attribute with the %1 type is not processed";
800 theError.arg(theAttribute->attributeType());
803 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
804 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
805 if (anArcTypeAttr && anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE())
806 return true; // not applicable for non-tangent arcs
808 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
809 AttributePtr anAttr = aRefAttr->attr();
811 theError = "The attribute %1 should be a point";
812 theError.arg(theAttribute->id());
816 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
817 const std::string& aFeatureType = anAttrFeature->getKind();
818 if (aFeatureType == SketchPlugin_Arc::ID()) {
819 // selected point should not be a center of arc
820 const std::string& aPntId = anAttr->id();
821 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
822 theError = "The attribute %1 is not supported";
823 theError.arg(aPntId);
827 else if (aFeatureType == SketchPlugin_Line::ID()) {
828 // selected point should be bound point of line
829 const std::string& aPntId = anAttr->id();
830 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
831 theError = "The attribute %1 is not supported";
832 theError.arg(aPntId);
837 theError = "Unable to build tangent arc on %1";
838 theError.arg(anAttrFeature->getKind());
845 bool SketchPlugin_ArcTransversalPointValidator::isValid(
846 const AttributePtr& theAttribute,
847 const std::list<std::string>& /*theArguments*/,
848 Events_InfoMessage& theError) const
850 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
851 theError = "The attribute with the %1 type is not processed";
852 theError.arg(theAttribute->attributeType());
855 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
856 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
858 anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TRANSVERSAL_LINE())
859 return true; // not applicable for non-transversal arcs
861 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
862 AttributePtr anAttr = aRefAttr->attr();
864 theError = "The attribute %1 should be a point";
865 theError.arg(theAttribute->id());
869 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
870 const std::string& aFeatureType = anAttrFeature->getKind();
871 if (aFeatureType == SketchPlugin_Line::ID()) {
872 // selected point should be bound point of line
873 const std::string& aPntId = anAttr->id();
874 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
875 theError = "The attribute %1 is not supported";
876 theError.arg(aPntId);
881 theError = "Unable to build transversal arc on %1";
882 theError.arg(anAttrFeature->getKind());
889 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
890 const std::list<std::string>& theArguments,
891 Events_InfoMessage& theError) const
893 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
894 theError = "The attribute with the %1 type is not processed";
895 theError.arg(theAttribute->attributeType());
898 AttributeSelectionPtr anExternalAttr =
899 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
900 std::shared_ptr<GeomAPI_Edge> anEdge;
901 if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
902 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
903 } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
904 anExternalAttr->context()->shape()->isEdge()) {
905 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
909 theError = "The attribute %1 should be an edge";
910 theError.arg(theAttribute->id());
915 std::shared_ptr<SketchPlugin_Sketch> aSketch;
916 std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
917 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
918 for (; anIt != aRefs.end(); ++anIt) {
919 CompositeFeaturePtr aComp =
920 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
921 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
922 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
927 theError = "There is no sketch referring to the current feature";
931 // check the edge is intersected with sketch plane
932 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
934 std::list<GeomPointPtr> anIntersectionsPoints;
935 anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
936 if (anIntersectionsPoints.empty()) {
937 theError = "The edge is not intersected with sketch plane";
943 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
944 const std::list<std::string>& theArguments,
945 Events_InfoMessage& theError) const
949 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
950 theError = "The attribute with the %1 type is not processed";
951 theError.arg(theAttribute->attributeType());
954 AttributeReferencePtr aFeatureAttr =
955 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
957 ObjectPtr anAttrObject = aFeatureAttr->value();
958 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
962 std::string aKind = anAttrFeature->getKind();
963 if (aKind == SketchPlugin_Line::ID() ||
964 aKind == SketchPlugin_Arc::ID() ||
965 aKind == SketchPlugin_Circle::ID()) {
967 std::set<ResultPtr> anEdgeShapes;
968 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
969 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
972 // coincidences to the feature
973 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
974 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
975 SketchPlugin_ConstraintCoincidence::ID(),
976 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
978 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
979 std::shared_ptr<SketchPlugin_Feature> aSFeature =
980 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
981 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
983 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
984 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
985 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
986 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
987 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
988 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
989 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
990 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
992 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
993 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
994 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
995 PointToRefsMap aPointsInfo;
997 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
998 aX->dir(), aDirY, aPointsInfo);
999 int aCoincidentToFeature = (int)aPointsInfo.size();
1000 if (aKind == SketchPlugin_Circle::ID())
1001 aValid = aCoincidentToFeature >= 2;
1003 aValid = aCoincidentToFeature >= 1;
1009 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
1010 const std::list<std::string>& theArguments,
1011 Events_InfoMessage& theError) const
1013 bool aValid = false;
1015 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1016 theError = "The attribute with the %1 type is not processed";
1017 theError.arg(theAttribute->attributeType());
1020 AttributeReferencePtr aBaseObjectAttr =
1021 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1023 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1024 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1026 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1028 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1029 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1030 aBaseObject = aBaseObjectAttr->value();
1033 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1037 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1038 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1039 if (!aSketchFeature.get() || aSketchFeature->isCopy())
1042 std::string aKind = aBaseFeature->getKind();
1043 if (aKind != SketchPlugin_Line::ID() &&
1044 aKind != SketchPlugin_Arc::ID() &&
1045 aKind != SketchPlugin_Circle::ID())
1049 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1050 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1052 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1054 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1055 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1056 anAttributePnt2d->y());
1058 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1059 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1060 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1061 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1062 SketchPlugin_Trim::fillObjectShapes(aBaseObject, aSketch->data()->owner(),
1063 aCashedShapes, anObjectToPoints);
1064 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1066 return aShapes.size() > 1;
1069 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1070 const std::list<std::string>& theArguments,
1071 Events_InfoMessage& theError) const
1073 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1074 theError = "The attribute with the %1 type is not processed";
1075 theError.arg(theAttribute->attributeType());
1079 AttributeSelectionPtr aFeatureAttr =
1080 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1081 std::shared_ptr<GeomAPI_Edge> anEdge;
1082 std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1083 if (aFeatureAttr.get()) {
1084 GeomShapePtr aVal = aFeatureAttr->value();
1085 ResultPtr aRes = aFeatureAttr->context();
1086 if (aVal && aVal->isVertex())
1087 return true; // vertex is always could be projected
1088 if (aVal && aVal->isEdge()) {
1089 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
1090 } else if(aRes && aRes->shape()) {
1091 if (aRes->shape()->isVertex())
1092 return true; // vertex is always could be projected
1093 else if (aRes->shape()->isEdge())
1094 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1097 // try to convert result to sketch feature
1100 std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1104 theError = "The attribute %1 should be an edge or vertex";
1105 theError.arg(theAttribute->id());
1110 std::shared_ptr<SketchPlugin_Sketch> aSketch;
1111 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1112 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1113 for (; anIt != aRefs.end(); ++anIt) {
1114 CompositeFeaturePtr aComp =
1115 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1116 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1117 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1122 theError = "There is no sketch referring to the current feature";
1125 if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1126 theError = "Unable to project feature from the same sketch";
1130 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1131 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1132 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1134 if (anEdge->isLine()) {
1135 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1136 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1137 double aDot = fabs(aNormal->dot(aLineDir));
1138 bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1140 theError = "Error: Edge is already in the sketch plane.";
1143 else if (anEdge->isCircle() || anEdge->isArc()) {
1144 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1145 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1146 double aDot = fabs(aNormal->dot(aCircNormal));
1147 bool aValid = fabs(aDot - 1.0) < tolerance * tolerance;
1149 theError.arg(anEdge->isCircle() ? "Error: Cirlce is already in the sketch plane."
1150 : "Error: Arc is already in the sketch plane.");
1154 theError = "Error: Selected object is not line, circle or arc.";
1159 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1160 const std::set<FeaturePtr>& theSet2)
1162 std::set<FeaturePtr> aCommon;
1163 if (theSet1.empty() || theSet2.empty())
1166 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1167 for (; anIt2 != theSet2.end(); ++anIt2)
1168 if (theSet1.find(*anIt2) != theSet1.end())
1169 aCommon.insert(*anIt2);
1173 bool SketchPlugin_DifferentReferenceValidator::isValid(
1174 const AttributePtr& theAttribute,
1175 const std::list<std::string>& theArguments,
1176 Events_InfoMessage& theError) const
1178 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1180 int aNbFeaturesReferred = 0;
1181 int aNbAttributesReferred = 0;
1182 std::set<FeaturePtr> aCommonReferredFeatures;
1184 // find all features referred by attributes listed in theArguments
1185 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1186 for (; anArgIt != theArguments.end(); ++anArgIt) {
1187 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1191 std::set<FeaturePtr> aCoincidentFeatures;
1192 if (aRefAttr->isObject()) {
1193 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1195 aCoincidentFeatures.insert(aFeature);
1196 aNbFeaturesReferred += 1;
1199 AttributePoint2DPtr aPoint =
1200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1202 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1203 aNbAttributesReferred += 1;
1207 if (aCommonReferredFeatures.empty())
1208 aCommonReferredFeatures = aCoincidentFeatures;
1210 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1212 if (aCommonReferredFeatures.empty())
1216 bool isOk = aNbFeaturesReferred < 1;
1217 if (aNbFeaturesReferred == 1) {
1218 if (aCommonReferredFeatures.size() == 1) {
1219 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1220 isOk = aNbAttributesReferred <= 1 ||
1221 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1222 aFeature->getKind() == SketchPlugin_Arc::ID();
1228 theError = "Attributes are referred to the same feature";
1232 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1233 const AttributePtr& theAttribute,
1234 const std::list<std::string>& theArguments,
1235 Events_InfoMessage& theError) const
1237 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1238 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1240 // find all points referred by attributes listed in theArguments
1241 bool hasRefsToPoints = false;
1242 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1243 for (; anArgIt != theArguments.end(); ++anArgIt) {
1244 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1248 if (!aRefAttr->isObject()) {
1249 AttributePoint2DPtr aPoint =
1250 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1251 if (aReferredCoincidentPoints.empty())
1252 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1253 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1254 return true; // non-coincident point has been found
1256 hasRefsToPoints = true;
1260 if (hasRefsToPoints)
1261 theError = "Attributes are referred to the same point";
1262 return !hasRefsToPoints;
1265 bool SketchPlugin_CirclePassedPointValidator::isValid(
1266 const AttributePtr& theAttribute,
1267 const std::list<std::string>&,
1268 Events_InfoMessage& theError) const
1270 static const std::string aErrorMessage(
1271 "Passed point refers to the same feature as a center point");
1273 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1275 AttributeRefAttrPtr aCenterRef =
1276 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1277 AttributeRefAttrPtr aPassedRef =
1278 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1280 if (!aPassedRef->isObject())
1283 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1284 if (!aPassedFeature)
1287 if (aCenterRef->isObject()) {
1288 if (aCenterRef->object() == aPassedRef->object()) {
1289 theError = aErrorMessage;
1293 AttributePoint2DPtr aCenterPoint =
1294 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1296 std::set<FeaturePtr> aCoincidentFeatures =
1297 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1298 // check one of coincident features is a feature referred by passed point
1299 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1300 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1301 if (*anIt == aPassedFeature) {
1302 theError = aErrorMessage;
1310 bool SketchPlugin_ThirdPointValidator::isValid(
1311 const AttributePtr& theAttribute,
1312 const std::list<std::string>& theArguments,
1313 Events_InfoMessage& theError) const
1315 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1316 return arePointsNotOnLine(anOwner, theError) &&
1317 arePointsNotSeparated(anOwner, theArguments, theError);
1320 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1321 const std::string& thePointAttrName,
1322 const std::string& theRefPointAttrName)
1324 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1325 theMacroCircle->attribute(thePointAttrName));
1326 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1328 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1330 if (aRefAttr->isObject()) {
1331 // project a point onto selected feature
1332 std::shared_ptr<SketchPlugin_Feature> aFeature =
1333 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1334 ModelAPI_Feature::feature(aRefAttr->object()));
1336 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1337 std::shared_ptr<GeomAPI_Edge> anEdge =
1338 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1340 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1341 if (anEdge->isLine())
1342 aPoint3D = anEdge->line()->project(aPoint3D);
1343 else if (anEdge->isCircle())
1344 aPoint3D = anEdge->circle()->project(aPoint3D);
1346 aPoint = aSketch->to2D(aPoint3D);
1350 AttributePoint2DPtr anOtherPoint =
1351 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1353 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1360 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1361 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1363 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1364 thePoints[0] = toPoint(theMacroFeature,
1365 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1366 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1367 thePoints[1] = toPoint(theMacroFeature,
1368 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1369 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1370 thePoints[2] = toPoint(theMacroFeature,
1371 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1372 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1373 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1374 thePoints[0] = toPoint(theMacroFeature,
1375 SketchPlugin_MacroArc::START_POINT_2_ID(),
1376 SketchPlugin_MacroArc::START_POINT_REF_ID());
1377 thePoints[1] = toPoint(theMacroFeature,
1378 SketchPlugin_MacroArc::END_POINT_2_ID(),
1379 SketchPlugin_MacroArc::END_POINT_REF_ID());
1380 thePoints[2] = toPoint(theMacroFeature,
1381 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1382 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1386 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1387 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1388 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1390 static const double aTolerance = 1.e-7;
1391 if (thePoint1->distance(thePoint2) < aTolerance ||
1392 thePoint1->distance(thePoint3) < aTolerance)
1395 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1396 thePoint2->y() - thePoint1->y()));
1397 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1398 thePoint3->y() - thePoint1->y()));
1399 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1402 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1403 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1404 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1406 static const double aTolerance = 1.e-7;
1407 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1408 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1410 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1411 // the first point is on the line
1412 if (aVec1->squareModulus() < aTolerance * aTolerance)
1414 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1415 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1416 // the second point is on the line
1417 if (aVec2->squareModulus() < aTolerance * aTolerance)
1419 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1421 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1424 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1425 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1426 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1428 static const double aTolerance = 1.e-7;
1429 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1430 double aDistP1C = thePoint1->distance(aCenter);
1431 double aDistP2C = thePoint2->distance(aCenter);
1432 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1435 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1436 const FeaturePtr& theMacroFeature,
1437 Events_InfoMessage& theError) const
1439 static const std::string aErrorPointsOnLine(
1440 "Selected points are on the same line");
1442 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1443 threePointsOfFeature(theMacroFeature, aPoints);
1445 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1446 theError = aErrorPointsOnLine;
1452 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1453 const FeaturePtr& theMacroFeature,
1454 const std::list<std::string>& theArguments,
1455 Events_InfoMessage& theError) const
1457 static const std::string aErrorPointsApart(
1458 "Selected entity is lying between first two points");
1460 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1461 FeaturePtr aRefByThird;
1462 if (aThirdPointRef->isObject())
1463 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1467 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1468 threePointsOfFeature(theMacroFeature, aPoints);
1470 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1471 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1475 SketchPlugin_Sketch* aSketch =
1476 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1477 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1478 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1481 if (aThirdShape->isLine())
1482 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1483 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1484 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1487 theError = aErrorPointsApart;
1491 bool SketchPlugin_ArcEndPointValidator::isValid(
1492 const AttributePtr& theAttribute,
1493 const std::list<std::string>& theArguments,
1494 Events_InfoMessage& theError) const
1496 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1497 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1499 if(!anEndPointRef.get()) {
1503 ObjectPtr anObject = anEndPointRef->object();
1504 AttributePtr anAttr = anEndPointRef->attr();
1505 if(!anObject.get() && !anAttr.get()) {
1509 if(anEndPointRef->attr().get()) {
1513 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1515 GeomShapePtr aShape = aResult->shape();
1516 if(aShape.get() && aShape->isVertex()) {
1521 aFeature = ModelAPI_Feature::feature(anObject);
1522 if(aFeature.get()) {
1523 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1531 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1533 if(!theShape.get()) {
1537 if(!theShape->isEdge()) {
1541 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1547 if(anEdge->isLine()) {
1548 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1549 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1553 if(anEdge->isCircle() || anEdge->isArc()) {
1554 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1555 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1562 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1563 const AttributePtr& theAttribute,
1564 const std::list<std::string>& theArguments,
1565 Events_InfoMessage& theError) const
1567 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1568 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1569 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1571 if(!anEndPointRef.get()) {
1575 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1577 if(!anArcShape.get() || anArcShape->isNull()) {
1581 ObjectPtr anObject = anEndPointRef->object();
1582 AttributePtr anAttr = anEndPointRef->attr();
1583 if(!anObject.get() && !anAttr.get()) {
1587 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1589 GeomShapePtr aShape = aResult->shape();
1590 if (!aShape->isEdge())
1592 aShape = toInfiniteEdge(aShape);
1593 if(aShape.get() && !aShape->isNull()) {
1594 if(anArcShape->isIntersect(aShape)) {
1600 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1601 if(aSelectedFeature.get()) {
1602 std::list<ResultPtr> aResults = aSelectedFeature->results();
1603 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1604 anIt != aResults.cend();
1607 GeomShapePtr aShape = (*anIt)->shape();
1608 if (!aShape->isEdge())
1610 aShape = toInfiniteEdge(aShape);
1611 if(aShape.get() && !aShape->isNull()) {
1612 if(anArcShape->isIntersect(aShape)) {
1622 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1623 const std::list<std::string>& theArguments,
1624 Events_InfoMessage& theError) const
1626 std::set<std::string> aFeatureKinds;
1627 for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1628 anArgIt != theArguments.end(); anArgIt++) {
1629 aFeatureKinds.insert(*anArgIt);
1632 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1633 theError = "The attribute with the %1 type is not processed";
1634 theError.arg(theAttribute->attributeType());
1638 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1640 bool isObject = aRefAttr->isObject();
1642 theError = "It uses an empty object";
1645 ObjectPtr anObject = aRefAttr->object();
1646 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1647 if (!aFeature.get()) {
1648 theError = "The feature of the checked attribute is empty";
1652 FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1654 std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1655 std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1656 for (; anIt != aRefsList.end(); anIt++) {
1657 FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1658 if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1659 aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1660 return false; // constraint is found, that means that the check is not valid
1665 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1666 const AttributePtr& theAttribute,
1667 const std::list<std::string>& theArguments,
1668 Events_InfoMessage& theError) const
1670 AttributeRefAttrPtr aRefAttr =
1671 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1674 theError = "Incorrect attribute";
1679 if (aRefAttr->isObject())
1680 anOwner = aRefAttr->object();
1683 AttributePtr anAttr = aRefAttr->attr();
1684 anOwner = anAttr->owner();
1686 FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1687 if (!anAttrOwnerFeature)
1689 AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1690 if (!aCopyAttr || !aCopyAttr->value())
1691 return true; // feature is not a copy, thus valid
1693 // check the copy feature is already referred by the "Multi" feature
1694 FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1695 AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1696 for (int i = 0; i < aRefList->size(); ++i)
1698 FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1699 if (aRefOwner == anAttrOwnerFeature)
1701 theError = "Attribute refers to the object generated by this feature";
1709 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1710 const std::list<std::string>& theArguments,
1711 Events_InfoMessage& theError) const
1713 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1714 theError = "The attribute with the %1 type is not processed";
1715 theError.arg(theAttribute->attributeType());
1719 // check the attribute refers to a sketch feature
1720 AttributeRefAttrPtr aRefAttr =
1721 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1722 bool isSketchFeature = aRefAttr->isObject();
1723 if (isSketchFeature) {
1724 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1725 isSketchFeature = aFeature.get() != NULL;
1726 if (isSketchFeature) {
1727 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1728 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1729 isSketchFeature = aSketchFeature.get() != NULL;
1733 if (isSketchFeature)
1736 theError = "The object selected is not a sketch feature";
1740 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1741 const std::list<std::string>& theArguments,
1742 Events_InfoMessage& theError) const
1744 if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1745 theError = "The attribute with the %1 type is not processed";
1746 theError.arg(theAttribute->attributeType());
1750 AttributeDoublePtr anAngleAttr =
1751 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1753 FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1754 AttributeStringPtr anAngleType =
1755 aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1756 AttributeIntegerPtr aNbCopies =
1757 aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1759 if (anAngleType->value() != "FullAngle")
1761 double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1762 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1764 theError = "Rotation single angle should produce full angle less than 360 degree";
1770 double aFullAngleValue = anAngleAttr->value();
1771 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1773 theError = "Rotation full angle should be in range [0, 360]";