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_Ellipse.h"
29 #include "SketchPlugin_EllipticArc.h"
30 #include "SketchPlugin_Fillet.h"
31 #include "SketchPlugin_Line.h"
32 #include "SketchPlugin_MacroArc.h"
33 #include "SketchPlugin_MacroCircle.h"
34 #include "SketchPlugin_MultiRotation.h"
35 #include "SketchPlugin_Point.h"
36 #include "SketchPlugin_Sketch.h"
37 #include "SketchPlugin_Trim.h"
38 #include "SketchPlugin_Tools.h"
40 #include "SketcherPrs_Tools.h"
42 #include <Events_InfoMessage.h>
44 #include <ModelAPI_Data.h>
45 #include <ModelAPI_Validator.h>
46 #include <ModelAPI_AttributeDouble.h>
47 #include <ModelAPI_AttributeInteger.h>
48 #include <ModelAPI_AttributeRefAttr.h>
49 #include <ModelAPI_AttributeRefList.h>
50 #include <ModelAPI_AttributeSelectionList.h>
51 #include <ModelAPI_AttributeString.h>
52 #include <ModelAPI_Session.h>
53 #include <ModelAPI_Tools.h>
54 #include <ModelAPI_ResultConstruction.h>
56 #include <ModelGeomAlgo_Point2D.h>
57 #include <ModelGeomAlgo_Shape.h>
59 #include <GeomAlgoAPI_EdgeBuilder.h>
60 #include <GeomAlgoAPI_ShapeTools.h>
62 #include <GeomAPI_Circ.h>
63 #include <GeomAPI_Dir2d.h>
64 #include <GeomAPI_Ellipse.h>
65 #include <GeomAPI_Lin.h>
66 #include <GeomAPI_Edge.h>
67 #include <GeomAPI_Vertex.h>
68 #include <GeomDataAPI_Point2D.h>
73 const double tolerance = 1.e-7;
75 bool SketchPlugin_DistanceAttrValidator::isValid(const AttributePtr& theAttribute,
76 const std::list<std::string>& theArguments,
77 Events_InfoMessage& theError) const
79 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
80 theError = "The attribute with the %1 type is not processed";
81 theError.arg(theAttribute->attributeType());
85 // there is a check whether the feature contains a point and a linear edge or two point values
86 std::string aParamA = theArguments.front();
87 SessionPtr aMgr = ModelAPI_Session::get();
88 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
90 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
92 bool isObject = aRefAttr->isObject();
94 // an attribute is a point. A point value is valid always for the distance
97 // 1. check whether the references object is a linear
98 ObjectPtr anObject = aRefAttr->object();
100 const ModelAPI_AttributeValidator* aShapeValidator =
101 dynamic_cast<const ModelAPI_AttributeValidator*>(
102 aFactory->validator("GeomValidators_ShapeType"));
103 std::list<std::string> anArguments;
104 anArguments.push_back("circle");
105 Events_InfoMessage aCircleError;
106 bool aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aCircleError);
107 // the circle line is not a valid case
109 theError = "Circle can not be used in distance constraint";
114 anArguments.push_back("line");
115 Events_InfoMessage aLineError;
116 aShapeValid = aShapeValidator->isValid(aRefAttr, anArguments, aLineError);
117 // if the attribute value is not a line, that means it is a vertex. A vertex is always valid
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
120 // If it is a line then we have to check that first attribute id not a line
121 std::shared_ptr<SketchPlugin_Feature> aSFeature =
122 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
123 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
124 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(aSketch);
125 std::shared_ptr<GeomDataAPI_Point2D> aPoint = SketcherPrs_Tools::getFeaturePoint(
126 aFeature->data(), aParamA, aPlane);
128 theError = "One of parameters of distance constraint should be a point";
136 bool SketchPlugin_TangentAttrValidator::isValid(const AttributePtr& theAttribute,
137 const std::list<std::string>& theArguments,
138 Events_InfoMessage& theError) const
140 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
141 theError = "The attribute with the %1 type is not processed";
142 theError.arg(theAttribute->attributeType());
146 // there is a check whether the feature contains a point and a linear edge or two point values
147 std::string aParamA = theArguments.front();
148 SessionPtr aMgr = ModelAPI_Session::get();
149 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
151 FeaturePtr anAttributeFeature =
152 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
153 AttributeRefAttrPtr aRefAttr =
154 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
156 bool isObject = aRefAttr->isObject();
157 ObjectPtr anObject = aRefAttr->object();
158 if (isObject && anObject.get()) {
159 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
161 AttributeRefAttrPtr aOtherAttr = anAttributeFeature->data()->refattr(aParamA);
162 ObjectPtr aOtherObject = aOtherAttr->object();
163 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
167 if (aRefFea->getKind() == SketchPlugin_Line::ID() &&
168 aOtherFea->getKind() == SketchPlugin_Line::ID()) {
169 theError = "Two segments cannot be tangent";
175 theError = "It uses an empty object";
182 bool SketchPlugin_PerpendicularAttrValidator::isValid(const AttributePtr& theAttribute,
183 const std::list<std::string>& theArguments,
184 Events_InfoMessage& theError) const
186 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
187 theError = "The attribute with the %1 type is not processed";
188 theError.arg(theAttribute->attributeType());
192 std::string aParamA = theArguments.front();
193 SessionPtr aMgr = ModelAPI_Session::get();
194 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
196 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
197 AttributeRefAttrPtr aRefAttr =
198 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
200 bool isObject = aRefAttr->isObject();
201 ObjectPtr anObject = aRefAttr->object();
202 if (isObject && anObject.get()) {
203 FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
205 AttributeRefAttrPtr aOtherAttr = anOwner->refattr(aParamA);
206 ObjectPtr aOtherObject = aOtherAttr->object();
207 FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
211 // at least one feature should be a line
212 if (aRefFea->getKind() != SketchPlugin_Line::ID() &&
213 aOtherFea->getKind() != SketchPlugin_Line::ID()) {
214 theError = "At least one feature should be a line";
219 theError = "It uses an empty object";
226 bool SketchPlugin_NotFixedValidator::isValid(const AttributePtr& theAttribute,
227 const std::list<std::string>& theArguments,
228 Events_InfoMessage& theError) const
230 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
231 theError = "The attribute with the %1 type is not processed";
232 theError.arg(theAttribute->attributeType());
236 std::shared_ptr<SketchPlugin_Feature> aFeature =
237 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
241 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
243 SketchPlugin_Sketch* aSketch = aFeature->sketch();
244 int aNbFeatures = aSketch->numberOfSubs();
245 for (int anInd = 0; anInd < aNbFeatures; anInd++) {
246 FeaturePtr aSubFeature = aSketch->subFeature(anInd);
247 if (aSubFeature->getKind() != SketchPlugin_ConstraintRigid::ID() || aSubFeature == aFeature)
249 AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
250 aSubFeature->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
251 if (aRefAttr->isObject()) {
252 if (aRefAttr->object() == aRAttr->object()) {
253 ObjectPtr anObject = aRefAttr->object();
254 std::string aName = anObject.get() ? anObject->data()->name() : "";
255 theError = "The object %1 has been already fixed.";
260 else if (aRefAttr->attr() == aRAttr->attr()) {
261 AttributePtr anAttribute = aRefAttr->attr();
262 std::string aName = anAttribute.get() ? anAttribute->id() : "";
263 theError = "The attribute %1 has been already fixed.";
271 bool SketchPlugin_EqualAttrValidator::isValid(const AttributePtr& theAttribute,
272 const std::list<std::string>& theArguments,
273 Events_InfoMessage& theError) const
275 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
276 theError = "The attribute with the %1 type is not processed";
277 theError.arg(theAttribute->attributeType());
281 std::string aParamA = theArguments.front();
282 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
283 AttributeRefAttrPtr aRefAttr[2];
284 aRefAttr[0] = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
285 aRefAttr[1] = aFeature->data()->refattr(aParamA);
287 if (!aRefAttr[0]->isObject() || !aRefAttr[1]->isObject()) {
288 theError = "Attributes can not be used in equal constraint";
292 std::string aType[2];
293 std::list<std::string> anArguments;
294 for (int i = 0; i < 2; i++) {
295 ObjectPtr anObject = aRefAttr[i]->object();
296 if (!anObject.get()) {
297 theError = "An empty object is used.";
301 aFeature = ModelAPI_Feature::feature(anObject);
302 if (!aFeature.get()) {
303 theError = "An empty feature is used.";
307 aType[i] = aFeature->getKind();
308 if (aFeature->getKind() != SketchPlugin_Line::ID() &&
309 aFeature->getKind() != SketchPlugin_Circle::ID() &&
310 aFeature->getKind() != SketchPlugin_Arc::ID() &&
311 aFeature->getKind() != SketchPlugin_Ellipse::ID() &&
312 aFeature->getKind() != SketchPlugin_EllipticArc::ID()) {
313 theError = "The %1 feature is not supported by the Equal constraint.";
314 theError.arg(aFeature->getKind());
315 // wrong type of attribute
320 bool isOk = aType[0] == aType[1];
322 // circle and arc may be equal
323 isOk = (aType[0] == SketchPlugin_Arc::ID() && aType[1] == SketchPlugin_Circle::ID())
324 || (aType[0] == SketchPlugin_Circle::ID() && aType[1] == SketchPlugin_Arc::ID());
327 // ellipse and elliptic arc may be equal
328 isOk = (aType[0] == SketchPlugin_EllipticArc::ID() && aType[1] == SketchPlugin_Ellipse::ID())
329 || (aType[0] == SketchPlugin_Ellipse::ID() && aType[1] == SketchPlugin_EllipticArc::ID());
332 theError = "Features with kinds %1 and %2 can not be equal.";
333 theError.arg(aType[0]).arg(aType[1]);
339 bool SketchPlugin_MirrorAttrValidator::isValid(const AttributePtr& theAttribute,
340 const std::list<std::string>& theArguments,
341 Events_InfoMessage& theError) const
343 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
344 theError = "The attribute with the %1 type is not processed";
345 theError.arg(theAttribute->attributeType());
349 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
350 AttributeRefListPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
352 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
353 aFeature->attribute(SketchPlugin_Constraint::ENTITY_C()));
354 std::list<ObjectPtr> aMirroredObjects = aRefListOfMirrored->list();
356 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
357 ObjectPtr aSelObject = aSelAttr->object(anInd);
358 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
359 std::list<ObjectPtr>::iterator aMirIter = aMirroredObjects.begin();
360 for (; aMirIter != aMirroredObjects.end(); aMirIter++)
361 if (aSelObject == *aMirIter) {
362 theError = "The object %1 is a result of mirror";
370 bool SketchPlugin_CoincidenceAttrValidator::isValid(const AttributePtr& theAttribute,
371 const std::list<std::string>& theArguments,
372 Events_InfoMessage& theError) const
374 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
375 theError = "The attribute with the %1 type is not processed";
376 theError.arg(theAttribute->attributeType());
380 // there is a check whether the feature contains a point and a linear edge or two point values
381 std::string aParamA = theArguments.front();
382 SessionPtr aMgr = ModelAPI_Session::get();
383 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
385 FeaturePtr aConstraint = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
386 AttributeRefAttrPtr aRefAttrA = aConstraint->data()->refattr(aParamA);
388 theError = "The %1 attribute should be %2";
389 theError.arg(aParamA).arg(ModelAPI_AttributeRefAttr::typeId());
393 AttributeRefAttrPtr aRefAttrB =
394 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
396 // first attribute is a point, it may coincide with any object
397 if (!aRefAttrA->isObject())
400 ObjectPtr anObject = aRefAttrA->object();
401 if (!anObject.get()) {
402 theError = "%1 attribute has an empty object";
403 theError.arg(aParamA);
406 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrA->object());
407 if (!aFeature.get()) {
408 theError = "%1 attribute has an empty feature";
409 theError.arg(aParamA);
413 if (aFeature->getKind() == SketchPlugin_Point::ID())
417 // second attribute is a point, it may coincide with any object
418 if (!aRefAttrB->isObject())
421 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrB->object());
423 theError = "%1 attribute has an empty object";
424 theError.arg(theAttribute->id());
427 if (aFeature->getKind() == SketchPlugin_Point::ID())
430 theError = "There is no an attribute filled by a point";
435 bool SketchPlugin_CopyValidator::isValid(const AttributePtr& theAttribute,
436 const std::list<std::string>& theArguments,
437 Events_InfoMessage& theError) const
439 if (theAttribute->attributeType() != ModelAPI_AttributeRefList::typeId()) {
440 theError = "The attribute with the %1 type is not processed";
441 theError.arg(theAttribute->attributeType());
445 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
446 AttributeRefListPtr aSelAttr =
447 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
449 AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
450 aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
451 AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
452 aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
453 std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
454 std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
456 std::list<ObjectPtr>::iterator anObjIter;
457 for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
458 ObjectPtr aSelObject = aSelAttr->object(anInd);
459 anObjIter = anInitialObjects.begin();
460 for (; anObjIter != anInitialObjects.end(); anObjIter++)
461 if (aSelObject == *anObjIter)
463 if (anObjIter != anInitialObjects.end())
465 anObjIter = aCopiedObjects.begin();
466 for (; anObjIter != aCopiedObjects.end(); anObjIter++)
467 if (aSelObject == *anObjIter) {
468 std::string aName = aSelObject.get() ? aSelObject->data()->name() : "";
469 theError = "The object %1 is a result of copy";
477 bool SketchPlugin_SolverErrorValidator::isValid(
478 const std::shared_ptr<ModelAPI_Feature>& theFeature,
479 const std::list<std::string>& theArguments,
480 Events_InfoMessage& theError) const
482 AttributeStringPtr aAttributeString = theFeature->string(SketchPlugin_Sketch::SOLVER_ERROR());
484 if (!aAttributeString->value().empty()) {
485 theError = aAttributeString->value();
492 bool SketchPlugin_SolverErrorValidator::isNotObligatory(std::string theFeature,
493 std::string theAttribute)
498 static bool hasSameTangentFeature(const std::set<AttributePtr>& theRefsList,
499 const FeaturePtr theFeature)
501 for(std::set<AttributePtr>::const_iterator
502 anIt = theRefsList.cbegin(); anIt != theRefsList.cend(); ++anIt) {
503 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
504 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
507 if (aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
508 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
509 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_A()));
510 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
511 aFeature->attribute(SketchPlugin_ConstraintTangent::ENTITY_B()));
512 if(anAttrRefA.get()) {
513 ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefA->object());
515 DocumentPtr aDoc = aResA->document();
517 FeaturePtr aFeatureA = aDoc->feature(aResA);
518 if(aFeatureA.get() && aFeatureA == theFeature) {
524 if(anAttrRefB.get()) {
525 ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(anAttrRefB->object());
527 DocumentPtr aDoc = aResB->document();
529 FeaturePtr aFeatureB = aDoc->feature(aResB);
530 if(aFeatureB.get() && aFeatureB == theFeature) {
541 static bool isPointPointCoincidence(const FeaturePtr& theCoincidence)
543 AttributeRefAttrPtr aRefAttr[2] = {
544 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_A()),
545 theCoincidence->refattr(SketchPlugin_Constraint::ENTITY_B())
548 bool arePoints = true;
549 for (int i = 0; i < 2 && arePoints; ++i) {
550 if (aRefAttr[i]->isObject()) {
551 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr[i]->object());
552 arePoints = aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID();
554 arePoints = aRefAttr[i]->attr().get() != NULL;
559 bool SketchPlugin_FilletVertexValidator::isValid(const AttributePtr& theAttribute,
560 const std::list<std::string>& theArguments,
561 Events_InfoMessage& theError) const
563 AttributeRefAttrPtr aPointRefAttr =
564 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
565 if(!aPointRefAttr.get()) {
566 theError = "Error: Point not selected.";
570 AttributePtr aPointAttribute = aPointRefAttr->attr();
571 if (!aPointAttribute.get()) {
572 theError = "Error: Bad point selected.";
575 std::shared_ptr<GeomAPI_Pnt2d> aSelectedPnt =
576 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttribute)->pnt();
578 // Obtain constraint coincidence for the fillet point.
579 const std::set<AttributePtr>& aRefsList = aPointAttribute->owner()->data()->refsToMe();
580 FeaturePtr aConstraintCoincidence;
581 for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
582 anIt != aRefsList.cend(); ++anIt) {
583 std::shared_ptr<ModelAPI_Attribute> aAttr = (*anIt);
584 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
585 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
586 if (!isPointPointCoincidence(aConstrFeature))
589 AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
590 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
591 AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
592 aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
594 AttributePtr anAttrA = anAttrRefA->attr();
595 if(aPointAttribute == anAttrA) {
596 aConstraintCoincidence = aConstrFeature;
600 AttributePtr anAttrB = anAttrRefB->attr();
601 if(aPointAttribute == anAttrB) {
602 aConstraintCoincidence = aConstrFeature;
608 if(!aConstraintCoincidence.get()) {
609 theError = "Error: one of the selected point does not have coincidence.";
613 // Get coincides from constraint.
614 std::set<FeaturePtr> aCoinsides;
615 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
616 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
619 SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
620 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
624 // Remove points and external lines from set of coincides.
625 std::set<FeaturePtr> aNewSetOfCoincides;
626 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
627 anIt != aCoinsides.end(); ++anIt) {
628 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
629 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
630 if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) {
633 if((*anIt)->getKind() != SketchPlugin_Line::ID() &&
634 (*anIt)->getKind() != SketchPlugin_Arc::ID()) {
637 if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
638 AttributePtr anArcCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
639 std::shared_ptr<GeomAPI_Pnt2d> anArcCenterPnt =
640 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anArcCenter)->pnt();
641 double aDistSelectedArcCenter = aSelectedPnt->distance(anArcCenterPnt);
642 if(aDistSelectedArcCenter < tolerance) {
646 aNewSetOfCoincides.insert(*anIt);
648 aCoinsides = aNewSetOfCoincides;
650 // If we still have more than two coincides remove auxilary entities from set of coincides.
651 if(aCoinsides.size() > 2) {
652 aNewSetOfCoincides.clear();
653 for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
654 anIt != aCoinsides.end(); ++anIt) {
655 if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
656 aNewSetOfCoincides.insert(*anIt);
659 aCoinsides = aNewSetOfCoincides;
662 if(aCoinsides.size() != 2) {
663 theError = "Error: One of the selected points does not have two suitable edges for fillet.";
667 // Check that selected edges don't have tangent constraint.
668 std::set<FeaturePtr>::iterator anIt = aCoinsides.begin();
669 FeaturePtr aFirstFeature = *anIt++;
670 FeaturePtr aSecondFeature = *anIt;
671 const std::set<AttributePtr>& aFirstFeatureRefsList = aFirstFeature->data()->refsToMe();
672 if(hasSameTangentFeature(aFirstFeatureRefsList, aSecondFeature)) {
673 theError = "Error: Edges in selected point has tangent constraint.";
677 std::list<ResultPtr> aFirstResults = aFirstFeature->results();
678 for(std::list<ResultPtr>::iterator aResIt = aFirstResults.begin();
679 aResIt != aFirstResults.end(); ++aResIt) {
680 ResultPtr aRes = *aResIt;
681 const std::set<AttributePtr>& aResRefsList = aRes->data()->refsToMe();
682 if(hasSameTangentFeature(aResRefsList, aSecondFeature)) {
683 theError = "Error: Edges in selected point has tangent constraint.";
688 // Check the features are not tangent
689 std::shared_ptr<GeomAPI_Shape> aFirstShape = aFirstFeature->lastResult()->shape();
690 std::shared_ptr<GeomAPI_Shape> aSecondShape = aSecondFeature->lastResult()->shape();
691 if (!aFirstShape || !aFirstShape->isEdge() ||
692 !aSecondShape || !aSecondShape->isEdge()) {
693 theError = "Error: At least on of the features is not an edge";
697 std::shared_ptr<GeomAPI_Edge> anEdge1 = std::dynamic_pointer_cast<GeomAPI_Edge>(aFirstShape);
698 std::shared_ptr<GeomAPI_Edge> anEdge2 = std::dynamic_pointer_cast<GeomAPI_Edge>(aSecondShape);
700 static const double TOL = 1.e-7;
701 if (anEdge1->isLine() && anEdge2->isLine()) {
702 // Check that lines not collinear
703 std::shared_ptr<GeomAPI_Dir> aDir1 = anEdge1->line()->direction();
704 std::shared_ptr<GeomAPI_Dir> aDir2 = anEdge2->line()->direction();
705 double aCross = aDir1->cross(aDir2)->squareModulus();
706 if (aCross < TOL * TOL)
708 } else if (anEdge1->isArc() && anEdge2->isArc()) {
709 // check the circles are not tangent
710 std::shared_ptr<GeomAPI_Circ> aCirc1 = anEdge1->circle();
711 std::shared_ptr<GeomAPI_Circ> aCirc2 = anEdge2->circle();
712 double aDistCC = aCirc1->center()->distance(aCirc2->center());
713 double aRadSum = aCirc1->radius() + aCirc2->radius();
714 double aRadDiff = fabs(aCirc1->radius() - aCirc2->radius());
715 if (fabs(aDistCC - aRadSum) < TOL || fabs(aDistCC - aRadDiff) < TOL)
718 // check whether line and arc are tangent
719 std::shared_ptr<GeomAPI_Circ> aCirc;
720 std::shared_ptr<GeomAPI_Lin> aLine;
721 if (anEdge1->isLine()) {
722 aLine = anEdge1->line();
723 aCirc = anEdge2->circle();
725 aCirc = anEdge1->circle();
726 aLine = anEdge2->line();
729 double aDistCL = aLine->distance(aCirc->center());
730 if (fabs(aDistCL - aCirc->radius()) < TOL)
737 bool SketchPlugin_MiddlePointAttrValidator::isValid(const AttributePtr& theAttribute,
738 const std::list<std::string>& theArguments,
739 Events_InfoMessage& theError) const
741 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
742 theError = "The attribute with the %1 type is not processed";
743 theError.arg(theAttribute->attributeType());
747 // there is a check whether the feature contains a point and a linear edge or two point values
748 std::string aParamA = theArguments.front();
749 SessionPtr aMgr = ModelAPI_Session::get();
750 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
752 FeaturePtr anAttributeFeature =
753 std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
754 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
755 AttributeRefAttrPtr anOtherAttr = anAttributeFeature->data()->refattr(aParamA);
757 AttributeRefAttrPtr aRefAttrs[2] = {aRefAttr, anOtherAttr};
760 for (int i = 0; i < 2; ++i) {
761 if (!aRefAttrs[i]->isObject())
764 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttrs[i]->object());
766 if (aNbPoints + aNbLines != 0)
771 if (aFeature->getKind() == SketchPlugin_Point::ID())
773 else if (aFeature->getKind() == SketchPlugin_Line::ID() ||
774 aFeature->getKind() == SketchPlugin_Arc::ID() ||
775 aFeature->getKind() == SketchPlugin_EllipticArc::ID())
780 if (aNbPoints != 1 || aNbLines != 1) {
781 theError = "Middle point constraint allows points and lines only";
787 bool SketchPlugin_ArcTangentPointValidator::isValid(const AttributePtr& theAttribute,
788 const std::list<std::string>& /*theArguments*/,
789 Events_InfoMessage& theError) const
791 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
792 theError = "The attribute with the %1 type is not processed";
793 theError.arg(theAttribute->attributeType());
796 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
797 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
798 if (anArcTypeAttr && anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE())
799 return true; // not applicable for non-tangent arcs
801 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
802 AttributePtr anAttr = aRefAttr->attr();
804 theError = "The attribute %1 should be a point";
805 theError.arg(theAttribute->id());
809 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
810 const std::string& aFeatureType = anAttrFeature->getKind();
811 if (aFeatureType == SketchPlugin_Arc::ID()) {
812 // selected point should not be a center of arc
813 const std::string& aPntId = anAttr->id();
814 if (aPntId != SketchPlugin_Arc::START_ID() && aPntId != SketchPlugin_Arc::END_ID()) {
815 theError = "The attribute %1 is not supported";
816 theError.arg(aPntId);
820 else if (aFeatureType == SketchPlugin_Line::ID()) {
821 // selected point should be bound point of line
822 const std::string& aPntId = anAttr->id();
823 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
824 theError = "The attribute %1 is not supported";
825 theError.arg(aPntId);
830 theError = "Unable to build tangent arc on %1";
831 theError.arg(anAttrFeature->getKind());
838 bool SketchPlugin_ArcTransversalPointValidator::isValid(
839 const AttributePtr& theAttribute,
840 const std::list<std::string>& /*theArguments*/,
841 Events_InfoMessage& theError) const
843 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
844 theError = "The attribute with the %1 type is not processed";
845 theError.arg(theAttribute->attributeType());
848 FeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
849 AttributeStringPtr anArcTypeAttr = anOwner->string(SketchPlugin_MacroArc::ARC_TYPE());
851 anArcTypeAttr->value() != SketchPlugin_MacroArc::ARC_TYPE_BY_TRANSVERSAL_LINE())
852 return true; // not applicable for non-transversal arcs
854 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
855 AttributePtr anAttr = aRefAttr->attr();
857 theError = "The attribute %1 should be a point";
858 theError.arg(theAttribute->id());
862 FeaturePtr anAttrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
863 const std::string& aFeatureType = anAttrFeature->getKind();
864 if (aFeatureType == SketchPlugin_Line::ID()) {
865 // selected point should be bound point of line
866 const std::string& aPntId = anAttr->id();
867 if (aPntId != SketchPlugin_Line::START_ID() && aPntId != SketchPlugin_Line::END_ID()) {
868 theError = "The attribute %1 is not supported";
869 theError.arg(aPntId);
874 theError = "Unable to build perpendicular arc on %1";
875 theError.arg(anAttrFeature->getKind());
882 bool SketchPlugin_IntersectionValidator::isValid(const AttributePtr& theAttribute,
883 const std::list<std::string>& theArguments,
884 Events_InfoMessage& theError) const
886 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
887 theError = "The attribute with the %1 type is not processed";
888 theError.arg(theAttribute->attributeType());
891 AttributeSelectionPtr anExternalAttr =
892 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
893 std::shared_ptr<GeomAPI_Edge> anEdge;
894 if (anExternalAttr && anExternalAttr->value() && anExternalAttr->value()->isEdge()) {
895 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->value()));
896 } else if(anExternalAttr->context() && anExternalAttr->context()->shape() &&
897 anExternalAttr->context()->shape()->isEdge()) {
898 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anExternalAttr->context()->shape()));
902 theError = "The attribute %1 should be an edge";
903 theError.arg(theAttribute->id());
908 std::shared_ptr<SketchPlugin_Sketch> aSketch;
909 std::set<AttributePtr> aRefs = anExternalAttr->owner()->data()->refsToMe();
910 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
911 for (; anIt != aRefs.end(); ++anIt) {
912 CompositeFeaturePtr aComp =
913 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
914 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
915 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
920 theError = "There is no sketch referring to the current feature";
924 // check the edge is intersected with sketch plane
925 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
927 std::list<GeomPointPtr> anIntersectionsPoints;
928 anEdge->intersectWithPlane(aPlane, anIntersectionsPoints);
929 if (anIntersectionsPoints.empty()) {
930 theError = "The edge is not intersected with sketch plane";
936 bool SketchPlugin_SplitValidator::isValid(const AttributePtr& theAttribute,
937 const std::list<std::string>& theArguments,
938 Events_InfoMessage& theError) const
942 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
943 theError = "The attribute with the %1 type is not processed";
944 theError.arg(theAttribute->attributeType());
947 AttributeReferencePtr aFeatureAttr =
948 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
950 ObjectPtr anAttrObject = aFeatureAttr->value();
951 FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttrObject);
955 std::set<ResultPtr> anEdgeShapes;
956 ModelGeomAlgo_Shape::shapesOfType(anAttrFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
957 if (anEdgeShapes.empty() || anEdgeShapes.size() > 1 /*there case has not existed yet*/)
960 // coincidences to the feature
961 std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
962 ModelGeomAlgo_Point2D::getPointsOfReference(anAttrFeature,
963 SketchPlugin_ConstraintCoincidence::ID(),
964 aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
966 GeomShapePtr anAttrShape = (*anEdgeShapes.begin())->shape();
967 std::shared_ptr<SketchPlugin_Feature> aSFeature =
968 std::dynamic_pointer_cast<SketchPlugin_Feature>(anAttrFeature);
971 SketchPlugin_Sketch* aSketch = aSFeature->sketch();
973 std::shared_ptr<ModelAPI_Data> aData = aSketch->data();
974 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
975 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
976 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
977 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
978 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
979 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
980 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
982 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
983 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
984 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
985 PointToRefsMap aPointsInfo;
987 ModelGeomAlgo_Point2D::getPointsInsideShape(anAttrShape, aRefAttributes, aC->pnt(),
988 aX->dir(), aDirY, aPointsInfo);
989 int aCoincidentToFeature = (int)aPointsInfo.size();
990 if (anAttrFeature->getKind() == SketchPlugin_Circle::ID() ||
991 anAttrFeature->getKind() == SketchPlugin_Ellipse::ID())
992 aValid = aCoincidentToFeature >= 2;
994 aValid = aCoincidentToFeature >= 1;
999 bool SketchPlugin_TrimValidator::isValid(const AttributePtr& theAttribute,
1000 const std::list<std::string>& theArguments,
1001 Events_InfoMessage& theError) const
1003 bool aValid = false;
1005 if (theAttribute->attributeType() != ModelAPI_AttributeReference::typeId()) {
1006 theError = "The attribute with the %1 type is not processed";
1007 theError.arg(theAttribute->attributeType());
1010 AttributeReferencePtr aBaseObjectAttr =
1011 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
1013 std::shared_ptr<SketchPlugin_Feature> aTrimFeature =
1014 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
1016 ObjectPtr aBaseObject = aBaseObjectAttr->value();
1018 AttributePtr aPreviewAttr = aTrimFeature->attribute(SketchPlugin_Trim::PREVIEW_OBJECT());
1019 aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aPreviewAttr);
1020 aBaseObject = aBaseObjectAttr->value();
1023 FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1027 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1028 std::dynamic_pointer_cast<SketchPlugin_Feature>(aBaseFeature);
1029 if (!aSketchFeature.get() || aSketchFeature->isCopy())
1033 AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1034 aTrimFeature->data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
1036 SketchPlugin_Sketch* aSketch = aTrimFeature->sketch();
1038 std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
1039 std::shared_ptr<GeomAPI_Pnt> anAttributePnt = aSketch->to3D(anAttributePnt2d->x(),
1040 anAttributePnt2d->y());
1042 std::map<ObjectPtr, std::set<GeomShapePtr> > aCashedShapes;
1043 std::map<ObjectPtr, std::map<std::shared_ptr<GeomAPI_Pnt>,
1044 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
1045 std::list<std::shared_ptr<ModelAPI_Object> > > > > anObjectToPoints;
1046 SketchPlugin_SegmentationTools::fillObjectShapes(
1047 aTrimFeature.get(), aBaseObject, aCashedShapes, anObjectToPoints);
1048 const std::set<GeomShapePtr>& aShapes = aCashedShapes[aBaseObject];
1050 return aShapes.size() > 1;
1053 bool SketchPlugin_ProjectionValidator::isValid(const AttributePtr& theAttribute,
1054 const std::list<std::string>& theArguments,
1055 Events_InfoMessage& theError) const
1057 if (theAttribute->attributeType() != ModelAPI_AttributeSelection::typeId()) {
1058 theError = "The attribute with the %1 type is not processed";
1059 theError.arg(theAttribute->attributeType());
1063 AttributeSelectionPtr aFeatureAttr =
1064 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
1065 std::shared_ptr<GeomAPI_Edge> anEdge;
1066 std::shared_ptr<SketchPlugin_Feature> aSketchFeature;
1067 if (aFeatureAttr.get()) {
1068 GeomShapePtr aVal = aFeatureAttr->value();
1069 ResultPtr aRes = aFeatureAttr->context();
1070 if (aVal && aVal->isVertex())
1071 return true; // vertex is always could be projected
1072 if (aVal && aVal->isEdge()) {
1073 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->value()));
1074 } else if(aRes && aRes->shape()) {
1075 if (aRes->shape()->isVertex())
1076 return true; // vertex is always could be projected
1077 else if (aRes->shape()->isEdge())
1078 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aFeatureAttr->context()->shape()));
1081 // try to convert result to sketch feature
1084 std::dynamic_pointer_cast<SketchPlugin_Feature>(ModelAPI_Feature::feature(aRes));
1088 theError = "The attribute %1 should be an edge or vertex";
1089 theError.arg(theAttribute->id());
1094 std::shared_ptr<SketchPlugin_Sketch> aSketch;
1095 std::set<AttributePtr> aRefs = theAttribute->owner()->data()->refsToMe();
1096 std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
1097 for (; anIt != aRefs.end(); ++anIt) {
1098 CompositeFeaturePtr aComp =
1099 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
1100 if (aComp && aComp->getKind() == SketchPlugin_Sketch::ID()) {
1101 aSketch = std::dynamic_pointer_cast<SketchPlugin_Sketch>(aComp);
1106 theError = "There is no sketch referring to the current feature";
1109 if (aSketchFeature && aSketch.get() == aSketchFeature->sketch()) {
1110 theError = "Unable to project feature from the same sketch";
1114 std::shared_ptr<GeomAPI_Pln> aPlane = aSketch->plane();
1115 std::shared_ptr<GeomAPI_Dir> aNormal = aPlane->direction();
1116 std::shared_ptr<GeomAPI_Pnt> anOrigin = aPlane->location();
1118 if (anEdge->isLine()) {
1119 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1120 std::shared_ptr<GeomAPI_Dir> aLineDir = aLine->direction();
1121 double aDot = fabs(aNormal->dot(aLineDir));
1122 bool aValid = fabs(aDot - 1.0) >= tolerance * tolerance;
1124 theError = "Error: Line is orthogonal to the sketch plane.";
1127 else if (anEdge->isCircle() || anEdge->isArc()) {
1128 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1129 std::shared_ptr<GeomAPI_Dir> aCircNormal = aCircle->normal();
1130 double aDot = fabs(aNormal->dot(aCircNormal));
1131 bool aValid = aDot >= tolerance * tolerance;
1133 theError.arg(anEdge->isCircle() ? "Error: Circle is orthogonal to the sketch plane."
1134 : "Error: Arc is orthogonal to the sketch plane.");
1137 else if (anEdge->isEllipse()) {
1138 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
1139 std::shared_ptr<GeomAPI_Dir> anEllipseNormal = anEllipse->normal();
1140 double aDot = fabs(aNormal->dot(anEllipseNormal));
1141 bool aValid = fabs(aDot - 1.0) <= tolerance * tolerance;
1143 theError.arg(anEdge->isClosed() ? "Error: Ellipse is orthogonal to the sketch plane."
1144 : "Error: Elliptic Arc is orthogonal to the sketch plane.");
1148 theError = "Error: Selected object is not supported for projection.";
1153 static std::set<FeaturePtr> common(const std::set<FeaturePtr>& theSet1,
1154 const std::set<FeaturePtr>& theSet2)
1156 std::set<FeaturePtr> aCommon;
1157 if (theSet1.empty() || theSet2.empty())
1160 std::set<FeaturePtr>::const_iterator anIt2 = theSet2.begin();
1161 for (; anIt2 != theSet2.end(); ++anIt2)
1162 if (theSet1.find(*anIt2) != theSet1.end())
1163 aCommon.insert(*anIt2);
1167 bool SketchPlugin_DifferentReferenceValidator::isValid(
1168 const AttributePtr& theAttribute,
1169 const std::list<std::string>& theArguments,
1170 Events_InfoMessage& theError) const
1172 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1174 int aNbFeaturesReferred = 0;
1175 int aNbAttributesReferred = 0;
1176 std::set<FeaturePtr> aCommonReferredFeatures;
1178 // find all features referred by attributes listed in theArguments
1179 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1180 for (; anArgIt != theArguments.end(); ++anArgIt) {
1181 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1185 std::set<FeaturePtr> aCoincidentFeatures;
1186 if (aRefAttr->isObject()) {
1187 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1189 aCoincidentFeatures.insert(aFeature);
1190 aNbFeaturesReferred += 1;
1193 AttributePoint2DPtr aPoint =
1194 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1196 aCoincidentFeatures = SketchPlugin_Tools::findFeaturesCoincidentToPoint(aPoint);
1197 aNbAttributesReferred += 1;
1201 if (aCommonReferredFeatures.empty())
1202 aCommonReferredFeatures = aCoincidentFeatures;
1204 aCommonReferredFeatures = common(aCommonReferredFeatures, aCoincidentFeatures);
1206 if (aCommonReferredFeatures.empty())
1210 bool isOk = aNbFeaturesReferred < 1;
1211 if (aNbFeaturesReferred == 1) {
1212 if (aCommonReferredFeatures.size() == 1) {
1213 FeaturePtr aFeature = *aCommonReferredFeatures.begin();
1214 isOk = aNbAttributesReferred <= 1 ||
1215 aFeature->getKind() == SketchPlugin_Circle::ID() ||
1216 aFeature->getKind() == SketchPlugin_Arc::ID();
1222 theError = "Attributes are referred to the same feature";
1226 bool SketchPlugin_DifferentPointReferenceValidator::isValid(
1227 const AttributePtr& theAttribute,
1228 const std::list<std::string>& theArguments,
1229 Events_InfoMessage& theError) const
1231 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1232 std::set<AttributePoint2DPtr> aReferredCoincidentPoints;
1234 // find all points referred by attributes listed in theArguments
1235 bool hasRefsToPoints = false;
1236 std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1237 for (; anArgIt != theArguments.end(); ++anArgIt) {
1238 AttributeRefAttrPtr aRefAttr = anOwner->refattr(*anArgIt);
1242 if (!aRefAttr->isObject()) {
1243 AttributePoint2DPtr aPoint =
1244 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1245 if (aReferredCoincidentPoints.empty())
1246 aReferredCoincidentPoints = SketchPlugin_Tools::findPointsCoincidentToPoint(aPoint);
1247 else if (aReferredCoincidentPoints.find(aPoint) == aReferredCoincidentPoints.end())
1248 return true; // non-coincident point has been found
1250 hasRefsToPoints = true;
1254 if (hasRefsToPoints)
1255 theError = "Attributes are referred to the same point";
1256 return !hasRefsToPoints;
1259 bool SketchPlugin_CirclePassedPointValidator::isValid(
1260 const AttributePtr& theAttribute,
1261 const std::list<std::string>&,
1262 Events_InfoMessage& theError) const
1264 static const std::string aErrorMessage(
1265 "Passed point refers to the same feature as a center point");
1267 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1269 AttributeRefAttrPtr aCenterRef =
1270 anOwner->refattr(SketchPlugin_MacroCircle::CENTER_POINT_REF_ID());
1271 AttributeRefAttrPtr aPassedRef =
1272 anOwner->refattr(SketchPlugin_MacroCircle::PASSED_POINT_REF_ID());
1274 if (!aPassedRef->isObject())
1277 FeaturePtr aPassedFeature = ModelAPI_Feature::feature(aPassedRef->object());
1278 if (!aPassedFeature)
1281 if (aCenterRef->isObject()) {
1282 if (aCenterRef->object() == aPassedRef->object()) {
1283 theError = aErrorMessage;
1287 AttributePoint2DPtr aCenterPoint =
1288 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterRef->attr());
1290 std::set<FeaturePtr> aCoincidentFeatures =
1291 SketchPlugin_Tools::findFeaturesCoincidentToPoint(aCenterPoint);
1292 // check one of coincident features is a feature referred by passed point
1293 std::set<FeaturePtr>::const_iterator anIt = aCoincidentFeatures.begin();
1294 for(; anIt != aCoincidentFeatures.end(); ++anIt)
1295 if (*anIt == aPassedFeature) {
1296 theError = aErrorMessage;
1304 bool SketchPlugin_ThirdPointValidator::isValid(
1305 const AttributePtr& theAttribute,
1306 const std::list<std::string>& theArguments,
1307 Events_InfoMessage& theError) const
1309 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
1310 return arePointsNotOnLine(anOwner, theError) &&
1311 arePointsNotSeparated(anOwner, theArguments, theError);
1314 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const FeaturePtr& theMacroCircle,
1315 const std::string& thePointAttrName,
1316 const std::string& theRefPointAttrName)
1318 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1319 theMacroCircle->attribute(thePointAttrName));
1320 AttributeRefAttrPtr aRefAttr = theMacroCircle->refattr(theRefPointAttrName);
1322 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
1324 if (aRefAttr->isObject()) {
1325 // project a point onto selected feature
1326 std::shared_ptr<SketchPlugin_Feature> aFeature =
1327 std::dynamic_pointer_cast<SketchPlugin_Feature>(
1328 ModelAPI_Feature::feature(aRefAttr->object()));
1330 SketchPlugin_Sketch* aSketch = aFeature->sketch();
1331 std::shared_ptr<GeomAPI_Edge> anEdge =
1332 std::dynamic_pointer_cast<GeomAPI_Edge>(aFeature->lastResult()->shape());
1334 std::shared_ptr<GeomAPI_Pnt> aPoint3D = aSketch->to3D(aPoint->x(), aPoint->y());
1335 if (anEdge->isLine())
1336 aPoint3D = anEdge->line()->project(aPoint3D);
1337 else if (anEdge->isCircle())
1338 aPoint3D = anEdge->circle()->project(aPoint3D);
1340 aPoint = aSketch->to2D(aPoint3D);
1344 AttributePoint2DPtr anOtherPoint =
1345 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
1347 aPoint = anOtherPoint->pnt(); // the reference point is much more precise, use it
1354 static void threePointsOfFeature(const FeaturePtr& theMacroFeature,
1355 std::shared_ptr<GeomAPI_Pnt2d> thePoints[3])
1357 if (theMacroFeature->getKind() == SketchPlugin_MacroCircle::ID()) {
1358 thePoints[0] = toPoint(theMacroFeature,
1359 SketchPlugin_MacroCircle::FIRST_POINT_ID(),
1360 SketchPlugin_MacroCircle::FIRST_POINT_REF_ID());
1361 thePoints[1] = toPoint(theMacroFeature,
1362 SketchPlugin_MacroCircle::SECOND_POINT_ID(),
1363 SketchPlugin_MacroCircle::SECOND_POINT_REF_ID());
1364 thePoints[2] = toPoint(theMacroFeature,
1365 SketchPlugin_MacroCircle::THIRD_POINT_ID(),
1366 SketchPlugin_MacroCircle::THIRD_POINT_REF_ID());
1367 } else if (theMacroFeature->getKind() == SketchPlugin_MacroArc::ID()) {
1368 thePoints[0] = toPoint(theMacroFeature,
1369 SketchPlugin_MacroArc::START_POINT_2_ID(),
1370 SketchPlugin_MacroArc::START_POINT_REF_ID());
1371 thePoints[1] = toPoint(theMacroFeature,
1372 SketchPlugin_MacroArc::END_POINT_2_ID(),
1373 SketchPlugin_MacroArc::END_POINT_REF_ID());
1374 thePoints[2] = toPoint(theMacroFeature,
1375 SketchPlugin_MacroArc::PASSED_POINT_ID(),
1376 SketchPlugin_MacroArc::PASSED_POINT_REF_ID());
1380 static bool isPointsOnLine(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
1381 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
1382 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
1384 static const double aTolerance = 1.e-7;
1385 if (thePoint1->distance(thePoint2) < aTolerance ||
1386 thePoint1->distance(thePoint3) < aTolerance)
1389 std::shared_ptr<GeomAPI_Dir2d> aDirP1P2(new GeomAPI_Dir2d(thePoint2->x() - thePoint1->x(),
1390 thePoint2->y() - thePoint1->y()));
1391 std::shared_ptr<GeomAPI_Dir2d> aDirP1P3(new GeomAPI_Dir2d(thePoint3->x() - thePoint1->x(),
1392 thePoint3->y() - thePoint1->y()));
1393 return fabs(aDirP1P2->cross(aDirP1P3)) < aTolerance;
1396 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Lin>& theLine,
1397 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1398 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1400 static const double aTolerance = 1.e-7;
1401 std::shared_ptr<GeomAPI_Dir> aLineDir = theLine->direction();
1402 std::shared_ptr<GeomAPI_XYZ> aLineLoc = theLine->location()->xyz();
1404 std::shared_ptr<GeomAPI_XYZ> aVec1 = thePoint1->xyz()->decreased(aLineLoc);
1405 // the first point is on the line
1406 if (aVec1->squareModulus() < aTolerance * aTolerance)
1408 std::shared_ptr<GeomAPI_Dir> aDirP1L(new GeomAPI_Dir(aVec1));
1409 std::shared_ptr<GeomAPI_XYZ> aVec2 = thePoint2->xyz()->decreased(aLineLoc);
1410 // the second point is on the line
1411 if (aVec2->squareModulus() < aTolerance * aTolerance)
1413 std::shared_ptr<GeomAPI_Dir> aDirP2L(new GeomAPI_Dir(aVec2));
1415 return aLineDir->cross(aDirP1L)->dot(aLineDir->cross(aDirP2L)) > -aTolerance;
1418 static bool isOnSameSide(const std::shared_ptr<GeomAPI_Circ>& theCircle,
1419 const std::shared_ptr<GeomAPI_Pnt>& thePoint1,
1420 const std::shared_ptr<GeomAPI_Pnt>& thePoint2)
1422 static const double aTolerance = 1.e-7;
1423 std::shared_ptr<GeomAPI_Pnt> aCenter = theCircle->center();
1424 double aDistP1C = thePoint1->distance(aCenter);
1425 double aDistP2C = thePoint2->distance(aCenter);
1426 return (aDistP1C - theCircle->radius()) * (aDistP2C - theCircle->radius()) > -aTolerance;
1429 bool SketchPlugin_ThirdPointValidator::arePointsNotOnLine(
1430 const FeaturePtr& theMacroFeature,
1431 Events_InfoMessage& theError) const
1433 static const std::string aErrorPointsOnLine(
1434 "Selected points are on the same line");
1436 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1437 threePointsOfFeature(theMacroFeature, aPoints);
1439 if (isPointsOnLine(aPoints[0], aPoints[1], aPoints[2])) {
1440 theError = aErrorPointsOnLine;
1446 bool SketchPlugin_ThirdPointValidator::arePointsNotSeparated(
1447 const FeaturePtr& theMacroFeature,
1448 const std::list<std::string>& theArguments,
1449 Events_InfoMessage& theError) const
1451 static const std::string aErrorPointsApart(
1452 "Selected entity is lying between first two points");
1454 AttributeRefAttrPtr aThirdPointRef = theMacroFeature->refattr(theArguments.front());
1455 FeaturePtr aRefByThird;
1456 if (aThirdPointRef->isObject())
1457 aRefByThird = ModelAPI_Feature::feature(aThirdPointRef->object());
1461 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
1462 threePointsOfFeature(theMacroFeature, aPoints);
1464 std::shared_ptr<GeomAPI_Edge> aThirdShape =
1465 std::dynamic_pointer_cast<GeomAPI_Edge>(aRefByThird->lastResult()->shape());
1469 SketchPlugin_Sketch* aSketch =
1470 std::dynamic_pointer_cast<SketchPlugin_Feature>(theMacroFeature)->sketch();
1471 std::shared_ptr<GeomAPI_Pnt> aFirstPnt3D = aSketch->to3D(aPoints[0]->x(), aPoints[0]->y());
1472 std::shared_ptr<GeomAPI_Pnt> aSecondPnt3D = aSketch->to3D(aPoints[1]->x(), aPoints[1]->y());
1475 if (aThirdShape->isLine())
1476 isOk = isOnSameSide(aThirdShape->line(), aFirstPnt3D, aSecondPnt3D);
1477 else if (aThirdShape->isCircle() || aThirdShape->isArc())
1478 isOk = isOnSameSide(aThirdShape->circle(), aFirstPnt3D, aSecondPnt3D);
1481 theError = aErrorPointsApart;
1485 bool SketchPlugin_ArcEndPointValidator::isValid(
1486 const AttributePtr& theAttribute,
1487 const std::list<std::string>& theArguments,
1488 Events_InfoMessage& theError) const
1490 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1491 AttributeRefAttrPtr anEndPointRef = aFeature->refattr(theArguments.front());
1493 if(!anEndPointRef.get()) {
1497 ObjectPtr anObject = anEndPointRef->object();
1498 AttributePtr anAttr = anEndPointRef->attr();
1499 if(!anObject.get() && !anAttr.get()) {
1503 if(anEndPointRef->attr().get()) {
1507 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1509 GeomShapePtr aShape = aResult->shape();
1510 if(aShape.get() && aShape->isVertex()) {
1515 aFeature = ModelAPI_Feature::feature(anObject);
1516 if(aFeature.get()) {
1517 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
1525 static GeomShapePtr toInfiniteEdge(const GeomShapePtr theShape)
1527 if(!theShape.get()) {
1531 if(!theShape->isEdge()) {
1535 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
1541 if(anEdge->isLine()) {
1542 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
1543 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::line(aLine);
1547 if(anEdge->isCircle() || anEdge->isArc()) {
1548 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
1549 GeomShapePtr aShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCircle);
1556 bool SketchPlugin_ArcEndPointIntersectionValidator::isValid(
1557 const AttributePtr& theAttribute,
1558 const std::list<std::string>& theArguments,
1559 Events_InfoMessage& theError) const
1561 std::shared_ptr<SketchPlugin_MacroArc> anArcFeature =
1562 std::dynamic_pointer_cast<SketchPlugin_MacroArc>(theAttribute->owner());
1563 AttributeRefAttrPtr anEndPointRef = anArcFeature->refattr(theArguments.front());
1565 if(!anEndPointRef.get()) {
1569 GeomShapePtr anArcShape = toInfiniteEdge(anArcFeature->getArcShape(false));
1571 if(!anArcShape.get() || anArcShape->isNull()) {
1575 ObjectPtr anObject = anEndPointRef->object();
1576 AttributePtr anAttr = anEndPointRef->attr();
1577 if(!anObject.get() && !anAttr.get()) {
1581 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1583 GeomShapePtr aShape = aResult->shape();
1584 if (!aShape->isEdge())
1586 aShape = toInfiniteEdge(aShape);
1587 if(aShape.get() && !aShape->isNull()) {
1588 if(anArcShape->isIntersect(aShape)) {
1594 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(anObject);
1595 if(aSelectedFeature.get()) {
1596 std::list<ResultPtr> aResults = aSelectedFeature->results();
1597 for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin();
1598 anIt != aResults.cend();
1601 GeomShapePtr aShape = (*anIt)->shape();
1602 if (!aShape->isEdge())
1604 aShape = toInfiniteEdge(aShape);
1605 if(aShape.get() && !aShape->isNull()) {
1606 if(anArcShape->isIntersect(aShape)) {
1616 bool SketchPlugin_HasNoConstraint::isValid(const AttributePtr& theAttribute,
1617 const std::list<std::string>& theArguments,
1618 Events_InfoMessage& theError) const
1620 std::set<std::string> aFeatureKinds;
1621 for (std::list<std::string>::const_iterator anArgIt = theArguments.begin();
1622 anArgIt != theArguments.end(); anArgIt++) {
1623 aFeatureKinds.insert(*anArgIt);
1626 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1627 theError = "The attribute with the %1 type is not processed";
1628 theError.arg(theAttribute->attributeType());
1632 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
1634 bool isObject = aRefAttr->isObject();
1636 theError = "It uses an empty object";
1639 ObjectPtr anObject = aRefAttr->object();
1640 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1641 if (!aFeature.get()) {
1642 theError = "The feature of the checked attribute is empty";
1646 FeaturePtr aCurrentFeature = ModelAPI_Feature::feature(aRefAttr->owner());
1648 std::set<AttributePtr> aRefsList = anObject->data()->refsToMe();
1649 std::set<AttributePtr>::const_iterator anIt = aRefsList.begin();
1650 for (; anIt != aRefsList.end(); anIt++) {
1651 FeaturePtr aRefFeature = ModelAPI_Feature::feature((*anIt)->owner());
1652 if (aRefFeature.get() && aCurrentFeature != aRefFeature &&
1653 aFeatureKinds.find(aRefFeature->getKind()) != aFeatureKinds.end())
1654 return false; // constraint is found, that means that the check is not valid
1659 bool SketchPlugin_ReplicationReferenceValidator::isValid(
1660 const AttributePtr& theAttribute,
1661 const std::list<std::string>& theArguments,
1662 Events_InfoMessage& theError) const
1664 AttributeRefAttrPtr aRefAttr =
1665 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1668 theError = "Incorrect attribute";
1673 if (aRefAttr->isObject())
1674 anOwner = aRefAttr->object();
1677 AttributePtr anAttr = aRefAttr->attr();
1678 anOwner = anAttr->owner();
1680 FeaturePtr anAttrOwnerFeature = ModelAPI_Feature::feature(anOwner);
1681 if (!anAttrOwnerFeature)
1683 AttributeBooleanPtr aCopyAttr = anAttrOwnerFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
1684 if (!aCopyAttr || !aCopyAttr->value())
1685 return true; // feature is not a copy, thus valid
1687 // check the copy feature is already referred by the "Multi" feature
1688 FeaturePtr aMultiFeature = ModelAPI_Feature::feature(theAttribute->owner());
1689 AttributeRefListPtr aRefList = aMultiFeature->reflist(theArguments.front());
1690 for (int i = 0; i < aRefList->size(); ++i)
1692 FeaturePtr aRefOwner = ModelAPI_Feature::feature(aRefList->object(i));
1693 if (aRefOwner == anAttrOwnerFeature)
1695 theError = "Attribute refers to the object generated by this feature";
1703 bool SketchPlugin_SketchFeatureValidator::isValid(const AttributePtr& theAttribute,
1704 const std::list<std::string>& theArguments,
1705 Events_InfoMessage& theError) const
1707 if (theAttribute->attributeType() != ModelAPI_AttributeRefAttr::typeId()) {
1708 theError = "The attribute with the %1 type is not processed";
1709 theError.arg(theAttribute->attributeType());
1713 // check the attribute refers to a sketch feature
1714 AttributeRefAttrPtr aRefAttr =
1715 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
1716 bool isSketchFeature = aRefAttr->isObject();
1717 if (isSketchFeature) {
1718 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1719 isSketchFeature = aFeature.get() != NULL;
1720 if (isSketchFeature) {
1721 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1722 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1723 isSketchFeature = aSketchFeature.get() != NULL;
1727 if (isSketchFeature)
1730 theError = "The object selected is not a sketch feature";
1734 bool SketchPlugin_MultiRotationAngleValidator::isValid(const AttributePtr& theAttribute,
1735 const std::list<std::string>& theArguments,
1736 Events_InfoMessage& theError) const
1738 if (theAttribute->attributeType() != ModelAPI_AttributeDouble::typeId()) {
1739 theError = "The attribute with the %1 type is not processed";
1740 theError.arg(theAttribute->attributeType());
1744 AttributeDoublePtr anAngleAttr =
1745 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1747 FeaturePtr aMultiRotation = ModelAPI_Feature::feature(theAttribute->owner());
1748 AttributeStringPtr anAngleType =
1749 aMultiRotation->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
1750 AttributeIntegerPtr aNbCopies =
1751 aMultiRotation->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID());
1753 if (anAngleType->value() != "FullAngle")
1755 double aFullAngleValue = anAngleAttr->value() * (aNbCopies->value() - 1);
1756 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 359.9999999)
1758 theError = "Rotation single angle should produce full angle less than 360 degree";
1764 double aFullAngleValue = anAngleAttr->value();
1765 if (aFullAngleValue < -1.e-7 || aFullAngleValue > 360.0000001)
1767 theError = "Rotation full angle should be in range [0, 360]";